mempool.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2002-2003 Hewlett-Packard Co
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of libunwind.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files (the
  7. "Software"), to deal in the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  21. #ifndef mempool_h
  22. #define mempool_h
  23. /* Memory pools provide simple memory management of fixed-size
  24. objects. Memory pools are used for two purposes:
  25. o To ensure a stack can be unwound even when a process
  26. is out of memory.
  27. o To ensure a stack can be unwound at any time in a
  28. multi-threaded process (e.g., even at a time when the normal
  29. malloc-lock is taken, possibly by the very thread that is
  30. being unwind).
  31. To achieve the second objective, memory pools allocate memory
  32. directly via mmap() system call (or an equivalent facility).
  33. The first objective is accomplished by reserving memory ahead of
  34. time. Since the memory requirements of stack unwinding generally
  35. depends on the complexity of the procedures being unwind, there is
  36. no absolute guarantee that unwinding will always work, but in
  37. practice, this should not be a serious problem. */
  38. #include <sys/types.h>
  39. #include "libunwind_i.h"
  40. #define sos_alloc(s) UNWI_ARCH_OBJ(_sos_alloc)(s)
  41. #define mempool_init(p,s,r) UNWI_ARCH_OBJ(_mempool_init)(p,s,r)
  42. #define mempool_alloc(p) UNWI_ARCH_OBJ(_mempool_alloc)(p)
  43. #define mempool_free(p,o) UNWI_ARCH_OBJ(_mempool_free)(p,o)
  44. /* The mempool structure should be treated as an opaque object. It's
  45. declared here only to enable static allocation of mempools. */
  46. struct mempool
  47. {
  48. pthread_mutex_t lock;
  49. size_t obj_size; /* object size (rounded up for alignment) */
  50. size_t chunk_size; /* allocation granularity */
  51. unsigned int reserve; /* minimum (desired) size of the free-list */
  52. unsigned int num_free; /* number of objects on the free-list */
  53. struct object
  54. {
  55. struct object *next;
  56. }
  57. *free_list;
  58. };
  59. /* Emergency allocation for one-time stuff that doesn't fit the memory
  60. pool model. A limited amount of memory is available in this
  61. fashion and once allocated, there is no way to free it. */
  62. extern void *sos_alloc (size_t size);
  63. /* Initialize POOL for an object size of OBJECT_SIZE bytes. RESERVE
  64. is the number of objects that should be reserved for use under
  65. tight memory situations. If it is zero, mempool attempts to pick a
  66. reasonable default value. */
  67. extern void mempool_init (struct mempool *pool,
  68. size_t obj_size, size_t reserve);
  69. extern void *mempool_alloc (struct mempool *pool);
  70. extern void mempool_free (struct mempool *pool, void *object);
  71. #endif /* mempool_h */