11#ifndef INCGUARD_MGL_STL_MEMORY_H_1651241952
12#define INCGUARD_MGL_STL_MEMORY_H_1651241952
36 T *allocate(std::size_t n)
38 auto *ptr =
reinterpret_cast<T *
>(MGL::Memory::Allocate(n *
sizeof(T)));
41 throw std::bad_alloc();
47 void deallocate(T *p, [[maybe_unused]] std::size_t n)
49 MGL::Memory::Deallocate(p);
53template <
class T,
class U>
59template <
class T,
class U>
60bool operator!=(
const Allocator<T> & ,
const Allocator<U> & )
noexcept
68 using DestroyFunc = void (*)(
void *obj,
size_t size);
72 DestroyFunc destroyer;
75 static_assert(
sizeof(
Header) <= 16);
77 void operator()(
void *ptr)
const
81 auto *top =
reinterpret_cast<Header *
>(ptr) - 1;
83 if (top->destroyer !=
nullptr)
85 top->destroyer(top + 1, top->size);
88 MGL::Memory::Deallocate(top);
97 std::destroy_n(
reinterpret_cast<T *
>(obj), size);
105 using UniquePtr_Single = std::unique_ptr<T, Deleter>;
112 using UniquePtr_Array = std::unique_ptr<T[], Deleter>;
116template <
class T,
size_t n>
119 using UniquePtr_Invalid = void;
124template <
class T,
class... Args>
125[[nodiscard]]
inline typename UniquePtrType<T>::UniquePtr_Single
make_unique(Args &&...args)
129 if (header ==
nullptr)
131 throw std::bad_alloc();
135 if constexpr (std::is_trivially_destructible_v<T>)
137 header->destroyer =
nullptr;
141 header->destroyer = NonTrivialDestroyer<T>;
148 auto *obj =
reinterpret_cast<T *
>(header + 1);
151 if constexpr (std::is_nothrow_constructible_v<T, Args...>)
153 obj =
new (obj) T(std::forward<Args>(args)...);
160 obj =
new (obj) T(std::forward<Args>(args)...);
164 MGL::Memory::Deallocate(header);
169 return std::unique_ptr<T, Deleter>(obj);
176 using U = std::remove_extent_t<T>;
180 if (header ==
nullptr)
182 return std::unique_ptr<T, Deleter>(
nullptr);
186 if constexpr (std::is_trivially_destructible_v<U>)
188 header->destroyer =
nullptr;
192 header->destroyer = NonTrivialDestroyer<U>;
199 auto *obj =
reinterpret_cast<U *
>(header + 1);
203 if constexpr (std::is_nothrow_constructible_v<U>)
205 for (
size_t i = 0; i < n; i++)
219 for (
size_t i = 0; i < n; i++)
224 ptr =
new (obj + i) U();
230 for (
auto j =
static_cast<int64_t
>(i) - 1; j >= 0; j--)
232 std::destroy_at(top + j);
236 MGL::Memory::Deallocate(header);
247 return std::unique_ptr<T, Deleter>(top);
251template <
class T,
class... Args>
252constexpr typename UniquePtrType<T>::UniquePtr_Invalid make_unique(Args &&...) =
delete;
259template <
class T,
class... Args>
263 return std::allocate_shared<T>(alloc, std::forward<Args>(args)...);
STL用アロケータ
Definition mgl_stl_memory.h:24
constexpr auto make_shared(Args &&...args)
シェアードポインタの生成
Definition mgl_stl_memory.h:260
constexpr void NonTrivialDestroyer(void *obj, size_t size)
デストラクタ呼び出し用テンプレート関数
Definition mgl_stl_memory.h:95
std::unique_ptr< T, Deleter > unique_ptr
MGLのアロケータを利用するユニークポインタ
Definition mgl_stl_memory.h:256
UniquePtrType< T >::UniquePtr_Single make_unique(Args &&...args)
非配列型のユニークポインタの生成
Definition mgl_stl_memory.h:125
STL用デリータ
Definition mgl_stl_memory.h:67
非配列型の判別用テンプレート
Definition mgl_stl_memory.h:104