[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] r12981: New, slightly esoteric function, tor_malloc_roundup(). While (in tor/trunk: . src/common)
Author: nickm
Date: 2007-12-25 19:12:01 -0500 (Tue, 25 Dec 2007)
New Revision: 12981
Modified:
tor/trunk/
tor/trunk/configure.in
tor/trunk/src/common/mempool.c
tor/trunk/src/common/util.c
tor/trunk/src/common/util.h
Log:
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.
Property changes on: tor/trunk
___________________________________________________________________
svk:merge ticket from /tor/trunk [r15691] on d9e39d38-0f13-419c-a857-e10a0ce2aa0c
Modified: tor/trunk/configure.in
===================================================================
--- tor/trunk/configure.in 2007-12-25 19:04:26 UTC (rev 12980)
+++ tor/trunk/configure.in 2007-12-26 00:12:01 UTC (rev 12981)
@@ -171,7 +171,7 @@
dnl Check for functions before libevent, since libevent-1.2 apparently
dnl exports strlcpy without defining it in a header.
-AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit strlcat strlcpy strtoull ftello getaddrinfo localtime_r gmtime_r memmem strtok_r inet_pton inet_ntop mallinfo)
+AC_CHECK_FUNCS(gettimeofday ftime socketpair uname inet_aton strptime getrlimit strlcat strlcpy strtoull ftello getaddrinfo localtime_r gmtime_r memmem strtok_r inet_pton inet_ntop mallinfo malloc_good_size malloc_usable_size)
if test "$enable_threads" = "yes"; then
AC_CHECK_HEADERS(pthread.h)
@@ -266,7 +266,7 @@
dnl These headers are not essential
-AC_CHECK_HEADERS(stdint.h sys/types.h inttypes.h sys/param.h sys/wait.h limits.h sys/limits.h netinet/in.h arpa/inet.h machine/limits.h syslog.h sys/time.h sys/resource.h inttypes.h utime.h sys/utime.h sys/mman.h netintet/in.h netinet/in6.h malloc.h sys/syslimits.h)
+AC_CHECK_HEADERS(stdint.h sys/types.h inttypes.h sys/param.h sys/wait.h limits.h sys/limits.h netinet/in.h arpa/inet.h machine/limits.h syslog.h sys/time.h sys/resource.h inttypes.h utime.h sys/utime.h sys/mman.h netintet/in.h netinet/in6.h malloc.h sys/syslimits.h malloc/malloc.h)
AC_CHECK_HEADERS(net/if.h, net_if_found=1, net_if_found=0,
[#ifdef HAVE_SYS_TYPES_H
Modified: tor/trunk/src/common/mempool.c
===================================================================
--- tor/trunk/src/common/mempool.c 2007-12-25 19:04:26 UTC (rev 12980)
+++ tor/trunk/src/common/mempool.c 2007-12-26 00:12:01 UTC (rev 12981)
@@ -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 @@
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;
Modified: tor/trunk/src/common/util.c
===================================================================
--- tor/trunk/src/common/util.c 2007-12-25 19:04:26 UTC (rev 12980)
+++ tor/trunk/src/common/util.c 2007-12-26 00:12:01 UTC (rev 12981)
@@ -69,7 +69,10 @@
#ifdef HAVE_TIME_H
#include <time.h>
#endif
-#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
+#ifdef HAVE_MALLOC_MALLOC_H
+#include <malloc/malloc.h>
+#endif
+#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
@@ -221,6 +224,26 @@
tor_free(mem);
}
+/** Allocate and return a chunk of memory of size at least *<b>size</p>, using
+ * the same resources we would use to malloc *<b>sizep</b>. Set *<b>sizep</b>
+ * 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 <b>severity</b>. If no such function exists, do nothing. */
void
Modified: tor/trunk/src/common/util.h
===================================================================
--- tor/trunk/src/common/util.h 2007-12-25 19:04:26 UTC (rev 12980)
+++ tor/trunk/src/common/util.h 2007-12-26 00:12:01 UTC (rev 12981)
@@ -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 @@
#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)