Use the difference in HF scale for upsampling ambisonics
[openal-soft.git] / common / almalloc.h
blob295107dc67d29f6fc1d17d778ad0d89b93882fcf
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 static_assert(&operator new == &T::operator new, \
33 "Incorrect container type specified"); \
34 if(void *ret{al_malloc(alignof(T), size)}) \
35 return ret; \
36 throw std::bad_alloc(); \
37 } \
38 void *operator new[](size_t size) { return operator new(size); } \
39 void operator delete(void *block) noexcept { al_free(block); } \
40 void operator delete[](void *block) noexcept { operator delete(block); }
42 #define DEF_PLACE_NEWDEL() \
43 void *operator new(size_t /*size*/, void *ptr) noexcept { return ptr; } \
44 void *operator new[](size_t /*size*/, void *ptr) noexcept { return ptr; } \
45 void operator delete(void *block, void*) noexcept { al_free(block); } \
46 void operator delete(void *block) noexcept { al_free(block); } \
47 void operator delete[](void *block, void*) noexcept { al_free(block); } \
48 void operator delete[](void *block) noexcept { al_free(block); }
50 enum FamCount : size_t { };
52 #define DEF_FAM_NEWDEL(T, FamMem) \
53 static constexpr size_t Sizeof(size_t count) noexcept \
54 { \
55 static_assert(&Sizeof == &T::Sizeof, \
56 "Incorrect container type specified"); \
57 return std::max(decltype(FamMem)::Sizeof(count, offsetof(T, FamMem)), \
58 sizeof(T)); \
59 } \
61 void *operator new(size_t /*size*/, FamCount count) \
62 { \
63 if(void *ret{al_malloc(alignof(T), T::Sizeof(count))}) \
64 return ret; \
65 throw std::bad_alloc(); \
66 } \
67 void *operator new[](size_t /*size*/) = delete; \
68 void operator delete(void *block, FamCount) { al_free(block); } \
69 void operator delete(void *block) noexcept { al_free(block); } \
70 void operator delete[](void* /*block*/) = delete;
73 namespace al {
75 template<typename T, std::size_t alignment=alignof(T)>
76 struct allocator {
77 using value_type = T;
78 using reference = T&;
79 using const_reference = const T&;
80 using pointer = T*;
81 using const_pointer = const T*;
82 using size_type = std::size_t;
83 using difference_type = std::ptrdiff_t;
84 using is_always_equal = std::true_type;
86 template<typename U>
87 struct rebind {
88 using other = allocator<U, (alignment<alignof(U))?alignof(U):alignment>;
91 constexpr explicit allocator() noexcept = default;
92 template<typename U, std::size_t N>
93 constexpr explicit allocator(const allocator<U,N>&) noexcept { }
95 T *allocate(std::size_t n)
97 if(n > std::numeric_limits<std::size_t>::max()/sizeof(T)) throw std::bad_alloc();
98 if(auto p = al_malloc(alignment, n*sizeof(T))) return static_cast<T*>(p);
99 throw std::bad_alloc();
101 void deallocate(T *p, std::size_t) noexcept { al_free(p); }
103 template<typename T, std::size_t N, typename U, std::size_t M>
104 constexpr bool operator==(const allocator<T,N>&, const allocator<U,M>&) noexcept { return true; }
105 template<typename T, std::size_t N, typename U, std::size_t M>
106 constexpr bool operator!=(const allocator<T,N>&, const allocator<U,M>&) noexcept { return false; }
109 template<typename T, typename ...Args>
110 constexpr T* construct_at(T *ptr, Args&& ...args)
111 noexcept(std::is_nothrow_constructible<T, Args...>::value)
112 { return ::new(static_cast<void*>(ptr)) T{std::forward<Args>(args)...}; }
114 /* At least VS 2015 complains that 'ptr' is unused when the given type's
115 * destructor is trivial (a no-op). So disable that warning for this call.
117 DIAGNOSTIC_PUSH
118 msc_pragma(warning(disable : 4100))
119 template<typename T>
120 constexpr std::enable_if_t<!std::is_array<T>::value>
121 destroy_at(T *ptr) noexcept(std::is_nothrow_destructible<T>::value)
122 { ptr->~T(); }
123 DIAGNOSTIC_POP
124 template<typename T>
125 constexpr std::enable_if_t<std::is_array<T>::value>
126 destroy_at(T *ptr) noexcept(std::is_nothrow_destructible<std::remove_all_extents_t<T>>::value)
128 for(auto &elem : *ptr)
129 al::destroy_at(std::addressof(elem));
132 template<typename T>
133 constexpr void destroy(T first, T end) noexcept(noexcept(al::destroy_at(std::addressof(*first))))
135 while(first != end)
137 al::destroy_at(std::addressof(*first));
138 ++first;
142 template<typename T, typename N>
143 constexpr std::enable_if_t<std::is_integral<N>::value,T>
144 destroy_n(T first, N count) noexcept(noexcept(al::destroy_at(std::addressof(*first))))
146 if(count != 0)
148 do {
149 al::destroy_at(std::addressof(*first));
150 ++first;
151 } while(--count);
153 return first;
157 template<typename T, typename N>
158 inline std::enable_if_t<std::is_integral<N>::value,
159 T> uninitialized_default_construct_n(T first, N count)
161 using ValueT = typename std::iterator_traits<T>::value_type;
162 T current{first};
163 if(count != 0)
165 try {
166 do {
167 ::new(static_cast<void*>(std::addressof(*current))) ValueT;
168 ++current;
169 } while(--count);
171 catch(...) {
172 al::destroy(first, current);
173 throw;
176 return current;
180 /* Storage for flexible array data. This is trivially destructible if type T is
181 * trivially destructible.
183 template<typename T, size_t alignment, bool = std::is_trivially_destructible<T>::value>
184 struct FlexArrayStorage {
185 const size_t mSize;
186 union {
187 char mDummy;
188 alignas(alignment) T mArray[1];
191 static constexpr size_t Sizeof(size_t count, size_t base=0u) noexcept
193 const size_t len{sizeof(T)*count};
194 return std::max(offsetof(FlexArrayStorage,mArray)+len, sizeof(FlexArrayStorage)) + base;
197 FlexArrayStorage(size_t size) : mSize{size}
198 { al::uninitialized_default_construct_n(mArray, mSize); }
199 ~FlexArrayStorage() = default;
201 FlexArrayStorage(const FlexArrayStorage&) = delete;
202 FlexArrayStorage& operator=(const FlexArrayStorage&) = delete;
205 template<typename T, size_t alignment>
206 struct FlexArrayStorage<T,alignment,false> {
207 const size_t mSize;
208 union {
209 char mDummy;
210 alignas(alignment) T mArray[1];
213 static constexpr size_t Sizeof(size_t count, size_t base) noexcept
215 const size_t len{sizeof(T)*count};
216 return std::max(offsetof(FlexArrayStorage,mArray)+len, sizeof(FlexArrayStorage)) + base;
219 FlexArrayStorage(size_t size) : mSize{size}
220 { al::uninitialized_default_construct_n(mArray, mSize); }
221 ~FlexArrayStorage() { al::destroy_n(mArray, mSize); }
223 FlexArrayStorage(const FlexArrayStorage&) = delete;
224 FlexArrayStorage& operator=(const FlexArrayStorage&) = delete;
227 /* A flexible array type. Used either standalone or at the end of a parent
228 * struct, with placement new, to have a run-time-sized array that's embedded
229 * with its size.
231 template<typename T, size_t alignment=alignof(T)>
232 struct FlexArray {
233 using element_type = T;
234 using value_type = std::remove_cv_t<T>;
235 using index_type = size_t;
236 using difference_type = ptrdiff_t;
238 using pointer = T*;
239 using const_pointer = const T*;
240 using reference = T&;
241 using const_reference = const T&;
243 using iterator = pointer;
244 using const_iterator = const_pointer;
245 using reverse_iterator = std::reverse_iterator<iterator>;
246 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
248 using Storage_t_ = FlexArrayStorage<element_type,alignment>;
250 Storage_t_ mStore;
252 static constexpr index_type Sizeof(index_type count, index_type base=0u) noexcept
253 { return Storage_t_::Sizeof(count, base); }
254 static std::unique_ptr<FlexArray> Create(index_type count)
256 void *ptr{al_calloc(alignof(FlexArray), Sizeof(count))};
257 return std::unique_ptr<FlexArray>{al::construct_at(static_cast<FlexArray*>(ptr), count)};
260 FlexArray(index_type size) : mStore{size} { }
261 ~FlexArray() = default;
263 index_type size() const noexcept { return mStore.mSize; }
264 bool empty() const noexcept { return mStore.mSize == 0; }
266 pointer data() noexcept { return mStore.mArray; }
267 const_pointer data() const noexcept { return mStore.mArray; }
269 reference operator[](index_type i) noexcept { return mStore.mArray[i]; }
270 const_reference operator[](index_type i) const noexcept { return mStore.mArray[i]; }
272 reference front() noexcept { return mStore.mArray[0]; }
273 const_reference front() const noexcept { return mStore.mArray[0]; }
275 reference back() noexcept { return mStore.mArray[mStore.mSize-1]; }
276 const_reference back() const noexcept { return mStore.mArray[mStore.mSize-1]; }
278 iterator begin() noexcept { return mStore.mArray; }
279 const_iterator begin() const noexcept { return mStore.mArray; }
280 const_iterator cbegin() const noexcept { return mStore.mArray; }
281 iterator end() noexcept { return mStore.mArray + mStore.mSize; }
282 const_iterator end() const noexcept { return mStore.mArray + mStore.mSize; }
283 const_iterator cend() const noexcept { return mStore.mArray + mStore.mSize; }
285 reverse_iterator rbegin() noexcept { return end(); }
286 const_reverse_iterator rbegin() const noexcept { return end(); }
287 const_reverse_iterator crbegin() const noexcept { return cend(); }
288 reverse_iterator rend() noexcept { return begin(); }
289 const_reverse_iterator rend() const noexcept { return begin(); }
290 const_reverse_iterator crend() const noexcept { return cbegin(); }
292 DEF_PLACE_NEWDEL()
295 } // namespace al
297 #endif /* AL_MALLOC_H */