ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/mempool.c
Revision: 8351
Committed: Sun Mar 4 13:27:35 2018 UTC (8 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 21824 byte(s)
Log Message:
- mempool.c:mp_pool_init(): fixed compile warning with --disable-mempool

File Contents

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

Properties

Name Value
svn:eol-style native
svn:keywords Id