mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2025-02-22 14:23:04 +01:00
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
This commit is contained in:
parent
0421e53c66
commit
0c8142e981
4 changed files with 39 additions and 3 deletions
|
@ -171,7 +171,7 @@ dnl -------------------------------------------------------------------
|
|||
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 @@ AC_CHECK_HEADERS(netdb.h sys/ioctl.h sys/socket.h arpa/inet.h netinet/in.h pwd.h
|
|||
|
||||
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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -69,7 +69,10 @@ const char util_c_id[] = "$Id$";
|
|||
#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(void *mem)
|
|||
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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue