#pragma once #include #include #include #include enum class Alignment : size_t { Normal = sizeof(void*), SSE = 16, AVX = 32, }; namespace detail { void* allocate_aligned_memory(size_t align, size_t size); void deallocate_aligned_memory(void* ptr) noexcept; } template class AlignedAllocator; template class AlignedAllocator { public: typedef void* pointer; typedef const void* const_pointer; typedef void value_type; template struct rebind { typedef AlignedAllocator other; }; }; template class AlignedAllocator { public: typedef T value_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef std::true_type propagate_on_container_move_assignment; template struct rebind { typedef AlignedAllocator other; }; public: AlignedAllocator() noexcept { } template AlignedAllocator(const AlignedAllocator&) noexcept { } size_type max_size() const noexcept { return (size_type(~0) - size_type(Align)) / sizeof(T); } pointer address(reference x) const noexcept { return std::addressof(x); } const_pointer address(const_reference x) const noexcept { return std::addressof(x); } pointer allocate(size_type n, typename AlignedAllocator::const_pointer = 0) { const size_type alignment = static_cast(Align); void* ptr = detail::allocate_aligned_memory(alignment, n * sizeof(T)); if (ptr == nullptr) { throw std::bad_alloc(); } return reinterpret_cast(ptr); } void deallocate(pointer p, size_type) noexcept { detail::deallocate_aligned_memory(p); } template void construct(U* p, Args&&... args) { ::new (reinterpret_cast(p)) U(std::forward(args)...); } void destroy(pointer p) { p->~T(); } }; template class AlignedAllocator { public: typedef T value_type; typedef const T* pointer; typedef const T* const_pointer; typedef const T& reference; typedef const T& const_reference; typedef size_t size_type; typedef ptrdiff_t difference_type; typedef std::true_type propagate_on_container_move_assignment; template struct rebind { typedef AlignedAllocator other; }; public: AlignedAllocator() noexcept { } template AlignedAllocator(const AlignedAllocator&) noexcept { } size_type max_size() const noexcept { return (size_type(~0) - size_type(Align)) / sizeof(T); } const_pointer address(const_reference x) const noexcept { return std::addressof(x); } pointer allocate(size_type n, typename AlignedAllocator::const_pointer = 0) { const size_type alignment = static_cast(Align); void* ptr = detail::allocate_aligned_memory(alignment, n * sizeof(T)); if (ptr == nullptr) { throw std::bad_alloc(); } return reinterpret_cast(ptr); } void deallocate(pointer p, size_type) noexcept { detail::deallocate_aligned_memory(p); } template void construct(U* p, Args&&... args) { ::new (reinterpret_cast(p)) U(std::forward(args)...); } void destroy(pointer p) { p->~T(); } }; template inline bool operator==(const AlignedAllocator&, const AlignedAllocator&) noexcept { return TAlign == UAlign; } template inline bool operator!=(const AlignedAllocator&, const AlignedAllocator&) noexcept { return TAlign != UAlign; } inline void* detail::allocate_aligned_memory(size_t align, size_t size) { assert(align >= sizeof(void*)); //assert(nail::is_power_of_two(align)); if (size == 0) { return nullptr; } void* ptr = nullptr; int rc = posix_memalign(&ptr, align, size); if (rc != 0) { return nullptr; } return ptr; } inline void detail::deallocate_aligned_memory(void *ptr) noexcept { free(ptr); }