pool.hpp 5.6 KB

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