]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add debug tracing capability to isc_mem_create/isc_mem_destroy
authorOndřej Surý <ondrej@sury.org>
Tue, 11 May 2021 10:18:56 +0000 (12:18 +0200)
committerOndřej Surý <ondrej@sury.org>
Fri, 9 Jul 2021 13:58:02 +0000 (15:58 +0200)
Previously, we only had capability to trace the memory gets and puts,
but for debugging, it's sometimes also important to keep track how many
and where do the memory contexts get created and destroyed.  This commit
adds such tracking capability.

lib/isc/include/isc/mem.h
lib/isc/mem.c

index 85973a755a20db2799d12c57a18294499f1a9bd5..a5f003bdced43e0f700d53241461e028d1555cb8 100644 (file)
@@ -175,8 +175,8 @@ extern unsigned int isc_mem_defaultflags;
        } while (0)
 
 /*@{*/
-void
-isc_mem_create(isc_mem_t **mctxp);
+#define isc_mem_create(cp) ISCMEMFUNC(create)((cp)_ISC_MEM_FILELINE)
+void ISCMEMFUNC(create)(isc_mem_t **_ISC_MEM_FLARG);
 
 /*!<
  * \brief Create a memory context.
@@ -188,8 +188,8 @@ isc_mem_create(isc_mem_t **mctxp);
 /*@{*/
 void
 isc_mem_attach(isc_mem_t *, isc_mem_t **);
-void
-isc_mem_detach(isc_mem_t **);
+#define isc_mem_detach(cp) ISCMEMFUNC(detach)((cp)_ISC_MEM_FILELINE)
+void ISCMEMFUNC(detach)(isc_mem_t **_ISC_MEM_FLARG);
 /*!<
  * \brief Attach to / detach from a memory context.
  *
@@ -204,8 +204,8 @@ isc_mem_detach(isc_mem_t **);
  */
 /*@}*/
 
-void
-isc_mem_destroy(isc_mem_t **);
+#define isc_mem_destroy(cp) ISCMEMFUNC(destroy)((cp)_ISC_MEM_FILELINE)
+void ISCMEMFUNC(destroy)(isc_mem_t **_ISC_MEM_FLARG);
 /*%<
  * Destroy a memory context.
  */
index 54d5411ef842eb1964785763021af5ad7ecf5997..6adfd9d671625665b6e5b3a7b0e7889596cd779f 100644 (file)
@@ -604,7 +604,7 @@ isc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) {
 }
 
 void
-isc_mem_detach(isc_mem_t **ctxp) {
+isc__mem_detach(isc_mem_t **ctxp FLARG) {
        REQUIRE(ctxp != NULL && VALID_CONTEXT(*ctxp));
 
        isc_mem_t *ctx = *ctxp;
@@ -612,6 +612,12 @@ isc_mem_detach(isc_mem_t **ctxp) {
 
        if (isc_refcount_decrement(&ctx->references) == 1) {
                isc_refcount_destroy(&ctx->references);
+#if ISC_MEM_TRACKLINES
+               if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+                       fprintf(stderr, "destroy mctx %p file %s line %u\n",
+                               ctx, file, line);
+               }
+#endif
                destroy(ctx);
        }
 }
@@ -663,7 +669,7 @@ destroy:
 }
 
 void
-isc_mem_destroy(isc_mem_t **ctxp) {
+isc__mem_destroy(isc_mem_t **ctxp FLARG) {
        /*
         * This routine provides legacy support for callers who use mctxs
         * without attaching/detaching.
@@ -674,6 +680,11 @@ isc_mem_destroy(isc_mem_t **ctxp) {
        isc_mem_t *ctx = *ctxp;
 
 #if ISC_MEM_TRACKLINES
+       if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+               fprintf(stderr, "destroy mctx %p file %s line %u\n", ctx, file,
+                       line);
+       }
+
        if (isc_refcount_decrement(&ctx->references) > 1) {
                print_active(ctx, stderr);
        }
@@ -1343,7 +1354,6 @@ isc__mempool_put(isc_mempool_t *mpctx, void *mem FLARG) {
 void *
 isc__mempool_get(isc_mempool_t *mpctx FLARG) {
        element *item = NULL;
-       unsigned int i;
 
        REQUIRE(VALID_MEMPOOL(mpctx));
 
@@ -1366,7 +1376,7 @@ isc__mempool_get(isc_mempool_t *mpctx FLARG) {
                 * We need to dip into the well.  Lock the memory
                 * context here and fill up our free list.
                 */
-               for (i = 0; i < fillcount; i++) {
+               for (size_t i = 0; i < fillcount; i++) {
                        item = mem_get(mctx, mpctx->size);
                        mem_getstats(mctx, mpctx->size);
                        item->next = mpctx->items;
@@ -1843,8 +1853,14 @@ error:
 #endif /* HAVE_JSON_C */
 
 void
-isc_mem_create(isc_mem_t **mctxp) {
+isc__mem_create(isc_mem_t **mctxp FLARG) {
        mem_create(mctxp, isc_mem_defaultflags);
+#if ISC_MEM_TRACKLINES
+       if ((isc_mem_debugging & ISC_MEM_DEBUGTRACE) != 0) {
+               fprintf(stderr, "create mctx %p file %s line %u\n", *mctxp,
+                       file, line);
+       }
+#endif /* ISC_MEM_TRACKLINES */
 }
 
 void