diff options
Diffstat (limited to 'src/memory-allocator.hpp')
| -rw-r--r-- | src/memory-allocator.hpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/memory-allocator.hpp b/src/memory-allocator.hpp new file mode 100644 index 0000000..c14304a --- /dev/null +++ b/src/memory-allocator.hpp @@ -0,0 +1,54 @@ +#ifndef PKE_MEMORY_ALLOCATOR_HPP +#define PKE_MEMORY_ALLOCATOR_HPP + +#include "memory.hpp" + +template <typename T, std::size_t SZ = DEFAULT_BUCKET_SIZE> class PkeTransAllocator { +public: + typedef T value_type; + MemBucket *transientBucket = nullptr; + + PkeTransAllocator() : transientBucket(Pke_BeginTransientBucket(SZ)) { + } + ~PkeTransAllocator() { + Pke_EndTransientBucket(this->transientBucket); + } + + template <class U> struct rebind {typedef PkeTransAllocator<U, SZ> other;}; + template <typename U> explicit PkeTransAllocator(const PkeTransAllocator<U> &other) { + (void)other; + } + + T *allocate(std::size_t n) { + auto *ptr = reinterpret_cast<T *>(Pke_New(sizeof(T) * n, this->transientBucket)); + if (ptr) return ptr; + throw "Pke-Allocator Failed to allocate"; + } + + void deallocate(const T *ptr, std::size_t n) { + Pke_Delete(ptr, sizeof(T) * n, this->transientBucket); + } +}; + +template <typename T> class PkeAllocator { +public: + typedef T value_type; + + PkeAllocator() = default; + + template <typename U> explicit PkeAllocator(const PkeAllocator<U> &other) { + (void)other; + } + + T *allocate(std::size_t n) { + auto *ptr = Pke_New<T>(n); + if (ptr) return ptr; + throw "Pke-Allocator Failed to allocate"; + } + + void deallocate(const T *ptr, std::size_t n) { + Pke_Delete<T>(ptr, n); + } +}; + +#endif /* PKE_MEMORY_ALLOCATOR_HPP */ |
