Add aligned_allocator

This commit is contained in:
JCRaymond
2019-12-07 13:49:57 -05:00
parent 04ecc147fb
commit 4cf3e89705

View File

@@ -0,0 +1,62 @@
#include <immintrin.h>
#include <memory>
template <class T, int N>
class aligned_allocator
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template <class U>
struct rebind
{
typedef aligned_allocator<U,N> other;
};
inline aligned_allocator() throw() {}
inline aligned_allocator(const aligned_allocator&) throw() {}
template <class U>
inline aligned_allocator(const aligned_allocator<U,N>&) throw() {}
inline ~aligned_allocator() throw() {}
inline pointer address(reference r) { return &r; }
inline const_pointer address(const_reference r) const { return &r; }
pointer allocate(size_type n, typename std::allocator<void>::const_pointer hint = 0);
inline void deallocate(pointer p, size_type);
inline void construct(pointer p, const_reference value) { new (p) value_type(value); }
inline void destroy(pointer p) { p->~value_type(); }
inline size_type max_size() const throw() { return size_type(-1) / sizeof(T); }
inline bool operator==(const aligned_allocator&) { return true; }
inline bool operator!=(const aligned_allocator& rhs) { return !operator==(rhs); }
};
template <class T, int N>
typename aligned_allocator<T,N>::pointer
aligned_allocator<T,N>::allocate(size_type n, typename std::allocator<void>::const_pointer) {
size_type alloc_size = ((sizeof(T)*(n-1))/N + 1)*N;
pointer res = reinterpret_cast<pointer>(_mm_malloc(alloc_size, N));
if (res == 0)
throw std::bad_alloc();
return res;
}
template <class T, int N>
void
aligned_allocator<T,N>::deallocate(pointer p, size_type) {
_mm_free(p);
}