-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryPools.h
More file actions
252 lines (199 loc) · 6.27 KB
/
MemoryPools.h
File metadata and controls
252 lines (199 loc) · 6.27 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* MemoryPools.h
*
* Created on: Dec 11, 2013
* Author: silviu
*/
#pragma once
#include "MemoryPool.h"
#include "Definition/ABException.h"
#include "Definition/Types.h"
namespace admin
{
template<class T>
class NavStdVector;
}
namespace AB
{
template <typename T>
struct variable_size_object{
T t;
};
template<int N>
struct _{ operator char() { return N + 256; } };
class MemoryPools
: public admin::ObjNode<MemoryPools>
{
public:
typedef typename PtrType< MemoryPools >::type Ptr;
static MemoryPools* instance;
MemoryPool
pool_32
, pool_64
, pool_96
, pool_128
, pool_192
, pool_256
, pool_384
, pool_512
, pool_640
, pool_768
, pool_896
, pool_1024
;
admin::NavStdVector<MemoryPool>* pools;
static MemoryPools& get()
{
if (!instance)
{
LOG(FATAL)<<"Initialize first!!!";
}
return *instance;
}
MemoryPools( const long factor, bool a_non_singleton = false );
~MemoryPools(){}
template< class T >
void reflectObject( T& f )
{
// pools->nodeChildren(f);
}
void dump( std::stringstream& out );
void dump();
//
// bool onAdmin( admin::AdminCtx& adm );
// void admin_dump( admin::AdminCtx& adm );
};
namespace memory_pools_aux {
template<int X> struct MemoryPoolSelector {};
inline MemoryPool& get(MemoryPoolSelector<12>) { return MemoryPools::instance->pool_32; }
inline MemoryPool& get(MemoryPoolSelector<11>) { return MemoryPools::instance->pool_64; }
inline MemoryPool& get(MemoryPoolSelector<10>) { return MemoryPools::instance->pool_96; }
inline MemoryPool& get(MemoryPoolSelector<9>) { return MemoryPools::instance->pool_128; }
inline MemoryPool& get(MemoryPoolSelector<8>) { return MemoryPools::instance->pool_192; }
inline MemoryPool& get(MemoryPoolSelector<7>) { return MemoryPools::instance->pool_256; }
inline MemoryPool& get(MemoryPoolSelector<6>) { return MemoryPools::instance->pool_384; }
inline MemoryPool& get(MemoryPoolSelector<5>) { return MemoryPools::instance->pool_512; }
inline MemoryPool& get(MemoryPoolSelector<4>) { return MemoryPools::instance->pool_640; }
inline MemoryPool& get(MemoryPoolSelector<3>) { return MemoryPools::instance->pool_768; }
inline MemoryPool& get(MemoryPoolSelector<2>) { return MemoryPools::instance->pool_896; }
inline MemoryPool& get(MemoryPoolSelector<1>) { return MemoryPools::instance->pool_1024; }
inline MemoryPool& get(MemoryPoolSelector<12>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_32; }
inline MemoryPool& get(MemoryPoolSelector<11>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_64; }
inline MemoryPool& get(MemoryPoolSelector<10>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_96; }
inline MemoryPool& get(MemoryPoolSelector<9>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_128; }
inline MemoryPool& get(MemoryPoolSelector<8>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_192; }
inline MemoryPool& get(MemoryPoolSelector<7>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_256; }
inline MemoryPool& get(MemoryPoolSelector<6>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_384; }
inline MemoryPool& get(MemoryPoolSelector<5>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_512; }
inline MemoryPool& get(MemoryPoolSelector<4>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_640; }
inline MemoryPool& get(MemoryPoolSelector<3>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_768; }
inline MemoryPool& get(MemoryPoolSelector<2>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_896; }
inline MemoryPool& get(MemoryPoolSelector<1>, MemoryPools &a_pool_instance) { return a_pool_instance.pool_1024; }
template<size_t Size>
struct PoolAllocatorImp
{
typedef MemoryPoolSelector<
(Size <= 32)
+ (Size <= 64)
+ (Size <= 96)
+ (Size <= 128)
+ (Size <= 192)
+ (Size <= 256)
+ (Size <= 384)
+ (Size <= 512)
+ (Size <= 640)
+ (Size <= 768)
+ (Size <= 896)
+ (Size <= 1024)
> Selector;
static MemoryPool& get()
{
return memory_pools_aux::get(Selector());
}
//Overload to get instance specific pool
static MemoryPool& get(MemoryPools &a_pool_instance)
{
return memory_pools_aux::get(Selector(), a_pool_instance);
}
static void* allocate()
{
return memory_pools_aux::get(Selector()).allocate();
}
static void deallocate(void* p)
{
memory_pools_aux::get(Selector()).deallocate(p);
}
};
template<class T>
inline MemoryPool& getPool()
{
return memory_pools_aux::PoolAllocatorImp<sizeof(T)>::get();
}
//Overload to get instance specific pool
template<class T>
inline MemoryPool& getPool(MemoryPools &a_pool_instance)
{
return memory_pools_aux::PoolAllocatorImp<sizeof(T)>::get(a_pool_instance);
}
template<class T>
inline void warm(int num)
{
COUT<<"Warming up the pools ...\n";
LOG(INFO)<<"Warming up the pools ...";
T* arr[num];
for (int i = 0; i < num; i++)
{
arr[i] = new T();
}
for (int i = 0; i < num; i++)
{
delete arr[i];
}
getPool<T>().resetTotalAlloc();
}
template<class T>
inline void warm(int num, MemoryPools &a_pool_instance)
{
COUT<<"Warming up instance pools ...\n";
LOG(INFO)<<"Warming up instance pools ...";
T* arr[num];
for (int i = 0; i < num; i++)
{
arr[i] = new (a_pool_instance)T(&a_pool_instance);
}
for (int i = 0; i < num; i++)
{
delete arr[i];
}
getPool<T>(a_pool_instance).resetTotalAlloc();
}
template<class T>
inline void* poolAllocate()
{
return memory_pools_aux::PoolAllocatorImp<sizeof(T)>::allocate();
}
template<class T>
inline void poolDeallocate(void* p)
{
// "error: incomplete type" here means the object size is too big to fit in any of the
// defined pools.
return memory_pools_aux::PoolAllocatorImp<sizeof(T)>::deallocate(p);
}
template<class T>
inline T* allocate(size_t n, void*)
{
if(1 != n)
abort();
if(void* p = poolAllocate<T>())
return static_cast<T*>(p);
throw ABException(" Bad Allocation");
}
template<class T>
inline void deallocate(T* p, size_t n)
{
if(1 != n)
abort();
poolDeallocate<T>(p);
}
}
} /* namespace AB */