mempool.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (c) 2007-2013, 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 MP_POOL_ITEM_OVERHEAD (sizeof(void*))
  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