From 78b2f25c9afee0d16f2e75882d9763abcb0872e5 Mon Sep 17 00:00:00 2001 From: Michael Graff Date: Tue, 19 Oct 1999 01:22:39 +0000 Subject: [PATCH] add isc_mem_setname() --- lib/isc/include/isc/mem.h | 18 ++++++++++++++- lib/isc/mem.c | 47 ++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index 3671d8fb0f..3d00e21100 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -32,6 +32,12 @@ ISC_LANG_BEGINDECLS typedef void * (*isc_memalloc_t)(void *, size_t); typedef void (*isc_memfree_t)(void *, void *); +/* + * Define to 0 to remove the names from memory pools. This will save + * about 16 bytes per pool. + */ +#define ISC_MEMPOOL_NAMES 1 + #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__) @@ -138,10 +144,20 @@ isc_mempool_destroy(isc_mempool_t **mpctxp); * Destroy a memory pool. * * Requires: - * mpctxp is a valid pool. + * mpctxp != NULL && *mpctxp is a valid pool. * The pool has no un"put" allocations outstanding */ +void +isc_mempool_setname(isc_mempool_t *mpctx, char *name); +/* + * Associate a name with a memory pool. At most 15 characters may be used. + * + * Requires: + * mpctx is a valid pool. + * name != NULL; + */ + void isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock); /* diff --git a/lib/isc/mem.c b/lib/isc/mem.c index e79989fa1d..99707d5e32 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -46,6 +46,14 @@ #define ISC_MEM_FILL 1 #endif +#ifndef ISC_MEMPOOL_NAMES +/* + * During development it is nice to be able to see names associated with + * memory pools. + */ +#define ISC_MEMPOOL_NAMES 1 +#endif + /* * Types. */ @@ -113,6 +121,10 @@ struct isc_mempool { unsigned int fillcount; /* # of items to fetch on each fill */ /* Stats only. */ unsigned int gets; /* # of requests to this pool */ + /* Debugging only. */ +#if ISC_MEMPOOL_NAMES + char name[16]; /* printed name in stats reports */ +#endif }; /* Forward. */ @@ -547,15 +559,15 @@ isc_mem_stats(isc_mem_t *ctx, FILE *out) pool = ISC_LIST_HEAD(ctx->pools); if (pool != NULL) { fprintf(out, "[Pool statistics]\n"); - fprintf(out, "%10s %10s %10s %10s %10s %10s %10s %1s\n", - "size", "maxalloc", "allocated", "freecount", + fprintf(out, "%15s %10s %10s %10s %10s %10s %10s %10s %1s\n", + "name", "size", "maxalloc", "allocated", "freecount", "freemax", "fillcount", "gets", "L"); } while (pool != NULL) { - fprintf(out, "%10u %10u %10u %10u %10u %10u %10u %s\n", - pool->size, pool->maxalloc, pool->allocated, - pool->freecount, pool->freemax, pool->fillcount, - pool->gets, + fprintf(out, "%15s %10u %10u %10u %10u %10u %10u %10u %s\n", + pool->name, pool->size, pool->maxalloc, + pool->allocated, pool->freecount, pool->freemax, + pool->fillcount, pool->gets, (pool->lock == NULL ? "N" : "Y")); pool = ISC_LIST_NEXT(pool, link); } @@ -838,6 +850,9 @@ isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) mpctx->freemax = 1; mpctx->fillcount = 1; mpctx->gets = 0; +#if ISC_MEMPOOL_NAMES + mpctx->name[0] = 0; +#endif mpctx->items = NULL; *mpctxp = mpctx; @@ -849,6 +864,26 @@ isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) return (ISC_R_SUCCESS); } +void +isc_mempool_setname(isc_mempool_t *mpctx, char *name) +{ + REQUIRE(name != NULL); + +#if ISC_MEMPOOL_NAMES + if (mpctx->lock != NULL) + LOCK(mpctx->lock); + + memset(mpctx->name, 0, sizeof(mpctx->name)); + strncpy(mpctx->name, name, sizeof(mpctx->name) - 1); + + if (mpctx->lock != NULL) + UNLOCK(mpctx->lock); +#else + (void)mpctx; + (void)name; +#endif +} + void isc_mempool_destroy(isc_mempool_t **mpctxp) {