Last active
January 17, 2020 21:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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