From 5f89718b7d354cb4ceb99d0dd952a4faf4597ab8 Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Sat, 26 Feb 2000 19:59:30 +0000 Subject: [PATCH] Debugging versions of isc_mem_allocate, isc_mem_free and isc_mem_strdup. This allows memory allocation/freeing to be tracked with ISC_MEM_DEBUG as with isc_mem_get/isc_mem_put. (To get the debugging information before, mem.c itself needed to be recompiled with ISC_MEM_DEBUG on.) --- lib/isc/include/isc/mem.h | 21 ++++++++++++--- lib/isc/mem.c | 54 ++++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index a91ca2d1dd..05b6c111b6 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -43,12 +43,20 @@ typedef void (*isc_memfree_t)(void *, void *); #ifdef ISC_MEM_DEBUG #define isc_mem_get(c, s) __isc_mem_getdebug(c, s, __FILE__, __LINE__) #define isc_mem_put(c, p, s) __isc_mem_putdebug(c, p, s, __FILE__, __LINE__) +#define isc_mem_allocate(c, p, s) __isc_mem_allocatedebug(c, s, \ + __FILE__, __LINE__) +#define isc_mem_free(c, p) __isc_mem_freedebug(c, p, __FILE__, __LINE__) +#define isc_mem_strdup(c, p) __isc_mem_strdupdebug(c, p, \ + __FILE__, __LINE__) #define isc_mempool_get(c) __isc_mempool_getdebug(c, __FILE__, __LINE__) #define isc_mempool_put(c, p) __isc_mempool_putdebug(c, p, \ __FILE__, __LINE__) #else #define isc_mem_get __isc_mem_get #define isc_mem_put __isc_mem_put +#define isc_mem_allocate __isc_mem_allocate +#define isc_mem_free __isc_mem_free +#define isc_mem_strdup __isc_mem_strdup #define isc_mempool_get __isc_mempool_get #define isc_mempool_put __isc_mempool_put #endif /* ISC_MEM_DEBUG */ @@ -66,9 +74,16 @@ void __isc_mem_putdebug(isc_mem_t *, void *, size_t, const char *, int); void isc_mem_stats(isc_mem_t *, FILE *); isc_boolean_t isc_mem_valid(isc_mem_t *, void *); -void * isc_mem_allocate(isc_mem_t *, size_t); -void isc_mem_free(isc_mem_t *, void *); -char * isc_mem_strdup(isc_mem_t *, const char *); +void * __isc_mem_allocate(isc_mem_t *, size_t); +void * __isc_mem_allocatedebug(isc_mem_t *, size_t, + const char *, int); +void __isc_mem_free(isc_mem_t *, void *); +void __isc_mem_freedebug(isc_mem_t *, void *, + const char *, int); +char * __isc_mem_strdup(isc_mem_t *, const char *); +char * __isc_mem_strdupdebug(isc_mem_t *, + const char *, + const char *, int); isc_boolean_t isc_mem_destroy_check(isc_mem_t *, isc_boolean_t); void isc_mem_setquota(isc_mem_t *, size_t); size_t isc_mem_getquota(isc_mem_t *); diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 1bf266498e..cb59bc71a3 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -568,8 +568,7 @@ __isc_mem_putdebug(isc_mem_t *ctx, void *ptr, size_t size, const char *file, * Print the stats[] on the stream "out" with suitable formatting. */ void -isc_mem_stats(isc_mem_t *ctx, FILE *out) -{ +isc_mem_stats(isc_mem_t *ctx, FILE *out) { size_t i; const struct stats *s; const isc_mempool_t *pool; @@ -636,27 +635,51 @@ isc_mem_valid(isc_mem_t *ctx, void *ptr) { } /* - * Replacements for malloc() and free(). + * Replacements for malloc() and free() -- they implicitly remember the + * size of the object allocated (with some additional overhead). */ void * -isc_mem_allocate(isc_mem_t *ctx, size_t size) { +__isc_mem_allocate(isc_mem_t *ctx, size_t size) { size_info *si; size += ALIGNMENT_SIZE; - si = isc_mem_get(ctx, size); + si = __isc_mem_get(ctx, size); if (si == NULL) return (NULL); si->u.size = size; return (&si[1]); } +void * +__isc_mem_allocatedebug(isc_mem_t *ctx, size_t size, const char *file, + int line) { + size_info *si; + + si = __isc_mem_allocate(ctx, size); + if (si == NULL) + return (NULL); + fprintf(stderr, "%s:%d: mem_get(%p, %lu) -> %p\n", file, line, + ctx, (unsigned long)si[-1].u.size, si); + return (si); +} + void -isc_mem_free(isc_mem_t *ctx, void *ptr) { +__isc_mem_free(isc_mem_t *ctx, void *ptr) { size_info *si; si = &(((size_info *)ptr)[-1]); - isc_mem_put(ctx, si, si->u.size); + __isc_mem_put(ctx, si, si->u.size); +} + +void +__isc_mem_freedebug(isc_mem_t *ctx, void *ptr, const char *file, int line) { + size_info *si; + + si = &(((size_info *)ptr)[-1]); + fprintf(stderr, "%s:%d: mem_put(%p, %p, %lu)\n", file, line, + ctx, ptr, (unsigned long)si->u.size); + __isc_mem_put(ctx, si, si->u.size); } /* @@ -664,12 +687,12 @@ isc_mem_free(isc_mem_t *ctx, void *ptr) { */ char * -isc_mem_strdup(isc_mem_t *mctx, const char *s) { +__isc_mem_strdup(isc_mem_t *mctx, const char *s) { size_t len; char *ns; len = strlen(s); - ns = isc_mem_allocate(mctx, len + 1); + ns = __isc_mem_allocate(mctx, len + 1); if (ns == NULL) return (NULL); strncpy(ns, s, len + 1); @@ -677,6 +700,19 @@ isc_mem_strdup(isc_mem_t *mctx, const char *s) { return (ns); } +char * +__isc_mem_strdupdebug(isc_mem_t *mctx, const char *s, const char *file, + int line) { + char *ptr; + size_info *si; + + ptr = __isc_mem_strdup(mctx, s); + si = &(((size_info *)ptr)[-1]); + fprintf(stderr, "%s:%d: mem_get(%p, %lu) -> %p\n", file, line, + mctx, (unsigned long)si->u.size, ptr); + return (ptr); +} + isc_boolean_t isc_mem_destroy_check(isc_mem_t *mctx, isc_boolean_t flag) { isc_boolean_t oldval;