From 1162a4e02a6594dbb4f57fd288a5d20ab467e4d7 Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Tue, 5 Sep 2000 03:30:19 +0000 Subject: [PATCH] New function isc_mem_putanddetach(). --- lib/isc/include/isc/mem.h | 11 ++++++++++- lib/isc/mem.c | 41 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/isc/include/isc/mem.h b/lib/isc/include/isc/mem.h index dc595d9ddb..11cd3c28bc 100644 --- a/lib/isc/include/isc/mem.h +++ b/lib/isc/include/isc/mem.h @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: mem.h,v 1.42 2000/08/31 16:58:28 gson Exp $ */ +/* $Id: mem.h,v 1.43 2000/09/05 03:30:19 marka Exp $ */ #ifndef ISC_MEM_H #define ISC_MEM_H 1 @@ -102,6 +102,11 @@ extern unsigned int isc_mem_debugging; isc__mem_put((c), (p), (s) _ISC_MEM_FILELINE); \ (p) = NULL; \ } while (0) +#define isc_mem_putanddetach(c, p, s) \ + do { \ + isc__mem_putanddetach((c), (p), (s) _ISC_MEM_FILELINE); \ + (p) = NULL; \ + } while (0) #define isc_mem_free(c, p) \ do { \ isc__mem_free((c), (p) _ISC_MEM_FILELINE); \ @@ -114,6 +119,8 @@ extern unsigned int isc_mem_debugging; } while (0) #else #define isc_mem_put(c, p, s) isc__mem_put((c), (p), (s) _ISC_MEM_FILELINE) +#define isc_mem_putanddetach(c, p, s) \ + isc__mem_putanddetach((c), (p), (s) _ISC_MEM_FILELINE) #define isc_mem_free(c, p) isc__mem_free((c), (p) _ISC_MEM_FILELINE) #define isc_mempool_put(c, p) isc__mempool_put((c), (p) _ISC_MEM_FILELINE) #endif @@ -127,6 +134,8 @@ isc_result_t isc_mem_ondestroy(isc_mem_t *ctx, isc_event_t **event); void * isc__mem_get(isc_mem_t *, size_t _ISC_MEM_FLARG); +void isc__mem_putanddetach(isc_mem_t **, void *, + size_t _ISC_MEM_FLARG); void isc__mem_put(isc_mem_t *, void *, size_t _ISC_MEM_FLARG); isc_result_t isc_mem_preallocate(isc_mem_t *); diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 977732d81c..e2f36ef4af 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -15,7 +15,7 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: mem.c,v 1.61 2000/08/31 12:15:16 marka Exp $ */ +/* $Id: mem.c,v 1.62 2000/09/05 03:30:18 marka Exp $ */ #include @@ -755,6 +755,45 @@ isc_mem_detach(isc_mem_t **ctxp) { *ctxp = NULL; } +/* + * isc_mem_putanddetach() is the equivalent of: + * + * mctx = NULL; + * isc_mem_attach(ptr->mctx, &mctx); + * isc_mem_detach(&ptr->mctx); + * isc_mem_put(mctx, ptr, sizeof(*ptr); + * isc_mem_detach(&mctx); + */ + +void +isc__mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) { + isc_mem_t *ctx; + isc_boolean_t want_destroy = ISC_FALSE; + + REQUIRE(ctxp != NULL); + ctx = *ctxp; + REQUIRE(VALID_CONTEXT(ctx)); + REQUIRE(ptr != NULL); + + /* + * Must be before mem_putunlocked() as ctxp is usually within + * [ptr..ptr+size). + */ + *ctxp = NULL; + + LOCK(&ctx->lock); + DELETE_TRACE(ctx, ptr, size, file, line); + mem_putunlocked(ctx, ptr, size); + INSIST(ctx->references > 0); + ctx->references--; + if (ctx->references == 0) + want_destroy = ISC_TRUE; + UNLOCK(&ctx->lock); + + if (want_destroy) + destroy(ctx); +} + void isc_mem_destroy(isc_mem_t **ctxp) { isc_mem_t *ctx;