CMS 3D CMS Logo

align_alloc.h
Go to the documentation of this file.
1 #include <cstdint>
2 
9 template <typename T, std::size_t Alignment>
11 public:
12  // The following will be the same for virtually all allocators.
13  typedef T* pointer;
14  typedef const T* const_pointer;
15  typedef T& reference;
16  typedef const T& const_reference;
17  typedef T value_type;
18  typedef std::size_t size_type;
19  typedef ptrdiff_t difference_type;
20 
21  T* address(T& r) const { return &r; }
22 
23  const T* address(const T& s) const { return &s; }
24 
25  std::size_t max_size() const {
26  // The following has been carefully written to be independent of
27  // the definition of size_t and to avoid signed/unsigned warnings.
28  return (static_cast<std::size_t>(0) - static_cast<std::size_t>(1)) / sizeof(T);
29  }
30 
31  // The following must be the same for all allocators.
32  template <typename U>
33  struct rebind {
35  };
36 
37  bool operator!=(const aligned_allocator& other) const { return !(*this == other); }
38 
39  void construct(T* const p, const T& t) const {
40  void* const pv = static_cast<void*>(p);
41 
42  new (pv) T(t);
43  }
44 
45  void construct(T* const p) { return construct(p, value_type()); }
46 
47  void destroy(T* const p) const { p->~T(); }
48 
49  // Returns true if and only if storage allocated from *this
50  // can be deallocated from other, and vice versa.
51  // Always returns true for stateless allocators.
52  bool operator==(const aligned_allocator& other) const { return true; }
53 
54  // Default constructor, copy constructor, rebinding constructor, and destructor.
55  // Empty for stateless allocators.
57 
59 
60  template <typename U>
62 
64 
65  // The following will be different for each allocator.
66  T* allocate(const std::size_t n) const {
67  // The return value of allocate(0) is unspecified.
68  // Mallocator returns NULL in order to avoid depending
69  // on malloc(0)'s implementation-defined behavior
70  // (the implementation can define malloc(0) to return NULL,
71  // in which case the bad_alloc check below would fire).
72  // All allocators can return NULL in this case.
73  if (n == 0) {
74  return NULL;
75  }
76 
77  // All allocators should contain an integer overflow check.
78  // The Standardization Committee recommends that std::length_error
79  // be thrown in the case of integer overflow.
80  if (n > max_size()) {
81  throw std::length_error("aligned_allocator<T>::allocate() - Integer overflow.");
82  }
83 
84  // Mallocator wraps malloc().
85  void* const pv = std::aligned_alloc(Alignment, n * sizeof(T));
86 
87  // Allocators should throw std::bad_alloc in the case of memory allocation failure.
88  if (pv == NULL) {
89  throw std::bad_alloc();
90  }
91 
92  return static_cast<T*>(pv);
93  }
94 
95  void deallocate(T* const p, const std::size_t n) const { std::free(p); }
96 
97  // The following will be the same for all allocators that ignore hints.
98  template <typename U>
99  T* allocate(const std::size_t n, const U* /* const hint */) const {
100  return allocate(n);
101  }
102 
103  // Allocators are not required to be assignable, so
104  // all allocators should have a private unimplemented
105  // assignment operator. Note that this will trigger the
106  // off-by-default (enabled under /Wall) warning C4626
107  // "assignment operator could not be generated because a
108  // base class assignment operator is inaccessible" within
109  // the STL headers, but that warning is useless.
110 private:
112 };
bool operator!=(const aligned_allocator &other) const
Definition: align_alloc.h:37
T * allocate(const std::size_t n) const
Definition: align_alloc.h:66
void deallocate(T *const p, const std::size_t n) const
Definition: align_alloc.h:95
aligned_allocator(const aligned_allocator &)
Definition: align_alloc.h:58
#define NULL
Definition: scimark2.h:8
const T * const_pointer
Definition: align_alloc.h:14
T * allocate(const std::size_t n, const U *) const
Definition: align_alloc.h:99
std::size_t max_size() const
Definition: align_alloc.h:25
ptrdiff_t difference_type
Definition: align_alloc.h:19
def pv(vc)
Definition: MetAnalyzer.py:7
aligned_allocator(const aligned_allocator< U, Alignment > &)
Definition: align_alloc.h:61
aligned_allocator & operator=(const aligned_allocator &)
void construct(T *const p)
Definition: align_alloc.h:45
void construct(T *const p, const T &t) const
Definition: align_alloc.h:39
void destroy(T *const p) const
Definition: align_alloc.h:47
aligned_allocator< U, Alignment > other
Definition: align_alloc.h:34
const T * address(const T &s) const
Definition: align_alloc.h:23
T * address(T &r) const
Definition: align_alloc.h:21
std::size_t size_type
Definition: align_alloc.h:18
bool operator==(const aligned_allocator &other) const
Definition: align_alloc.h:52
long double T
const T & const_reference
Definition: align_alloc.h:16