pool_buf.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_buf.h>
  20. #include <pj/assert.h>
  21. #include <pj/os.h>
  22. static struct pj_pool_factory stack_based_factory;
  23. struct creation_param
  24. {
  25. void *stack_buf;
  26. pj_size_t size;
  27. };
  28. static int is_initialized;
  29. static long tls = -1;
  30. static void* stack_alloc(pj_pool_factory *factory, pj_size_t size);
  31. static void pool_buf_cleanup(void)
  32. {
  33. if (tls != -1) {
  34. pj_thread_local_free(tls);
  35. tls = -1;
  36. }
  37. if (is_initialized)
  38. is_initialized = 0;
  39. }
  40. static pj_status_t pool_buf_initialize(void)
  41. {
  42. pj_atexit(&pool_buf_cleanup);
  43. stack_based_factory.policy.block_alloc = &stack_alloc;
  44. return pj_thread_local_alloc(&tls);
  45. }
  46. static void* stack_alloc(pj_pool_factory *factory, pj_size_t size)
  47. {
  48. struct creation_param *param;
  49. void *buf;
  50. PJ_UNUSED_ARG(factory);
  51. param = (struct creation_param*) pj_thread_local_get(tls);
  52. if (param == NULL) {
  53. /* Don't assert(), this is normal no-memory situation */
  54. return NULL;
  55. }
  56. pj_thread_local_set(tls, NULL);
  57. PJ_ASSERT_RETURN(size <= param->size, NULL);
  58. buf = param->stack_buf;
  59. /* Prevent the buffer from being reused */
  60. param->stack_buf = NULL;
  61. return buf;
  62. }
  63. PJ_DEF(pj_pool_t*) pj_pool_create_on_buf(const char *name,
  64. void *buf,
  65. pj_size_t size)
  66. {
  67. #if PJ_HAS_POOL_ALT_API == 0
  68. struct creation_param param;
  69. pj_size_t align_diff;
  70. PJ_ASSERT_RETURN(buf && size, NULL);
  71. if (!is_initialized) {
  72. if (pool_buf_initialize() != PJ_SUCCESS)
  73. return NULL;
  74. is_initialized = 1;
  75. }
  76. /* Check and align buffer */
  77. align_diff = (pj_size_t)buf;
  78. if (align_diff & (PJ_POOL_ALIGNMENT-1)) {
  79. align_diff &= (PJ_POOL_ALIGNMENT-1);
  80. buf = (void*) (((char*)buf) + align_diff);
  81. size -= align_diff;
  82. }
  83. param.stack_buf = buf;
  84. param.size = size;
  85. pj_thread_local_set(tls, &param);
  86. return pj_pool_create_int(&stack_based_factory, name, size, 0,
  87. pj_pool_factory_default_policy.callback);
  88. #else
  89. PJ_UNUSED_ARG(buf);
  90. return pj_pool_create(NULL, name, size, size, NULL);
  91. #endif
  92. }