Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | #ifndef __PJPP_POOL_HPP__ |
| 21 | #define __PJPP_POOL_HPP__ |
| 22 | |
| 23 | #include <pj/pool.h> |
| 24 | |
| 25 | class Pj_Pool; |
| 26 | class Pj_Caching_Pool; |
| 27 | |
| 28 | // |
| 29 | // Base class for all Pjlib objects |
| 30 | // |
| 31 | class Pj_Object |
| 32 | { |
| 33 | public: |
| 34 | void *operator new(unsigned int class_size, Pj_Pool *pool); |
| 35 | void *operator new(unsigned int class_size, Pj_Pool &pool); |
| 36 | |
| 37 | void operator delete(void*) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | void operator delete(void*, Pj_Pool*) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void operator delete(void*, Pj_Pool&) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | // |
| 50 | // Inline implementations at the end of this file. |
| 51 | // |
| 52 | |
| 53 | private: |
| 54 | // Can not use normal new operator; must use pool. |
| 55 | // e.g.: |
| 56 | // obj = new(pool) Pj_The_Object(pool, ...); |
| 57 | // |
| 58 | void *operator new(unsigned int) |
| 59 | {} |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | // |
| 64 | // Pool. |
| 65 | // |
| 66 | class Pj_Pool : public Pj_Object |
| 67 | { |
| 68 | public: |
| 69 | // |
| 70 | // Default constructor, initializes internal pool to NULL. |
| 71 | // Application must call attach() some time later. |
| 72 | // |
| 73 | Pj_Pool() |
| 74 | : p_(NULL) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | // |
| 79 | // Create pool. |
| 80 | // |
| 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); |
| 86 | |
| 87 | // |
| 88 | // Construct from existing pool. |
| 89 | // |
| 90 | explicit Pj_Pool(pj_pool_t *pool) |
| 91 | : p_(pool) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | // |
| 96 | // Attach existing pool. |
| 97 | // |
| 98 | void attach(pj_pool_t *pool) |
| 99 | { |
| 100 | p_ = pool; |
| 101 | } |
| 102 | |
| 103 | // |
| 104 | // Destructor. |
| 105 | // |
| 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. |
| 109 | // |
| 110 | // This is where C++ is trickier than plain C!! |
| 111 | // |
| 112 | ~Pj_Pool() |
| 113 | { |
| 114 | if (p_) |
| 115 | pj_pool_release(p_); |
| 116 | } |
| 117 | |
| 118 | // |
| 119 | // Get name. |
| 120 | // |
| 121 | const char *getobjname() const |
| 122 | { |
| 123 | return pj_pool_getobjname(p_); |
| 124 | } |
| 125 | |
| 126 | // |
| 127 | // You can cast Pj_Pool to pj_pool_t* |
| 128 | // |
| 129 | operator pj_pool_t*() |
| 130 | { |
| 131 | return p_; |
| 132 | } |
| 133 | |
| 134 | // |
| 135 | // Get pjlib compatible pool object. |
| 136 | // |
| 137 | pj_pool_t *pool_() |
| 138 | { |
| 139 | return p_; |
| 140 | } |
| 141 | |
| 142 | // |
| 143 | // Get pjlib compatible pool object. |
| 144 | // |
| 145 | const pj_pool_t *pool_() const |
| 146 | { |
| 147 | return p_; |
| 148 | } |
| 149 | |
| 150 | // |
| 151 | // Get pjlib compatible pool object. |
| 152 | // |
| 153 | pj_pool_t *pj_pool_t_() |
| 154 | { |
| 155 | return p_; |
| 156 | } |
| 157 | |
| 158 | // |
| 159 | // Reset pool. |
| 160 | // |
| 161 | void reset() |
| 162 | { |
| 163 | pj_pool_reset(p_); |
| 164 | } |
| 165 | |
| 166 | // |
| 167 | // Get current capacity. |
| 168 | // |
| 169 | pj_size_t get_capacity() |
| 170 | { |
| 171 | pj_pool_get_capacity(p_); |
| 172 | } |
| 173 | |
| 174 | // |
| 175 | // Get current total bytes allocated from the pool. |
| 176 | // |
| 177 | pj_size_t get_used_size() |
| 178 | { |
| 179 | pj_pool_get_used_size(p_); |
| 180 | } |
| 181 | |
| 182 | // |
| 183 | // Allocate. |
| 184 | // |
| 185 | void *alloc(pj_size_t size) |
| 186 | { |
| 187 | return pj_pool_alloc(p_, size); |
| 188 | } |
| 189 | |
| 190 | // |
| 191 | // Allocate elements and zero fill the memory. |
| 192 | // |
| 193 | void *calloc(pj_size_t count, pj_size_t elem) |
| 194 | { |
| 195 | return pj_pool_calloc(p_, count, elem); |
| 196 | } |
| 197 | |
| 198 | // |
| 199 | // Allocate and zero fill memory. |
| 200 | // |
| 201 | void *zalloc(pj_size_t size) |
| 202 | { |
| 203 | return pj_pool_zalloc(p_, size); |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | pj_pool_t *p_; |
| 208 | }; |
| 209 | |
| 210 | |
| 211 | // |
| 212 | // Caching pool. |
| 213 | // |
| 214 | class Pj_Caching_Pool |
| 215 | { |
| 216 | public: |
| 217 | // |
| 218 | // Construct caching pool. |
| 219 | // |
| 220 | Pj_Caching_Pool( pj_size_t cache_capacity = 0, |
| 221 | const pj_pool_factory_policy *pol=&pj_pool_factory_default_policy) |
| 222 | { |
| 223 | pj_caching_pool_init(&cp_, pol, cache_capacity); |
| 224 | } |
| 225 | |
| 226 | // |
| 227 | // Destroy caching pool. |
| 228 | // |
| 229 | ~Pj_Caching_Pool() |
| 230 | { |
| 231 | pj_caching_pool_destroy(&cp_); |
| 232 | } |
| 233 | |
| 234 | // |
| 235 | // Create pool. |
| 236 | // |
| 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) |
| 241 | { |
| 242 | return (pj_pool_t*)(*cp_.factory.create_pool)(&cp_.factory, name, |
| 243 | initial_size, |
| 244 | increment_size, |
| 245 | callback); |
| 246 | } |
| 247 | |
| 248 | private: |
| 249 | pj_caching_pool cp_; |
| 250 | }; |
| 251 | |
| 252 | // |
| 253 | // Inlines for Pj_Object |
| 254 | // |
| 255 | inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool *pool) |
| 256 | { |
| 257 | return pool->alloc(class_size); |
| 258 | } |
| 259 | inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool &pool) |
| 260 | { |
| 261 | return pool.alloc(class_size); |
| 262 | } |
| 263 | |
| 264 | // |
| 265 | // Inlines for Pj_Pool |
| 266 | // |
| 267 | inline Pj_Pool::Pj_Pool( Pj_Caching_Pool &caching_pool, |
| 268 | pj_size_t initial_size, |
| 269 | pj_size_t increment_size, |
| 270 | const char *name, |
| 271 | pj_pool_callback *callback) |
| 272 | { |
| 273 | p_ = caching_pool.create_pool(initial_size, increment_size, name, |
| 274 | callback); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | #endif /* __PJPP_POOL_HPP__ */ |
| 279 | |