mempool.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* Copyright (c) 2007-2008, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file mempool.h
  6. * \brief Headers for mempool.c
  7. **/
  8. #ifndef _TOR_MEMPOOL_H
  9. #define _TOR_MEMPOOL_H
  10. /** A memory pool is a context in which a large number of fixed-sized
  11. * objects can be allocated efficiently. See mempool.c for implementation
  12. * details. */
  13. typedef struct mp_pool_t mp_pool_t;
  14. void *mp_pool_get(mp_pool_t *pool);
  15. void mp_pool_release(void *item);
  16. mp_pool_t *mp_pool_new(size_t item_size, size_t chunk_capacity);
  17. void mp_pool_clean(mp_pool_t *pool, int n_to_keep, int keep_recently_used);
  18. void mp_pool_destroy(mp_pool_t *pool);
  19. void mp_pool_assert_ok(mp_pool_t *pool);
  20. void mp_pool_log_status(mp_pool_t *pool, int severity);
  21. #define MEMPOOL_STATS
  22. #ifdef MEMPOOL_PRIVATE
  23. /* These declarations are only used by mempool.c and test.c */
  24. struct mp_pool_t {
  25. /** Doubly-linked list of chunks in which no items have been allocated.
  26. * The front of the list is the most recently emptied chunk. */
  27. struct mp_chunk_t *empty_chunks;
  28. /** Doubly-linked list of chunks in which some items have been allocated,
  29. * but which are not yet full. The front of the list is the chunk that has
  30. * most recently been modified. */
  31. struct mp_chunk_t *used_chunks;
  32. /** Doubly-linked list of chunks in which no more items can be allocated.
  33. * The front of the list is the chunk that has most recently become full. */
  34. struct mp_chunk_t *full_chunks;
  35. /** Length of <b>empty_chunks</b>. */
  36. int n_empty_chunks;
  37. /** Lowest value of <b>empty_chunks</b> since last call to
  38. * mp_pool_clean(-1). */
  39. int min_empty_chunks;
  40. /** Size of each chunk (in items). */
  41. int new_chunk_capacity;
  42. /** Size to allocate for each item, including overhead and alignment
  43. * padding. */
  44. size_t item_alloc_size;
  45. #ifdef MEMPOOL_STATS
  46. /** Total number of items allocated ever. */
  47. uint64_t total_items_allocated;
  48. /** Total number of chunks allocated ever. */
  49. uint64_t total_chunks_allocated;
  50. /** Total number of chunks freed ever. */
  51. uint64_t total_chunks_freed;
  52. #endif
  53. };
  54. #endif
  55. #endif