10 #include <type_traits>
13 #include "pragmadefs.h"
16 [[gnu::alloc_align(1), gnu::alloc_size(2)]] void *al_malloc(size_t alignment
, size_t size
);
17 [[gnu::alloc_align(1), gnu::alloc_size(2)]] void *al_calloc(size_t alignment
, size_t size
);
18 void al_free(void *ptr
) noexcept
;
21 #define DISABLE_ALLOC() \
22 void *operator new(size_t) = delete; \
23 void *operator new[](size_t) = delete; \
24 void operator delete(void*) noexcept = delete; \
25 void operator delete[](void*) noexcept = delete;
27 #define DEF_NEWDEL(T) \
28 void *operator new(size_t size) \
30 void *ret = al_malloc(alignof(T), size); \
31 if(!ret) throw std::bad_alloc(); \
34 void *operator new[](size_t size) { return operator new(size); } \
35 void operator delete(void *block) noexcept { al_free(block); } \
36 void operator delete[](void *block) noexcept { operator delete(block); }
38 #define DEF_PLACE_NEWDEL() \
39 void *operator new(size_t /*size*/, void *ptr) noexcept { return ptr; } \
40 void *operator new[](size_t /*size*/, void *ptr) noexcept { return ptr; } \
41 void operator delete(void *block, void*) noexcept { al_free(block); } \
42 void operator delete(void *block) noexcept { al_free(block); } \
43 void operator delete[](void *block, void*) noexcept { al_free(block); } \
44 void operator delete[](void *block) noexcept { al_free(block); }
46 enum FamCount
: size_t { };
48 #define DEF_FAM_NEWDEL(T, FamMem) \
49 static constexpr size_t Sizeof(size_t count) noexcept \
51 return std::max<size_t>(sizeof(T), \
52 decltype(FamMem)::Sizeof(count, offsetof(T, FamMem))); \
55 void *operator new(size_t /*size*/, FamCount count) \
57 if(void *ret{al_malloc(alignof(T), T::Sizeof(count))}) \
59 throw std::bad_alloc(); \
61 void *operator new[](size_t /*size*/) = delete; \
62 void operator delete(void *block, FamCount) { al_free(block); } \
63 void operator delete(void *block) noexcept { al_free(block); } \
64 void operator delete[](void* /*block*/) = delete;
69 template<typename T
, std::size_t alignment
=alignof(T
)>
73 using const_reference
= const T
&;
75 using const_pointer
= const T
*;
76 using size_type
= std::size_t;
77 using difference_type
= std::ptrdiff_t;
78 using is_always_equal
= std::true_type
;
82 using other
= allocator
<U
, (alignment
<alignof(U
))?alignof(U
):alignment
>;
85 constexpr explicit allocator() noexcept
= default;
86 template<typename U
, std::size_t N
>
87 constexpr explicit allocator(const allocator
<U
,N
>&) noexcept
{ }
89 T
*allocate(std::size_t n
)
91 if(n
> std::numeric_limits
<std::size_t>::max()/sizeof(T
)) throw std::bad_alloc();
92 if(auto p
= al_malloc(alignment
, n
*sizeof(T
))) return static_cast<T
*>(p
);
93 throw std::bad_alloc();
95 void deallocate(T
*p
, std::size_t) noexcept
{ al_free(p
); }
97 template<typename T
, std::size_t N
, typename U
, std::size_t M
>
98 bool operator==(const allocator
<T
,N
>&, const allocator
<U
,M
>&) noexcept
{ return true; }
99 template<typename T
, std::size_t N
, typename U
, std::size_t M
>
100 bool operator!=(const allocator
<T
,N
>&, const allocator
<U
,M
>&) noexcept
{ return false; }
102 template<size_t alignment
, typename T
>
103 [[gnu::assume_aligned(alignment
)]] inline T
* assume_aligned(T
*ptr
) noexcept
{ return ptr
; }
105 /* At least VS 2015 complains that 'ptr' is unused when the given type's
106 * destructor is trivial (a no-op). So disable that warning for this call.
109 msc_pragma(warning(disable
: 4100))
111 constexpr std::enable_if_t
<!std::is_array
<T
>::value
>
112 destroy_at(T
*ptr
) noexcept(std::is_nothrow_destructible
<T
>::value
)
116 constexpr std::enable_if_t
<std::is_array
<T
>::value
>
117 destroy_at(T
*ptr
) noexcept(std::is_nothrow_destructible
<T
>::value
)
119 for(auto &elem
: *ptr
)
120 al::destroy_at(std::addressof(elem
));
124 constexpr void destroy(T first
, T end
)
128 al::destroy_at(std::addressof(*first
));
133 template<typename T
, typename N
>
134 constexpr std::enable_if_t
<std::is_integral
<N
>::value
,T
>
135 destroy_n(T first
, N count
)
140 al::destroy_at(std::addressof(*first
));
148 template<typename T
, typename N
>
149 inline std::enable_if_t
<std::is_integral
<N
>::value
,T
>
150 uninitialized_default_construct_n(T first
, N count
)
152 using ValueT
= typename
std::iterator_traits
<T
>::value_type
;
158 ::new(static_cast<void*>(std::addressof(*current
))) ValueT
;
163 al::destroy(first
, current
);
171 /* Storage for flexible array data. This is trivially destructible if type T is
172 * trivially destructible.
174 template<typename T
, size_t alignment
, bool = std::is_trivially_destructible
<T
>::value
>
175 struct FlexArrayStorage
;
177 template<typename T
, size_t alignment
>
178 struct FlexArrayStorage
<T
,alignment
,true> {
182 alignas(alignment
) T mArray
[1];
185 static constexpr size_t Sizeof(size_t count
, size_t base
=0u) noexcept
187 return std::max
<size_t>(offsetof(FlexArrayStorage
, mArray
) + sizeof(T
)*count
,
188 sizeof(FlexArrayStorage
)) + base
;
191 FlexArrayStorage(size_t size
) : mSize
{size
}
192 { al::uninitialized_default_construct_n(mArray
, mSize
); }
193 ~FlexArrayStorage() = default;
195 FlexArrayStorage(const FlexArrayStorage
&) = delete;
196 FlexArrayStorage
& operator=(const FlexArrayStorage
&) = delete;
199 template<typename T
, size_t alignment
>
200 struct FlexArrayStorage
<T
,alignment
,false> {
204 alignas(alignment
) T mArray
[1];
207 static constexpr size_t Sizeof(size_t count
, size_t base
) noexcept
209 return std::max
<size_t>(offsetof(FlexArrayStorage
, mArray
) + sizeof(T
)*count
,
210 sizeof(FlexArrayStorage
)) + base
;
213 FlexArrayStorage(size_t size
) : mSize
{size
}
214 { al::uninitialized_default_construct_n(mArray
, mSize
); }
215 ~FlexArrayStorage() { al::destroy_n(mArray
, mSize
); }
217 FlexArrayStorage(const FlexArrayStorage
&) = delete;
218 FlexArrayStorage
& operator=(const FlexArrayStorage
&) = delete;
221 /* A flexible array type. Used either standalone or at the end of a parent
222 * struct, with placement new, to have a run-time-sized array that's embedded
225 template<typename T
, size_t alignment
=alignof(T
)>
227 using element_type
= T
;
228 using value_type
= std::remove_cv_t
<T
>;
229 using index_type
= size_t;
230 using difference_type
= ptrdiff_t;
233 using const_pointer
= const T
*;
234 using reference
= T
&;
235 using const_reference
= const T
&;
237 using iterator
= pointer
;
238 using const_iterator
= const_pointer
;
239 using reverse_iterator
= std::reverse_iterator
<iterator
>;
240 using const_reverse_iterator
= std::reverse_iterator
<const_iterator
>;
242 using Storage_t_
= FlexArrayStorage
<element_type
,alignment
>;
246 static constexpr index_type
Sizeof(index_type count
, index_type base
=0u) noexcept
247 { return Storage_t_::Sizeof(count
, base
); }
248 static std::unique_ptr
<FlexArray
> Create(index_type count
)
250 void *ptr
{al_calloc(alignof(FlexArray
), Sizeof(count
))};
251 return std::unique_ptr
<FlexArray
>{new(ptr
) FlexArray
{count
}};
254 FlexArray(index_type size
) : mStore
{size
} { }
255 ~FlexArray() = default;
257 index_type
size() const noexcept
{ return mStore
.mSize
; }
258 bool empty() const noexcept
{ return mStore
.mSize
== 0; }
260 pointer
data() noexcept
{ return mStore
.mArray
; }
261 const_pointer
data() const noexcept
{ return mStore
.mArray
; }
263 reference
operator[](index_type i
) noexcept
{ return mStore
.mArray
[i
]; }
264 const_reference
operator[](index_type i
) const noexcept
{ return mStore
.mArray
[i
]; }
266 reference
front() noexcept
{ return mStore
.mArray
[0]; }
267 const_reference
front() const noexcept
{ return mStore
.mArray
[0]; }
269 reference
back() noexcept
{ return mStore
.mArray
[mStore
.mSize
-1]; }
270 const_reference
back() const noexcept
{ return mStore
.mArray
[mStore
.mSize
-1]; }
272 iterator
begin() noexcept
{ return mStore
.mArray
; }
273 const_iterator
begin() const noexcept
{ return mStore
.mArray
; }
274 const_iterator
cbegin() const noexcept
{ return mStore
.mArray
; }
275 iterator
end() noexcept
{ return mStore
.mArray
+ mStore
.mSize
; }
276 const_iterator
end() const noexcept
{ return mStore
.mArray
+ mStore
.mSize
; }
277 const_iterator
cend() const noexcept
{ return mStore
.mArray
+ mStore
.mSize
; }
279 reverse_iterator
rbegin() noexcept
{ return end(); }
280 const_reverse_iterator
rbegin() const noexcept
{ return end(); }
281 const_reverse_iterator
crbegin() const noexcept
{ return cend(); }
282 reverse_iterator
rend() noexcept
{ return begin(); }
283 const_reverse_iterator
rend() const noexcept
{ return begin(); }
284 const_reverse_iterator
crend() const noexcept
{ return cbegin(); }
291 #endif /* AL_MALLOC_H */