ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/mempool.c
Revision: 4086
Committed: Sat Jun 28 16:44:20 2014 UTC (11 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 21548 byte(s)
Log Message:
- Let mp_pool_get() clear memory

File Contents

# User Rev Content
1 michael 1656 /*
2     * Copyright (c) 2007-2012, The Tor Project, Inc.
3 michael 2916 * Copyright (c) 2012-2014 ircd-hybrid development team
4 michael 1656 *
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 michael 1662 * \version $Id$
37 michael 1656 */
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 michael 3279
57 michael 1656 if (u64 >= (1LLU << 16))
58     {
59     u64 >>= 16;
60     r += 16;
61     }
62 michael 3279
63 michael 1656 if (u64 >= (1LLU << 8))
64     {
65     u64 >>= 8;
66     r += 8;
67     }
68 michael 3279
69 michael 1656 if (u64 >= (1LLU << 4))
70     {
71     u64 >>= 4;
72     r += 4;
73     }
74 michael 3279
75 michael 1656 if (u64 >= (1LLU << 2))
76     {
77     u64 >>= 2;
78     r += 2;
79     }
80 michael 3279
81 michael 1656 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 michael 3279 struct mp_allocated_t
180     {
181 michael 1656 /** 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 michael 3279 union
187     {
188 michael 1656 /** 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 michael 3279 struct mp_chunk_t
205     {
206 michael 1656 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 michael 3504 mp_chunk_t *chunk = MyCalloc(CHUNK_OVERHEAD + sz);
248 michael 1656
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 michael 4086 void *ptr = NULL;
280 michael 1656
281 michael 3279 if (pool->used_chunks)
282     {
283 michael 1656 /*
284     * Common case: there is some chunk that is neither full nor empty. Use
285     * that one. (We can't use the full ones, obviously, and we should fill
286     * up the used ones before we start on any empty ones.
287     */
288     chunk = pool->used_chunks;
289    
290 michael 3279 }
291     else if (pool->empty_chunks)
292     {
293 michael 1656 /*
294     * We have no used chunks, but we have an empty chunk that we haven't
295     * freed yet: use that. (We pull from the front of the list, which should
296     * get us the most recently emptied chunk.)
297     */
298     chunk = pool->empty_chunks;
299    
300     /* Remove the chunk from the empty list. */
301     pool->empty_chunks = chunk->next;
302     if (chunk->next)
303     chunk->next->prev = NULL;
304    
305     /* Put the chunk on the 'used' list*/
306     add_newly_used_chunk_to_used_list(pool, chunk);
307    
308     assert(!chunk->prev);
309     --pool->n_empty_chunks;
310     if (pool->n_empty_chunks < pool->min_empty_chunks)
311     pool->min_empty_chunks = pool->n_empty_chunks;
312 michael 3279 }
313     else
314     {
315 michael 1656 /* We have no used or empty chunks: allocate a new chunk. */
316     chunk = mp_chunk_new(pool);
317    
318     /* Add the new chunk to the used list. */
319     add_newly_used_chunk_to_used_list(pool, chunk);
320     }
321    
322     assert(chunk->n_allocated < chunk->capacity);
323    
324 michael 3279 if (chunk->first_free)
325     {
326 michael 1656 /* If there's anything on the chunk's freelist, unlink it and use it. */
327     allocated = chunk->first_free;
328     chunk->first_free = allocated->u.next_free;
329     allocated->u.next_free = NULL; /* For debugging; not really needed. */
330     assert(allocated->in_chunk == chunk);
331 michael 3279 }
332     else
333     {
334 michael 1656 /* Otherwise, the chunk had better have some free space left on it. */
335     assert(chunk->next_mem + pool->item_alloc_size <=
336     chunk->mem + chunk->mem_size);
337    
338     /* Good, it did. Let's carve off a bit of that free space, and use
339     * that. */
340     allocated = (void *)chunk->next_mem;
341     chunk->next_mem += pool->item_alloc_size;
342     allocated->in_chunk = chunk;
343     allocated->u.next_free = NULL; /* For debugging; not really needed. */
344     }
345    
346     ++chunk->n_allocated;
347     #ifdef MEMPOOL_STATS
348     ++pool->total_items_allocated;
349     #endif
350    
351 michael 3279 if (chunk->n_allocated == chunk->capacity)
352     {
353 michael 1656 /* This chunk just became full. */
354     assert(chunk == pool->used_chunks);
355     assert(chunk->prev == NULL);
356    
357     /* Take it off the used list. */
358     pool->used_chunks = chunk->next;
359     if (chunk->next)
360     chunk->next->prev = NULL;
361    
362     /* Put it on the full list. */
363     chunk->next = pool->full_chunks;
364     if (chunk->next)
365     chunk->next->prev = chunk;
366     pool->full_chunks = chunk;
367     }
368 michael 4086
369 michael 1656 /* And return the memory portion of the mp_allocated_t. */
370 michael 4086 ptr = A2M(allocated);
371     memset(ptr, 0, pool->item_size);
372    
373     return ptr;
374 michael 1656 }
375    
376     /** Return an allocated memory item to its memory pool. */
377     void
378     mp_pool_release(void *item)
379     {
380     mp_allocated_t *allocated = (void *)M2A(item);
381     mp_chunk_t *chunk = allocated->in_chunk;
382    
383     assert(chunk);
384     assert(chunk->magic == MP_CHUNK_MAGIC);
385     assert(chunk->n_allocated > 0);
386    
387     allocated->u.next_free = chunk->first_free;
388     chunk->first_free = allocated;
389    
390 michael 3279 if (chunk->n_allocated == chunk->capacity)
391     {
392 michael 1656 /* This chunk was full and is about to be used. */
393     mp_pool_t *pool = chunk->pool;
394     /* unlink from the full list */
395     if (chunk->prev)
396     chunk->prev->next = chunk->next;
397     if (chunk->next)
398     chunk->next->prev = chunk->prev;
399     if (chunk == pool->full_chunks)
400     pool->full_chunks = chunk->next;
401    
402     /* link to the used list. */
403     chunk->next = pool->used_chunks;
404     chunk->prev = NULL;
405 michael 3279
406 michael 1656 if (chunk->next)
407     chunk->next->prev = chunk;
408     pool->used_chunks = chunk;
409 michael 3279 }
410     else if (chunk->n_allocated == 1)
411     {
412 michael 1656 /* This was used and is about to be empty. */
413     mp_pool_t *pool = chunk->pool;
414    
415     /* Unlink from the used list */
416     if (chunk->prev)
417     chunk->prev->next = chunk->next;
418     if (chunk->next)
419     chunk->next->prev = chunk->prev;
420     if (chunk == pool->used_chunks)
421     pool->used_chunks = chunk->next;
422    
423     /* Link to the empty list */
424     chunk->next = pool->empty_chunks;
425     chunk->prev = NULL;
426     if (chunk->next)
427     chunk->next->prev = chunk;
428     pool->empty_chunks = chunk;
429    
430     /* Reset the guts of this chunk to defragment it, in case it gets
431     * used again. */
432     chunk->first_free = NULL;
433     chunk->next_mem = chunk->mem;
434    
435     ++pool->n_empty_chunks;
436     }
437    
438     --chunk->n_allocated;
439     }
440    
441     /** Allocate a new memory pool to hold items of size <b>item_size</b>. We'll
442     * try to fit about <b>chunk_capacity</b> bytes in each chunk. */
443     mp_pool_t *
444     mp_pool_new(size_t item_size, size_t chunk_capacity)
445     {
446     mp_pool_t *pool;
447     size_t alloc_size, new_chunk_cap;
448    
449     /* assert(item_size < SIZE_T_CEILING);
450     assert(chunk_capacity < SIZE_T_CEILING);
451     assert(SIZE_T_CEILING / item_size > chunk_capacity);
452     */
453 michael 3504 pool = MyCalloc(sizeof(mp_pool_t));
454 michael 1656 /*
455     * First, we figure out how much space to allow per item. We'll want to
456     * use make sure we have enough for the overhead plus the item size.
457     */
458     alloc_size = (size_t)(offsetof(mp_allocated_t, u.mem) + item_size);
459     /*
460     * If the item_size is less than sizeof(next_free), we need to make
461     * the allocation bigger.
462     */
463     if (alloc_size < sizeof(mp_allocated_t))
464     alloc_size = sizeof(mp_allocated_t);
465    
466     /* If we're not an even multiple of ALIGNMENT, round up. */
467 michael 3279 if (alloc_size % ALIGNMENT)
468 michael 1656 alloc_size = alloc_size + ALIGNMENT - (alloc_size % ALIGNMENT);
469     if (alloc_size < ALIGNMENT)
470     alloc_size = ALIGNMENT;
471 michael 3279
472 michael 1656 assert((alloc_size % ALIGNMENT) == 0);
473    
474     /*
475     * Now we figure out how many items fit in each chunk. We need to fit at
476     * least 2 items per chunk. No chunk can be more than MAX_CHUNK bytes long,
477     * or less than MIN_CHUNK.
478     */
479     if (chunk_capacity > MAX_CHUNK)
480     chunk_capacity = MAX_CHUNK;
481    
482     /*
483     * Try to be around a power of 2 in size, since that's what allocators like
484     * handing out. 512K-1 byte is a lot better than 512K+1 byte.
485     */
486     chunk_capacity = (size_t) round_to_power_of_2(chunk_capacity);
487 michael 3279
488 michael 1656 while (chunk_capacity < alloc_size * 2 + CHUNK_OVERHEAD)
489     chunk_capacity *= 2;
490     if (chunk_capacity < MIN_CHUNK)
491     chunk_capacity = MIN_CHUNK;
492    
493     new_chunk_cap = (chunk_capacity-CHUNK_OVERHEAD) / alloc_size;
494     assert(new_chunk_cap < INT_MAX);
495     pool->new_chunk_capacity = (int)new_chunk_cap;
496    
497 michael 4086 pool->item_size = item_size;
498 michael 1656 pool->item_alloc_size = alloc_size;
499    
500     pool->next = mp_allocated_pools;
501     mp_allocated_pools = pool;
502    
503 michael 1967 ilog(LOG_TYPE_DEBUG, "Capacity is %lu, item size is %lu, alloc size is %lu",
504 michael 1656 (unsigned long)pool->new_chunk_capacity,
505     (unsigned long)pool->item_alloc_size,
506     (unsigned long)(pool->new_chunk_capacity*pool->item_alloc_size));
507    
508     return pool;
509     }
510    
511     /** Helper function for qsort: used to sort pointers to mp_chunk_t into
512     * descending order of fullness. */
513     static int
514     mp_pool_sort_used_chunks_helper(const void *_a, const void *_b)
515     {
516     mp_chunk_t *a = *(mp_chunk_t * const *)_a;
517     mp_chunk_t *b = *(mp_chunk_t * const *)_b;
518     return b->n_allocated - a->n_allocated;
519     }
520    
521     /** Sort the used chunks in <b>pool</b> into descending order of fullness,
522     * so that we preferentially fill up mostly full chunks before we make
523     * nearly empty chunks less nearly empty. */
524     static void
525     mp_pool_sort_used_chunks(mp_pool_t *pool)
526     {
527     int i, n = 0, inverted = 0;
528     mp_chunk_t **chunks, *chunk;
529    
530 michael 3279 for (chunk = pool->used_chunks; chunk; chunk = chunk->next)
531     {
532 michael 1656 ++n;
533     if (chunk->next && chunk->next->n_allocated > chunk->n_allocated)
534     ++inverted;
535     }
536    
537     if (!inverted)
538     return;
539    
540 michael 3504 chunks = MyCalloc(sizeof(mp_chunk_t *) * n);
541 michael 1656
542 michael 3079 for (i = 0, chunk = pool->used_chunks; chunk; chunk = chunk->next)
543 michael 1656 chunks[i++] = chunk;
544    
545     qsort(chunks, n, sizeof(mp_chunk_t *), mp_pool_sort_used_chunks_helper);
546     pool->used_chunks = chunks[0];
547     chunks[0]->prev = NULL;
548    
549 michael 3279 for (i = 1; i < n; ++i)
550     {
551 michael 1656 chunks[i - 1]->next = chunks[i];
552     chunks[i]->prev = chunks[i - 1];
553     }
554    
555     chunks[n - 1]->next = NULL;
556     MyFree(chunks);
557     mp_pool_assert_ok(pool);
558     }
559    
560     /** If there are more than <b>n</b> empty chunks in <b>pool</b>, free the
561     * excess ones that have been empty for the longest. If
562     * <b>keep_recently_used</b> is true, do not free chunks unless they have been
563     * empty since the last call to this function.
564     **/
565     void
566     mp_pool_clean(mp_pool_t *pool, int n_to_keep, int keep_recently_used)
567     {
568     mp_chunk_t *chunk, **first_to_free;
569    
570     mp_pool_sort_used_chunks(pool);
571     assert(n_to_keep >= 0);
572    
573 michael 3279 if (keep_recently_used)
574     {
575 michael 1656 int n_recently_used = pool->n_empty_chunks - pool->min_empty_chunks;
576 michael 3279
577 michael 1656 if (n_to_keep < n_recently_used)
578     n_to_keep = n_recently_used;
579     }
580    
581     assert(n_to_keep >= 0);
582    
583     first_to_free = &pool->empty_chunks;
584 michael 3279
585     while (*first_to_free && n_to_keep > 0)
586     {
587 michael 1656 first_to_free = &(*first_to_free)->next;
588     --n_to_keep;
589     }
590 michael 3279
591     if (!*first_to_free)
592     {
593 michael 1656 pool->min_empty_chunks = pool->n_empty_chunks;
594     return;
595     }
596    
597     chunk = *first_to_free;
598 michael 3279
599     while (chunk)
600     {
601 michael 1656 mp_chunk_t *next = chunk->next;
602     chunk->magic = 0xdeadbeef;
603     MyFree(chunk);
604     #ifdef MEMPOOL_STATS
605     ++pool->total_chunks_freed;
606     #endif
607     --pool->n_empty_chunks;
608     chunk = next;
609     }
610    
611     pool->min_empty_chunks = pool->n_empty_chunks;
612     *first_to_free = NULL;
613     }
614    
615 michael 3036 #if 0
616 michael 1656 /** Helper: Given a list of chunks, free all the chunks in the list. */
617     static void
618     destroy_chunks(mp_chunk_t *chunk)
619     {
620     mp_chunk_t *next;
621    
622     while (chunk) {
623     chunk->magic = 0xd3adb33f;
624     next = chunk->next;
625     MyFree(chunk);
626     chunk = next;
627     }
628     }
629 michael 3036 #endif
630 michael 1656
631     /** Helper: make sure that a given chunk list is not corrupt. */
632     static int
633     assert_chunks_ok(mp_pool_t *pool, mp_chunk_t *chunk, int empty, int full)
634     {
635     mp_allocated_t *allocated;
636     int n = 0;
637    
638     if (chunk)
639     assert(chunk->prev == NULL);
640    
641 michael 3279 while (chunk)
642     {
643 michael 1656 n++;
644     assert(chunk->magic == MP_CHUNK_MAGIC);
645     assert(chunk->pool == pool);
646 michael 3279
647 michael 1656 for (allocated = chunk->first_free; allocated;
648 michael 3279 allocated = allocated->u.next_free)
649 michael 1656 assert(allocated->in_chunk == chunk);
650 michael 3279
651 michael 1656 if (empty)
652     assert(chunk->n_allocated == 0);
653     else if (full)
654     assert(chunk->n_allocated == chunk->capacity);
655     else
656     assert(chunk->n_allocated > 0 && chunk->n_allocated < chunk->capacity);
657    
658     assert(chunk->capacity == pool->new_chunk_capacity);
659    
660     assert(chunk->mem_size ==
661     pool->new_chunk_capacity * pool->item_alloc_size);
662    
663     assert(chunk->next_mem >= chunk->mem &&
664     chunk->next_mem <= chunk->mem + chunk->mem_size);
665    
666     if (chunk->next)
667     assert(chunk->next->prev == chunk);
668    
669     chunk = chunk->next;
670     }
671    
672     return n;
673     }
674    
675     /** Fail with an assertion if <b>pool</b> is not internally consistent. */
676     void
677     mp_pool_assert_ok(mp_pool_t *pool)
678     {
679     int n_empty;
680    
681     n_empty = assert_chunks_ok(pool, pool->empty_chunks, 1, 0);
682     assert_chunks_ok(pool, pool->full_chunks, 0, 1);
683     assert_chunks_ok(pool, pool->used_chunks, 0, 0);
684    
685     assert(pool->n_empty_chunks == n_empty);
686     }
687    
688     void
689     mp_pool_garbage_collect(void *arg)
690     {
691     mp_pool_t *pool = mp_allocated_pools;
692    
693     for (; pool; pool = pool->next)
694     mp_pool_clean(pool, 0, 1);
695     }
696    
697     /** Dump information about <b>pool</b>'s memory usage to the Tor log at level
698     * <b>severity</b>. */
699     void
700     mp_pool_log_status(mp_pool_t *pool)
701     {
702     uint64_t bytes_used = 0;
703     uint64_t bytes_allocated = 0;
704     uint64_t bu = 0, ba = 0;
705     mp_chunk_t *chunk;
706     int n_full = 0, n_used = 0;
707    
708     assert(pool);
709    
710     for (chunk = pool->empty_chunks; chunk; chunk = chunk->next)
711     bytes_allocated += chunk->mem_size;
712    
713     ilog(LOG_TYPE_DEBUG, "%llu bytes in %d empty chunks",
714     bytes_allocated, pool->n_empty_chunks);
715 michael 3279 for (chunk = pool->used_chunks; chunk; chunk = chunk->next)
716     {
717 michael 1656 ++n_used;
718     bu += chunk->n_allocated * pool->item_alloc_size;
719     ba += chunk->mem_size;
720    
721     ilog(LOG_TYPE_DEBUG, " used chunk: %d items allocated",
722     chunk->n_allocated);
723     }
724    
725     ilog(LOG_TYPE_DEBUG, "%llu/%llu bytes in %d partially full chunks",
726     bu, ba, n_used);
727     bytes_used += bu;
728     bytes_allocated += ba;
729     bu = ba = 0;
730    
731 michael 3279 for (chunk = pool->full_chunks; chunk; chunk = chunk->next)
732     {
733 michael 1656 ++n_full;
734     bu += chunk->n_allocated * pool->item_alloc_size;
735     ba += chunk->mem_size;
736     }
737    
738     ilog(LOG_TYPE_DEBUG, "%llu/%llu bytes in %d full chunks",
739     bu, ba, n_full);
740     bytes_used += bu;
741     bytes_allocated += ba;
742    
743     ilog(LOG_TYPE_DEBUG, "Total: %llu/%llu bytes allocated "
744     "for cell pools are full.",
745     bytes_used, bytes_allocated);
746    
747     #ifdef MEMPOOL_STATS
748     ilog(LOG_TYPE_DEBUG, "%llu cell allocations ever; "
749     "%llu chunk allocations ever; "
750     "%llu chunk frees ever.",
751     pool->total_items_allocated,
752     pool->total_chunks_allocated,
753     pool->total_chunks_freed);
754     #endif
755     }

Properties

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