pool_buf.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 __POOL_STACK_H__
  20. #define __POOL_STACK_H__
  21. #include <pj/pool.h>
  22. /**
  23. * @defgroup PJ_POOL_BUFFER Stack/Buffer Based Memory Pool Allocator
  24. * @ingroup PJ_POOL_GROUP
  25. * @brief Stack/buffer based pool.
  26. *
  27. * This section describes an implementation of memory pool which uses
  28. * memory allocated from the stack. Application creates this pool
  29. * by specifying a buffer (which can be allocated from static memory or
  30. * stack variable), and then use normal pool API to access/use the pool.
  31. *
  32. * If the buffer specified during pool creation is a buffer located in the
  33. * stack, the pool will be invalidated (or implicitly destroyed) when the
  34. * execution leaves the enclosing block containing the buffer. Note
  35. * that application must make sure that any objects allocated from this
  36. * pool (such as mutexes) have been destroyed before the pool gets
  37. * invalidated.
  38. *
  39. * Sample usage:
  40. *
  41. * \code
  42. #include <pjlib.h>
  43. static void test()
  44. {
  45. char buffer[500];
  46. pj_pool_t *pool;
  47. void *p;
  48. pool = pj_pool_create_on_buf("thepool", buffer, sizeof(buffer));
  49. // Use the pool as usual
  50. p = pj_pool_alloc(pool, ...);
  51. ...
  52. // No need to release the pool
  53. }
  54. int main()
  55. {
  56. pj_init();
  57. test();
  58. return 0;
  59. }
  60. \endcode
  61. *
  62. * @{
  63. */
  64. PJ_BEGIN_DECL
  65. /**
  66. * Create the pool using the specified buffer as the pool's memory.
  67. * Subsequent allocations made from the pool will use the memory from
  68. * this buffer.
  69. *
  70. * If the buffer specified in the parameter is a buffer located in the
  71. * stack, the pool will be invalid (or implicitly destroyed) when the
  72. * execution leaves the enclosing block containing the buffer. Note
  73. * that application must make sure that any objects allocated from this
  74. * pool (such as mutexes) have been destroyed before the pool gets
  75. * invalidated.
  76. *
  77. * @param name Optional pool name.
  78. * @param buf Buffer to be used by the pool.
  79. * @param size The size of the buffer.
  80. *
  81. * @return The memory pool instance.
  82. */
  83. PJ_DECL(pj_pool_t*) pj_pool_create_on_buf(const char *name,
  84. void *buf,
  85. pj_size_t size);
  86. PJ_END_DECL
  87. /**
  88. * @}
  89. */
  90. #endif /* __POOL_STACK_H__ */