Compressed Sparse Blocks  1.2
 All Classes Files Functions Variables Typedefs Friends Macros Pages
aligned.h
Go to the documentation of this file.
1 #ifdef _WIN32
2 #include <malloc.h>
3 #endif
4 #include <cstdint>
5 #include <vector>
6 #include <iostream>
7 using namespace std;
8 
15 template <typename T, std::size_t Alignment>
17 {
18  public:
19 
20  // The following will be the same for virtually all allocators.
21  typedef T * pointer;
22  typedef const T * const_pointer;
23  typedef T& reference;
24  typedef const T& const_reference;
25  typedef T value_type;
26  typedef std::size_t size_type;
27  typedef ptrdiff_t difference_type;
28 
29  T * address(T& r) const
30  {
31  return &r;
32  }
33 
34  const T * address(const T& s) const
35  {
36  return &s;
37  }
38 
39  std::size_t max_size() const
40  {
41  // The following has been carefully written to be independent of
42  // the definition of size_t and to avoid signed/unsigned warnings.
43  return (static_cast<std::size_t>(0) - static_cast<std::size_t>(1)) / sizeof(T);
44  }
45 
46 
47  // The following must be the same for all allocators.
48  template <typename U>
49  struct rebind
50  {
52  } ;
53 
54  bool operator!=(const aligned_allocator& other) const
55  {
56  return !(*this == other);
57  }
58 
59  void construct(T * const p, const T& t) const
60  {
61  void * const pv = static_cast<void *>(p);
62 
63  new (pv) T(t);
64  }
65 
66  void destroy(T * const p) const
67  {
68  p->~T();
69  }
70 
71  // Returns true if and only if storage allocated from *this
72  // can be deallocated from other, and vice versa.
73  // Always returns true for stateless allocators.
74  bool operator==(const aligned_allocator& other) const
75  {
76  return true;
77  }
78 
79 
80  // Default constructor, copy constructor, rebinding constructor, and destructor.
81  // Empty for stateless allocators.
83 
85 
86  template <typename U> aligned_allocator(const aligned_allocator<U, Alignment>&) { }
87 
89 
90 
91  // The following will be different for each allocator.
92  T * allocate(const std::size_t n) const
93  {
94  // The return value of allocate(0) is unspecified.
95  // Mallocator returns NULL in order to avoid depending
96  // on malloc(0)'s implementation-defined behavior
97  // (the implementation can define malloc(0) to return NULL,
98  // in which case the bad_alloc check below would fire).
99  // All allocators can return NULL in this case.
100  if (n == 0) {
101  return NULL;
102  }
103 
104  // All allocators should contain an integer overflow check.
105  // The Standardization Committee recommends that std::length_error
106  // be thrown in the case of integer overflow.
107  if (n > max_size())
108  {
109  cerr << "aligned_allocator<T>::allocate() - Integer overflow." << endl;
110  }
111 
112  // Mallocator wraps malloc().
113  void * const pv = _mm_malloc(n * sizeof(T), Alignment);
114 
115  // Allocators should throw std::bad_alloc in the case of memory allocation failure.
116  if (pv == NULL)
117  {
118  cerr << "std::bad_alloc" << endl;
119  }
120 
121  return static_cast<T *>(pv);
122  }
123 
124  void deallocate(T * const p, const std::size_t n) const
125  {
126  _mm_free(p);
127  }
128 
129 
130  // The following will be the same for all allocators that ignore hints.
131  template <typename U>
132  T * allocate(const std::size_t n, const U * /* const hint */) const
133  {
134  return allocate(n);
135  }
136 
137 
138  // Allocators are not required to be assignable, so
139  // all allocators should have a private unimplemented
140  // assignment operator. Note that this will trigger the
141  // off-by-default (enabled under /Wall) warning C4626
142  // "assignment operator could not be generated because a
143  // base class assignment operator is inaccessible" within
144  // the STL headers, but that warning is useless.
145  private:
146  aligned_allocator& operator=(const aligned_allocator&);
147 };
void destroy(T *const p) const
Definition: aligned.h:66
T * allocate(const std::size_t n, const U *) const
Definition: aligned.h:132
const T * address(const T &s) const
Definition: aligned.h:34
aligned_allocator(const aligned_allocator &)
Definition: aligned.h:84
void deallocate(T *const p, const std::size_t n) const
Definition: aligned.h:124
const T * const_pointer
Definition: aligned.h:22
bool operator!=(const aligned_allocator &other) const
Definition: aligned.h:54
ptrdiff_t difference_type
Definition: aligned.h:27
bool operator==(const aligned_allocator &other) const
Definition: aligned.h:74
void construct(T *const p, const T &t) const
Definition: aligned.h:59
aligned_allocator(const aligned_allocator< U, Alignment > &)
Definition: aligned.h:86
T * address(T &r) const
Definition: aligned.h:29
aligned_allocator< U, Alignment > other
Definition: aligned.h:51
std::size_t max_size() const
Definition: aligned.h:39
std::size_t size_type
Definition: aligned.h:26
T * allocate(const std::size_t n) const
Definition: aligned.h:92
const T & const_reference
Definition: aligned.h:24