From 791c153aabd579f518a9b00613459cba13734797 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Fri, 6 Oct 2023 14:42:03 -0400 Subject: Memory management - ctors and dtors --- src/memory.hpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/memory.hpp') diff --git a/src/memory.hpp b/src/memory.hpp index bde3f2b..6d431fc 100644 --- a/src/memory.hpp +++ b/src/memory.hpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include // 256MB #define DEFAULT_BUCKET_SIZE 1UL << 27 @@ -35,18 +37,35 @@ void Pke_MemoryFlush(); template inline T *Pke_New() { void *ptr = Pke_New(sizeof(T)); - return new (ptr) T{}; + if IS_CONSTRUCTIBLE(T) { + return new (ptr) T{}; + } + return reinterpret_cast(ptr); } template inline T *Pke_New(long count) { - return reinterpret_cast(Pke_New(sizeof(T) * count)); + char *ptr = static_cast(Pke_New(sizeof(T) * count)); + if IS_CONSTRUCTIBLE(T) { + for (long i = 0; i < count; ++i) { + new (ptr + (i * sizeof(T))) T{}; + } + } + return reinterpret_cast(ptr); } template inline void Pke_Delete(const void *ptr) { + if IS_DESTRUCTIBLE(T) { + reinterpret_cast(ptr)->~T(); + } return Pke_Delete(ptr, sizeof(T)); } template inline void Pke_Delete(const void *ptr, long count) { + if IS_DESTRUCTIBLE(T) { + for (long i = 0; i < count; ++i) { + reinterpret_cast(reinterpret_cast(ptr) + (i * sizeof(T)))->~T(); + } + } return Pke_Delete(ptr, sizeof(T) * count); } -- cgit v1.2.3