Skip to content

Instantly share code, notes, and snippets.

@gradbot
Last active January 17, 2020 21:49
Show Gist options
  • Save gradbot/4226c0c802c36738c92a to your computer and use it in GitHub Desktop.
Save gradbot/4226c0c802c36738c92a to your computer and use it in GitHub Desktop.
Simple aligned allocator for use with std::vector and DirectXMath in Visual Studio 2013.
#include <memory>
#include <vector>
struct XMVECTOR
{
float x, y, z, w;
};
template <typename T, unsigned int Alignment = 16>
class AlignedAllocator : public std::allocator<T>
{
public:
typedef typename std::allocator<T>::pointer pointer;
typedef typename std::allocator<T>::size_type size_type;
pointer allocate(size_type n, std::allocator<void>::const_pointer = 0)
{
return reinterpret_cast<pointer>(_aligned_malloc(n * sizeof(T), Alignment));
}
void deallocate(pointer p, size_type)
{
_aligned_free(p);
}
};
std::vector<XMVECTOR, AlignedAllocator<XMVECTOR>> samples;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment