Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-21 23:14:21

0001 #include <cstdint>
0002 
0003 /**
0004  * Allocator for aligned data.
0005  *
0006  * Modified from the Mallocator from Stephan T. Lavavej.
0007  * <http://blogs.msdn.com/b/vcblog/archive/2008/08/28/the-mallocator.aspx>
0008  */
0009 template <typename T, std::size_t Alignment>
0010 class aligned_allocator {
0011 public:
0012   // The following will be the same for virtually all allocators.
0013   typedef T* pointer;
0014   typedef const T* const_pointer;
0015   typedef T& reference;
0016   typedef const T& const_reference;
0017   typedef T value_type;
0018   typedef std::size_t size_type;
0019   typedef ptrdiff_t difference_type;
0020 
0021   T* address(T& r) const { return &r; }
0022 
0023   const T* address(const T& s) const { return &s; }
0024 
0025   std::size_t max_size() const {
0026     // The following has been carefully written to be independent of
0027     // the definition of size_t and to avoid signed/unsigned warnings.
0028     return (static_cast<std::size_t>(0) - static_cast<std::size_t>(1)) / sizeof(T);
0029   }
0030 
0031   // The following must be the same for all allocators.
0032   template <typename U>
0033   struct rebind {
0034     typedef aligned_allocator<U, Alignment> other;
0035   };
0036 
0037   bool operator!=(const aligned_allocator& other) const { return !(*this == other); }
0038 
0039   void construct(T* const p, const T& t) const {
0040     void* const pv = static_cast<void*>(p);
0041 
0042     new (pv) T(t);
0043   }
0044 
0045   void construct(T* const p) { return construct(p, value_type()); }
0046 
0047   void destroy(T* const p) const { p->~T(); }
0048 
0049   // Returns true if and only if storage allocated from *this
0050   // can be deallocated from other, and vice versa.
0051   // Always returns true for stateless allocators.
0052   bool operator==(const aligned_allocator& other) const { return true; }
0053 
0054   // Default constructor, copy constructor, rebinding constructor, and destructor.
0055   // Empty for stateless allocators.
0056   aligned_allocator() {}
0057 
0058   aligned_allocator(const aligned_allocator&) {}
0059 
0060   template <typename U>
0061   aligned_allocator(const aligned_allocator<U, Alignment>&) {}
0062 
0063   ~aligned_allocator() {}
0064 
0065   // The following will be different for each allocator.
0066   T* allocate(const std::size_t n) const {
0067     // The return value of allocate(0) is unspecified.
0068     // Mallocator returns NULL in order to avoid depending
0069     // on malloc(0)'s implementation-defined behavior
0070     // (the implementation can define malloc(0) to return NULL,
0071     // in which case the bad_alloc check below would fire).
0072     // All allocators can return NULL in this case.
0073     if (n == 0) {
0074       return NULL;
0075     }
0076 
0077     // All allocators should contain an integer overflow check.
0078     // The Standardization Committee recommends that std::length_error
0079     // be thrown in the case of integer overflow.
0080     if (n > max_size()) {
0081       throw std::length_error("aligned_allocator<T>::allocate() - Integer overflow.");
0082     }
0083 
0084     // Mallocator wraps malloc().
0085     void* const pv = std::aligned_alloc(Alignment, n * sizeof(T));
0086 
0087     // Allocators should throw std::bad_alloc in the case of memory allocation failure.
0088     if (pv == NULL) {
0089       throw std::bad_alloc();
0090     }
0091 
0092     return static_cast<T*>(pv);
0093   }
0094 
0095   void deallocate(T* const p, const std::size_t n) const { std::free(p); }
0096 
0097   // The following will be the same for all allocators that ignore hints.
0098   template <typename U>
0099   T* allocate(const std::size_t n, const U* /* const hint */) const {
0100     return allocate(n);
0101   }
0102 
0103   // Allocators are not required to be assignable, so
0104   // all allocators should have a private unimplemented
0105   // assignment operator. Note that this will trigger the
0106   // off-by-default (enabled under /Wall) warning C4626
0107   // "assignment operator could not be generated because a
0108   // base class assignment operator is inaccessible" within
0109   // the STL headers, but that warning is useless.
0110 private:
0111   aligned_allocator& operator=(const aligned_allocator&);
0112 };