Created
February 25, 2017 11:42
-
-
Save vertexoperator/a07744e43651bf1c52e602ab68141e53 to your computer and use it in GitHub Desktop.
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 <vector> | |
#include <cstdint> | |
#include <iostream> | |
#include <memory> | |
template <typename T, size_t N=16> | |
class aligned_allocator : public std::allocator<T> | |
{ | |
public: | |
using size_type = typename std::allocator<T>::size_type; | |
using pointer = typename std::allocator<T>::pointer; | |
using const_pointer = typename std::allocator<T>::const_pointer; | |
aligned_allocator() noexcept | |
{} | |
aligned_allocator& operator=(const aligned_allocator &rhs){ | |
std::allocator<T>::operator=(rhs); | |
return *this; | |
} | |
pointer allocate(size_type n){ | |
return (pointer)aligned_alloc(n*sizeof(T), N); | |
} | |
void deallocate(pointer p, size_type n){ | |
free(p); | |
} | |
void construct(pointer p, const T &val){ | |
new(p) T(val); | |
} | |
void destroy(pointer p){ | |
p->~T(); | |
} | |
}; | |
int main(){ | |
std::vector<float , aligned_allocator<float, 16> > v; | |
v.resize(1000); | |
std::cout << v.data() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment