From 0c8142e981497359d86d6c8825455d315d99d3fb Mon Sep 17 00:00:00 2001
From: Nick Mathewson
Date: Wed, 26 Dec 2007 00:12:01 +0000
Subject: 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
---
src/common/mempool.c | 11 +++++++++++
src/common/util.c | 25 ++++++++++++++++++++++++-
src/common/util.h | 2 ++
3 files changed, 37 insertions(+), 1 deletion(-)
(limited to 'src')
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;
diff --git a/src/common/util.c b/src/common/util.c
index 91f588ba47..d11b508b92 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -69,7 +69,10 @@ const char util_c_id[] = "$Id$";
#ifdef HAVE_TIME_H
#include
#endif
-#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
+#ifdef HAVE_MALLOC_MALLOC_H
+#include
+#endif
+#ifdef HAVE_MALLOC_H
#include
#endif
@@ -221,6 +224,26 @@ _tor_free(void *mem)
tor_free(mem);
}
+/** Allocate and return a chunk of memory of size at least *size
, using
+ * the same resources we would use to malloc *sizep. Set *sizep
+ * to the number of usable bytes in the chunk of memory. */
+void *
+_tor_malloc_roundup(size_t *sizep DMALLOC_PARAMS)
+{
+#ifdef HAVE_MALLOC_GOOD_SIZE
+ *sizep = malloc_good_size(*sizep);
+ return _tor_malloc(*sizep DMALLOC_FN_ARGS);
+#else
+#if defined(HAVE_MALLOC_USABLE_SIZE) && !defined(USE_DMALLOC)
+ void *result = _tor_malloc(*sizep DMALLOC_FN_ARGS);
+ *sizep = malloc_usable_size(result);
+ return result;
+#else
+ return _tor_malloc(*sizep);
+#endif
+#endif
+}
+
/** Call the platform malloc info function, and dump the results to the log at
* level severity. If no such function exists, do nothing. */
void
diff --git a/src/common/util.h b/src/common/util.h
index c0343cf2fd..d69c919964 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -75,6 +75,7 @@
/* Memory management */
void *_tor_malloc(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
void *_tor_malloc_zero(size_t size DMALLOC_PARAMS) ATTR_MALLOC;
+void *_tor_malloc_roundup(size_t *size DMALLOC_PARAMS) ATTR_MALLOC;
void *_tor_realloc(void *ptr, size_t size DMALLOC_PARAMS);
char *_tor_strdup(const char *s DMALLOC_PARAMS) ATTR_MALLOC ATTR_NONNULL((1));
char *_tor_strndup(const char *s, size_t n DMALLOC_PARAMS)
@@ -102,6 +103,7 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
#define tor_malloc(size) _tor_malloc(size DMALLOC_ARGS)
#define tor_malloc_zero(size) _tor_malloc_zero(size DMALLOC_ARGS)
+#define tor_malloc_roundup(szp) _tor_malloc_roundup(szp DMALLOC_ARGS)
#define tor_realloc(ptr, size) _tor_realloc(ptr, size DMALLOC_ARGS)
#define tor_strdup(s) _tor_strdup(s DMALLOC_ARGS)
#define tor_strndup(s, n) _tor_strndup(s, n DMALLOC_ARGS)
--
cgit v1.2.3-54-g00ecf