diff options
author | Nick Mathewson <nickm@torproject.org> | 2007-05-24 17:12:57 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2007-05-24 17:12:57 +0000 |
commit | d3d86b17a761011f62dc2e06c04c0af8f1a75ef6 (patch) | |
tree | efb329c2d77ef6f39c02db8db62bfa18ad5f4d6d /src/common/mempool.c | |
parent | d0a5c4f9848f44c46f001428bd3078673033ef19 (diff) | |
download | tor-d3d86b17a761011f62dc2e06c04c0af8f1a75ef6.tar.gz tor-d3d86b17a761011f62dc2e06c04c0af8f1a75ef6.zip |
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
Diffstat (limited to 'src/common/mempool.c')
-rw-r--r-- | src/common/mempool.c | 14 |
1 files changed, 10 insertions, 4 deletions
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; } |