Debian Codegraph @

Xpjproject/pjlib/include/pj++/pool.hpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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
24 class Pj_Pool;
25 class Pj_Caching_Pool;
26
27 //
28 // Base class for all Pjlib objects
29 //
30 class Pj_Object
31 {
32 public:
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
52 private:
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 //
65 class Pj_Pool : public Pj_Object
66 {
67 public:
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 // You can cast Pj_Pool to pj_pool_t*
127 //
128 operator pj_pool_t*()
129 {
130 return p_;
131 }
132
133 //
134 // Get pjlib compatible pool object.
135 //
136 pj_pool_t *pool_()
137 {
138 return p_;
139 }
140
141 //
142 // Get pjlib compatible pool object.
143 //
144 const pj_pool_t *pool_() const
145 {
146 return p_;
147 }
148
149 //
150 // Get pjlib compatible pool object.
151 //
152 pj_pool_t *pj_pool_t_()
153 {
154 return p_;
155 }
156
157 //
158 // Reset pool.
159 //
160 void reset()
161 {
162 pj_pool_reset(p_);
163 }
164
165 //
166 // Get current capacity.
167 //
168 pj_size_t get_capacity()
169 {
170 pj_pool_get_capacity(p_);
171 }
172
173 //
174 // Get current total bytes allocated from the pool.
175 //
176 pj_size_t get_used_size()
177 {
178 pj_pool_get_used_size(p_);
179 }
180
181 //
182 // Allocate.
183 //
184 void *alloc(pj_size_t size)
185 {
186 return pj_pool_alloc(p_, size);
187 }
188
189 //
190 // Allocate elements and zero fill the memory.
191 //
192 void *calloc(pj_size_t count, pj_size_t elem)
193 {
194 return pj_pool_calloc(p_, count, elem);
195 }
196
197 //
198 // Allocate and zero fill memory.
199 //
200 void *zalloc(pj_size_t size)
201 {
202 return pj_pool_zalloc(p_, size);
203 }
204
205 private:
206 pj_pool_t *p_;
207 };
208
209
210 //
211 // Caching pool.
212 //
213 class Pj_Caching_Pool
214 {
215 public:
216 //
217 // Construct caching pool.
218 //
219 Pj_Caching_Pool( pj_size_t cache_capacity = 0,
220 const pj_pool_factory_policy *pol=&pj_pool_factory_default_policy)
221 {
222 pj_caching_pool_init(&cp_, pol, cache_capacity);
223 }
224
225 //
226 // Destroy caching pool.
227 //
228 ~Pj_Caching_Pool()
229 {
230 pj_caching_pool_destroy(&cp_);
231 }
232
233 //
234 // Create pool.
235 //
236 pj_pool_t *create_pool( pj_size_t initial_size,
237 pj_size_t increment_size,
238 const char *name = NULL,
239 pj_pool_callback *callback = NULL)
240 {
241 return (pj_pool_t*)(*cp_.factory.create_pool)(&cp_.factory, name,
242 initial_size,
243 increment_size,
244 callback);
245 }
246
247 private:
248 pj_caching_pool cp_;
249 };
250
251 //
252 // Inlines for Pj_Object
253 //
254 inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool *pool)
255 {
256 return pool->alloc(class_size);
257 }
258 inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool &pool)
259 {
260 return pool.alloc(class_size);
261 }
262
263 //
264 // Inlines for Pj_Pool
265 //
266 inline Pj_Pool::Pj_Pool( Pj_Caching_Pool &caching_pool,
267 pj_size_t initial_size,
268 pj_size_t increment_size,
269 const char *name,
270 pj_pool_callback *callback)
271 {
272 p_ = caching_pool.create_pool(initial_size, increment_size, name,
273 callback);
274 }
275
276
277 #endif /* __PJPP_POOL_HPP__ */
278