-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPool.hpp
177 lines (144 loc) · 4.69 KB
/
Pool.hpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#pragma once
#include "Common.hpp"
#ifndef NDEBUG
#define POOL_ATMGUARD
#endif
template<class tPool>
class PoolDeleter {
public:
constexpr PoolDeleter() noexcept = default;
constexpr PoolDeleter(const PoolDeleter &) noexcept = default;
constexpr PoolDeleter(PoolDeleter &&) noexcept = default;
constexpr PoolDeleter(tPool &vPool) noexcept : x_pPool(&vPool) {}
PoolDeleter &operator =(const PoolDeleter &) = default;
PoolDeleter &operator =(PoolDeleter &&) = default;
public:
inline void operator ()(typename tPool::Obj *pObj) noexcept {
assert(x_pPool);
x_pPool->Delete(pObj);
}
private:
tPool *x_pPool = nullptr;
};
template<class tObj>
struct SysPool {
using Obj = tObj;
using Deleter = PoolDeleter<SysPool>;
using UniquePtr = std::unique_ptr<Obj, Deleter>;
static inline SysPool &DefaultPool() noexcept {
static SysPool vPool;
return vPool;
}
inline Obj *Alloc() noexcept {
return reinterpret_cast<Obj *>(::operator new(sizeof(tObj)));
}
inline void Dealloc(Obj *pObj) noexcept {
::operator delete(pObj);
}
template<class ...tvArgs>
inline Obj *New(tvArgs &&...vArgs) noexcept(std::is_nothrow_constructible_v<Obj, tvArgs...>) {
return ::new Obj(std::forward<tvArgs>(vArgs)...);
}
inline void Delete(Obj *pObj) noexcept {
::delete pObj;
}
template<class ...tvArgs>
inline UniquePtr MakeUnique(tvArgs &&...vArgs) noexcept(std::is_nothrow_constructible_v<Obj, tvArgs...>) {
auto pObj = New(std::forward<tvArgs>(vArgs)...);
return UniquePtr(pObj, Deleter(*this));
}
inline UniquePtr Wrap(Obj *pObj) noexcept {
return UniquePtr(pObj, Deleter(*this));
}
constexpr void Shrink() noexcept {}
};
template<class tObj, USize kuAlignOff = 0, USize kuAlign = MEMORY_ALLOCATION_ALIGNMENT>
class Pool {
public:
using Obj = tObj;
using Deleter = PoolDeleter<Pool>;
using UniquePtr = std::unique_ptr<Obj, Deleter>;
public:
static inline Pool &DefaultPool() noexcept {
static Pool vPool;
return vPool;
}
private:
struct alignas(MEMORY_ALLOCATION_ALIGNMENT) X_Block {
Obj vObj;
SLIST_ENTRY vEntry;
};
public:
inline Pool(U32 uPrepared = 0) noexcept {
InitializeSListHead(&x_vHeader);
while (uPrepared--) {
auto pBlock = reinterpret_cast<X_Block *>(
::_aligned_offset_malloc(sizeof(X_Block), kuAlign, kuAlignOff)
);
assert(pBlock);
InterlockedPushEntrySList(&x_vHeader, &pBlock->vEntry);
}
}
Pool(const Pool &) = delete;
Pool(Pool &&) = delete;
inline ~Pool() {
#ifdef POOL_ATMGUARD
while (x_atmuAlloc.load())
#endif
Shrink();
}
Pool &operator =(const Pool &) = delete;
Pool &operator =(Pool &&) = delete;
public:
inline Obj *Alloc() noexcept {
auto pEntry = InterlockedPopEntrySList(&x_vHeader);
if (pEntry == nullptr) {
#ifdef POOL_ATMGUARD
x_atmuAlloc.fetch_add(1);
#endif
auto pBlock = reinterpret_cast<X_Block *>(
::_aligned_malloc(sizeof(X_Block), MEMORY_ALLOCATION_ALIGNMENT)
);
assert(pBlock);
return &pBlock->vObj;
}
auto pBlock = CONTAINING_RECORD(pEntry, X_Block, vEntry);
return &pBlock->vObj;
}
inline void Dealloc(Obj *pObj) noexcept {
auto pBlock = CONTAINING_RECORD(pObj, X_Block, vObj);
InterlockedPushEntrySList(&x_vHeader, &pBlock->vEntry);
}
template<class ...tvArgs>
inline Obj *New(tvArgs &&...vArgs) noexcept(std::is_nothrow_constructible_v<Obj, tvArgs...>) {
return ::new(Alloc()) Obj(std::forward<tvArgs>(vArgs)...);
}
inline void Delete(Obj *pObj) noexcept {
pObj->~Obj();
Dealloc(pObj);
}
template<class ...tvArgs>
inline UniquePtr MakeUnique(tvArgs &&...vArgs) noexcept(std::is_nothrow_constructible_v<Obj, tvArgs...>) {
auto pObj = New(std::forward<tvArgs>(vArgs)...);
return UniquePtr(pObj, Deleter(*this));
}
inline UniquePtr Wrap(Obj *pObj) noexcept {
return UniquePtr(pObj, Deleter(*this));
}
inline void Shrink() noexcept {
auto pEntry = InterlockedPopEntrySList(&x_vHeader);
while (pEntry) {
auto pBlock = CONTAINING_RECORD(pEntry, X_Block, vEntry);
::_aligned_free(pBlock);
#ifdef POOL_ATMGUARD
x_atmuAlloc.fetch_sub(1);
#endif
pEntry = InterlockedPopEntrySList(&x_vHeader);
}
}
private:
alignas(MEMORY_ALLOCATION_ALIGNMENT) SLIST_HEADER x_vHeader;
#ifdef POOL_ATMGUARD
std::atomic<U32> x_atmuAlloc = 0;
#endif
};