1 |
/* |
2 |
* Copyright (c) 2007-2012, The Tor Project, Inc. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are |
6 |
* met: |
7 |
* |
8 |
* * Redistributions of source code must retain the above copyright |
9 |
* notice, this list of conditions and the following disclaimer. |
10 |
* |
11 |
* * Redistributions in binary form must reproduce the above |
12 |
* copyright notice, this list of conditions and the following disclaimer |
13 |
* in the documentation and/or other materials provided with the |
14 |
* distribution. |
15 |
* |
16 |
* * Neither the names of the copyright owners nor the names of its |
17 |
* contributors may be used to endorse or promote products derived from |
18 |
* this software without specific prior written permission. |
19 |
* |
20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 |
*/ |
32 |
|
33 |
/*! \file mempool.h |
34 |
* \brief Pooling allocator |
35 |
* \version $Id$ |
36 |
*/ |
37 |
|
38 |
#ifndef TOR_MEMPOOL_H |
39 |
#define TOR_MEMPOOL_H |
40 |
|
41 |
/** A memory pool is a context in which a large number of fixed-sized |
42 |
* objects can be allocated efficiently. See mempool.c for implementation |
43 |
* details. */ |
44 |
typedef struct mp_pool_t mp_pool_t; |
45 |
|
46 |
extern void mp_pool_init(void); |
47 |
extern void *mp_pool_get(mp_pool_t *); |
48 |
extern void mp_pool_release(void *); |
49 |
extern mp_pool_t *mp_pool_new(size_t, size_t); |
50 |
extern void mp_pool_clean(mp_pool_t *, int, int); |
51 |
extern void mp_pool_destroy(mp_pool_t *); |
52 |
extern void mp_pool_assert_ok(mp_pool_t *); |
53 |
extern void mp_pool_log_status(mp_pool_t *); |
54 |
extern void mp_pool_garbage_collect(struct Event *); |
55 |
|
56 |
#define MEMPOOL_STATS |
57 |
|
58 |
struct mp_pool_t { |
59 |
/** Next pool. A pool is usually linked into the mp_allocated_pools list. */ |
60 |
mp_pool_t *next; |
61 |
|
62 |
/** Doubly-linked list of chunks in which no items have been allocated. |
63 |
* The front of the list is the most recently emptied chunk. */ |
64 |
struct mp_chunk_t *empty_chunks; |
65 |
|
66 |
/** Doubly-linked list of chunks in which some items have been allocated, |
67 |
* but which are not yet full. The front of the list is the chunk that has |
68 |
* most recently been modified. */ |
69 |
struct mp_chunk_t *used_chunks; |
70 |
|
71 |
/** Doubly-linked list of chunks in which no more items can be allocated. |
72 |
* The front of the list is the chunk that has most recently become full. */ |
73 |
struct mp_chunk_t *full_chunks; |
74 |
|
75 |
/** Length of <b>empty_chunks</b>. */ |
76 |
int n_empty_chunks; |
77 |
|
78 |
/** Lowest value of <b>empty_chunks</b> since last call to |
79 |
* mp_pool_clean(-1). */ |
80 |
int min_empty_chunks; |
81 |
|
82 |
/** Size of each chunk (in items). */ |
83 |
int new_chunk_capacity; |
84 |
|
85 |
/** Size to allocate for each item, including overhead and alignment |
86 |
* padding. */ |
87 |
size_t item_alloc_size; |
88 |
#ifdef MEMPOOL_STATS |
89 |
/** Total number of items allocated ever. */ |
90 |
uint64_t total_items_allocated; |
91 |
|
92 |
/** Total number of chunks allocated ever. */ |
93 |
uint64_t total_chunks_allocated; |
94 |
|
95 |
/** Total number of chunks freed ever. */ |
96 |
uint64_t total_chunks_freed; |
97 |
#endif |
98 |
}; |
99 |
#endif |