pool_policy_malloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/pool.h>
  20. #include <pj/except.h>
  21. #include <pj/os.h>
  22. #include <pj/compat/malloc.h>
  23. #if !PJ_HAS_POOL_ALT_API
  24. /*
  25. * This file contains pool default policy definition and implementation.
  26. */
  27. #include "pool_signature.h"
  28. static void *default_block_alloc(pj_pool_factory *factory, pj_size_t size)
  29. {
  30. void *p;
  31. PJ_CHECK_STACK();
  32. if (factory->on_block_alloc) {
  33. int rc;
  34. rc = factory->on_block_alloc(factory, size);
  35. if (!rc)
  36. return NULL;
  37. }
  38. p = malloc(size+(SIG_SIZE << 1));
  39. if (p == NULL) {
  40. if (factory->on_block_free)
  41. factory->on_block_free(factory, size);
  42. } else {
  43. /* Apply signature when PJ_SAFE_POOL is set. It will move
  44. * "p" pointer forward.
  45. */
  46. APPLY_SIG(p, size);
  47. }
  48. return p;
  49. }
  50. static void default_block_free(pj_pool_factory *factory, void *mem,
  51. pj_size_t size)
  52. {
  53. PJ_CHECK_STACK();
  54. if (factory->on_block_free)
  55. factory->on_block_free(factory, size);
  56. /* Check and remove signature when PJ_SAFE_POOL is set. It will
  57. * move "mem" pointer backward.
  58. */
  59. REMOVE_SIG(mem, size);
  60. /* Note that when PJ_SAFE_POOL is set, the actual size of the block
  61. * is size + SIG_SIZE*2.
  62. */
  63. free(mem);
  64. }
  65. static void default_pool_callback(pj_pool_t *pool, pj_size_t size)
  66. {
  67. PJ_CHECK_STACK();
  68. PJ_UNUSED_ARG(pool);
  69. PJ_UNUSED_ARG(size);
  70. PJ_THROW(PJ_NO_MEMORY_EXCEPTION);
  71. }
  72. PJ_DEF_DATA(pj_pool_factory_policy) pj_pool_factory_default_policy =
  73. {
  74. &default_block_alloc,
  75. &default_block_free,
  76. &default_pool_callback,
  77. 0
  78. };
  79. PJ_DEF(const pj_pool_factory_policy*) pj_pool_factory_get_default_policy(void)
  80. {
  81. return &pj_pool_factory_default_policy;
  82. }
  83. #endif /* PJ_HAS_POOL_ALT_API */