Ignore:
Timestamp:
Nov 9, 2005 3:37:19 PM (18 years ago)
Author:
bennylp
Message:

Rework pjlib++

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/main/pjlib/include/pj++/pool.hpp

    r29 r36  
    11/* $Id$ 
    2  * 
    32 */ 
    43#ifndef __PJPP_POOL_H__ 
     
    76#include <pj/pool.h> 
    87 
    9 class PJ_Pool 
     8class Pj_Pool; 
     9class Pj_Caching_Pool; 
     10 
     11// 
     12// Base class for all Pjlib objects 
     13// 
     14class Pj_Object 
    1015{ 
    1116public: 
     17    void *operator new(unsigned int class_size, Pj_Pool *pool); 
     18    void operator delete(void*); 
     19    void operator delete(void*, Pj_Pool*); 
     20 
     21    // 
     22    // Inline implementations at the end of this file. 
     23    // 
     24 
     25private: 
     26    // Can not use normal new operator; must use pool. 
     27    // e.g.: 
     28    //   obj = new(pool) Pj_The_Object(pool, ...); 
     29    // 
     30    void *operator new(unsigned int) 
     31    {} 
     32}; 
     33 
     34 
     35// 
     36// Pool. 
     37// 
     38class Pj_Pool : public Pj_Object 
     39{ 
     40public: 
     41    // 
     42    // Default constructor, initializes internal pool to NULL. 
     43    // Application must call attach() some time later. 
     44    // 
     45    Pj_Pool() 
     46        : p_(NULL) 
     47    { 
     48    } 
     49 
     50    // 
     51    // Create pool. 
     52    // 
     53    Pj_Pool(Pj_Caching_Pool &caching_pool, 
     54            pj_size_t initial_size,  
     55            pj_size_t increment_size,  
     56            const char *name = NULL,  
     57            pj_pool_callback *callback = NULL); 
     58 
     59    // 
     60    // Construct from existing pool. 
     61    // 
     62    explicit Pj_Pool(pj_pool_t *pool) 
     63        : p_(pool) 
     64    { 
     65    } 
     66 
     67    // 
     68    // Attach existing pool. 
     69    // 
     70    void attach(pj_pool_t *pool) 
     71    { 
     72        p_ = pool; 
     73    } 
     74 
     75    // 
     76    // Destructor. 
     77    // 
     78    // Release pool back to factory. Remember: if you delete pool, then  
     79    // make sure that all objects that have been allocated from this pool 
     80    // have been properly destroyed. 
     81    // 
     82    // This is where C++ is trickier than plain C!! 
     83    // 
     84    ~Pj_Pool() 
     85    { 
     86        if (p_) 
     87            pj_pool_release(p_); 
     88    } 
     89 
     90    // 
     91    // Get name. 
     92    // 
    1293    const char *getobjname() const 
    1394    { 
    14         return pj_pool_getobjname(this->pool_()); 
    15     } 
    16  
     95        return pj_pool_getobjname(p_); 
     96    } 
     97 
     98    // 
     99    // Get pjlib compatible pool object. 
     100    // 
    17101    pj_pool_t *pool_() 
    18102    { 
    19         return (pj_pool_t*)this; 
    20     } 
    21  
     103        return p_; 
     104    } 
     105 
     106    // 
     107    // Get pjlib compatible pool object. 
     108    // 
    22109    const pj_pool_t *pool_() const 
    23110    { 
    24         return (const pj_pool_t*)this; 
    25     } 
    26  
    27     void release() 
    28     { 
    29         pj_pool_release(this->pool_()); 
    30     } 
    31  
     111        return p_; 
     112    } 
     113 
     114    // 
     115    // Get pjlib compatible pool object. 
     116    // 
     117    pj_pool_t *pj_pool_t_() 
     118    { 
     119        return p_; 
     120    } 
     121 
     122    // 
     123    // Reset pool. 
     124    // 
    32125    void reset() 
    33126    { 
    34         pj_pool_reset(this->pool_()); 
    35     } 
    36  
     127        pj_pool_reset(p_); 
     128    } 
     129 
     130    // 
     131    // Get current capacity. 
     132    // 
    37133    pj_size_t get_capacity() 
    38134    { 
    39         pj_pool_get_capacity(this->pool_()); 
    40     } 
    41  
     135        pj_pool_get_capacity(p_); 
     136    } 
     137 
     138    // 
     139    // Get current total bytes allocated from the pool. 
     140    // 
    42141    pj_size_t get_used_size() 
    43142    { 
    44         pj_pool_get_used_size(this->pool_()); 
    45     } 
    46  
     143        pj_pool_get_used_size(p_); 
     144    } 
     145 
     146    // 
     147    // Allocate. 
     148    // 
    47149    void *alloc(pj_size_t size) 
    48150    { 
    49         return pj_pool_alloc(this->pool_(), size); 
    50     } 
    51  
     151        return pj_pool_alloc(p_, size); 
     152    } 
     153 
     154    // 
     155    // Allocate elements and zero fill the memory. 
     156    // 
    52157    void *calloc(pj_size_t count, pj_size_t elem) 
    53158    { 
    54         return pj_pool_calloc(this->pool_(), count, elem); 
    55     } 
     159        return pj_pool_calloc(p_, count, elem); 
     160    } 
     161 
     162    // 
     163    // Allocate and zero fill memory. 
     164    // 
     165    void *zalloc(pj_size_t size) 
     166    { 
     167        return pj_pool_zalloc(p_, size); 
     168    } 
     169 
     170private: 
     171    pj_pool_t *p_; 
    56172}; 
    57173 
    58 class PJ_Caching_Pool 
     174 
     175// 
     176// Caching pool. 
     177// 
     178class Pj_Caching_Pool 
    59179{ 
    60180public: 
    61     void init(pj_size_t max_capacity, 
    62               const pj_pool_factory_policy *pol=&pj_pool_factory_default_policy) 
    63     { 
    64         pj_caching_pool_init(&cp_, pol, max_capacity); 
    65     } 
    66  
    67     void destroy() 
     181    // 
     182    // Construct caching pool. 
     183    // 
     184    Pj_Caching_Pool( pj_size_t cache_capacity = 0, 
     185                     const pj_pool_factory_policy *pol=&pj_pool_factory_default_policy) 
     186    { 
     187        pj_caching_pool_init(&cp_, pol, cache_capacity); 
     188    } 
     189 
     190    // 
     191    // Destroy caching pool. 
     192    // 
     193    ~Pj_Caching_Pool() 
    68194    { 
    69195        pj_caching_pool_destroy(&cp_); 
    70196    } 
    71197 
    72     PJ_Pool *create_pool(const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback) 
    73     { 
    74         return (PJ_Pool*) (*cp_.factory.create_pool)(&cp_.factory, name, initial_size, increment_size, callback); 
    75     } 
    76  
    77     void release_pool( PJ_Pool *pool ) 
    78     { 
    79         pj_pool_release(pool->pool_()); 
     198    // 
     199    // Create pool. 
     200    // 
     201    pj_pool_t *create_pool( pj_size_t initial_size,  
     202                            pj_size_t increment_size,  
     203                            const char *name = NULL,  
     204                            pj_pool_callback *callback = NULL) 
     205    { 
     206        return (pj_pool_t*)(*cp_.factory.create_pool)(&cp_.factory, name,  
     207                                                     initial_size,  
     208                                                     increment_size,  
     209                                                     callback); 
    80210    } 
    81211 
     
    84214}; 
    85215 
     216// 
     217// Inlines for Pj_Object 
     218// 
     219inline void *Pj_Object::operator new(unsigned int class_size, Pj_Pool *pool) 
     220{ 
     221    return pool->alloc(class_size); 
     222} 
     223inline void Pj_Object::operator delete(void *ptr) 
     224{ 
     225} 
     226inline void Pj_Object::operator delete(void *ptr, Pj_Pool*) 
     227{ 
     228} 
     229 
     230// 
     231// Inlines for Pj_Pool 
     232// 
     233inline Pj_Pool::Pj_Pool( Pj_Caching_Pool &caching_pool, 
     234                         pj_size_t initial_size,  
     235                         pj_size_t increment_size,  
     236                         const char *name,  
     237                         pj_pool_callback *callback) 
     238{ 
     239    p_ = caching_pool.create_pool(initial_size, increment_size, name, 
     240                                  callback); 
     241} 
     242 
     243 
    86244#endif  /* __PJPP_POOL_H__ */ 
Note: See TracChangeset for help on using the changeset viewer.