2016-03-29 07:44:58 +00:00
|
|
|
#ifndef AL_MALLOC_H
|
|
|
|
#define AL_MALLOC_H
|
|
|
|
|
2019-01-11 16:07:25 +00:00
|
|
|
#include <algorithm>
|
2019-07-29 16:29:35 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
|
|
|
#include <new>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2018-11-18 09:33:26 +00:00
|
|
|
|
2019-10-07 22:17:45 +00:00
|
|
|
#include "pragmadefs.h"
|
2017-02-28 04:43:16 +00:00
|
|
|
|
2019-09-18 19:16:08 +00:00
|
|
|
|
2020-04-10 18:04:19 +00:00
|
|
|
[[gnu::alloc_align(1), gnu::alloc_size(2)]] void *al_malloc(size_t alignment, size_t size);
|
|
|
|
[[gnu::alloc_align(1), gnu::alloc_size(2)]] void *al_calloc(size_t alignment, size_t size);
|
2018-11-18 08:38:31 +00:00
|
|
|
void al_free(void *ptr) noexcept;
|
2016-03-29 07:44:58 +00:00
|
|
|
|
2018-11-18 08:38:31 +00:00
|
|
|
|
2020-03-23 23:00:50 +00:00
|
|
|
#define DISABLE_ALLOC() \
|
|
|
|
void *operator new(size_t) = delete; \
|
|
|
|
void *operator new[](size_t) = delete; \
|
|
|
|
void operator delete(void*) noexcept = delete; \
|
|
|
|
void operator delete[](void*) noexcept = delete;
|
|
|
|
|
2018-11-18 08:38:31 +00:00
|
|
|
#define DEF_NEWDEL(T) \
|
|
|
|
void *operator new(size_t size) \
|
|
|
|
{ \
|
|
|
|
void *ret = al_malloc(alignof(T), size); \
|
|
|
|
if(!ret) throw std::bad_alloc(); \
|
|
|
|
return ret; \
|
|
|
|
} \
|
2020-03-22 20:28:45 +00:00
|
|
|
void *operator new[](size_t size) { return operator new(size); } \
|
|
|
|
void operator delete(void *block) noexcept { al_free(block); } \
|
|
|
|
void operator delete[](void *block) noexcept { operator delete(block); }
|
2016-03-29 07:44:58 +00:00
|
|
|
|
2018-11-22 15:06:42 +00:00
|
|
|
#define DEF_PLACE_NEWDEL() \
|
|
|
|
void *operator new(size_t /*size*/, void *ptr) noexcept { return ptr; } \
|
2020-03-22 20:28:45 +00:00
|
|
|
void *operator new[](size_t /*size*/, void *ptr) noexcept { return ptr; } \
|
2019-09-11 10:59:53 +00:00
|
|
|
void operator delete(void *block, void*) noexcept { al_free(block); } \
|
2020-03-22 20:28:45 +00:00
|
|
|
void operator delete(void *block) noexcept { al_free(block); } \
|
|
|
|
void operator delete[](void *block, void*) noexcept { al_free(block); } \
|
|
|
|
void operator delete[](void *block) noexcept { al_free(block); }
|
2019-09-11 10:59:53 +00:00
|
|
|
|
2020-05-19 19:15:57 +00:00
|
|
|
enum FamCount : size_t { };
|
2019-09-11 10:59:53 +00:00
|
|
|
|
|
|
|
#define DEF_FAM_NEWDEL(T, FamMem) \
|
|
|
|
static constexpr size_t Sizeof(size_t count) noexcept \
|
2020-08-16 06:53:43 +00:00
|
|
|
{ \
|
|
|
|
return std::max<size_t>(sizeof(T), \
|
|
|
|
decltype(FamMem)::Sizeof(count, offsetof(T, FamMem))); \
|
|
|
|
} \
|
2019-09-11 10:59:53 +00:00
|
|
|
\
|
2020-05-19 19:15:57 +00:00
|
|
|
void *operator new(size_t /*size*/, FamCount count) \
|
2019-09-11 10:59:53 +00:00
|
|
|
{ \
|
2020-05-19 19:15:57 +00:00
|
|
|
if(void *ret{al_malloc(alignof(T), T::Sizeof(count))}) \
|
2019-09-11 10:59:53 +00:00
|
|
|
return ret; \
|
|
|
|
throw std::bad_alloc(); \
|
|
|
|
} \
|
2020-03-22 20:28:45 +00:00
|
|
|
void *operator new[](size_t /*size*/) = delete; \
|
2019-09-12 11:30:52 +00:00
|
|
|
void operator delete(void *block, FamCount) { al_free(block); } \
|
2020-03-22 20:28:45 +00:00
|
|
|
void operator delete(void *block) noexcept { al_free(block); } \
|
|
|
|
void operator delete[](void* /*block*/) = delete;
|
2019-09-11 10:59:53 +00:00
|
|
|
|
2018-11-22 15:06:42 +00:00
|
|
|
|
2018-11-18 09:33:26 +00:00
|
|
|
namespace al {
|
|
|
|
|
2019-06-05 23:38:53 +00:00
|
|
|
#define REQUIRES(...) typename std::enable_if<(__VA_ARGS__),int>::type = 0
|
|
|
|
|
2019-09-11 10:22:10 +00:00
|
|
|
template<typename T, std::size_t alignment=alignof(T)>
|
|
|
|
struct allocator {
|
|
|
|
using value_type = T;
|
2019-10-01 08:49:21 +00:00
|
|
|
using reference = T&;
|
|
|
|
using const_reference = const T&;
|
|
|
|
using pointer = T*;
|
|
|
|
using const_pointer = const T*;
|
|
|
|
using size_type = std::size_t;
|
|
|
|
using difference_type = std::ptrdiff_t;
|
2019-09-11 10:22:10 +00:00
|
|
|
using is_always_equal = std::true_type;
|
2018-11-18 09:33:26 +00:00
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
struct rebind {
|
2019-09-11 10:22:10 +00:00
|
|
|
using other = allocator<U, (alignment<alignof(U))?alignof(U):alignment>;
|
2018-11-18 09:33:26 +00:00
|
|
|
};
|
|
|
|
|
2020-04-10 02:32:02 +00:00
|
|
|
allocator() noexcept = default;
|
2019-09-12 11:30:52 +00:00
|
|
|
template<typename U, std::size_t N>
|
|
|
|
constexpr allocator(const allocator<U,N>&) noexcept { }
|
2018-11-18 09:33:26 +00:00
|
|
|
|
2020-04-10 18:04:19 +00:00
|
|
|
[[gnu::assume_aligned(alignment), gnu::alloc_size(2)]] T *allocate(std::size_t n)
|
2019-09-11 10:22:10 +00:00
|
|
|
{
|
2019-09-12 11:30:52 +00:00
|
|
|
if(n > std::numeric_limits<std::size_t>::max()/sizeof(T)) throw std::bad_alloc();
|
2019-09-11 10:22:10 +00:00
|
|
|
if(auto p = static_cast<T*>(al_malloc(alignment, n*sizeof(T)))) return p;
|
|
|
|
throw std::bad_alloc();
|
2018-11-18 09:33:26 +00:00
|
|
|
}
|
2019-09-11 10:22:10 +00:00
|
|
|
void deallocate(T *p, std::size_t) noexcept { al_free(p); }
|
2018-11-18 09:33:26 +00:00
|
|
|
};
|
2019-09-11 14:32:14 +00:00
|
|
|
template<typename T, std::size_t N, typename U, std::size_t M>
|
|
|
|
bool operator==(const allocator<T,N>&, const allocator<U,M>&) noexcept { return true; }
|
|
|
|
template<typename T, std::size_t N, typename U, std::size_t M>
|
|
|
|
bool operator!=(const allocator<T,N>&, const allocator<U,M>&) noexcept { return false; }
|
2018-11-18 09:33:26 +00:00
|
|
|
|
2018-12-20 19:46:40 +00:00
|
|
|
template<size_t alignment, typename T>
|
2020-04-10 18:04:19 +00:00
|
|
|
[[gnu::assume_aligned(alignment)]] inline T* assume_aligned(T *ptr) noexcept { return ptr; }
|
2018-12-20 19:46:40 +00:00
|
|
|
|
2019-09-18 19:16:08 +00:00
|
|
|
/* At least VS 2015 complains that 'ptr' is unused when the given type's
|
|
|
|
* destructor is trivial (a no-op). So disable that warning for this call.
|
|
|
|
*/
|
|
|
|
DIAGNOSTIC_PUSH
|
2019-10-07 22:17:45 +00:00
|
|
|
msc_pragma(warning(disable : 4100))
|
2019-06-05 23:38:53 +00:00
|
|
|
template<typename T>
|
|
|
|
inline void destroy_at(T *ptr) { ptr->~T(); }
|
2019-09-18 19:16:08 +00:00
|
|
|
DIAGNOSTIC_POP
|
2019-06-05 23:38:53 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void destroy(T first, const T end)
|
|
|
|
{
|
|
|
|
while(first != end)
|
|
|
|
{
|
2019-06-21 16:25:09 +00:00
|
|
|
al::destroy_at(std::addressof(*first));
|
2019-06-05 23:38:53 +00:00
|
|
|
++first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename N, REQUIRES(std::is_integral<N>::value)>
|
|
|
|
inline T destroy_n(T first, N count)
|
|
|
|
{
|
|
|
|
if(count != 0)
|
|
|
|
{
|
|
|
|
do {
|
2019-06-21 16:25:09 +00:00
|
|
|
al::destroy_at(std::addressof(*first));
|
2019-06-05 23:38:53 +00:00
|
|
|
++first;
|
|
|
|
} while(--count);
|
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, typename N, REQUIRES(std::is_integral<N>::value)>
|
|
|
|
inline T uninitialized_default_construct_n(T first, N count)
|
|
|
|
{
|
|
|
|
using ValueT = typename std::iterator_traits<T>::value_type;
|
|
|
|
T current{first};
|
|
|
|
if(count != 0)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
do {
|
2020-03-24 00:18:11 +00:00
|
|
|
::new(static_cast<void*>(std::addressof(*current))) ValueT;
|
2019-06-05 23:38:53 +00:00
|
|
|
++current;
|
|
|
|
} while(--count);
|
|
|
|
}
|
|
|
|
catch(...) {
|
2020-01-06 09:40:21 +00:00
|
|
|
al::destroy(first, current);
|
2019-06-05 23:38:53 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-11 14:00:41 +00:00
|
|
|
/* A flexible array type. Used either standalone or at the end of a parent
|
|
|
|
* struct, with placement new, to have a run-time-sized array that's embedded
|
|
|
|
* with its size.
|
|
|
|
*/
|
2019-06-04 05:58:56 +00:00
|
|
|
template<typename T, size_t alignment=alignof(T)>
|
2019-01-11 14:00:41 +00:00
|
|
|
struct FlexArray {
|
2019-06-08 06:42:31 +00:00
|
|
|
using element_type = T;
|
2020-03-24 00:18:11 +00:00
|
|
|
using value_type = std::remove_cv_t<T>;
|
2019-06-08 06:42:31 +00:00
|
|
|
using index_type = size_t;
|
|
|
|
using difference_type = ptrdiff_t;
|
2019-01-11 14:00:41 +00:00
|
|
|
|
2019-06-08 06:42:31 +00:00
|
|
|
using pointer = T*;
|
|
|
|
using const_pointer = const T*;
|
|
|
|
using reference = T&;
|
|
|
|
using const_reference = const T&;
|
|
|
|
|
|
|
|
using iterator = pointer;
|
|
|
|
using const_iterator = const_pointer;
|
|
|
|
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
|
|
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
|
|
|
|
|
|
|
|
|
|
const index_type mSize;
|
2020-08-16 06:22:10 +00:00
|
|
|
union {
|
|
|
|
char mDummy;
|
|
|
|
alignas(alignment) element_type mArray[1];
|
|
|
|
};
|
2019-06-08 06:42:31 +00:00
|
|
|
|
2019-08-03 21:59:01 +00:00
|
|
|
static std::unique_ptr<FlexArray> Create(index_type count)
|
|
|
|
{
|
|
|
|
void *ptr{al_calloc(alignof(FlexArray), Sizeof(count))};
|
2020-03-24 00:18:11 +00:00
|
|
|
return std::unique_ptr<FlexArray>{new(ptr) FlexArray{count}};
|
2019-08-03 21:59:01 +00:00
|
|
|
}
|
2019-06-08 06:42:31 +00:00
|
|
|
static constexpr index_type Sizeof(index_type count, index_type base=0u) noexcept
|
2019-01-12 04:06:23 +00:00
|
|
|
{
|
|
|
|
return base +
|
2019-06-08 06:42:31 +00:00
|
|
|
std::max<index_type>(offsetof(FlexArray, mArray) + sizeof(T)*count, sizeof(FlexArray));
|
2019-01-12 04:06:23 +00:00
|
|
|
}
|
2019-01-11 14:00:41 +00:00
|
|
|
|
2019-06-08 06:42:31 +00:00
|
|
|
FlexArray(index_type size) : mSize{size}
|
2020-01-06 09:40:21 +00:00
|
|
|
{ al::uninitialized_default_construct_n(mArray, mSize); }
|
|
|
|
~FlexArray() { al::destroy_n(mArray, mSize); }
|
2019-01-11 14:00:41 +00:00
|
|
|
|
|
|
|
FlexArray(const FlexArray&) = delete;
|
|
|
|
FlexArray& operator=(const FlexArray&) = delete;
|
|
|
|
|
2019-06-08 06:42:31 +00:00
|
|
|
index_type size() const noexcept { return mSize; }
|
2019-06-30 01:53:20 +00:00
|
|
|
bool empty() const noexcept { return mSize == 0; }
|
2019-06-08 06:42:31 +00:00
|
|
|
|
|
|
|
pointer data() noexcept { return mArray; }
|
|
|
|
const_pointer data() const noexcept { return mArray; }
|
2019-01-11 14:00:41 +00:00
|
|
|
|
2019-06-08 06:42:31 +00:00
|
|
|
reference operator[](index_type i) noexcept { return mArray[i]; }
|
|
|
|
const_reference operator[](index_type i) const noexcept { return mArray[i]; }
|
2019-01-11 14:00:41 +00:00
|
|
|
|
2019-06-08 06:42:31 +00:00
|
|
|
reference front() noexcept { return mArray[0]; }
|
|
|
|
const_reference front() const noexcept { return mArray[0]; }
|
2019-01-11 14:00:41 +00:00
|
|
|
|
2019-06-08 06:42:31 +00:00
|
|
|
reference back() noexcept { return mArray[mSize-1]; }
|
|
|
|
const_reference back() const noexcept { return mArray[mSize-1]; }
|
2019-01-17 10:23:57 +00:00
|
|
|
|
2019-06-08 06:42:31 +00:00
|
|
|
iterator begin() noexcept { return mArray; }
|
|
|
|
const_iterator begin() const noexcept { return mArray; }
|
|
|
|
const_iterator cbegin() const noexcept { return mArray; }
|
|
|
|
iterator end() noexcept { return mArray + mSize; }
|
|
|
|
const_iterator end() const noexcept { return mArray + mSize; }
|
|
|
|
const_iterator cend() const noexcept { return mArray + mSize; }
|
2019-01-17 10:23:57 +00:00
|
|
|
|
2019-06-08 06:42:31 +00:00
|
|
|
reverse_iterator rbegin() noexcept { return end(); }
|
|
|
|
const_reverse_iterator rbegin() const noexcept { return end(); }
|
|
|
|
const_reverse_iterator crbegin() const noexcept { return cend(); }
|
|
|
|
reverse_iterator rend() noexcept { return begin(); }
|
|
|
|
const_reverse_iterator rend() const noexcept { return begin(); }
|
|
|
|
const_reverse_iterator crend() const noexcept { return cbegin(); }
|
2019-01-11 14:00:41 +00:00
|
|
|
|
|
|
|
DEF_PLACE_NEWDEL()
|
|
|
|
};
|
|
|
|
|
2019-06-05 23:38:53 +00:00
|
|
|
#undef REQUIRES
|
|
|
|
|
2018-11-18 09:33:26 +00:00
|
|
|
} // namespace al
|
|
|
|
|
2016-03-29 07:44:58 +00:00
|
|
|
#endif /* AL_MALLOC_H */
|