🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
11 KiB

  1. #ifndef AL_MALLOC_H
  2. #define AL_MALLOC_H
  3. #include <algorithm>
  4. #include <cstddef>
  5. #include <iterator>
  6. #include <limits>
  7. #include <memory>
  8. #include <new>
  9. #include <type_traits>
  10. #include <utility>
  11. #include "pragmadefs.h"
  12. void al_free(void *ptr) noexcept;
  13. [[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]]
  14. void *al_malloc(size_t alignment, size_t size);
  15. [[gnu::alloc_align(1), gnu::alloc_size(2), gnu::malloc]]
  16. void *al_calloc(size_t alignment, size_t size);
  17. #define DISABLE_ALLOC() \
  18. void *operator new(size_t) = delete; \
  19. void *operator new[](size_t) = delete; \
  20. void operator delete(void*) noexcept = delete; \
  21. void operator delete[](void*) noexcept = delete;
  22. #define DEF_NEWDEL(T) \
  23. void *operator new(size_t size) \
  24. { \
  25. void *ret = al_malloc(alignof(T), size); \
  26. if(!ret) throw std::bad_alloc(); \
  27. return ret; \
  28. } \
  29. void *operator new[](size_t size) { return operator new(size); } \
  30. void operator delete(void *block) noexcept { al_free(block); } \
  31. void operator delete[](void *block) noexcept { operator delete(block); }
  32. #define DEF_PLACE_NEWDEL() \
  33. void *operator new(size_t /*size*/, void *ptr) noexcept { return ptr; } \
  34. void *operator new[](size_t /*size*/, void *ptr) noexcept { return ptr; } \
  35. void operator delete(void *block, void*) noexcept { al_free(block); } \
  36. void operator delete(void *block) noexcept { al_free(block); } \
  37. void operator delete[](void *block, void*) noexcept { al_free(block); } \
  38. void operator delete[](void *block) noexcept { al_free(block); }
  39. enum FamCount : size_t { };
  40. #define DEF_FAM_NEWDEL(T, FamMem) \
  41. static constexpr size_t Sizeof(size_t count) noexcept \
  42. { \
  43. return std::max(decltype(FamMem)::Sizeof(count, offsetof(T, FamMem)), \
  44. sizeof(T)); \
  45. } \
  46. \
  47. void *operator new(size_t /*size*/, FamCount count) \
  48. { \
  49. if(void *ret{al_malloc(alignof(T), T::Sizeof(count))}) \
  50. return ret; \
  51. throw std::bad_alloc(); \
  52. } \
  53. void *operator new[](size_t /*size*/) = delete; \
  54. void operator delete(void *block, FamCount) { al_free(block); } \
  55. void operator delete(void *block) noexcept { al_free(block); } \
  56. void operator delete[](void* /*block*/) = delete;
  57. namespace al {
  58. template<typename T, std::size_t alignment=alignof(T)>
  59. struct allocator {
  60. using value_type = T;
  61. using reference = T&;
  62. using const_reference = const T&;
  63. using pointer = T*;
  64. using const_pointer = const T*;
  65. using size_type = std::size_t;
  66. using difference_type = std::ptrdiff_t;
  67. using is_always_equal = std::true_type;
  68. template<typename U>
  69. struct rebind {
  70. using other = allocator<U, (alignment<alignof(U))?alignof(U):alignment>;
  71. };
  72. constexpr explicit allocator() noexcept = default;
  73. template<typename U, std::size_t N>
  74. constexpr explicit allocator(const allocator<U,N>&) noexcept { }
  75. T *allocate(std::size_t n)
  76. {
  77. if(n > std::numeric_limits<std::size_t>::max()/sizeof(T)) throw std::bad_alloc();
  78. if(auto p = al_malloc(alignment, n*sizeof(T))) return static_cast<T*>(p);
  79. throw std::bad_alloc();
  80. }
  81. void deallocate(T *p, std::size_t) noexcept { al_free(p); }
  82. };
  83. template<typename T, std::size_t N, typename U, std::size_t M>
  84. constexpr bool operator==(const allocator<T,N>&, const allocator<U,M>&) noexcept { return true; }
  85. template<typename T, std::size_t N, typename U, std::size_t M>
  86. constexpr bool operator!=(const allocator<T,N>&, const allocator<U,M>&) noexcept { return false; }
  87. template<typename T, typename ...Args>
  88. constexpr T* construct_at(T *ptr, Args&& ...args)
  89. noexcept(std::is_nothrow_constructible<T, Args...>::value)
  90. { return ::new(static_cast<void*>(ptr)) T{std::forward<Args>(args)...}; }
  91. /* At least VS 2015 complains that 'ptr' is unused when the given type's
  92. * destructor is trivial (a no-op). So disable that warning for this call.
  93. */
  94. DIAGNOSTIC_PUSH
  95. msc_pragma(warning(disable : 4100))
  96. template<typename T>
  97. constexpr std::enable_if_t<!std::is_array<T>::value>
  98. destroy_at(T *ptr) noexcept(std::is_nothrow_destructible<T>::value)
  99. { ptr->~T(); }
  100. DIAGNOSTIC_POP
  101. template<typename T>
  102. constexpr std::enable_if_t<std::is_array<T>::value>
  103. destroy_at(T *ptr) noexcept(std::is_nothrow_destructible<std::remove_all_extents_t<T>>::value)
  104. {
  105. for(auto &elem : *ptr)
  106. al::destroy_at(std::addressof(elem));
  107. }
  108. template<typename T>
  109. constexpr void destroy(T first, T end) noexcept(noexcept(al::destroy_at(std::addressof(*first))))
  110. {
  111. while(first != end)
  112. {
  113. al::destroy_at(std::addressof(*first));
  114. ++first;
  115. }
  116. }
  117. template<typename T, typename N>
  118. constexpr std::enable_if_t<std::is_integral<N>::value,T>
  119. destroy_n(T first, N count) noexcept(noexcept(al::destroy_at(std::addressof(*first))))
  120. {
  121. if(count != 0)
  122. {
  123. do {
  124. al::destroy_at(std::addressof(*first));
  125. ++first;
  126. } while(--count);
  127. }
  128. return first;
  129. }
  130. template<typename T, typename N>
  131. inline std::enable_if_t<std::is_integral<N>::value,
  132. T> uninitialized_default_construct_n(T first, N count)
  133. {
  134. using ValueT = typename std::iterator_traits<T>::value_type;
  135. T current{first};
  136. if(count != 0)
  137. {
  138. try {
  139. do {
  140. ::new(static_cast<void*>(std::addressof(*current))) ValueT;
  141. ++current;
  142. } while(--count);
  143. }
  144. catch(...) {
  145. al::destroy(first, current);
  146. throw;
  147. }
  148. }
  149. return current;
  150. }
  151. /* Storage for flexible array data. This is trivially destructible if type T is
  152. * trivially destructible.
  153. */
  154. template<typename T, size_t alignment, bool = std::is_trivially_destructible<T>::value>
  155. struct FlexArrayStorage {
  156. const size_t mSize;
  157. union {
  158. char mDummy;
  159. alignas(alignment) T mArray[1];
  160. };
  161. static constexpr size_t Sizeof(size_t count, size_t base=0u) noexcept
  162. {
  163. const size_t len{sizeof(T)*count};
  164. return std::max(offsetof(FlexArrayStorage,mArray)+len, sizeof(FlexArrayStorage)) + base;
  165. }
  166. FlexArrayStorage(size_t size) : mSize{size}
  167. { al::uninitialized_default_construct_n(mArray, mSize); }
  168. ~FlexArrayStorage() = default;
  169. FlexArrayStorage(const FlexArrayStorage&) = delete;
  170. FlexArrayStorage& operator=(const FlexArrayStorage&) = delete;
  171. };
  172. template<typename T, size_t alignment>
  173. struct FlexArrayStorage<T,alignment,false> {
  174. const size_t mSize;
  175. union {
  176. char mDummy;
  177. alignas(alignment) T mArray[1];
  178. };
  179. static constexpr size_t Sizeof(size_t count, size_t base) noexcept
  180. {
  181. const size_t len{sizeof(T)*count};
  182. return std::max(offsetof(FlexArrayStorage,mArray)+len, sizeof(FlexArrayStorage)) + base;
  183. }
  184. FlexArrayStorage(size_t size) : mSize{size}
  185. { al::uninitialized_default_construct_n(mArray, mSize); }
  186. ~FlexArrayStorage() { al::destroy_n(mArray, mSize); }
  187. FlexArrayStorage(const FlexArrayStorage&) = delete;
  188. FlexArrayStorage& operator=(const FlexArrayStorage&) = delete;
  189. };
  190. /* A flexible array type. Used either standalone or at the end of a parent
  191. * struct, with placement new, to have a run-time-sized array that's embedded
  192. * with its size.
  193. */
  194. template<typename T, size_t alignment=alignof(T)>
  195. struct FlexArray {
  196. using element_type = T;
  197. using value_type = std::remove_cv_t<T>;
  198. using index_type = size_t;
  199. using difference_type = ptrdiff_t;
  200. using pointer = T*;
  201. using const_pointer = const T*;
  202. using reference = T&;
  203. using const_reference = const T&;
  204. using iterator = pointer;
  205. using const_iterator = const_pointer;
  206. using reverse_iterator = std::reverse_iterator<iterator>;
  207. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  208. using Storage_t_ = FlexArrayStorage<element_type,alignment>;
  209. Storage_t_ mStore;
  210. static constexpr index_type Sizeof(index_type count, index_type base=0u) noexcept
  211. { return Storage_t_::Sizeof(count, base); }
  212. static std::unique_ptr<FlexArray> Create(index_type count)
  213. {
  214. void *ptr{al_calloc(alignof(FlexArray), Sizeof(count))};
  215. return std::unique_ptr<FlexArray>{al::construct_at(static_cast<FlexArray*>(ptr), count)};
  216. }
  217. FlexArray(index_type size) : mStore{size} { }
  218. ~FlexArray() = default;
  219. index_type size() const noexcept { return mStore.mSize; }
  220. bool empty() const noexcept { return mStore.mSize == 0; }
  221. pointer data() noexcept { return mStore.mArray; }
  222. const_pointer data() const noexcept { return mStore.mArray; }
  223. reference operator[](index_type i) noexcept { return mStore.mArray[i]; }
  224. const_reference operator[](index_type i) const noexcept { return mStore.mArray[i]; }
  225. reference front() noexcept { return mStore.mArray[0]; }
  226. const_reference front() const noexcept { return mStore.mArray[0]; }
  227. reference back() noexcept { return mStore.mArray[mStore.mSize-1]; }
  228. const_reference back() const noexcept { return mStore.mArray[mStore.mSize-1]; }
  229. iterator begin() noexcept { return mStore.mArray; }
  230. const_iterator begin() const noexcept { return mStore.mArray; }
  231. const_iterator cbegin() const noexcept { return mStore.mArray; }
  232. iterator end() noexcept { return mStore.mArray + mStore.mSize; }
  233. const_iterator end() const noexcept { return mStore.mArray + mStore.mSize; }
  234. const_iterator cend() const noexcept { return mStore.mArray + mStore.mSize; }
  235. reverse_iterator rbegin() noexcept { return end(); }
  236. const_reverse_iterator rbegin() const noexcept { return end(); }
  237. const_reverse_iterator crbegin() const noexcept { return cend(); }
  238. reverse_iterator rend() noexcept { return begin(); }
  239. const_reverse_iterator rend() const noexcept { return begin(); }
  240. const_reverse_iterator crend() const noexcept { return cbegin(); }
  241. DEF_PLACE_NEWDEL()
  242. };
  243. } // namespace al
  244. #endif /* AL_MALLOC_H */