1 |
/* |
2 |
* Copyright (c) 2007-2012, The Tor Project, Inc. |
3 |
* Copyright (c) 2012-2014 ircd-hybrid development team |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions are |
7 |
* met: |
8 |
* |
9 |
* * Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
12 |
* * Redistributions in binary form must reproduce the above |
13 |
* copyright notice, this list of conditions and the following disclaimer |
14 |
* in the documentation and/or other materials provided with the |
15 |
* distribution. |
16 |
* |
17 |
* * Neither the names of the copyright owners nor the names of its |
18 |
* contributors may be used to endorse or promote products derived from |
19 |
* this software without specific prior written permission. |
20 |
* |
21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 |
*/ |
33 |
|
34 |
/*! \file mempool.c |
35 |
* \brief A pooling allocator |
36 |
* \version $Id$ |
37 |
*/ |
38 |
|
39 |
#include "stdinc.h" |
40 |
#include "memory.h" |
41 |
#include "event.h" |
42 |
#include "log.h" |
43 |
#include "mempool.h" |
44 |
|
45 |
/** Returns floor(log2(u64)). If u64 is 0, (incorrectly) returns 0. */ |
46 |
static int |
47 |
tor_log2(uint64_t u64) |
48 |
{ |
49 |
int r = 0; |
50 |
|
51 |
if (u64 >= (1LLU << 32)) |
52 |
{ |
53 |
u64 >>= 32; |
54 |
r = 32; |
55 |
} |
56 |
|
57 |
if (u64 >= (1LLU << 16)) |
58 |
{ |
59 |
u64 >>= 16; |
60 |
r += 16; |
61 |
} |
62 |
|
63 |
if (u64 >= (1LLU << 8)) |
64 |
{ |
65 |
u64 >>= 8; |
66 |
r += 8; |
67 |
} |
68 |
|
69 |
if (u64 >= (1LLU << 4)) |
70 |
{ |
71 |
u64 >>= 4; |
72 |
r += 4; |
73 |
} |
74 |
|
75 |
if (u64 >= (1LLU << 2)) |
76 |
{ |
77 |
u64 >>= 2; |
78 |
r += 2; |
79 |
} |
80 |
|
81 |
if (u64 >= (1LLU << 1)) |
82 |
{ |
83 |
u64 >>= 1; |
84 |
r += 1; |
85 |
} |
86 |
|
87 |
return r; |
88 |
} |
89 |
|
90 |
/** Return the power of 2 in range [1,UINT64_MAX] closest to <b>u64</b>. If |
91 |
* there are two powers of 2 equally close, round down. */ |
92 |
static uint64_t |
93 |
round_to_power_of_2(uint64_t u64) |
94 |
{ |
95 |
int lg2; |
96 |
uint64_t low; |
97 |
uint64_t high; |
98 |
|
99 |
if (u64 == 0) |
100 |
return 1; |
101 |
|
102 |
lg2 = tor_log2(u64); |
103 |
low = 1LLU << lg2; |
104 |
|
105 |
if (lg2 == 63) |
106 |
return low; |
107 |
|
108 |
high = 1LLU << (lg2 + 1); |
109 |
if (high - u64 < u64 - low) |
110 |
return high; |
111 |
else |
112 |
return low; |
113 |
} |
114 |
|
115 |
/* OVERVIEW: |
116 |
* |
117 |
* This is an implementation of memory pools for Tor cells. It may be |
118 |
* useful for you too. |
119 |
* |
120 |
* Generally, a memory pool is an allocation strategy optimized for large |
121 |
* numbers of identically-sized objects. Rather than the elaborate arena |
122 |
* and coalescing strategies you need to get good performance for a |
123 |
* general-purpose malloc(), pools use a series of large memory "chunks", |
124 |
* each of which is carved into a bunch of smaller "items" or |
125 |
* "allocations". |
126 |
* |
127 |
* To get decent performance, you need to: |
128 |
* - Minimize the number of times you hit the underlying allocator. |
129 |
* - Try to keep accesses as local in memory as possible. |
130 |
* - Try to keep the common case fast. |
131 |
* |
132 |
* Our implementation uses three lists of chunks per pool. Each chunk can |
133 |
* be either "full" (no more room for items); "empty" (no items); or |
134 |
* "used" (not full, not empty). There are independent doubly-linked |
135 |
* lists for each state. |
136 |
* |
137 |
* CREDIT: |
138 |
* |
139 |
* I wrote this after looking at 3 or 4 other pooling allocators, but |
140 |
* without copying. The strategy this most resembles (which is funny, |
141 |
* since that's the one I looked at longest ago) is the pool allocator |
142 |
* underlying Python's obmalloc code. Major differences from obmalloc's |
143 |
* pools are: |
144 |
* - We don't even try to be threadsafe. |
145 |
* - We only handle objects of one size. |
146 |
* - Our list of empty chunks is doubly-linked, not singly-linked. |
147 |
* (This could change pretty easily; it's only doubly-linked for |
148 |
* consistency.) |
149 |
* - We keep a list of full chunks (so we can have a "nuke everything" |
150 |
* function). Obmalloc's pools leave full chunks to float unanchored. |
151 |
* |
152 |
* LIMITATIONS: |
153 |
* - Not even slightly threadsafe. |
154 |
* - Likes to have lots of items per chunks. |
155 |
* - One pointer overhead per allocated thing. (The alternative is |
156 |
* something like glib's use of an RB-tree to keep track of what |
157 |
* chunk any given piece of memory is in.) |
158 |
* - Only aligns allocated things to void* level: redefine ALIGNMENT_TYPE |
159 |
* if you need doubles. |
160 |
* - Could probably be optimized a bit; the representation contains |
161 |
* a bit more info than it really needs to have. |
162 |
*/ |
163 |
|
164 |
/* Tuning parameters */ |
165 |
/** Largest type that we need to ensure returned memory items are aligned to. |
166 |
* Change this to "double" if we need to be safe for structs with doubles. */ |
167 |
#define ALIGNMENT_TYPE void * |
168 |
/** Increment that we need to align allocated. */ |
169 |
#define ALIGNMENT sizeof(ALIGNMENT_TYPE) |
170 |
/** Largest memory chunk that we should allocate. */ |
171 |
#define MAX_CHUNK (8 *(1L << 20)) |
172 |
/** Smallest memory chunk size that we should allocate. */ |
173 |
#define MIN_CHUNK 4096 |
174 |
|
175 |
typedef struct mp_allocated_t mp_allocated_t; |
176 |
typedef struct mp_chunk_t mp_chunk_t; |
177 |
|
178 |
/** Holds a single allocated item, allocated as part of a chunk. */ |
179 |
struct mp_allocated_t |
180 |
{ |
181 |
/** The chunk that this item is allocated in. This adds overhead to each |
182 |
* allocated item, thus making this implementation inappropriate for |
183 |
* very small items. */ |
184 |
mp_chunk_t *in_chunk; |
185 |
|
186 |
union |
187 |
{ |
188 |
/** If this item is free, the next item on the free list. */ |
189 |
mp_allocated_t *next_free; |
190 |
|
191 |
/** If this item is not free, the actual memory contents of this item. |
192 |
* (Not actual size.) */ |
193 |
char mem[1]; |
194 |
|
195 |
/** An extra element to the union to insure correct alignment. */ |
196 |
ALIGNMENT_TYPE dummy_; |
197 |
} u; |
198 |
}; |
199 |
|
200 |
/** 'Magic' value used to detect memory corruption. */ |
201 |
#define MP_CHUNK_MAGIC 0x09870123 |
202 |
|
203 |
/** A chunk of memory. Chunks come from malloc; we use them */ |
204 |
struct mp_chunk_t |
205 |
{ |
206 |
uint32_t magic; /**< Must be MP_CHUNK_MAGIC if this chunk is valid. */ |
207 |
mp_chunk_t *next; /**< The next free, used, or full chunk in sequence. */ |
208 |
mp_chunk_t *prev; /**< The previous free, used, or full chunk in sequence. */ |
209 |
mp_pool_t *pool; /**< The pool that this chunk is part of. */ |
210 |
|
211 |
/** First free item in the freelist for this chunk. Note that this may be |
212 |
* NULL even if this chunk is not at capacity: if so, the free memory at |
213 |
* next_mem has not yet been carved into items. |
214 |
*/ |
215 |
mp_allocated_t *first_free; |
216 |
int n_allocated; /**< Number of currently allocated items in this chunk. */ |
217 |
int capacity; /**< Number of items that can be fit into this chunk. */ |
218 |
size_t mem_size; /**< Number of usable bytes in mem. */ |
219 |
char *next_mem; /**< Pointer into part of <b>mem</b> not yet carved up. */ |
220 |
char mem[]; /**< Storage for this chunk. */ |
221 |
}; |
222 |
|
223 |
static mp_pool_t *mp_allocated_pools = NULL; |
224 |
|
225 |
/** Number of extra bytes needed beyond mem_size to allocate a chunk. */ |
226 |
#define CHUNK_OVERHEAD offsetof(mp_chunk_t, mem[0]) |
227 |
|
228 |
/** Given a pointer to a mp_allocated_t, return a pointer to the memory |
229 |
* item it holds. */ |
230 |
#define A2M(a) (&(a)->u.mem) |
231 |
/** Given a pointer to a memory_item_t, return a pointer to its enclosing |
232 |
* mp_allocated_t. */ |
233 |
#define M2A(p) (((char *)p) - offsetof(mp_allocated_t, u.mem)) |
234 |
|
235 |
void |
236 |
mp_pool_init(void) |
237 |
{ |
238 |
eventAdd("mp_pool_garbage_collect", &mp_pool_garbage_collect, NULL, 119); |
239 |
} |
240 |
|
241 |
/** Helper: Allocate and return a new memory chunk for <b>pool</b>. Does not |
242 |
* link the chunk into any list. */ |
243 |
static mp_chunk_t * |
244 |
mp_chunk_new(mp_pool_t *pool) |
245 |
{ |
246 |
size_t sz = pool->new_chunk_capacity * pool->item_alloc_size; |
247 |
mp_chunk_t *chunk = MyMalloc(CHUNK_OVERHEAD + sz); |
248 |
|
249 |
#ifdef MEMPOOL_STATS |
250 |
++pool->total_chunks_allocated; |
251 |
#endif |
252 |
chunk->magic = MP_CHUNK_MAGIC; |
253 |
chunk->capacity = pool->new_chunk_capacity; |
254 |
chunk->mem_size = sz; |
255 |
chunk->next_mem = chunk->mem; |
256 |
chunk->pool = pool; |
257 |
return chunk; |
258 |
} |
259 |
|
260 |
/** Take a <b>chunk</b> that has just been allocated or removed from |
261 |
* <b>pool</b>'s empty chunk list, and add it to the head of the used chunk |
262 |
* list. */ |
263 |
static void |
264 |
add_newly_used_chunk_to_used_list(mp_pool_t *pool, mp_chunk_t *chunk) |
265 |
{ |
266 |
chunk->next = pool->used_chunks; |
267 |
if (chunk->next) |
268 |
chunk->next->prev = chunk; |
269 |
pool->used_chunks = chunk; |
270 |
assert(!chunk->prev); |
271 |
} |
272 |
|
273 |
/** Return a newly allocated item from <b>pool</b>. */ |
274 |
void * |
275 |
mp_pool_get(mp_pool_t *pool) |
276 |
{ |
277 |
mp_chunk_t *chunk; |
278 |
mp_allocated_t *allocated; |
279 |
|
280 |
if (pool->used_chunks) |
281 |
{ |
282 |
/* |
283 |
* Common case: there is some chunk that is neither full nor empty. Use |
284 |
* that one. (We can't use the full ones, obviously, and we should fill |
285 |
* up the used ones before we start on any empty ones. |
286 |
*/ |
287 |
chunk = pool->used_chunks; |
288 |
|
289 |
} |
290 |
else if (pool->empty_chunks) |
291 |
{ |
292 |
/* |
293 |
* We have no used chunks, but we have an empty chunk that we haven't |
294 |
* freed yet: use that. (We pull from the front of the list, which should |
295 |
* get us the most recently emptied chunk.) |
296 |
*/ |
297 |
chunk = pool->empty_chunks; |
298 |
|
299 |
/* Remove the chunk from the empty list. */ |
300 |
pool->empty_chunks = chunk->next; |
301 |
if (chunk->next) |
302 |
chunk->next->prev = NULL; |
303 |
|
304 |
/* Put the chunk on the 'used' list*/ |
305 |
add_newly_used_chunk_to_used_list(pool, chunk); |
306 |
|
307 |
assert(!chunk->prev); |
308 |
--pool->n_empty_chunks; |
309 |
if (pool->n_empty_chunks < pool->min_empty_chunks) |
310 |
pool->min_empty_chunks = pool->n_empty_chunks; |
311 |
} |
312 |
else |
313 |
{ |
314 |
/* We have no used or empty chunks: allocate a new chunk. */ |
315 |
chunk = mp_chunk_new(pool); |
316 |
|
317 |
/* Add the new chunk to the used list. */ |
318 |
add_newly_used_chunk_to_used_list(pool, chunk); |
319 |
} |
320 |
|
321 |
assert(chunk->n_allocated < chunk->capacity); |
322 |
|
323 |
if (chunk->first_free) |
324 |
{ |
325 |
/* If there's anything on the chunk's freelist, unlink it and use it. */ |
326 |
allocated = chunk->first_free; |
327 |
chunk->first_free = allocated->u.next_free; |
328 |
allocated->u.next_free = NULL; /* For debugging; not really needed. */ |
329 |
assert(allocated->in_chunk == chunk); |
330 |
} |
331 |
else |
332 |
{ |
333 |
/* Otherwise, the chunk had better have some free space left on it. */ |
334 |
assert(chunk->next_mem + pool->item_alloc_size <= |
335 |
chunk->mem + chunk->mem_size); |
336 |
|
337 |
/* Good, it did. Let's carve off a bit of that free space, and use |
338 |
* that. */ |
339 |
allocated = (void *)chunk->next_mem; |
340 |
chunk->next_mem += pool->item_alloc_size; |
341 |
allocated->in_chunk = chunk; |
342 |
allocated->u.next_free = NULL; /* For debugging; not really needed. */ |
343 |
} |
344 |
|
345 |
++chunk->n_allocated; |
346 |
#ifdef MEMPOOL_STATS |
347 |
++pool->total_items_allocated; |
348 |
#endif |
349 |
|
350 |
if (chunk->n_allocated == chunk->capacity) |
351 |
{ |
352 |
/* This chunk just became full. */ |
353 |
assert(chunk == pool->used_chunks); |
354 |
assert(chunk->prev == NULL); |
355 |
|
356 |
/* Take it off the used list. */ |
357 |
pool->used_chunks = chunk->next; |
358 |
if (chunk->next) |
359 |
chunk->next->prev = NULL; |
360 |
|
361 |
/* Put it on the full list. */ |
362 |
chunk->next = pool->full_chunks; |
363 |
if (chunk->next) |
364 |
chunk->next->prev = chunk; |
365 |
pool->full_chunks = chunk; |
366 |
} |
367 |
/* And return the memory portion of the mp_allocated_t. */ |
368 |
return A2M(allocated); |
369 |
} |
370 |
|
371 |
/** Return an allocated memory item to its memory pool. */ |
372 |
void |
373 |
mp_pool_release(void *item) |
374 |
{ |
375 |
mp_allocated_t *allocated = (void *)M2A(item); |
376 |
mp_chunk_t *chunk = allocated->in_chunk; |
377 |
|
378 |
assert(chunk); |
379 |
assert(chunk->magic == MP_CHUNK_MAGIC); |
380 |
assert(chunk->n_allocated > 0); |
381 |
|
382 |
allocated->u.next_free = chunk->first_free; |
383 |
chunk->first_free = allocated; |
384 |
|
385 |
if (chunk->n_allocated == chunk->capacity) |
386 |
{ |
387 |
/* This chunk was full and is about to be used. */ |
388 |
mp_pool_t *pool = chunk->pool; |
389 |
/* unlink from the full list */ |
390 |
if (chunk->prev) |
391 |
chunk->prev->next = chunk->next; |
392 |
if (chunk->next) |
393 |
chunk->next->prev = chunk->prev; |
394 |
if (chunk == pool->full_chunks) |
395 |
pool->full_chunks = chunk->next; |
396 |
|
397 |
/* link to the used list. */ |
398 |
chunk->next = pool->used_chunks; |
399 |
chunk->prev = NULL; |
400 |
|
401 |
if (chunk->next) |
402 |
chunk->next->prev = chunk; |
403 |
pool->used_chunks = chunk; |
404 |
} |
405 |
else if (chunk->n_allocated == 1) |
406 |
{ |
407 |
/* This was used and is about to be empty. */ |
408 |
mp_pool_t *pool = chunk->pool; |
409 |
|
410 |
/* Unlink from the used list */ |
411 |
if (chunk->prev) |
412 |
chunk->prev->next = chunk->next; |
413 |
if (chunk->next) |
414 |
chunk->next->prev = chunk->prev; |
415 |
if (chunk == pool->used_chunks) |
416 |
pool->used_chunks = chunk->next; |
417 |
|
418 |
/* Link to the empty list */ |
419 |
chunk->next = pool->empty_chunks; |
420 |
chunk->prev = NULL; |
421 |
if (chunk->next) |
422 |
chunk->next->prev = chunk; |
423 |
pool->empty_chunks = chunk; |
424 |
|
425 |
/* Reset the guts of this chunk to defragment it, in case it gets |
426 |
* used again. */ |
427 |
chunk->first_free = NULL; |
428 |
chunk->next_mem = chunk->mem; |
429 |
|
430 |
++pool->n_empty_chunks; |
431 |
} |
432 |
|
433 |
--chunk->n_allocated; |
434 |
} |
435 |
|
436 |
/** Allocate a new memory pool to hold items of size <b>item_size</b>. We'll |
437 |
* try to fit about <b>chunk_capacity</b> bytes in each chunk. */ |
438 |
mp_pool_t * |
439 |
mp_pool_new(size_t item_size, size_t chunk_capacity) |
440 |
{ |
441 |
mp_pool_t *pool; |
442 |
size_t alloc_size, new_chunk_cap; |
443 |
|
444 |
/* assert(item_size < SIZE_T_CEILING); |
445 |
assert(chunk_capacity < SIZE_T_CEILING); |
446 |
assert(SIZE_T_CEILING / item_size > chunk_capacity); |
447 |
*/ |
448 |
pool = MyMalloc(sizeof(mp_pool_t)); |
449 |
/* |
450 |
* First, we figure out how much space to allow per item. We'll want to |
451 |
* use make sure we have enough for the overhead plus the item size. |
452 |
*/ |
453 |
alloc_size = (size_t)(offsetof(mp_allocated_t, u.mem) + item_size); |
454 |
/* |
455 |
* If the item_size is less than sizeof(next_free), we need to make |
456 |
* the allocation bigger. |
457 |
*/ |
458 |
if (alloc_size < sizeof(mp_allocated_t)) |
459 |
alloc_size = sizeof(mp_allocated_t); |
460 |
|
461 |
/* If we're not an even multiple of ALIGNMENT, round up. */ |
462 |
if (alloc_size % ALIGNMENT) |
463 |
alloc_size = alloc_size + ALIGNMENT - (alloc_size % ALIGNMENT); |
464 |
if (alloc_size < ALIGNMENT) |
465 |
alloc_size = ALIGNMENT; |
466 |
|
467 |
assert((alloc_size % ALIGNMENT) == 0); |
468 |
|
469 |
/* |
470 |
* Now we figure out how many items fit in each chunk. We need to fit at |
471 |
* least 2 items per chunk. No chunk can be more than MAX_CHUNK bytes long, |
472 |
* or less than MIN_CHUNK. |
473 |
*/ |
474 |
if (chunk_capacity > MAX_CHUNK) |
475 |
chunk_capacity = MAX_CHUNK; |
476 |
|
477 |
/* |
478 |
* Try to be around a power of 2 in size, since that's what allocators like |
479 |
* handing out. 512K-1 byte is a lot better than 512K+1 byte. |
480 |
*/ |
481 |
chunk_capacity = (size_t) round_to_power_of_2(chunk_capacity); |
482 |
|
483 |
while (chunk_capacity < alloc_size * 2 + CHUNK_OVERHEAD) |
484 |
chunk_capacity *= 2; |
485 |
if (chunk_capacity < MIN_CHUNK) |
486 |
chunk_capacity = MIN_CHUNK; |
487 |
|
488 |
new_chunk_cap = (chunk_capacity-CHUNK_OVERHEAD) / alloc_size; |
489 |
assert(new_chunk_cap < INT_MAX); |
490 |
pool->new_chunk_capacity = (int)new_chunk_cap; |
491 |
|
492 |
pool->item_alloc_size = alloc_size; |
493 |
|
494 |
pool->next = mp_allocated_pools; |
495 |
mp_allocated_pools = pool; |
496 |
|
497 |
ilog(LOG_TYPE_DEBUG, "Capacity is %lu, item size is %lu, alloc size is %lu", |
498 |
(unsigned long)pool->new_chunk_capacity, |
499 |
(unsigned long)pool->item_alloc_size, |
500 |
(unsigned long)(pool->new_chunk_capacity*pool->item_alloc_size)); |
501 |
|
502 |
return pool; |
503 |
} |
504 |
|
505 |
/** Helper function for qsort: used to sort pointers to mp_chunk_t into |
506 |
* descending order of fullness. */ |
507 |
static int |
508 |
mp_pool_sort_used_chunks_helper(const void *_a, const void *_b) |
509 |
{ |
510 |
mp_chunk_t *a = *(mp_chunk_t * const *)_a; |
511 |
mp_chunk_t *b = *(mp_chunk_t * const *)_b; |
512 |
return b->n_allocated - a->n_allocated; |
513 |
} |
514 |
|
515 |
/** Sort the used chunks in <b>pool</b> into descending order of fullness, |
516 |
* so that we preferentially fill up mostly full chunks before we make |
517 |
* nearly empty chunks less nearly empty. */ |
518 |
static void |
519 |
mp_pool_sort_used_chunks(mp_pool_t *pool) |
520 |
{ |
521 |
int i, n = 0, inverted = 0; |
522 |
mp_chunk_t **chunks, *chunk; |
523 |
|
524 |
for (chunk = pool->used_chunks; chunk; chunk = chunk->next) |
525 |
{ |
526 |
++n; |
527 |
if (chunk->next && chunk->next->n_allocated > chunk->n_allocated) |
528 |
++inverted; |
529 |
} |
530 |
|
531 |
if (!inverted) |
532 |
return; |
533 |
|
534 |
chunks = MyMalloc(sizeof(mp_chunk_t *) * n); |
535 |
|
536 |
for (i = 0, chunk = pool->used_chunks; chunk; chunk = chunk->next) |
537 |
chunks[i++] = chunk; |
538 |
|
539 |
qsort(chunks, n, sizeof(mp_chunk_t *), mp_pool_sort_used_chunks_helper); |
540 |
pool->used_chunks = chunks[0]; |
541 |
chunks[0]->prev = NULL; |
542 |
|
543 |
for (i = 1; i < n; ++i) |
544 |
{ |
545 |
chunks[i - 1]->next = chunks[i]; |
546 |
chunks[i]->prev = chunks[i - 1]; |
547 |
} |
548 |
|
549 |
chunks[n - 1]->next = NULL; |
550 |
MyFree(chunks); |
551 |
mp_pool_assert_ok(pool); |
552 |
} |
553 |
|
554 |
/** If there are more than <b>n</b> empty chunks in <b>pool</b>, free the |
555 |
* excess ones that have been empty for the longest. If |
556 |
* <b>keep_recently_used</b> is true, do not free chunks unless they have been |
557 |
* empty since the last call to this function. |
558 |
**/ |
559 |
void |
560 |
mp_pool_clean(mp_pool_t *pool, int n_to_keep, int keep_recently_used) |
561 |
{ |
562 |
mp_chunk_t *chunk, **first_to_free; |
563 |
|
564 |
mp_pool_sort_used_chunks(pool); |
565 |
assert(n_to_keep >= 0); |
566 |
|
567 |
if (keep_recently_used) |
568 |
{ |
569 |
int n_recently_used = pool->n_empty_chunks - pool->min_empty_chunks; |
570 |
|
571 |
if (n_to_keep < n_recently_used) |
572 |
n_to_keep = n_recently_used; |
573 |
} |
574 |
|
575 |
assert(n_to_keep >= 0); |
576 |
|
577 |
first_to_free = &pool->empty_chunks; |
578 |
|
579 |
while (*first_to_free && n_to_keep > 0) |
580 |
{ |
581 |
first_to_free = &(*first_to_free)->next; |
582 |
--n_to_keep; |
583 |
} |
584 |
|
585 |
if (!*first_to_free) |
586 |
{ |
587 |
pool->min_empty_chunks = pool->n_empty_chunks; |
588 |
return; |
589 |
} |
590 |
|
591 |
chunk = *first_to_free; |
592 |
|
593 |
while (chunk) |
594 |
{ |
595 |
mp_chunk_t *next = chunk->next; |
596 |
chunk->magic = 0xdeadbeef; |
597 |
MyFree(chunk); |
598 |
#ifdef MEMPOOL_STATS |
599 |
++pool->total_chunks_freed; |
600 |
#endif |
601 |
--pool->n_empty_chunks; |
602 |
chunk = next; |
603 |
} |
604 |
|
605 |
pool->min_empty_chunks = pool->n_empty_chunks; |
606 |
*first_to_free = NULL; |
607 |
} |
608 |
|
609 |
#if 0 |
610 |
/** Helper: Given a list of chunks, free all the chunks in the list. */ |
611 |
static void |
612 |
destroy_chunks(mp_chunk_t *chunk) |
613 |
{ |
614 |
mp_chunk_t *next; |
615 |
|
616 |
while (chunk) { |
617 |
chunk->magic = 0xd3adb33f; |
618 |
next = chunk->next; |
619 |
MyFree(chunk); |
620 |
chunk = next; |
621 |
} |
622 |
} |
623 |
#endif |
624 |
|
625 |
/** Helper: make sure that a given chunk list is not corrupt. */ |
626 |
static int |
627 |
assert_chunks_ok(mp_pool_t *pool, mp_chunk_t *chunk, int empty, int full) |
628 |
{ |
629 |
mp_allocated_t *allocated; |
630 |
int n = 0; |
631 |
|
632 |
if (chunk) |
633 |
assert(chunk->prev == NULL); |
634 |
|
635 |
while (chunk) |
636 |
{ |
637 |
n++; |
638 |
assert(chunk->magic == MP_CHUNK_MAGIC); |
639 |
assert(chunk->pool == pool); |
640 |
|
641 |
for (allocated = chunk->first_free; allocated; |
642 |
allocated = allocated->u.next_free) |
643 |
assert(allocated->in_chunk == chunk); |
644 |
|
645 |
if (empty) |
646 |
assert(chunk->n_allocated == 0); |
647 |
else if (full) |
648 |
assert(chunk->n_allocated == chunk->capacity); |
649 |
else |
650 |
assert(chunk->n_allocated > 0 && chunk->n_allocated < chunk->capacity); |
651 |
|
652 |
assert(chunk->capacity == pool->new_chunk_capacity); |
653 |
|
654 |
assert(chunk->mem_size == |
655 |
pool->new_chunk_capacity * pool->item_alloc_size); |
656 |
|
657 |
assert(chunk->next_mem >= chunk->mem && |
658 |
chunk->next_mem <= chunk->mem + chunk->mem_size); |
659 |
|
660 |
if (chunk->next) |
661 |
assert(chunk->next->prev == chunk); |
662 |
|
663 |
chunk = chunk->next; |
664 |
} |
665 |
|
666 |
return n; |
667 |
} |
668 |
|
669 |
/** Fail with an assertion if <b>pool</b> is not internally consistent. */ |
670 |
void |
671 |
mp_pool_assert_ok(mp_pool_t *pool) |
672 |
{ |
673 |
int n_empty; |
674 |
|
675 |
n_empty = assert_chunks_ok(pool, pool->empty_chunks, 1, 0); |
676 |
assert_chunks_ok(pool, pool->full_chunks, 0, 1); |
677 |
assert_chunks_ok(pool, pool->used_chunks, 0, 0); |
678 |
|
679 |
assert(pool->n_empty_chunks == n_empty); |
680 |
} |
681 |
|
682 |
void |
683 |
mp_pool_garbage_collect(void *arg) |
684 |
{ |
685 |
mp_pool_t *pool = mp_allocated_pools; |
686 |
|
687 |
for (; pool; pool = pool->next) |
688 |
mp_pool_clean(pool, 0, 1); |
689 |
} |
690 |
|
691 |
/** Dump information about <b>pool</b>'s memory usage to the Tor log at level |
692 |
* <b>severity</b>. */ |
693 |
void |
694 |
mp_pool_log_status(mp_pool_t *pool) |
695 |
{ |
696 |
uint64_t bytes_used = 0; |
697 |
uint64_t bytes_allocated = 0; |
698 |
uint64_t bu = 0, ba = 0; |
699 |
mp_chunk_t *chunk; |
700 |
int n_full = 0, n_used = 0; |
701 |
|
702 |
assert(pool); |
703 |
|
704 |
for (chunk = pool->empty_chunks; chunk; chunk = chunk->next) |
705 |
bytes_allocated += chunk->mem_size; |
706 |
|
707 |
ilog(LOG_TYPE_DEBUG, "%llu bytes in %d empty chunks", |
708 |
bytes_allocated, pool->n_empty_chunks); |
709 |
for (chunk = pool->used_chunks; chunk; chunk = chunk->next) |
710 |
{ |
711 |
++n_used; |
712 |
bu += chunk->n_allocated * pool->item_alloc_size; |
713 |
ba += chunk->mem_size; |
714 |
|
715 |
ilog(LOG_TYPE_DEBUG, " used chunk: %d items allocated", |
716 |
chunk->n_allocated); |
717 |
} |
718 |
|
719 |
ilog(LOG_TYPE_DEBUG, "%llu/%llu bytes in %d partially full chunks", |
720 |
bu, ba, n_used); |
721 |
bytes_used += bu; |
722 |
bytes_allocated += ba; |
723 |
bu = ba = 0; |
724 |
|
725 |
for (chunk = pool->full_chunks; chunk; chunk = chunk->next) |
726 |
{ |
727 |
++n_full; |
728 |
bu += chunk->n_allocated * pool->item_alloc_size; |
729 |
ba += chunk->mem_size; |
730 |
} |
731 |
|
732 |
ilog(LOG_TYPE_DEBUG, "%llu/%llu bytes in %d full chunks", |
733 |
bu, ba, n_full); |
734 |
bytes_used += bu; |
735 |
bytes_allocated += ba; |
736 |
|
737 |
ilog(LOG_TYPE_DEBUG, "Total: %llu/%llu bytes allocated " |
738 |
"for cell pools are full.", |
739 |
bytes_used, bytes_allocated); |
740 |
|
741 |
#ifdef MEMPOOL_STATS |
742 |
ilog(LOG_TYPE_DEBUG, "%llu cell allocations ever; " |
743 |
"%llu chunk allocations ever; " |
744 |
"%llu chunk frees ever.", |
745 |
pool->total_items_allocated, |
746 |
pool->total_chunks_allocated, |
747 |
pool->total_chunks_freed); |
748 |
#endif |
749 |
} |