123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
- #define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
- #include <boost/align/detail/addressof.hpp>
- #include <boost/align/detail/is_alignment_constant.hpp>
- #include <boost/align/detail/max_objects.hpp>
- #include <boost/align/detail/max_size.hpp>
- #include <boost/align/aligned_alloc.hpp>
- #include <boost/align/aligned_allocator_forward.hpp>
- #include <boost/align/alignment_of.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/throw_exception.hpp>
- #include <new>
- #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
- #include <utility>
- #endif
- namespace boost {
- namespace alignment {
- template<class T, std::size_t Alignment>
- class aligned_allocator {
- BOOST_STATIC_ASSERT(detail::
- is_alignment_constant<Alignment>::value);
- public:
- typedef T value_type;
- typedef T* pointer;
- typedef const T* const_pointer;
- typedef void* void_pointer;
- typedef const void* const_void_pointer;
- typedef std::size_t size_type;
- typedef std::ptrdiff_t difference_type;
- typedef T& reference;
- typedef const T& const_reference;
- private:
- enum {
- min_align = detail::max_size<Alignment,
- alignment_of<value_type>::value>::value
- };
- public:
- template<class U>
- struct rebind {
- typedef aligned_allocator<U, Alignment> other;
- };
- #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
- aligned_allocator() = default;
- #else
- aligned_allocator() BOOST_NOEXCEPT {
- }
- #endif
- template<class U>
- aligned_allocator(const aligned_allocator<U, Alignment>&)
- BOOST_NOEXCEPT {
- }
- pointer address(reference value) const BOOST_NOEXCEPT {
- return detail::addressof(value);
- }
- const_pointer address(const_reference value) const BOOST_NOEXCEPT {
- return detail::addressof(value);
- }
- pointer allocate(size_type size, const_void_pointer = 0) {
- void* p = 0;
- if (size > 0) {
- p = aligned_alloc(min_align, sizeof(T) * size);
- if (!p) {
- ::boost::throw_exception(std::bad_alloc());
- }
- }
- return static_cast<T*>(p);
- }
- void deallocate(pointer ptr, size_type) {
- ::boost::alignment::aligned_free(ptr);
- }
- BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
- return detail::max_objects<T>::value;
- }
- #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
- #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
- template<class U, class... Args>
- void construct(U* ptr, Args&&... args) {
- ::new(static_cast<void*>(ptr)) U(std::forward<Args>(args)...);
- }
- #else
- template<class U, class V>
- void construct(U* ptr, V&& value) {
- ::new(static_cast<void*>(ptr)) U(std::forward<V>(value));
- }
- #endif
- #else
- template<class U, class V>
- void construct(U* ptr, const V& value) {
- ::new(static_cast<void*>(ptr)) U(value);
- }
- #endif
- template<class U>
- void construct(U* ptr) {
- ::new(static_cast<void*>(ptr)) U();
- }
- template<class U>
- void destroy(U* ptr) {
- (void)ptr;
- ptr->~U();
- }
- };
- template<std::size_t Alignment>
- class aligned_allocator<void, Alignment> {
- BOOST_STATIC_ASSERT(detail::
- is_alignment_constant<Alignment>::value);
- public:
- typedef void value_type;
- typedef void* pointer;
- typedef const void* const_pointer;
- template<class U>
- struct rebind {
- typedef aligned_allocator<U, Alignment> other;
- };
- };
- template<class T1, class T2, std::size_t Alignment>
- inline bool operator==(const aligned_allocator<T1, Alignment>&,
- const aligned_allocator<T2, Alignment>&) BOOST_NOEXCEPT
- {
- return true;
- }
- template<class T1, class T2, std::size_t Alignment>
- inline bool operator!=(const aligned_allocator<T1, Alignment>&,
- const aligned_allocator<T2, Alignment>&) BOOST_NOEXCEPT
- {
- return false;
- }
- }
- }
- #endif
|