-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_mem_pool.h
398 lines (373 loc) · 12.4 KB
/
static_mem_pool.h
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
// vim:tabstop=4:shiftwidth=4:expandtab:
/*
* Copyright (C) 2004-2014 Wu Yongwei <adah at users dot sourceforge dot net>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must
* not claim that you wrote the original software. If you use this
* software in a product, an acknowledgement in the product
* documentation would be appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must
* not be misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source
* distribution.
*
* This file is part of Stones of Nvwa:
* http://sourceforge.net/projects/nvwa
*
*/
/**
* @file static_mem_pool.h
*
* Header file for the `static' memory pool.
*
* @date 2014-11-29
*/
#ifndef NVWA_STATIC_MEM_POOL_H
#define NVWA_STATIC_MEM_POOL_H
#include <new> // std::bad_alloc
#include <stdexcept> // std::runtime_error
#include <string> // std::string
#include <vector> // std::vector
#include <assert.h> // assert
#include <stddef.h> // size_t/NULL
#include "_nvwa.h" // NVWA/NVWA_NAMESPACE_*
#include "c++11.h" // _NOEXCEPT/_NULLPTR/_OVERRIDE
#include "class_level_lock.h" // nvwa::class_level_lock
#include "mem_pool_base.h" // nvwa::mem_pool_base
/* Defines the macro for debugging output */
# ifdef _STATIC_MEM_POOL_DEBUG
# include <iostream>
# define _STATIC_MEM_POOL_TRACE(_Lck, _Msg) \
{ \
if (_Lck) { \
static_mem_pool_set::lock guard; \
std::cerr << "static_mem_pool: " << _Msg << std::endl; \
} else { \
std::cerr << "static_mem_pool: " << _Msg << std::endl; \
} \
}
# else
# define _STATIC_MEM_POOL_TRACE(_Lck, _Msg) \
((void)0)
# endif
NVWA_NAMESPACE_BEGIN
/**
* Singleton class to maintain a set of existing instantiations of
* static_mem_pool.
*/
class static_mem_pool_set
{
public:
typedef class_level_lock<static_mem_pool_set>::lock lock;
static static_mem_pool_set& instance();
void recycle();
void add(mem_pool_base* memory_pool_p);
private:
static_mem_pool_set();
~static_mem_pool_set();
typedef std::vector<mem_pool_base*> container_type;
container_type _M_memory_pool_set;
/* Forbid their use */
static_mem_pool_set(const static_mem_pool_set&);
const static_mem_pool_set& operator=(const static_mem_pool_set&);
};
/**
* Singleton class template to manage the allocation/deallocation of
* memory blocks of one specific size.
*
* @param _Sz size of elements in the static_mem_pool
* @param _Gid group ID of a static_mem_pool: if it is negative,
* simultaneous accesses to this static_mem_pool will be
* protected from each other; otherwise no protection is
* given
*/
template <size_t _Sz, int _Gid = -1>
class static_mem_pool : public mem_pool_base
{
typedef typename class_level_lock<static_mem_pool<_Sz, _Gid>, (_Gid < 0)>
::lock lock;
public:
/**
* Gets the instance of the static memory pool. It will create the
* instance if it does not already exist. Generally this function
* is now not needed.
*
* @return reference to the instance of the static memory pool
* @see instance_known
*/
static static_mem_pool& instance()
{
lock guard;
if (!_S_instance_p)
{
_S_instance_p = _S_create_instance();
}
return *_S_instance_p;
}
/**
* Gets the known instance of the static memory pool. The instance
* must already exist. Generally the static initializer of the
* template guarantees it.
*
* @return reference to the instance of the static memory pool
*/
static static_mem_pool& instance_known()
{
assert(_S_instance_p != _NULLPTR);
return *_S_instance_p;
}
/**
* Allocates memory and returns its pointer. The template will try
* to get it from the memory pool first, and request memory from the
* system if there is no free memory in the pool.
*
* @return pointer to allocated memory if successful; null
* otherwise
*/
void* allocate()
{
{
lock guard;
if (_S_memory_block_p)
{
void* result = _S_memory_block_p;
_S_memory_block_p = _S_memory_block_p->_M_next;
return result;
}
}
return _S_alloc_sys(_S_align(_Sz));
}
/**
* Deallocates memory by putting the memory block into the pool.
*
* @param ptr pointer to memory to be deallocated
*/
void deallocate(void* ptr)
{
assert(ptr != _NULLPTR);
lock guard;
_Block_list* block = reinterpret_cast<_Block_list*>(ptr);
block->_M_next = _S_memory_block_p;
_S_memory_block_p = block;
}
virtual void recycle() _OVERRIDE;
private:
static_mem_pool()
{
_STATIC_MEM_POOL_TRACE(true, "static_mem_pool<" << _Sz << ','
<< _Gid << "> is created");
}
~static_mem_pool()
{
# ifdef _DEBUG
// Empty the pool to avoid false memory leakage alarms. This is
// generally not necessary for release binaries.
_Block_list* block = _S_memory_block_p;
while (block)
{
_Block_list* next = block->_M_next;
dealloc_sys(block);
block = next;
}
_S_memory_block_p = _NULLPTR;
# endif
_S_instance_p = _NULLPTR;
_S_destroyed = true;
_STATIC_MEM_POOL_TRACE(false, "static_mem_pool<" << _Sz << ','
<< _Gid << "> is destroyed");
}
static size_t _S_align(size_t size)
{
return size >= sizeof(_Block_list) ? size : sizeof(_Block_list);
}
static void* _S_alloc_sys(size_t size);
static static_mem_pool* _S_create_instance();
static bool _S_destroyed;
static static_mem_pool* _S_instance_p;
static mem_pool_base::_Block_list* _S_memory_block_p;
/* Forbid their use */
static_mem_pool(const static_mem_pool&);
const static_mem_pool& operator=(const static_mem_pool&);
};
template <size_t _Sz, int _Gid> bool
static_mem_pool<_Sz, _Gid>::_S_destroyed = false;
template <size_t _Sz, int _Gid> mem_pool_base::_Block_list*
static_mem_pool<_Sz, _Gid>::_S_memory_block_p = _NULLPTR;
template <size_t _Sz, int _Gid> static_mem_pool<_Sz, _Gid>*
static_mem_pool<_Sz, _Gid>::_S_instance_p = _S_create_instance();
/**
* Recycles half of the free memory blocks in the memory pool to the
* system. It is called when a memory request to the system (in other
* instances of the static memory pool) fails.
*/
template <size_t _Sz, int _Gid>
void static_mem_pool<_Sz, _Gid>::recycle()
{
// Only here the global lock in static_mem_pool_set is obtained
// before the pool-specific lock. However, no race conditions are
// found so far.
lock guard;
_Block_list* block = _S_memory_block_p;
while (block)
{
if (_Block_list* temp = block->_M_next)
{
_Block_list* next = temp->_M_next;
block->_M_next = next;
dealloc_sys(temp);
block = next;
}
else
{
break;
}
}
_STATIC_MEM_POOL_TRACE(false, "static_mem_pool<" << _Sz << ','
<< _Gid << "> is recycled");
}
template <size_t _Sz, int _Gid>
void* static_mem_pool<_Sz, _Gid>::_S_alloc_sys(size_t size)
{
static_mem_pool_set::lock guard;
void* result = mem_pool_base::alloc_sys(size);
if (!result)
{
static_mem_pool_set::instance().recycle();
result = mem_pool_base::alloc_sys(size);
}
return result;
}
template <size_t _Sz, int _Gid>
static_mem_pool<_Sz, _Gid>* static_mem_pool<_Sz, _Gid>::_S_create_instance()
{
if (_S_destroyed)
throw std::runtime_error("dead reference detected");
static_mem_pool_set::instance(); // Force its creation
static_mem_pool* inst_p = new static_mem_pool();
try
{
static_mem_pool_set::instance().add(inst_p);
}
catch (...)
{
_STATIC_MEM_POOL_TRACE(true,
"Exception occurs in static_mem_pool_set::add");
// The strange cast below is to work around a bug in GCC 2.95.3
delete static_cast<mem_pool_base*>(inst_p);
throw;
}
return inst_p;
}
NVWA_NAMESPACE_END
/**
* Declares the normal (throwing) allocation and deallocation functions.
* This macro uses the default group.
*
* @param _Cls class to use the static_mem_pool
* @see DECLARE_STATIC_MEM_POOL__NOTHROW
* @see DECLARE_STATIC_MEM_POOL_GROUPED
* @see DECLARE_STATIC_MEM_POOL_GROUPED__NOTHROW
*/
#define DECLARE_STATIC_MEM_POOL(_Cls) \
public: \
static void* operator new(size_t size) \
{ \
assert(size == sizeof(_Cls)); \
void* ptr; \
ptr = NVWA::static_mem_pool<sizeof(_Cls)>:: \
instance_known().allocate(); \
if (ptr == _NULLPTR) \
throw std::bad_alloc(); \
return ptr; \
} \
static void operator delete(void* ptr) \
{ \
if (ptr) \
NVWA::static_mem_pool<sizeof(_Cls)>:: \
instance_known().deallocate(ptr); \
}
/**
* Declares the nothrow allocation and deallocation functions. This macro
* uses the default group.
*
* @param _Cls class to use the static_mem_pool
* @see DECLARE_STATIC_MEM_POOL
* @see DECLARE_STATIC_MEM_POOL_GROUPED
* @see DECLARE_STATIC_MEM_POOL_GROUPED__NOTHROW
*/
#define DECLARE_STATIC_MEM_POOL__NOTHROW(_Cls) \
public: \
static void* operator new(size_t size) _NOEXCEPT \
{ \
assert(size == sizeof(_Cls)); \
return NVWA::static_mem_pool<sizeof(_Cls)>:: \
instance_known().allocate(); \
} \
static void operator delete(void* ptr) \
{ \
if (ptr) \
NVWA::static_mem_pool<sizeof(_Cls)>:: \
instance_known().deallocate(ptr); \
}
/**
* Declares the normal (throwing) allocation and deallocation functions.
* Users need to specify a group ID.
*
* @param _Cls class to use the static_mem_pool
* @param _Gid group ID (negative to protect multi-threaded access)
* @see DECLARE_STATIC_MEM_POOL
* @see DECLARE_STATIC_MEM_POOL__NOTHROW
* @see DECLARE_STATIC_MEM_POOL_GROUPED__NOTHROW
*/
#define DECLARE_STATIC_MEM_POOL_GROUPED(_Cls, _Gid) \
public: \
static void* operator new(size_t size) \
{ \
assert(size == sizeof(_Cls)); \
void* ptr; \
ptr = NVWA::static_mem_pool<sizeof(_Cls), (_Gid)>:: \
instance_known().allocate(); \
if (ptr == _NULLPTR) \
throw std::bad_alloc(); \
return ptr; \
} \
static void operator delete(void* ptr) \
{ \
if (ptr) \
NVWA::static_mem_pool<sizeof(_Cls), (_Gid)>:: \
instance_known().deallocate(ptr); \
}
/**
* Declares the nothrow allocation and deallocation functions. Users need
* to specify a group ID.
*
* @param _Cls class to use the static_mem_pool
* @param _Gid group ID (negative to protect multi-threaded access)
* @see DECLARE_STATIC_MEM_POOL
* @see DECLARE_STATIC_MEM_POOL__NOTHROW
* @see DECLARE_STATIC_MEM_POOL_GROUPED
*/
#define DECLARE_STATIC_MEM_POOL_GROUPED__NOTHROW(_Cls, _Gid) \
public: \
static void* operator new(size_t size) _NOEXCEPT \
{ \
assert(size == sizeof(_Cls)); \
return NVWA::static_mem_pool<sizeof(_Cls), (_Gid)>:: \
instance_known().allocate(); \
} \
static void operator delete(void* ptr) \
{ \
if (ptr) \
NVWA::static_mem_pool<sizeof(_Cls), (_Gid)>:: \
instance_known().deallocate(ptr); \
}
#endif // NVWA_STATIC_MEM_POOL_H