Skip to content

Instantly share code, notes, and snippets.

@amaya382
Last active February 4, 2017 12:19
Show Gist options
  • Save amaya382/d86d3168a071b888307106f866173cd0 to your computer and use it in GitHub Desktop.
Save amaya382/d86d3168a071b888307106f866173cd0 to your computer and use it in GitHub Desktop.
#include <vector>
using namespace std;
template<typename T, size_t N>
class aligned_allocator : public allocator<T> {
using size_type = typename allocator<T>::size_type;
using pointer = typename allocator<T>::pointer;
using const_pointer = typename allocator<T>::const_pointer;
public:
pointer allocate(size_type n, const_pointer = nullptr) const {
return reinterpret_cast<pointer>(aligned_alloc(N, n * sizeof(T)));
}
void deallocate(pointer p, size_type) const noexcept {
free(p);
}
};
template<typename T, size_t N>
using aligned_vector = vector<T, aligned_allocator<T, N>>;
template<typename T>
using a32_vector = aligned_vector<T, 32>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment