aboutsummaryrefslogtreecommitdiff
path: root/src/common/mempool.h
blob: 0fc1e4c6766a31543fd1f8acc3a2124dd058b7ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* Copyright (c) 2007-2013, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file mempool.h
 * \brief Headers for mempool.c
 **/

#ifndef TOR_MEMPOOL_H
#define TOR_MEMPOOL_H

/** A memory pool is a context in which a large number of fixed-sized
* objects can be allocated efficiently.  See mempool.c for implementation
* details. */
typedef struct mp_pool_t mp_pool_t;

void *mp_pool_get(mp_pool_t *pool);
void mp_pool_release(void *item);
mp_pool_t *mp_pool_new(size_t item_size, size_t chunk_capacity);
void mp_pool_clean(mp_pool_t *pool, int n_to_keep, int keep_recently_used);
void mp_pool_destroy(mp_pool_t *pool);
void mp_pool_assert_ok(mp_pool_t *pool);
void mp_pool_log_status(mp_pool_t *pool, int severity);

#define MP_POOL_ITEM_OVERHEAD (sizeof(void*))

#define MEMPOOL_STATS

#ifdef MEMPOOL_PRIVATE
/* These declarations are only used by mempool.c and test.c */

struct mp_pool_t {
  /** Doubly-linked list of chunks in which no items have been allocated.
   * The front of the list is the most recently emptied chunk. */
  struct mp_chunk_t *empty_chunks;
  /** Doubly-linked list of chunks in which some items have been allocated,
   * but which are not yet full. The front of the list is the chunk that has
   * most recently been modified. */
  struct mp_chunk_t *used_chunks;
  /** Doubly-linked list of chunks in which no more items can be allocated.
   * The front of the list is the chunk that has most recently become full. */
  struct mp_chunk_t *full_chunks;
  /** Length of <b>empty_chunks</b>. */
  int n_empty_chunks;
  /** Lowest value of <b>empty_chunks</b> since last call to
   * mp_pool_clean(-1). */
  int min_empty_chunks;
  /** Size of each chunk (in items). */
  int new_chunk_capacity;
  /** Size to allocate for each item, including overhead and alignment
   * padding. */
  size_t item_alloc_size;
#ifdef MEMPOOL_STATS
  /** Total number of items allocated ever. */
  uint64_t total_items_allocated;
  /** Total number of chunks allocated ever. */
  uint64_t total_chunks_allocated;
  /** Total number of chunks freed ever. */
  uint64_t total_chunks_freed;
#endif
};
#endif

#endif