summaryrefslogtreecommitdiff
path: root/src/memory-allocator.hpp
blob: c14304a8b8c72ec7ddeb5115cf4e4a31d0a8aebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 */