AlignedAllocator: simplify and fix for MSVC, C++11

- Simplify thanks to minimal allocator requirements in C++11.
- Add required copy constructor.
This commit is contained in:
Justin Viiret 2016-03-04 10:10:26 +11:00 committed by Matthew Barr
parent 93a262e54c
commit e466e67b79

View File

@ -78,50 +78,41 @@ void aligned_free_internal(void *ptr);
/** \brief Aligned allocator class for use with STL containers. Ensures that /** \brief Aligned allocator class for use with STL containers. Ensures that
* your objects are aligned to N bytes. */ * your objects are aligned to N bytes. */
template <typename T, std::size_t N> class AlignedAllocator { template <class T, std::size_t N>
class AlignedAllocator {
public: public:
typedef T value_type; using value_type = T;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef const T *const_pointer;
typedef T &reference;
typedef const T &const_reference;
template <typename U> struct rebind { AlignedAllocator() noexcept {}
typedef AlignedAllocator<U, N> other;
template <class U, std::size_t N2>
AlignedAllocator(const AlignedAllocator<U, N2> &) noexcept {}
template <class U> struct rebind {
using other = AlignedAllocator<U, N>;
}; };
pointer address(reference x) const { return &x; } T *allocate(std::size_t size) const {
const_pointer address(const_reference x) const { return &x; } size_t alloc_size = size * sizeof(T);
return static_cast<T *>(aligned_malloc_internal(alloc_size, N));
size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(value_type);
} }
pointer allocate(size_type size) const { void deallocate(T *x, std::size_t) const noexcept {
return static_cast<pointer>( aligned_free_internal(x);
aligned_malloc_internal(size * sizeof(value_type), N));
} }
};
void deallocate(pointer x, size_type) const { aligned_free_internal(x); } template <class T, class U, std::size_t N, std::size_t N2>
bool operator==(const AlignedAllocator<T, N> &,
void construct(pointer x, const value_type &val) const { const AlignedAllocator<U, N2> &) {
new (x) value_type(val);
}
void destroy(pointer p) const { p->~value_type(); }
bool operator==(const AlignedAllocator<T, N> &) const {
// All instances of AlignedAllocator can dealloc each others' memory.
return true; return true;
} }
bool operator!=(const AlignedAllocator<T, N> &) const { template <class T, class U, std::size_t N, std::size_t N2>
// All instances of AlignedAllocator can dealloc each others' memory. bool operator!=(const AlignedAllocator<T, N> &a,
return false; const AlignedAllocator<U, N2> &b) {
return !(a == b);
} }
};
} // namespace ue2 } // namespace ue2