11#ifndef INCGUARD_MGL_STL_MEMORY_H_1651241952
12#define INCGUARD_MGL_STL_MEMORY_H_1651241952
35 T *allocate(std::size_t n)
37 auto *ptr =
reinterpret_cast<T *
>(MGL::Memory::Allocate(n *
sizeof(T)));
40 throw std::bad_alloc();
46 void deallocate(T *p, [[maybe_unused]] std::size_t n)
48 MGL::Memory::Deallocate(p);
52template<
class T,
class U>
58template<
class T,
class U>
59bool operator!=(
const Allocator<T> & ,
const Allocator<U> & )
noexcept
67 using DestroyFunc = void (*)(
void *obj,
size_t size);
71 DestroyFunc destroyer;
74 static_assert(
sizeof(
Header) <= 16);
76 void operator()(
void *ptr)
const
80 auto *top =
reinterpret_cast<Header *
>(ptr) - 1;
82 if (top->destroyer !=
nullptr)
84 top->destroyer(top + 1, top->size);
87 MGL::Memory::Deallocate(top);
96 std::destroy_n(
reinterpret_cast<T *
>(obj), size);
104 using UniquePtr_Single = std::unique_ptr<T, Deleter>;
111 using UniquePtr_Array = std::unique_ptr<T[], Deleter>;
115template<
class T,
size_t n>
118 using UniquePtr_Invalid = void;
123template<
class T,
class... Args>
124[[nodiscard]]
inline typename UniquePtrType<T>::UniquePtr_Single
make_unique(Args &&...args)
128 if (header ==
nullptr)
130 throw std::bad_alloc();
134 if constexpr (std::is_trivially_destructible_v<T>)
136 header->destroyer =
nullptr;
140 header->destroyer = NonTrivialDestroyer<T>;
147 auto *obj =
reinterpret_cast<T *
>(header + 1);
150 if constexpr (!std::is_trivially_constructible_v<T>)
153 if constexpr (std::is_nothrow_constructible_v<T, Args...>)
155 new (obj) T(std::forward<Args>(args)...);
162 new (obj) T(std::forward<Args>(args)...);
166 MGL::Memory::Deallocate(header);
172 return std::unique_ptr<T, Deleter>(obj);
179 using U = std::remove_extent_t<T>;
183 if (header ==
nullptr)
185 return std::unique_ptr<T, Deleter>(
nullptr);
189 if constexpr (std::is_trivially_destructible_v<U>)
191 header->destroyer =
nullptr;
195 header->destroyer = NonTrivialDestroyer<U>;
202 auto *obj =
reinterpret_cast<U *
>(header + 1);
205 if constexpr (!std::is_trivially_constructible_v<U>)
208 if constexpr (std::is_nothrow_constructible_v<U>)
210 for (
size_t i = 0; i < n; i++)
218 for (
size_t i = 0; i < n; i++)
226 for (
auto j =
static_cast<int64_t
>(i) - 1; j >= 0; j--)
228 std::destroy_at(obj + j);
231 MGL::Memory::Deallocate(header);
238 return std::unique_ptr<T, Deleter>(obj);
242template<
class T,
class... Args>
243constexpr typename UniquePtrType<T>::UniquePtr_Invalid make_unique(Args &&...) =
delete;
250template<
class T,
class... Args>
254 return std::allocate_shared<T>(alloc, std::forward<Args>(args)...);
STL用アロケータ
Definition mgl_stl_memory.h:23
constexpr auto make_shared(Args &&...args)
シェアードポインタの生成
Definition mgl_stl_memory.h:251
constexpr void NonTrivialDestroyer(void *obj, size_t size)
デストラクタ呼び出し用テンプレート関数
Definition mgl_stl_memory.h:94
std::unique_ptr< T, Deleter > unique_ptr
MGLのアロケータを利用するユニークポインタ
Definition mgl_stl_memory.h:247
UniquePtrType< T >::UniquePtr_Single make_unique(Args &&...args)
非配列型のユニークポインタの生成
Definition mgl_stl_memory.h:124
STL用デリータ
Definition mgl_stl_memory.h:66
非配列型の判別用テンプレート
Definition mgl_stl_memory.h:103