mempool.h 2.1 KB

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