diff options
Diffstat (limited to 'lib/isc/mem.c')
-rw-r--r-- | lib/isc/mem.c | 46 |
1 files changed, 40 insertions, 6 deletions
diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 408770d82c4c..7d237329b21d 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC") + * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 1997-2003 Internet Software Consortium. * * Permission to use, copy, modify, and/or distribute this software for any @@ -15,7 +15,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: mem.c,v 1.116.18.21 2008/02/07 23:45:56 tbox Exp $ */ +/* $Id: mem.c,v 1.116.18.25 2009/02/16 03:17:57 marka Exp $ */ /*! \file */ @@ -51,7 +51,7 @@ LIBISC_EXTERNAL_DATA unsigned int isc_mem_debugging = ISC_MEM_DEBUGGING; #define DEF_MAX_SIZE 1100 #define DEF_MEM_TARGET 4096 -#define ALIGNMENT_SIZE 8 /*%< must be a power of 2 */ +#define ALIGNMENT_SIZE 8U /*%< must be a power of 2 */ #define NUM_BASIC_BLOCKS 64 /*%< must be > 1 */ #define TABLE_INCREMENT 1024 #define DEBUGLIST_COUNT 1024 @@ -1173,7 +1173,7 @@ print_active(isc_mem_t *mctx, FILE *out) { const char *format; isc_boolean_t found; - fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM, + fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM, ISC_MSG_DUMPALLOC, "Dump of all outstanding " "memory allocations:\n")); @@ -1199,7 +1199,7 @@ print_active(isc_mem_t *mctx, FILE *out) { } } if (!found) - fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM, + fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM, ISC_MSG_NONE, "\tNone.\n")); } } @@ -1241,7 +1241,7 @@ isc_mem_stats(isc_mem_t *ctx, FILE *out) { */ pool = ISC_LIST_HEAD(ctx->pools); if (pool != NULL) { - fprintf(out, isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM, + fprintf(out, "%s", isc_msgcat_get(isc_msgcat, ISC_MSGSET_MEM, ISC_MSG_POOLSTATS, "[Pool statistics]\n")); fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n", @@ -1347,6 +1347,40 @@ isc__mem_allocate(isc_mem_t *ctx, size_t size FLARG) { return (si); } +void * +isc__mem_reallocate(isc_mem_t *ctx, void *ptr, size_t size FLARG) { + void *new_ptr = NULL; + size_t oldsize, copysize; + + REQUIRE(VALID_CONTEXT(ctx)); + + /* + * This function emulates the realloc(3) standard library function: + * - if size > 0, allocate new memory; and if ptr is non NULL, copy + * as much of the old contents to the new buffer and free the old one. + * Note that when allocation fails the original pointer is intact; + * the caller must free it. + * - if size is 0 and ptr is non NULL, simply free the given ptr. + * - this function returns: + * pointer to the newly allocated memory, or + * NULL if allocation fails or doesn't happen. + */ + if (size > 0U) { + new_ptr = isc__mem_allocate(ctx, size FLARG_PASS); + if (new_ptr != NULL && ptr != NULL) { + oldsize = (((size_info *)ptr)[-1]).u.size; + INSIST(oldsize >= ALIGNMENT_SIZE); + oldsize -= ALIGNMENT_SIZE; + copysize = oldsize > size ? size : oldsize; + memcpy(new_ptr, ptr, copysize); + isc__mem_free(ctx, ptr FLARG_PASS); + } + } else if (ptr != NULL) + isc__mem_free(ctx, ptr FLARG_PASS); + + return (new_ptr); +} + void isc__mem_free(isc_mem_t *ctx, void *ptr FLARG) { size_info *si; |