pool_i.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <pj/string.h>
  20. PJ_IDEF(pj_size_t) pj_pool_get_capacity( pj_pool_t *pool )
  21. {
  22. return pool->capacity;
  23. }
  24. PJ_IDEF(pj_size_t) pj_pool_get_used_size( pj_pool_t *pool )
  25. {
  26. pj_pool_block *b = pool->block_list.next;
  27. pj_size_t used_size = sizeof(pj_pool_t);
  28. while (b != &pool->block_list) {
  29. used_size += (b->cur - b->buf) + sizeof(pj_pool_block);
  30. b = b->next;
  31. }
  32. return used_size;
  33. }
  34. PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t size )
  35. {
  36. /* The operation below is valid for size==0.
  37. * When size==0, the function will return the pointer to the pool
  38. * memory address, but no memory will be allocated.
  39. */
  40. if (size & (PJ_POOL_ALIGNMENT-1)) {
  41. size = (size + PJ_POOL_ALIGNMENT) & ~(PJ_POOL_ALIGNMENT-1);
  42. }
  43. if ((pj_size_t)(block->end - block->cur) >= size) {
  44. void *ptr = block->cur;
  45. block->cur += size;
  46. return ptr;
  47. }
  48. return NULL;
  49. }
  50. PJ_IDEF(void*) pj_pool_alloc( pj_pool_t *pool, pj_size_t size)
  51. {
  52. void *ptr = pj_pool_alloc_from_block(pool->block_list.next, size);
  53. if (!ptr)
  54. ptr = pj_pool_allocate_find(pool, size);
  55. return ptr;
  56. }
  57. PJ_IDEF(void*) pj_pool_calloc( pj_pool_t *pool, pj_size_t count, pj_size_t size)
  58. {
  59. void *buf = pj_pool_alloc( pool, size*count);
  60. if (buf)
  61. pj_bzero(buf, size * count);
  62. return buf;
  63. }
  64. PJ_IDEF(const char *) pj_pool_getobjname( const pj_pool_t *pool )
  65. {
  66. return pool->obj_name;
  67. }
  68. PJ_IDEF(pj_pool_t*) pj_pool_create( pj_pool_factory *f,
  69. const char *name,
  70. pj_size_t initial_size,
  71. pj_size_t increment_size,
  72. pj_pool_callback *callback)
  73. {
  74. return (*f->create_pool)(f, name, initial_size, increment_size, callback);
  75. }
  76. PJ_IDEF(void) pj_pool_release( pj_pool_t *pool )
  77. {
  78. #if PJ_POOL_RELEASE_WIPE_DATA
  79. pj_pool_block *b;
  80. b = pool->block_list.next;
  81. while (b != &pool->block_list) {
  82. volatile unsigned char *p = b->buf;
  83. while (p < b->end) *p++ = 0;
  84. b = b->next;
  85. }
  86. #endif
  87. if (pool->factory->release_pool)
  88. (*pool->factory->release_pool)(pool->factory, pool);
  89. }
  90. PJ_IDEF(void) pj_pool_safe_release( pj_pool_t **ppool )
  91. {
  92. pj_pool_t *pool = *ppool;
  93. *ppool = NULL;
  94. if (pool)
  95. pj_pool_release(pool);
  96. }
  97. PJ_IDEF(void) pj_pool_secure_release( pj_pool_t **ppool )
  98. {
  99. pj_pool_block *b;
  100. pj_pool_t *pool = *ppool;
  101. *ppool = NULL;
  102. if (!pool)
  103. return;
  104. b = pool->block_list.next;
  105. while (b != &pool->block_list) {
  106. volatile unsigned char *p = b->buf;
  107. while (p < b->end) *p++ = 0;
  108. b = b->next;
  109. }
  110. pj_pool_release(pool);
  111. }