pool_alt.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2008-2011 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 __PJ_POOL_ALT_H__
  20. #define __PJ_POOL_ALT_H__
  21. #define __PJ_POOL_H__
  22. PJ_BEGIN_DECL
  23. /**
  24. * The type for function to receive callback from the pool when it is unable
  25. * to allocate memory. The elegant way to handle this condition is to throw
  26. * exception, and this is what is expected by most of this library
  27. * components.
  28. */
  29. typedef void pj_pool_callback(pj_pool_t *pool, pj_size_t size);
  30. struct pj_pool_mem
  31. {
  32. struct pj_pool_mem *next;
  33. /* data follows immediately */
  34. };
  35. struct pj_pool_t
  36. {
  37. struct pj_pool_mem *first_mem;
  38. pj_pool_factory *factory;
  39. char obj_name[32];
  40. pj_size_t used_size;
  41. pj_pool_callback *cb;
  42. };
  43. #define PJ_POOL_SIZE (sizeof(struct pj_pool_t))
  44. /**
  45. * This constant denotes the exception number that will be thrown by default
  46. * memory factory policy when memory allocation fails.
  47. */
  48. PJ_DECL_DATA(int) PJ_NO_MEMORY_EXCEPTION;
  49. /**
  50. * Get #PJ_NO_MEMORY_EXCEPTION constant.
  51. */
  52. PJ_DECL(int) pj_NO_MEMORY_EXCEPTION(void);
  53. /*
  54. * Declare all pool API as macro that calls the implementation
  55. * function.
  56. */
  57. #define pj_pool_create(fc,nm,init,inc,cb) \
  58. pj_pool_create_imp(__FILE__, __LINE__, fc, nm, init, inc, cb)
  59. #define pj_pool_release(pool) pj_pool_release_imp(pool)
  60. #define pj_pool_safe_release(pool) pj_pool_safe_release_imp(pool)
  61. #define pj_pool_secure_release(pool) pj_pool_secure_release_imp(pool)
  62. #define pj_pool_getobjname(pool) pj_pool_getobjname_imp(pool)
  63. #define pj_pool_reset(pool) pj_pool_reset_imp(pool)
  64. #define pj_pool_get_capacity(pool) pj_pool_get_capacity_imp(pool)
  65. #define pj_pool_get_used_size(pool) pj_pool_get_used_size_imp(pool)
  66. #define pj_pool_alloc(pool,sz) \
  67. pj_pool_alloc_imp(__FILE__, __LINE__, pool, sz)
  68. #define pj_pool_calloc(pool,cnt,elem) \
  69. pj_pool_calloc_imp(__FILE__, __LINE__, pool, cnt, elem)
  70. #define pj_pool_zalloc(pool,sz) \
  71. pj_pool_zalloc_imp(__FILE__, __LINE__, pool, sz)
  72. /*
  73. * Declare prototypes for pool implementation API.
  74. */
  75. /* Create pool */
  76. PJ_DECL(pj_pool_t*) pj_pool_create_imp(const char *file, int line,
  77. void *factory,
  78. const char *name,
  79. pj_size_t initial_size,
  80. pj_size_t increment_size,
  81. pj_pool_callback *callback);
  82. /* Release pool */
  83. PJ_DECL(void) pj_pool_release_imp(pj_pool_t *pool);
  84. /* Safe release pool */
  85. PJ_DECL(void) pj_pool_safe_release_imp(pj_pool_t **pool);
  86. /* Secure release pool */
  87. PJ_DECL(void) pj_pool_secure_release_imp(pj_pool_t **pool);
  88. /* Get pool name */
  89. PJ_DECL(const char*) pj_pool_getobjname_imp(pj_pool_t *pool);
  90. /* Reset pool */
  91. PJ_DECL(void) pj_pool_reset_imp(pj_pool_t *pool);
  92. /* Get capacity */
  93. PJ_DECL(pj_size_t) pj_pool_get_capacity_imp(pj_pool_t *pool);
  94. /* Get total used size */
  95. PJ_DECL(pj_size_t) pj_pool_get_used_size_imp(pj_pool_t *pool);
  96. /* Allocate memory from the pool */
  97. PJ_DECL(void*) pj_pool_alloc_imp(const char *file, int line,
  98. pj_pool_t *pool, pj_size_t sz);
  99. /* Allocate memory from the pool and zero the memory */
  100. PJ_DECL(void*) pj_pool_calloc_imp(const char *file, int line,
  101. pj_pool_t *pool, unsigned cnt,
  102. unsigned elemsz);
  103. /* Allocate memory from the pool and zero the memory */
  104. PJ_DECL(void*) pj_pool_zalloc_imp(const char *file, int line,
  105. pj_pool_t *pool, pj_size_t sz);
  106. #define PJ_POOL_ZALLOC_T(pool,type) \
  107. ((type*)pj_pool_zalloc(pool, sizeof(type)))
  108. #define PJ_POOL_ALLOC_T(pool,type) \
  109. ((type*)pj_pool_alloc(pool, sizeof(type)))
  110. #ifndef PJ_POOL_ALIGNMENT
  111. # define PJ_POOL_ALIGNMENT 4
  112. #endif
  113. /**
  114. * This structure declares pool factory interface.
  115. */
  116. typedef struct pj_pool_factory_policy
  117. {
  118. /**
  119. * Allocate memory block (for use by pool). This function is called
  120. * by memory pool to allocate memory block.
  121. *
  122. * @param factory Pool factory.
  123. * @param size The size of memory block to allocate.
  124. *
  125. * @return Memory block.
  126. */
  127. void* (*block_alloc)(pj_pool_factory *factory, pj_size_t size);
  128. /**
  129. * Free memory block.
  130. *
  131. * @param factory Pool factory.
  132. * @param mem Memory block previously allocated by block_alloc().
  133. * @param size The size of memory block.
  134. */
  135. void (*block_free)(pj_pool_factory *factory, void *mem, pj_size_t size);
  136. /**
  137. * Default callback to be called when memory allocation fails.
  138. */
  139. pj_pool_callback *callback;
  140. /**
  141. * Option flags.
  142. */
  143. unsigned flags;
  144. } pj_pool_factory_policy;
  145. struct pj_pool_factory
  146. {
  147. pj_pool_factory_policy policy;
  148. int dummy;
  149. };
  150. struct pj_caching_pool
  151. {
  152. pj_pool_factory factory;
  153. /* just to make it compilable */
  154. unsigned used_count;
  155. unsigned used_size;
  156. unsigned peak_used_size;
  157. };
  158. /* just to make it compilable */
  159. typedef struct pj_pool_block
  160. {
  161. int dummy;
  162. } pj_pool_block;
  163. #define pj_caching_pool_init( cp, pol, mac)
  164. #define pj_caching_pool_destroy(cp)
  165. #define pj_pool_factory_dump(pf, detail)
  166. PJ_END_DECL
  167. #endif /* __PJ_POOL_ALT_H__ */