blob: 02b54336589e0d470c04808e8c438740070c9c5c [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJPP_POOL_HPP__
20#define __PJPP_POOL_HPP__
21
22#include <pj/pool.h>
23
24class Pj_Pool;
25class Pj_Caching_Pool;
26
27//
28// Base class for all Pjlib objects
29//
30class Pj_Object
31{
32public:
33 void *operator new(unsigned int class_size, Pj_Pool *pool);
34 void *operator new(unsigned int class_size, Pj_Pool &pool);
35
36 void operator delete(void*)
37 {
38 }
39
40 void operator delete(void*, Pj_Pool*)
41 {
42 }
43
44 void operator delete(void*, Pj_Pool&)
45 {
46 }
47
48 //
49 // Inline implementations at the end of this file.
50 //
51
52private:
53 // Can not use normal new operator; must use pool.
54 // e.g.:
55 // obj = new(pool) Pj_The_Object(pool, ...);
56 //
57 void *operator new(unsigned int)
58 {}
59};
60
61
62//
63// Pool.
64//
65class Pj_Pool : public Pj_Object
66{
67public:
68 //
69 // Default constructor, initializes internal pool to NULL.
70 // Application must call attach() some time later.
71 //
72 Pj_Pool()
73 : p_(NULL)
74 {
75 }
76
77 //
78 // Create pool.
79 //
80 Pj_Pool(Pj_Caching_Pool &caching_pool,
81 pj_size_t initial_size,
82 pj_size_t increment_size,
83 const char *name = NULL,
84 pj_pool_callback *callback = NULL);
85
86 //
87 // Construct from existing pool.
88 //
89 explicit Pj_Pool(pj_pool_t *pool)
90 : p_(pool)
91 {
92 }
93
94 //
95 // Attach existing pool.
96 //
97 void attach(pj_pool_t *pool)
98 {
99 p_ = pool;
100 }
101
102 //
103 // Destructor.
104 //
105 // Release pool back to factory. Remember: if you delete pool, then
106 // make sure that all objects that have been allocated from this pool
107 // have been properly destroyed.
108 //
109 // This is where C++ is trickier than plain C!!
110 //
111 ~Pj_Pool()
112 {
113 if (p_)
114 pj_pool_release(p_);
115 }
116
117 //
118 // Get name.
119 //
120 const char *getobjname() const
121 {
122 return pj_pool_getobjname(p_);
123 }
124
125 //
126 // Get pjlib compatible pool object.
127 //
128 pj_pool_t *pool_()
129 {
130 return p_;
131 }
132
133 //
134 // Get pjlib compatible pool object.
135 //
136 const pj_pool_t *pool_() const
137 {
138 return p_;
139 }
140
141 //
142 // Get pjlib compatible pool object.
143 //
144 pj_pool_t *pj_pool_t_()
145 {
146 return p_;
147 }
148
149 //
150 // Reset pool.
151 //
152 void reset()
153 {
154 pj_pool_reset(p_);
155 }
156
157 //
158 // Get current capacity.
159 //
160 pj_size_t get_capacity()
161 {
162 pj_pool_get_capacity(p_);
163 }
164
165 //
166 // Get current total bytes allocated from the pool.
167 //
168 pj_size_t get_used_size()
169 {
170 pj_pool_get_used_size(p_);
171 }
172
173 //
174 // Allocate.
175 //
176 void *alloc(pj_size_t size)
177 {
178 return pj_pool_alloc(p_, size);
179 }
180
181 //
182 // Allocate elements and zero fill the memory.
183 //
184 void *calloc(pj_size_t count, pj_size_t elem)
185 {
186 return pj_pool_calloc(p_, count, elem);
187 }
188
189 //
190 // Allocate and zero fill memory.
191 //
192 void *zalloc(pj_size_t size)
193 {
194 return pj_pool_zalloc(p_, size);
195 }
196
197private:
198 pj_pool_t *p_;
199};
200
201
202//
203// Caching pool.
204//
205class Pj_Caching_Pool
206{
207public:
208 //
209 // Construct caching pool.
210 //
211 Pj_Caching_Pool( pj_size_t cache_capacity = 0,
212 const pj_pool_factory_policy *pol=&pj_pool_factory_default_policy)
213 {
214 pj_caching_pool_init(&cp_, pol, cache_capacity);
215 }
216
217 //
218 // Destroy caching pool.
219 //
220 ~Pj_Caching_Pool()
221 {
222 pj_caching_pool_destroy(&cp_);
223 }
224
225 //
226 // Create pool.
227 //
228 pj_pool_t *create_pool( pj_size_t initial_size,
229 pj_size_t increment_size,
230 const char *name = NULL,
231 pj_pool_callback *callback = NULL)
232 {
233 return (pj_pool_t*)(*cp_.factory.create_pool)(&cp_.factory, name,
234 initial_size,
235 increment_size,
236 callback);
237 }
238
239private:
240 pj_caching_pool cp_;
241};
242
243//
244// Inlines for Pj_Object
245//
246inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool *pool)
247{
248 return pool->alloc(class_size);
249}
250inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool &pool)
251{
252 return pool.alloc(class_size);
253}
254
255//
256// Inlines for Pj_Pool
257//
258inline Pj_Pool::Pj_Pool( Pj_Caching_Pool &caching_pool,
259 pj_size_t initial_size,
260 pj_size_t increment_size,
261 const char *name,
262 pj_pool_callback *callback)
263{
264 p_ = caching_pool.create_pool(initial_size, increment_size, name,
265 callback);
266}
267
268
269#endif /* __PJPP_POOL_HPP__ */
270