Use an array of pointers for the UHJ encoder input
[openal-soft.git] / common / almalloc.h
blob711d02fd03ed45a2b6f8b8ccb20e3ad8d463c792
1 #ifndef AL_MALLOC_H
2 #define AL_MALLOC_H
4 #include <algorithm>
5 #include <cstddef>
6 #include <iterator>
7 #include <limits>
8 #include <memory>
9 #include <new>
10 #include <type_traits>
11 #include <utility>
13 #include "pragmadefs.h"
16 void al_free(void *ptr) noexcept;
17 [[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]]
18 void *al_malloc(size_t alignment, size_t size);
19 [[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]]
20 void *al_calloc(size_t alignment, size_t size);
23 #define DISABLE_ALLOC() \
24 void *operator new(size_t) = delete; \
25 void *operator new[](size_t) = delete; \
26 void operator delete(void*) noexcept = delete; \
27 void operator delete[](void*) noexcept = delete;
29 #define DEF_NEWDEL(T) \
30 void *operator new(size_t size) \
31 { \
32 void *ret = al_malloc(alignof(T), size); \
33 if(!ret) throw std::bad_alloc(); \
34 return ret; \
35 } \
36 void *operator new[](size_t size) { return operator new(size); } \
37 void operator delete(void *block) noexcept { al_free(block); } \
38 void operator delete[](void *block) noexcept { operator delete(block); }
40 #define DEF_PLACE_NEWDEL() \
41 void *operator new(size_t /*size*/, void *ptr) noexcept { return ptr; } \
42 void *operator new[](size_t /*size*/, void *ptr) noexcept { return ptr; } \
43 void operator delete(void *block, void*) noexcept { al_free(block); } \
44 void operator delete(void *block) noexcept { al_free(block); } \
45 void operator delete[](void *block, void*) noexcept { al_free(block); } \
46 void operator delete[](void *block) noexcept { al_free(block); }
48 enum FamCount : size_t { };
50 #define DEF_FAM_NEWDEL(T, FamMem) \
51 static constexpr size_t Sizeof(size_t count) noexcept \
52 { \
53 return std::max(decltype(FamMem)::Sizeof(count, offsetof(T, FamMem)), \
54 sizeof(T)); \
55 } \
57 void *operator new(size_t /*size*/, FamCount count) \
58 { \
59 if(void *ret{al_malloc(alignof(T), T::Sizeof(count))}) \
60 return ret; \
61 throw std::bad_alloc(); \
62 } \
63 void *operator new[](size_t /*size*/) = delete; \
64 void operator delete(void *block, FamCount) { al_free(block); } \
65 void operator delete(void *block) noexcept { al_free(block); } \
66 void operator delete[](void* /*block*/) = delete;
69 namespace al {
71 template<typename T, std::size_t alignment=alignof(T)>
72 struct allocator {
73 using value_type = T;
74 using reference = T&;
75 using const_reference = const T&;
76 using pointer = T*;
77 using const_pointer = const T*;
78 using size_type = std::size_t;
79 using difference_type = std::ptrdiff_t;
80 using is_always_equal = std::true_type;
82 template<typename U>
83 struct rebind {
84 using other = allocator<U, (alignment<alignof(U))?alignof(U):alignment>;
87 constexpr explicit allocator() noexcept = default;
88 template<typename U, std::size_t N>
89 constexpr explicit allocator(const allocator<U,N>&) noexcept { }
91 T *allocate(std::size_t n)
93 if(n > std::numeric_limits<std::size_t>::max()/sizeof(T)) throw std::bad_alloc();
94 if(auto p = al_malloc(alignment, n*sizeof(T))) return static_cast<T*>(p);
95 throw std::bad_alloc();
97 void deallocate(T *p, std::size_t) noexcept { al_free(p); }
99 template<typename T, std::size_t N, typename U, std::size_t M>
100 constexpr bool operator==(const allocator<T,N>&, const allocator<U,M>&) noexcept { return true; }
101 template<typename T, std::size_t N, typename U, std::size_t M>
102 constexpr bool operator!=(const allocator<T,N>&, const allocator<U,M>&) noexcept { return false; }
105 template<typename T, typename ...Args>
106 constexpr T* construct_at(T *ptr, Args&& ...args)
107 noexcept(std::is_nothrow_constructible<T, Args...>::value)
108 { return ::new(static_cast<void*>(ptr)) T{std::forward<Args>(args)...}; }
110 /* At least VS 2015 complains that 'ptr' is unused when the given type's
111 * destructor is trivial (a no-op). So disable that warning for this call.
113 DIAGNOSTIC_PUSH
114 msc_pragma(warning(disable : 4100))
115 template<typename T>
116 constexpr std::enable_if_t<!std::is_array<T>::value>
117 destroy_at(T *ptr) noexcept(std::is_nothrow_destructible<T>::value)
118 { ptr->~T(); }
119 DIAGNOSTIC_POP
120 template<typename T>
121 constexpr std::enable_if_t<std::is_array<T>::value>
122 destroy_at(T *ptr) noexcept(std::is_nothrow_destructible<std::remove_all_extents_t<T>>::value)
124 for(auto &elem : *ptr)
125 al::destroy_at(std::addressof(elem));
128 template<typename T>
129 constexpr void destroy(T first, T end) noexcept(noexcept(al::destroy_at(std::addressof(*first))))
131 while(first != end)
133 al::destroy_at(std::addressof(*first));
134 ++first;
138 template<typename T, typename N>
139 constexpr std::enable_if_t<std::is_integral<N>::value,T>
140 destroy_n(T first, N count) noexcept(noexcept(al::destroy_at(std::addressof(*first))))
142 if(count != 0)
144 do {
145 al::destroy_at(std::addressof(*first));
146 ++first;
147 } while(--count);
149 return first;
153 template<typename T, typename N>
154 inline std::enable_if_t<std::is_integral<N>::value,
155 T> uninitialized_default_construct_n(T first, N count)
157 using ValueT = typename std::iterator_traits<T>::value_type;
158 T current{first};
159 if(count != 0)
161 try {
162 do {
163 ::new(static_cast<void*>(std::addressof(*current))) ValueT;
164 ++current;
165 } while(--count);
167 catch(...) {
168 al::destroy(first, current);
169 throw;
172 return current;
176 /* Storage for flexible array data. This is trivially destructible if type T is
177 * trivially destructible.
179 template<typename T, size_t alignment, bool = std::is_trivially_destructible<T>::value>
180 struct FlexArrayStorage {
181 const size_t mSize;
182 union {
183 char mDummy;
184 alignas(alignment) T mArray[1];
187 static constexpr size_t Sizeof(size_t count, size_t base=0u) noexcept
189 const size_t len{sizeof(T)*count};
190 return std::max(offsetof(FlexArrayStorage,mArray)+len, sizeof(FlexArrayStorage)) + base;
193 FlexArrayStorage(size_t size) : mSize{size}
194 { al::uninitialized_default_construct_n(mArray, mSize); }
195 ~FlexArrayStorage() = default;
197 FlexArrayStorage(const FlexArrayStorage&) = delete;
198 FlexArrayStorage& operator=(const FlexArrayStorage&) = delete;
201 template<typename T, size_t alignment>
202 struct FlexArrayStorage<T,alignment,false> {
203 const size_t mSize;
204 union {
205 char mDummy;
206 alignas(alignment) T mArray[1];
209 static constexpr size_t Sizeof(size_t count, size_t base) noexcept
211 const size_t len{sizeof(T)*count};
212 return std::max(offsetof(FlexArrayStorage,mArray)+len, sizeof(FlexArrayStorage)) + base;
215 FlexArrayStorage(size_t size) : mSize{size}
216 { al::uninitialized_default_construct_n(mArray, mSize); }
217 ~FlexArrayStorage() { al::destroy_n(mArray, mSize); }
219 FlexArrayStorage(const FlexArrayStorage&) = delete;
220 FlexArrayStorage& operator=(const FlexArrayStorage&) = delete;
223 /* A flexible array type. Used either standalone or at the end of a parent
224 * struct, with placement new, to have a run-time-sized array that's embedded
225 * with its size.
227 template<typename T, size_t alignment=alignof(T)>
228 struct FlexArray {
229 using element_type = T;
230 using value_type = std::remove_cv_t<T>;
231 using index_type = size_t;
232 using difference_type = ptrdiff_t;
234 using pointer = T*;
235 using const_pointer = const T*;
236 using reference = T&;
237 using const_reference = const T&;
239 using iterator = pointer;
240 using const_iterator = const_pointer;
241 using reverse_iterator = std::reverse_iterator<iterator>;
242 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
244 using Storage_t_ = FlexArrayStorage<element_type,alignment>;
246 Storage_t_ mStore;
248 static constexpr index_type Sizeof(index_type count, index_type base=0u) noexcept
249 { return Storage_t_::Sizeof(count, base); }
250 static std::unique_ptr<FlexArray> Create(index_type count)
252 void *ptr{al_calloc(alignof(FlexArray), Sizeof(count))};
253 return std::unique_ptr<FlexArray>{al::construct_at(static_cast<FlexArray*>(ptr), count)};
256 FlexArray(index_type size) : mStore{size} { }
257 ~FlexArray() = default;
259 index_type size() const noexcept { return mStore.mSize; }
260 bool empty() const noexcept { return mStore.mSize == 0; }
262 pointer data() noexcept { return mStore.mArray; }
263 const_pointer data() const noexcept { return mStore.mArray; }
265 reference operator[](index_type i) noexcept { return mStore.mArray[i]; }
266 const_reference operator[](index_type i) const noexcept { return mStore.mArray[i]; }
268 reference front() noexcept { return mStore.mArray[0]; }
269 const_reference front() const noexcept { return mStore.mArray[0]; }
271 reference back() noexcept { return mStore.mArray[mStore.mSize-1]; }
272 const_reference back() const noexcept { return mStore.mArray[mStore.mSize-1]; }
274 iterator begin() noexcept { return mStore.mArray; }
275 const_iterator begin() const noexcept { return mStore.mArray; }
276 const_iterator cbegin() const noexcept { return mStore.mArray; }
277 iterator end() noexcept { return mStore.mArray + mStore.mSize; }
278 const_iterator end() const noexcept { return mStore.mArray + mStore.mSize; }
279 const_iterator cend() const noexcept { return mStore.mArray + mStore.mSize; }
281 reverse_iterator rbegin() noexcept { return end(); }
282 const_reverse_iterator rbegin() const noexcept { return end(); }
283 const_reverse_iterator crbegin() const noexcept { return cend(); }
284 reverse_iterator rend() noexcept { return begin(); }
285 const_reverse_iterator rend() const noexcept { return begin(); }
286 const_reverse_iterator crend() const noexcept { return cbegin(); }
288 DEF_PLACE_NEWDEL()
291 } // namespace al
293 #endif /* AL_MALLOC_H */