From d3d86b17a761011f62dc2e06c04c0af8f1a75ef6 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 24 May 2007 17:12:57 +0000 Subject: r12916@catbus: nickm | 2007-05-24 12:43:45 -0400 Add math functions to round values to the nearest power of 2. Make mempools more careful about making sure that the size of their chunks is a little less than a power of 2, not a little more. svn:r10304 --- src/common/mempool.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/common/mempool.c') diff --git a/src/common/mempool.c b/src/common/mempool.c index 2b1775c1ba..665a803af2 100644 --- a/src/common/mempool.c +++ b/src/common/mempool.c @@ -361,18 +361,24 @@ mp_pool_new(size_t item_size, size_t chunk_capacity) /* Now we figure out how many items fit in each chunk. We need to fit at * least 2 items per chunk. No chunk can be more than MAX_CHUNK bytes long, * or less than MIN_CHUNK. */ - /* XXXX020 Try a bit harder here: we want to be a bit less than a power of - 2, not a bit over. */ if (chunk_capacity > MAX_CHUNK) chunk_capacity = MAX_CHUNK; - if (chunk_capacity < alloc_size * 2 + CHUNK_OVERHEAD) - chunk_capacity = alloc_size * 2 + CHUNK_OVERHEAD; + /* Try to be around a power of 2 in size, since that's what allocators like + * handing out. 512K-1 byte is a lot better than 512K+1 byte. */ + chunk_capacity = (size_t) round_to_power_of_2(chunk_capacity); + while (chunk_capacity < alloc_size * 2 + CHUNK_OVERHEAD) + chunk_capacity *= 2; if (chunk_capacity < MIN_CHUNK) chunk_capacity = MIN_CHUNK; pool->new_chunk_capacity = (chunk_capacity-CHUNK_OVERHEAD) / alloc_size; pool->item_alloc_size = alloc_size; + log_debug(LD_MM, "Capacity is %lu, item size is %lu, alloc size is %lu", + (unsigned long)pool->new_chunk_capacity, + (unsigned long)pool->item_alloc_size, + (unsigned long)(pool->new_chunk_capacity*pool->item_alloc_size)); + return pool; } -- cgit v1.2.3-54-g00ecf