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_trivially_constructible_v<T>)
154 if constexpr (std::is_nothrow_constructible_v<T, Args...>)
156 new (obj) T(std::forward<Args>(args)...);
163 new (obj) T(std::forward<Args>(args)...);
167 MGL::Memory::Deallocate(header);
173 return std::unique_ptr<T, Deleter>(obj);
180 using U = std::remove_extent_t<T>;
184 if (header ==
nullptr)
186 return std::unique_ptr<T, Deleter>(
nullptr);
190 if constexpr (std::is_trivially_destructible_v<U>)
192 header->destroyer =
nullptr;
196 header->destroyer = NonTrivialDestroyer<U>;
203 auto *obj =
reinterpret_cast<U *
>(header + 1);
206 if constexpr (!std::is_trivially_constructible_v<U>)
209 if constexpr (std::is_nothrow_constructible_v<U>)
211 for (
size_t i = 0; i < n; i++)
219 for (
size_t i = 0; i < n; i++)
227 for (
auto j =
static_cast<int64_t
>(i) - 1; j >= 0; j--)
229 std::destroy_at(obj + j);
232 MGL::Memory::Deallocate(header);
239 return std::unique_ptr<T, Deleter>(obj);
243template<
class T,
class... Args>
244constexpr typename UniquePtrType<T>::UniquePtr_Invalid make_unique(Args &&...) =
delete;
251template<
class T,
class... Args>
255 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:252
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:248
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