1
/* $Id: pool.hpp 2394 2008-12-23 17:27:53Z bennylp $ */
3
* Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJPP_POOL_HPP__
21
#define __PJPP_POOL_HPP__
26
class Pj_Caching_Pool;
29
// Base class for all Pjlib objects
34
void *operator new(unsigned int class_size, Pj_Pool *pool);
35
void *operator new(unsigned int class_size, Pj_Pool &pool);
37
void operator delete(void*)
41
void operator delete(void*, Pj_Pool*)
45
void operator delete(void*, Pj_Pool&)
50
// Inline implementations at the end of this file.
54
// Can not use normal new operator; must use pool.
56
// obj = new(pool) Pj_The_Object(pool, ...);
58
void *operator new(unsigned int)
66
class Pj_Pool : public Pj_Object
70
// Default constructor, initializes internal pool to NULL.
71
// Application must call attach() some time later.
81
Pj_Pool(Pj_Caching_Pool &caching_pool,
82
pj_size_t initial_size,
83
pj_size_t increment_size,
84
const char *name = NULL,
85
pj_pool_callback *callback = NULL);
88
// Construct from existing pool.
90
explicit Pj_Pool(pj_pool_t *pool)
96
// Attach existing pool.
98
void attach(pj_pool_t *pool)
106
// Release pool back to factory. Remember: if you delete pool, then
107
// make sure that all objects that have been allocated from this pool
108
// have been properly destroyed.
110
// This is where C++ is trickier than plain C!!
121
const char *getobjname() const
123
return pj_pool_getobjname(p_);
127
// You can cast Pj_Pool to pj_pool_t*
129
operator pj_pool_t*()
135
// Get pjlib compatible pool object.
143
// Get pjlib compatible pool object.
145
const pj_pool_t *pool_() const
151
// Get pjlib compatible pool object.
153
pj_pool_t *pj_pool_t_()
167
// Get current capacity.
169
pj_size_t get_capacity()
171
pj_pool_get_capacity(p_);
175
// Get current total bytes allocated from the pool.
177
pj_size_t get_used_size()
179
pj_pool_get_used_size(p_);
185
void *alloc(pj_size_t size)
187
return pj_pool_alloc(p_, size);
191
// Allocate elements and zero fill the memory.
193
void *calloc(pj_size_t count, pj_size_t elem)
195
return pj_pool_calloc(p_, count, elem);
199
// Allocate and zero fill memory.
201
void *zalloc(pj_size_t size)
203
return pj_pool_zalloc(p_, size);
214
class Pj_Caching_Pool
218
// Construct caching pool.
220
Pj_Caching_Pool( pj_size_t cache_capacity = 0,
221
const pj_pool_factory_policy *pol=&pj_pool_factory_default_policy)
223
pj_caching_pool_init(&cp_, pol, cache_capacity);
227
// Destroy caching pool.
231
pj_caching_pool_destroy(&cp_);
237
pj_pool_t *create_pool( pj_size_t initial_size,
238
pj_size_t increment_size,
239
const char *name = NULL,
240
pj_pool_callback *callback = NULL)
242
return (pj_pool_t*)(*cp_.factory.create_pool)(&cp_.factory, name,
253
// Inlines for Pj_Object
255
inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool *pool)
257
return pool->alloc(class_size);
259
inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool &pool)
261
return pool.alloc(class_size);
265
// Inlines for Pj_Pool
267
inline Pj_Pool::Pj_Pool( Pj_Caching_Pool &caching_pool,
268
pj_size_t initial_size,
269
pj_size_t increment_size,
271
pj_pool_callback *callback)
273
p_ = caching_pool.create_pool(initial_size, increment_size, name,
278
#endif /* __PJPP_POOL_HPP__ */