pool_policy_new.cpp 2.6 KB

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