Skip to content

Instantly share code, notes, and snippets.

@vertexoperator
Created February 25, 2017 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vertexoperator/a07744e43651bf1c52e602ab68141e53 to your computer and use it in GitHub Desktop.
Save vertexoperator/a07744e43651bf1c52e602ab68141e53 to your computer and use it in GitHub Desktop.
#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