79 bool Initialize(
void* ownedMemory,
size_t totalSize) {
81 totalMemorySize = totalSize;
82 chunkSize =
sizeof(T);
84 totalChunkCount = totalSize / chunkSize;
85 hasAllocatedOwnMemory =
false;
87 deleteFn = [
this](
void* ptr) ->
void {
94 bool Initialize(
size_t maxChunkCount) {
95 chunkSize =
sizeof(T);
96 totalMemorySize = chunkSize * maxChunkCount;
98 totalChunkCount = maxChunkCount;
100 memory = malloc(totalMemorySize);
101 hasAllocatedOwnMemory =
true;
105 deleteFn = [
this](
void* ptr) ->
void {
109 return memory !=
nullptr;
112 template<
typename... Args>
113 SharedPtr<T> AllocateShared(Args&&... params) {
114 T* ptr =
static_cast<T*
>(AllocateImpl());
115 if (ptr !=
nullptr) {
117 new (ptr) T(std::forward<Args>(params)...);
120 return SharedPtr<T>(ptr, deleteFn);
123 template<
typename... Args>
124 UniquePtr<T> AllocateUnique(Args&&... params) {
125 T* ptr =
static_cast<T*
>(AllocateImpl());
126 if (ptr !=
nullptr) {
128 new (ptr) T(std::forward<Args>(params)...);
131 return UniquePtr<T>(ptr, deleteFn);
134 template<
typename... Args>
135 T* AllocateRaw(Args&&... params) {
136 T* ptr =
static_cast<T*
>(AllocateImpl());
137 if (ptr !=
nullptr) {
139 new (ptr) T(std::forward<Args>(params)...);
145 T* AllocateWithoutConstructor() {
146 return static_cast<T*
>(AllocateImpl());
149 void Deallocate(
size_t index) {
150 size_t chunkOffset = index * chunkSize;
151 void* ptr =
reinterpret_cast<char*
>(memory) + chunkOffset;
152 reinterpret_cast<T*
>(ptr)->~T();
156 void Deallocate(
void* ptr) {
157 reinterpret_cast<T*
>(ptr)->~T();
161 void DeallocateWithoutDestructor(
size_t index) {
162 size_t chunkOffset = index * chunkSize;
163 void* ptr =
reinterpret_cast<char*
>(memory) + chunkOffset;
167 void DeallocateWithoutDestructor(
void* ptr) {