summaryrefslogtreecommitdiff
path: root/src/common/mempool.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2007-12-26 00:12:01 +0000
committerNick Mathewson <nickm@torproject.org>2007-12-26 00:12:01 +0000
commit0c8142e981497359d86d6c8825455d315d99d3fb (patch)
treeaadf197afe2c9ca486a723a71950b43af04077f6 /src/common/mempool.c
parent0421e53c66547275bdf4bf2e6aafe112402a330a (diff)
downloadtor-0c8142e981497359d86d6c8825455d315d99d3fb.tar.gz
tor-0c8142e981497359d86d6c8825455d315d99d3fb.zip
r15691@tombo: nickm | 2007-12-25 18:13:54 -0500
New, slightly esoteric function, tor_malloc_roundup(). While tor_malloc(x) allocates x bytes, tor_malloc_roundup(&x) allocates the same size of chunk it would use to store x bytes, and sets x to the usable size of that chunk. svn:r12981
Diffstat (limited to 'src/common/mempool.c')
-rw-r--r--src/common/mempool.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/common/mempool.c b/src/common/mempool.c
index 142ba0b9ae..36eb0e545f 100644
--- a/src/common/mempool.c
+++ b/src/common/mempool.c
@@ -78,6 +78,7 @@
#define ASSERT(x) tor_assert(x)
#undef ALLOC_CAN_RETURN_NULL
#define TOR
+//#define ALLOC_ROUNDUP(p) tor_malloc_roundup(p)
/* End Tor dependencies */
#else
/* If you're not building this as part of Tor, you'll want to define the
@@ -172,12 +173,22 @@ static mp_chunk_t *
mp_chunk_new(mp_pool_t *pool)
{
size_t sz = pool->new_chunk_capacity * pool->item_alloc_size;
+#ifdef ALLOC_ROUNDUP
+ size_t alloc_size = CHUNK_OVERHEAD + sz;
+ mp_chunk_t *chunk = ALLOC_ROUNDUP(&alloc_size);
+#else
mp_chunk_t *chunk = ALLOC(CHUNK_OVERHEAD + sz);
+#endif
CHECK_ALLOC(chunk);
memset(chunk, 0, sizeof(mp_chunk_t)); /* Doesn't clear the whole thing. */
chunk->magic = MP_CHUNK_MAGIC;
+#ifdef ALLOC_ROUNDUP
+ chunk->mem_size = alloc_size - CHUNK_OVERHEAD;
+ chunk->capacity = chunk->mem_size / pool->item_alloc_size;
+#else
chunk->capacity = pool->new_chunk_capacity;
chunk->mem_size = sz;
+#endif
chunk->next_mem = chunk->mem;
chunk->pool = pool;
return chunk;