ngscopeclient 0.1-dev+51fbda87c
AlignedAllocator.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2024 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
37#ifndef AlignedAllocator_h
38#define AlignedAllocator_h
39
40#ifdef _WIN32
41#include <windows.h>
42#endif
43
51template <class T, size_t alignment>
53{
54public:
55
57 typedef T* pointer;
58
60 typedef const T* const_pointer;
61
63 typedef T& reference;
64
66 typedef const T& const_reference;
67
69 typedef T value_type;
70
72 typedef size_t size_type;
73
75 typedef ptrdiff_t difference_type;
76
85 T* address(T& rhs)
86 { return &rhs; }
87
96 const T* address(T& rhs) const
97 { return &rhs; }
98
104 size_t max_size() const
105 { return (static_cast<size_t>(0) - static_cast<size_t>(1)) / sizeof(T); }
106
108 template<typename U>
109 struct rebind
110 {
112 };
113
119 bool operator!=(const AlignedAllocator& other) const
120 { return !(*this == other); }
121
122 //Look at that, a placement new! First time I've ever used one.
129 void construct(T* const p, const T& t) const
130 { new( static_cast<void*>(p) ) T(t); }
131
132 void destroy(T* const p) const
133 { p->~T(); }
134
135 //Check if this allocator is functionally equivalent to another
136 //We have no member variables, so all objects of the same type are equivalent
137 bool operator== (const AlignedAllocator& /* unused */) const
138 { return true; }
139
140 //Default ctors, do nothing
142 {}
143
144 AlignedAllocator(const AlignedAllocator& /* unused */)
145 {}
146
147 template<typename U>
149 {}
150
152 {}
153
159 T* allocate(size_t n) const
160 {
161 //Fail if we got an invalid size
162 if(n == 0)
163 return NULL;
164 if(n > max_size())
165 throw std::length_error("AlignedAllocator<T>::allocate(): requested size is too large, integer overflow?");
166
167 //Round size up to multiple of alignment
168 if( (n % alignment) != 0)
169 {
170 n |= (alignment - 1);
171 n ++;
172 }
173
174 //Do the actual allocation
175#ifdef _WIN32
176 T* ret = static_cast<T*>(_aligned_malloc(n*sizeof(T), alignment));
177#else
178 T* ret = static_cast<T*>(aligned_alloc(alignment, n*sizeof(T)));
179#endif
180
181 //Error check
182 if(ret == NULL)
183 throw std::bad_alloc();
184
185 return ret;
186 }
187
194 void deallocate(T* const p, [[maybe_unused]] const size_t unused) const
195 {
196#ifdef _WIN32
197 _aligned_free(p);
198#else
199 free(p);
200#endif
201 }
202
208 void deallocate(T* const p) const
209 { deallocate(p, 1); }
210
211 //Not quite sure what this is for but apparently we need it?
218 template<typename U>
219 T* allocate(const size_t n, [[maybe_unused]] const U* const hint)
220 { return allocate(n); }
221
222 //Disallow assignment
223 AlignedAllocator& operator=(const AlignedAllocator&) = delete;
224};
225
226#endif
Aligned memory allocator for STL containers.
Definition: AlignedAllocator.h:53
size_t size_type
Type of the size of an allocated object.
Definition: AlignedAllocator.h:72
const T & const_reference
Const reference to the allocated type.
Definition: AlignedAllocator.h:66
T * allocate(const size_t n, const U *const hint)
Allocate an object.
Definition: AlignedAllocator.h:219
ptrdiff_t difference_type
Type of the difference between two allocated pointers.
Definition: AlignedAllocator.h:75
const T * address(T &rhs) const
Get the address of an object.
Definition: AlignedAllocator.h:96
void deallocate(T *const p, const size_t unused) const
Free a block of memory.
Definition: AlignedAllocator.h:194
T * address(T &rhs)
Get the address of an object.
Definition: AlignedAllocator.h:85
T * allocate(size_t n) const
Allocate a block of memory.
Definition: AlignedAllocator.h:159
void construct(T *const p, const T &t) const
Construct an object in-place given a reference one.
Definition: AlignedAllocator.h:129
T value_type
The allocated type.
Definition: AlignedAllocator.h:69
bool operator!=(const AlignedAllocator &other) const
Check if two allocators are the same.
Definition: AlignedAllocator.h:119
const T * const_pointer
Const pointer to the allocated type.
Definition: AlignedAllocator.h:60
T & reference
Reference to the allocated type.
Definition: AlignedAllocator.h:63
T * pointer
Pointer to the allocated type.
Definition: AlignedAllocator.h:57
size_t max_size() const
Get the max possible allocation size the allocator supports.
Definition: AlignedAllocator.h:104
void deallocate(T *const p) const
Free a single object.
Definition: AlignedAllocator.h:208
Rebind to a different type of allocator.
Definition: AlignedAllocator.h:110