1 /* Copyright (c) 2007-2011, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
11 #define MEMPOOL_PRIVATE
16 * This is an implementation of memory pools for Tor cells. It may be
19 * Generally, a memory pool is an allocation strategy optimized for large
20 * numbers of identically-sized objects. Rather than the elaborate arena
21 * and coalescing strategies you need to get good performance for a
22 * general-purpose malloc(), pools use a series of large memory "chunks",
23 * each of which is carved into a bunch of smaller "items" or
26 * To get decent performance, you need to:
27 * - Minimize the number of times you hit the underlying allocator.
28 * - Try to keep accesses as local in memory as possible.
29 * - Try to keep the common case fast.
31 * Our implementation uses three lists of chunks per pool. Each chunk can
32 * be either "full" (no more room for items); "empty" (no items); or
33 * "used" (not full, not empty). There are independent doubly-linked
34 * lists for each state.
38 * I wrote this after looking at 3 or 4 other pooling allocators, but
39 * without copying. The strategy this most resembles (which is funny,
40 * since that's the one I looked at longest ago) is the pool allocator
41 * underlying Python's obmalloc code. Major differences from obmalloc's
43 * - We don't even try to be threadsafe.
44 * - We only handle objects of one size.
45 * - Our list of empty chunks is doubly-linked, not singly-linked.
46 * (This could change pretty easily; it's only doubly-linked for
48 * - We keep a list of full chunks (so we can have a "nuke everything"
49 * function). Obmalloc's pools leave full chunks to float unanchored.
52 * - Not even slightly threadsafe.
53 * - Likes to have lots of items per chunks.
54 * - One pointer overhead per allocated thing. (The alternative is
55 * something like glib's use of an RB-tree to keep track of what
56 * chunk any given piece of memory is in.)
57 * - Only aligns allocated things to void* level: redefine ALIGNMENT_TYPE
58 * if you need doubles.
59 * - Could probably be optimized a bit; the representation contains
60 * a bit more info than it really needs to have.
64 /* Tor dependencies */
69 #define ALLOC(x) tor_malloc(x)
70 #define FREE(x) tor_free(x)
71 #define ASSERT(x) tor_assert(x)
72 #undef ALLOC_CAN_RETURN_NULL
74 //#define ALLOC_ROUNDUP(p) tor_malloc_roundup(p)
75 /* End Tor dependencies */
77 /* If you're not building this as part of Tor, you'll want to define the
78 * following macros. For now, these should do as defaults.
81 #define PREDICT_UNLIKELY(x) (x)
82 #define PREDICT_LIKELY(x) (x)
83 #define ALLOC(x) malloc(x)
84 #define FREE(x) free(x)
85 #define STRUCT_OFFSET(tp, member) \
86 ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
87 #define ASSERT(x) assert(x)
88 #define ALLOC_CAN_RETURN_NULL
91 /* Tuning parameters */
92 /** Largest type that we need to ensure returned memory items are aligned to.
93 * Change this to "double" if we need to be safe for structs with doubles. */
94 #define ALIGNMENT_TYPE void *
95 /** Increment that we need to align allocated. */
96 #define ALIGNMENT sizeof(ALIGNMENT_TYPE)
97 /** Largest memory chunk that we should allocate. */
98 #define MAX_CHUNK (8*(1L<<20))
99 /** Smallest memory chunk size that we should allocate. */
100 #define MIN_CHUNK 4096
102 typedef struct mp_allocated_t mp_allocated_t
;
103 typedef struct mp_chunk_t mp_chunk_t
;
105 /** Holds a single allocated item, allocated as part of a chunk. */
106 struct mp_allocated_t
{
107 /** The chunk that this item is allocated in. This adds overhead to each
108 * allocated item, thus making this implementation inappropriate for
109 * very small items. */
110 mp_chunk_t
*in_chunk
;
112 /** If this item is free, the next item on the free list. */
113 mp_allocated_t
*next_free
;
114 /** If this item is not free, the actual memory contents of this item.
115 * (Not actual size.) */
117 /** An extra element to the union to insure correct alignment. */
118 ALIGNMENT_TYPE _dummy
;
122 /** 'Magic' value used to detect memory corruption. */
123 #define MP_CHUNK_MAGIC 0x09870123
125 /** A chunk of memory. Chunks come from malloc; we use them */
127 unsigned long magic
; /**< Must be MP_CHUNK_MAGIC if this chunk is valid. */
128 mp_chunk_t
*next
; /**< The next free, used, or full chunk in sequence. */
129 mp_chunk_t
*prev
; /**< The previous free, used, or full chunk in sequence. */
130 mp_pool_t
*pool
; /**< The pool that this chunk is part of. */
131 /** First free item in the freelist for this chunk. Note that this may be
132 * NULL even if this chunk is not at capacity: if so, the free memory at
133 * next_mem has not yet been carved into items.
135 mp_allocated_t
*first_free
;
136 int n_allocated
; /**< Number of currently allocated items in this chunk. */
137 int capacity
; /**< Number of items that can be fit into this chunk. */
138 size_t mem_size
; /**< Number of usable bytes in mem. */
139 char *next_mem
; /**< Pointer into part of <b>mem</b> not yet carved up. */
140 char mem
[1]; /**< Storage for this chunk. (Not actual size.) */
143 /** Number of extra bytes needed beyond mem_size to allocate a chunk. */
144 #define CHUNK_OVERHEAD STRUCT_OFFSET(mp_chunk_t, mem[0])
146 /** Given a pointer to a mp_allocated_t, return a pointer to the memory
148 #define A2M(a) (&(a)->u.mem)
149 /** Given a pointer to a memory_item_t, return a pointer to its enclosing
151 #define M2A(p) ( ((char*)p) - STRUCT_OFFSET(mp_allocated_t, u.mem) )
153 #ifdef ALLOC_CAN_RETURN_NULL
154 /** If our ALLOC() macro can return NULL, check whether <b>x</b> is NULL,
155 * and if so, return NULL. */
156 #define CHECK_ALLOC(x) \
157 if (PREDICT_UNLIKELY(!x)) { return NULL; }
159 /** If our ALLOC() macro can't return NULL, do nothing. */
160 #define CHECK_ALLOC(x)
163 /** Helper: Allocate and return a new memory chunk for <b>pool</b>. Does not
164 * link the chunk into any list. */
166 mp_chunk_new(mp_pool_t
*pool
)
168 size_t sz
= pool
->new_chunk_capacity
* pool
->item_alloc_size
;
170 size_t alloc_size
= CHUNK_OVERHEAD
+ sz
;
171 mp_chunk_t
*chunk
= ALLOC_ROUNDUP(&alloc_size
);
173 mp_chunk_t
*chunk
= ALLOC(CHUNK_OVERHEAD
+ sz
);
176 ++pool
->total_chunks_allocated
;
179 memset(chunk
, 0, sizeof(mp_chunk_t
)); /* Doesn't clear the whole thing. */
180 chunk
->magic
= MP_CHUNK_MAGIC
;
182 chunk
->mem_size
= alloc_size
- CHUNK_OVERHEAD
;
183 chunk
->capacity
= chunk
->mem_size
/ pool
->item_alloc_size
;
185 chunk
->capacity
= pool
->new_chunk_capacity
;
186 chunk
->mem_size
= sz
;
188 chunk
->next_mem
= chunk
->mem
;
193 /** Take a <b>chunk</b> that has just been allocated or removed from
194 * <b>pool</b>'s empty chunk list, and add it to the head of the used chunk
197 add_newly_used_chunk_to_used_list(mp_pool_t
*pool
, mp_chunk_t
*chunk
)
199 chunk
->next
= pool
->used_chunks
;
201 chunk
->next
->prev
= chunk
;
202 pool
->used_chunks
= chunk
;
203 ASSERT(!chunk
->prev
);
206 /** Return a newly allocated item from <b>pool</b>. */
208 mp_pool_get(mp_pool_t
*pool
)
211 mp_allocated_t
*allocated
;
213 if (PREDICT_LIKELY(pool
->used_chunks
!= NULL
)) {
214 /* Common case: there is some chunk that is neither full nor empty. Use
215 * that one. (We can't use the full ones, obviously, and we should fill
216 * up the used ones before we start on any empty ones. */
217 chunk
= pool
->used_chunks
;
219 } else if (pool
->empty_chunks
) {
220 /* We have no used chunks, but we have an empty chunk that we haven't
221 * freed yet: use that. (We pull from the front of the list, which should
222 * get us the most recently emptied chunk.) */
223 chunk
= pool
->empty_chunks
;
225 /* Remove the chunk from the empty list. */
226 pool
->empty_chunks
= chunk
->next
;
228 chunk
->next
->prev
= NULL
;
230 /* Put the chunk on the 'used' list*/
231 add_newly_used_chunk_to_used_list(pool
, chunk
);
233 ASSERT(!chunk
->prev
);
234 --pool
->n_empty_chunks
;
235 if (pool
->n_empty_chunks
< pool
->min_empty_chunks
)
236 pool
->min_empty_chunks
= pool
->n_empty_chunks
;
238 /* We have no used or empty chunks: allocate a new chunk. */
239 chunk
= mp_chunk_new(pool
);
242 /* Add the new chunk to the used list. */
243 add_newly_used_chunk_to_used_list(pool
, chunk
);
246 ASSERT(chunk
->n_allocated
< chunk
->capacity
);
248 if (chunk
->first_free
) {
249 /* If there's anything on the chunk's freelist, unlink it and use it. */
250 allocated
= chunk
->first_free
;
251 chunk
->first_free
= allocated
->u
.next_free
;
252 allocated
->u
.next_free
= NULL
; /* For debugging; not really needed. */
253 ASSERT(allocated
->in_chunk
== chunk
);
255 /* Otherwise, the chunk had better have some free space left on it. */
256 ASSERT(chunk
->next_mem
+ pool
->item_alloc_size
<=
257 chunk
->mem
+ chunk
->mem_size
);
259 /* Good, it did. Let's carve off a bit of that free space, and use
261 allocated
= (void*)chunk
->next_mem
;
262 chunk
->next_mem
+= pool
->item_alloc_size
;
263 allocated
->in_chunk
= chunk
;
264 allocated
->u
.next_free
= NULL
; /* For debugging; not really needed. */
267 ++chunk
->n_allocated
;
269 ++pool
->total_items_allocated
;
272 if (PREDICT_UNLIKELY(chunk
->n_allocated
== chunk
->capacity
)) {
273 /* This chunk just became full. */
274 ASSERT(chunk
== pool
->used_chunks
);
275 ASSERT(chunk
->prev
== NULL
);
277 /* Take it off the used list. */
278 pool
->used_chunks
= chunk
->next
;
280 chunk
->next
->prev
= NULL
;
282 /* Put it on the full list. */
283 chunk
->next
= pool
->full_chunks
;
285 chunk
->next
->prev
= chunk
;
286 pool
->full_chunks
= chunk
;
288 /* And return the memory portion of the mp_allocated_t. */
289 return A2M(allocated
);
292 /** Return an allocated memory item to its memory pool. */
294 mp_pool_release(void *item
)
296 mp_allocated_t
*allocated
= (void*) M2A(item
);
297 mp_chunk_t
*chunk
= allocated
->in_chunk
;
300 ASSERT(chunk
->magic
== MP_CHUNK_MAGIC
);
301 ASSERT(chunk
->n_allocated
> 0);
303 allocated
->u
.next_free
= chunk
->first_free
;
304 chunk
->first_free
= allocated
;
306 if (PREDICT_UNLIKELY(chunk
->n_allocated
== chunk
->capacity
)) {
307 /* This chunk was full and is about to be used. */
308 mp_pool_t
*pool
= chunk
->pool
;
309 /* unlink from the full list */
311 chunk
->prev
->next
= chunk
->next
;
313 chunk
->next
->prev
= chunk
->prev
;
314 if (chunk
== pool
->full_chunks
)
315 pool
->full_chunks
= chunk
->next
;
317 /* link to the used list. */
318 chunk
->next
= pool
->used_chunks
;
321 chunk
->next
->prev
= chunk
;
322 pool
->used_chunks
= chunk
;
323 } else if (PREDICT_UNLIKELY(chunk
->n_allocated
== 1)) {
324 /* This was used and is about to be empty. */
325 mp_pool_t
*pool
= chunk
->pool
;
327 /* Unlink from the used list */
329 chunk
->prev
->next
= chunk
->next
;
331 chunk
->next
->prev
= chunk
->prev
;
332 if (chunk
== pool
->used_chunks
)
333 pool
->used_chunks
= chunk
->next
;
335 /* Link to the empty list */
336 chunk
->next
= pool
->empty_chunks
;
339 chunk
->next
->prev
= chunk
;
340 pool
->empty_chunks
= chunk
;
342 /* Reset the guts of this chunk to defragment it, in case it gets
344 chunk
->first_free
= NULL
;
345 chunk
->next_mem
= chunk
->mem
;
347 ++pool
->n_empty_chunks
;
349 --chunk
->n_allocated
;
352 /** Allocate a new memory pool to hold items of size <b>item_size</b>. We'll
353 * try to fit about <b>chunk_capacity</b> bytes in each chunk. */
355 mp_pool_new(size_t item_size
, size_t chunk_capacity
)
358 size_t alloc_size
, new_chunk_cap
;
360 tor_assert(item_size
< SIZE_T_CEILING
);
361 tor_assert(chunk_capacity
< SIZE_T_CEILING
);
362 tor_assert(SIZE_T_CEILING
/ item_size
> chunk_capacity
);
364 pool
= ALLOC(sizeof(mp_pool_t
));
366 memset(pool
, 0, sizeof(mp_pool_t
));
368 /* First, we figure out how much space to allow per item. We'll want to
369 * use make sure we have enough for the overhead plus the item size. */
370 alloc_size
= (size_t)(STRUCT_OFFSET(mp_allocated_t
, u
.mem
) + item_size
);
371 /* If the item_size is less than sizeof(next_free), we need to make
372 * the allocation bigger. */
373 if (alloc_size
< sizeof(mp_allocated_t
))
374 alloc_size
= sizeof(mp_allocated_t
);
376 /* If we're not an even multiple of ALIGNMENT, round up. */
377 if (alloc_size
% ALIGNMENT
) {
378 alloc_size
= alloc_size
+ ALIGNMENT
- (alloc_size
% ALIGNMENT
);
380 if (alloc_size
< ALIGNMENT
)
381 alloc_size
= ALIGNMENT
;
382 ASSERT((alloc_size
% ALIGNMENT
) == 0);
384 /* Now we figure out how many items fit in each chunk. We need to fit at
385 * least 2 items per chunk. No chunk can be more than MAX_CHUNK bytes long,
386 * or less than MIN_CHUNK. */
387 if (chunk_capacity
> MAX_CHUNK
)
388 chunk_capacity
= MAX_CHUNK
;
389 /* Try to be around a power of 2 in size, since that's what allocators like
390 * handing out. 512K-1 byte is a lot better than 512K+1 byte. */
391 chunk_capacity
= (size_t) round_to_power_of_2(chunk_capacity
);
392 while (chunk_capacity
< alloc_size
* 2 + CHUNK_OVERHEAD
)
394 if (chunk_capacity
< MIN_CHUNK
)
395 chunk_capacity
= MIN_CHUNK
;
397 new_chunk_cap
= (chunk_capacity
-CHUNK_OVERHEAD
) / alloc_size
;
398 tor_assert(new_chunk_cap
< INT_MAX
);
399 pool
->new_chunk_capacity
= (int)new_chunk_cap
;
401 pool
->item_alloc_size
= alloc_size
;
403 log_debug(LD_MM
, "Capacity is %lu, item size is %lu, alloc size is %lu",
404 (unsigned long)pool
->new_chunk_capacity
,
405 (unsigned long)pool
->item_alloc_size
,
406 (unsigned long)(pool
->new_chunk_capacity
*pool
->item_alloc_size
));
411 /** Helper function for qsort: used to sort pointers to mp_chunk_t into
412 * descending order of fullness. */
414 mp_pool_sort_used_chunks_helper(const void *_a
, const void *_b
)
416 mp_chunk_t
*a
= *(mp_chunk_t
**)_a
;
417 mp_chunk_t
*b
= *(mp_chunk_t
**)_b
;
418 return b
->n_allocated
- a
->n_allocated
;
421 /** Sort the used chunks in <b>pool</b> into descending order of fullness,
422 * so that we preferentially fill up mostly full chunks before we make
423 * nearly empty chunks less nearly empty. */
425 mp_pool_sort_used_chunks(mp_pool_t
*pool
)
427 int i
, n
=0, inverted
=0;
428 mp_chunk_t
**chunks
, *chunk
;
429 for (chunk
= pool
->used_chunks
; chunk
; chunk
= chunk
->next
) {
431 if (chunk
->next
&& chunk
->next
->n_allocated
> chunk
->n_allocated
)
436 //printf("Sort %d/%d\n",inverted,n);
437 chunks
= ALLOC(sizeof(mp_chunk_t
*)*n
);
438 #ifdef ALLOC_CAN_RETURN_NULL
439 if (PREDICT_UNLIKELY(!chunks
)) return;
441 for (i
=0,chunk
= pool
->used_chunks
; chunk
; chunk
= chunk
->next
)
443 qsort(chunks
, n
, sizeof(mp_chunk_t
*), mp_pool_sort_used_chunks_helper
);
444 pool
->used_chunks
= chunks
[0];
445 chunks
[0]->prev
= NULL
;
447 chunks
[i
-1]->next
= chunks
[i
];
448 chunks
[i
]->prev
= chunks
[i
-1];
450 chunks
[n
-1]->next
= NULL
;
452 mp_pool_assert_ok(pool
);
455 /** If there are more than <b>n</b> empty chunks in <b>pool</b>, free the
456 * excess ones that have been empty for the longest. If
457 * <b>keep_recently_used</b> is true, do not free chunks unless they have been
458 * empty since the last call to this function.
461 mp_pool_clean(mp_pool_t
*pool
, int n_to_keep
, int keep_recently_used
)
463 mp_chunk_t
*chunk
, **first_to_free
;
465 mp_pool_sort_used_chunks(pool
);
466 ASSERT(n_to_keep
>= 0);
468 if (keep_recently_used
) {
469 int n_recently_used
= pool
->n_empty_chunks
- pool
->min_empty_chunks
;
470 if (n_to_keep
< n_recently_used
)
471 n_to_keep
= n_recently_used
;
474 ASSERT(n_to_keep
>= 0);
476 first_to_free
= &pool
->empty_chunks
;
477 while (*first_to_free
&& n_to_keep
> 0) {
478 first_to_free
= &(*first_to_free
)->next
;
481 if (!*first_to_free
) {
482 pool
->min_empty_chunks
= pool
->n_empty_chunks
;
486 chunk
= *first_to_free
;
488 mp_chunk_t
*next
= chunk
->next
;
489 chunk
->magic
= 0xdeadbeef;
492 ++pool
->total_chunks_freed
;
494 --pool
->n_empty_chunks
;
498 pool
->min_empty_chunks
= pool
->n_empty_chunks
;
499 *first_to_free
= NULL
;
502 /** Helper: Given a list of chunks, free all the chunks in the list. */
504 destroy_chunks(mp_chunk_t
*chunk
)
508 chunk
->magic
= 0xd3adb33f;
515 /** Free all space held in <b>pool</b> This makes all pointers returned from
516 * mp_pool_get(<b>pool</b>) invalid. */
518 mp_pool_destroy(mp_pool_t
*pool
)
520 destroy_chunks(pool
->empty_chunks
);
521 destroy_chunks(pool
->used_chunks
);
522 destroy_chunks(pool
->full_chunks
);
523 memset(pool
, 0xe0, sizeof(mp_pool_t
));
527 /** Helper: make sure that a given chunk list is not corrupt. */
529 assert_chunks_ok(mp_pool_t
*pool
, mp_chunk_t
*chunk
, int empty
, int full
)
531 mp_allocated_t
*allocated
;
534 ASSERT(chunk
->prev
== NULL
);
538 ASSERT(chunk
->magic
== MP_CHUNK_MAGIC
);
539 ASSERT(chunk
->pool
== pool
);
540 for (allocated
= chunk
->first_free
; allocated
;
541 allocated
= allocated
->u
.next_free
) {
542 ASSERT(allocated
->in_chunk
== chunk
);
545 ASSERT(chunk
->n_allocated
== 0);
547 ASSERT(chunk
->n_allocated
== chunk
->capacity
);
549 ASSERT(chunk
->n_allocated
> 0 && chunk
->n_allocated
< chunk
->capacity
);
551 ASSERT(chunk
->capacity
== pool
->new_chunk_capacity
);
553 ASSERT(chunk
->mem_size
==
554 pool
->new_chunk_capacity
* pool
->item_alloc_size
);
556 ASSERT(chunk
->next_mem
>= chunk
->mem
&&
557 chunk
->next_mem
<= chunk
->mem
+ chunk
->mem_size
);
560 ASSERT(chunk
->next
->prev
== chunk
);
567 /** Fail with an assertion if <b>pool</b> is not internally consistent. */
569 mp_pool_assert_ok(mp_pool_t
*pool
)
573 n_empty
= assert_chunks_ok(pool
, pool
->empty_chunks
, 1, 0);
574 assert_chunks_ok(pool
, pool
->full_chunks
, 0, 1);
575 assert_chunks_ok(pool
, pool
->used_chunks
, 0, 0);
577 ASSERT(pool
->n_empty_chunks
== n_empty
);
581 /** Dump information about <b>pool</b>'s memory usage to the Tor log at level
582 * <b>severity</b>. */
583 /*FFFF uses Tor logging functions. */
585 mp_pool_log_status(mp_pool_t
*pool
, int severity
)
587 uint64_t bytes_used
= 0;
588 uint64_t bytes_allocated
= 0;
589 uint64_t bu
= 0, ba
= 0;
591 int n_full
= 0, n_used
= 0;
595 for (chunk
= pool
->empty_chunks
; chunk
; chunk
= chunk
->next
) {
596 bytes_allocated
+= chunk
->mem_size
;
598 log_fn(severity
, LD_MM
, U64_FORMAT
" bytes in %d empty chunks",
599 U64_PRINTF_ARG(bytes_allocated
), pool
->n_empty_chunks
);
600 for (chunk
= pool
->used_chunks
; chunk
; chunk
= chunk
->next
) {
602 bu
+= chunk
->n_allocated
* pool
->item_alloc_size
;
603 ba
+= chunk
->mem_size
;
604 log_fn(severity
, LD_MM
, " used chunk: %d items allocated",
607 log_fn(severity
, LD_MM
, U64_FORMAT
"/"U64_FORMAT
608 " bytes in %d partially full chunks",
609 U64_PRINTF_ARG(bu
), U64_PRINTF_ARG(ba
), n_used
);
611 bytes_allocated
+= ba
;
613 for (chunk
= pool
->full_chunks
; chunk
; chunk
= chunk
->next
) {
615 bu
+= chunk
->n_allocated
* pool
->item_alloc_size
;
616 ba
+= chunk
->mem_size
;
618 log_fn(severity
, LD_MM
, U64_FORMAT
"/"U64_FORMAT
619 " bytes in %d full chunks",
620 U64_PRINTF_ARG(bu
), U64_PRINTF_ARG(ba
), n_full
);
622 bytes_allocated
+= ba
;
624 log_fn(severity
, LD_MM
, "Total: "U64_FORMAT
"/"U64_FORMAT
" bytes allocated "
625 "for cell pools are full.",
626 U64_PRINTF_ARG(bytes_used
), U64_PRINTF_ARG(bytes_allocated
));
629 log_fn(severity
, LD_MM
, U64_FORMAT
" cell allocations ever; "
630 U64_FORMAT
" chunk allocations ever; "
631 U64_FORMAT
" chunk frees ever.",
632 U64_PRINTF_ARG(pool
->total_items_allocated
),
633 U64_PRINTF_ARG(pool
->total_chunks_allocated
),
634 U64_PRINTF_ARG(pool
->total_chunks_freed
));