QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
fixed_mem_pool.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2// vim:tabstop=4:shiftwidth=4:expandtab:
3
4/*
5 * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any
9 * damages arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute
13 * it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must
16 * not claim that you wrote the original software. If you use this
17 * software in a product, an acknowledgement in the product
18 * documentation would be appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must
20 * not be misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source
22 * distribution.
23 *
24 * This file is part of Stones of Nvwa:
25 * http://sourceforge.net/projects/nvwa
26 *
27 */
28
54#ifndef QT_NO_DEBUG
55#ifndef _QX_MODE_RELEASE
56#if _QX_USE_MEM_LEAK_DETECTION
57
58#ifndef _FIXED_MEM_POOL_H
59#define _FIXED_MEM_POOL_H
60
61#ifdef _MSC_VER
62#pragma once
63#endif
64
65#include <new>
66#include <assert.h>
67#include <stdlib.h>
68#include "class_level_lock.h"
69#include "mem_pool_base.h"
70
74#ifndef MEM_POOL_ALIGNMENT
75#define MEM_POOL_ALIGNMENT 4
76#endif
77
78namespace qx {
79namespace memory {
80
87template <class _Tp>
89{
90public:
92 static void* allocate();
93 static void deallocate(void*);
94 static bool initialize(size_t __size);
95 static int deinitialize();
96 static int get_alloc_count();
97 static bool is_initialized();
98protected:
99 static bool bad_alloc_handler();
100private:
101 static size_t _S_align(size_t __size);
102 static void* _S_mem_pool_ptr;
103 static void* _S_first_avail_ptr;
104 static int _S_alloc_cnt;
105};
106
108template <class _Tp>
110
112template <class _Tp>
114
116template <class _Tp>
118
124template <class _Tp>
126{
127 lock __guard;
128 for (;;)
129 {
130 if (void* __result = _S_first_avail_ptr)
131 {
132 _S_first_avail_ptr = *(void**)_S_first_avail_ptr;
133 ++_S_alloc_cnt;
134 return __result;
135 }
136 else
137 if (!bad_alloc_handler())
138 return NULL;
139 }
140}
141
147template <class _Tp>
148inline void fixed_mem_pool<_Tp>::deallocate(void* __block_ptr)
149{
150 if (__block_ptr == NULL)
151 return;
152 lock __guard;
153 assert(_S_alloc_cnt != 0);
154 --_S_alloc_cnt;
155 *(void**)__block_ptr = _S_first_avail_ptr;
156 _S_first_avail_ptr = __block_ptr;
157}
158
165template <class _Tp>
167{
168 size_t __block_size = _S_align(sizeof(_Tp));
169 assert(!is_initialized());
170 assert(__size > 0 && __block_size >= sizeof(void*));
171 _S_mem_pool_ptr = mem_pool_base::alloc_sys(__size * __block_size);
172 _S_first_avail_ptr = _S_mem_pool_ptr;
173 if (_S_mem_pool_ptr == NULL)
174 return false;
175 char* __block_ = (char*)_S_mem_pool_ptr;
176 while (--__size != 0)
177 {
178 char* __next_ = __block_ + __block_size;
179 *(void**)__block_ = __next_;
180 __block_ = __next_;
181 }
182 *(void**)__block_ = NULL;
183 return true;
184}
185
193template <class _Tp>
195{
196 if (_S_alloc_cnt != 0)
197 return _S_alloc_cnt;
198 assert(is_initialized());
199 mem_pool_base::dealloc_sys(_S_mem_pool_ptr);
200 _S_mem_pool_ptr = NULL;
201 _S_first_avail_ptr = NULL;
202 return 0;
203}
204
210template <class _Tp>
212{
213 return _S_alloc_cnt;
214}
215
221template <class _Tp>
223{
224 return _S_mem_pool_ptr != NULL;;
225}
226
235template <class _Tp>
237{
238 return false;
239}
240
247template <class _Tp>
248inline size_t fixed_mem_pool<_Tp>::_S_align(size_t __size)
249{
250 return (__size + MEM_POOL_ALIGNMENT - 1)
252}
253
254} // namespace memory
255} // namespace qx
256
267#define DECLARE_FIXED_MEM_POOL(_Cls) \
268public: \
269 static void* operator new(size_t __size) \
270 { \
271 assert(__size == sizeof(_Cls)); \
272 if (void* __ptr = fixed_mem_pool<_Cls>::allocate()) \
273 return __ptr; \
274 else \
275 throw std::bad_alloc(); \
276 } \
277 static void operator delete(void* __ptr) \
278 { \
279 if (__ptr != NULL) \
280 fixed_mem_pool<_Cls>::deallocate(__ptr); \
281 }
282
289#define DECLARE_FIXED_MEM_POOL__NOTHROW(_Cls) \
290public: \
291 static void* operator new(size_t __size) throw() \
292 { \
293 assert(__size == sizeof(_Cls)); \
294 return fixed_mem_pool<_Cls>::allocate(); \
295 } \
296 static void operator delete(void* __ptr) \
297 { \
298 if (__ptr != NULL) \
299 fixed_mem_pool<_Cls>::deallocate(__ptr); \
300 }
301
314#define DECLARE_FIXED_MEM_POOL__THROW_NOCHECK(_Cls) \
315public: \
316 static void* operator new(size_t __size) \
317 { \
318 assert(__size == sizeof(_Cls)); \
319 return fixed_mem_pool<_Cls>::allocate(); \
320 } \
321 static void operator delete(void* __ptr) \
322 { \
323 if (__ptr != NULL) \
324 fixed_mem_pool<_Cls>::deallocate(__ptr); \
325 }
326
327#endif // _FIXED_MEM_POOL_H
328#endif // _QX_USE_MEM_LEAK_DETECTION
329#endif // _QX_MODE_RELEASE
330#endif // QT_NO_DEBUG
static size_t _S_align(size_t __size)
static bool initialize(size_t __size)
class_level_lock< fixed_mem_pool< _Tp > >::lock lock
static void deallocate(void *)
static void * alloc_sys(size_t __size)
static void dealloc_sys(void *__ptr)
#define MEM_POOL_ALIGNMENT
Root namespace for all QxOrm library features.