[njs] Memory: added cleanup handlers support.

Dmitry Volyntsev xeioex at nginx.com
Tue Aug 31 13:17:44 UTC 2021


details:   https://hg.nginx.org/njs/rev/99afe1a7f71d
branches:  
changeset: 1693:99afe1a7f71d
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Aug 31 13:16:43 2021 +0000
description:
Memory: added cleanup handlers support.

diffstat:

 src/njs_mp.c |  41 +++++++++++++++++++++++++++++++++++++++++
 src/njs_mp.h |  10 ++++++++++
 2 files changed, 51 insertions(+), 0 deletions(-)

diffs (96 lines):

diff -r 4c0b2392a5ef -r 99afe1a7f71d src/njs_mp.c
--- a/src/njs_mp.c	Tue Aug 31 13:16:42 2021 +0000
+++ b/src/njs_mp.c	Tue Aug 31 13:16:43 2021 +0000
@@ -106,6 +106,8 @@ struct njs_mp_s {
     uint32_t                    page_alignment;
     uint32_t                    cluster_size;
 
+    njs_mp_cleanup_t            *cleanup;
+
     njs_mp_slot_t               slots[];
 };
 
@@ -251,10 +253,18 @@ njs_mp_destroy(njs_mp_t *mp)
 {
     void               *p;
     njs_mp_block_t     *block;
+    njs_mp_cleanup_t   *c;
     njs_rbtree_node_t  *node, *next;
 
     njs_debug_alloc("mp destroy\n");
 
+    for (c = mp->cleanup; c != NULL; c = c->next) {
+        if (c->handler != NULL) {
+            njs_debug_alloc("mp run cleanup: @%p\n", c);
+            c->handler(c->data);
+        }
+    }
+
     next = njs_rbtree_root(&mp->blocks);
 
     while (next != njs_rbtree_sentinel(&mp->blocks)) {
@@ -606,6 +616,37 @@ njs_mp_rbtree_compare(njs_rbtree_node_t 
 }
 
 
+njs_mp_cleanup_t *
+njs_mp_cleanup_add(njs_mp_t *mp, size_t size)
+{
+    njs_mp_cleanup_t  *c;
+
+    c = njs_mp_alloc(mp, sizeof(njs_mp_cleanup_t));
+    if (njs_slow_path(c == NULL)) {
+        return NULL;
+    }
+
+    if (size) {
+        c->data = njs_mp_alloc(mp, size);
+        if (njs_slow_path(c->data == NULL)) {
+            return NULL;
+        }
+
+    } else {
+        c->data = NULL;
+    }
+
+    c->handler = NULL;
+    c->next = mp->cleanup;
+
+    mp->cleanup = c;
+
+    njs_debug_alloc("mp add cleanup: @%p\n", c);
+
+    return c;
+}
+
+
 void
 njs_mp_free(njs_mp_t *mp, void *p)
 {
diff -r 4c0b2392a5ef -r 99afe1a7f71d src/njs_mp.h
--- a/src/njs_mp.h	Tue Aug 31 13:16:42 2021 +0000
+++ b/src/njs_mp.h	Tue Aug 31 13:16:43 2021 +0000
@@ -9,6 +9,15 @@
 
 
 typedef struct njs_mp_s  njs_mp_t;
+typedef struct njs_mp_cleanup_s  njs_mp_cleanup_t;
+
+typedef void (*njs_mp_cleanup_pt)(void *data);
+
+struct njs_mp_cleanup_s {
+        njs_mp_cleanup_pt   handler;
+        void                *data;
+        njs_mp_cleanup_t    *next;
+};
 
 
 NJS_EXPORT njs_mp_t *njs_mp_create(size_t cluster_size, size_t page_alignment,
@@ -28,6 +37,7 @@ NJS_EXPORT void *njs_mp_align(njs_mp_t *
 NJS_EXPORT void *njs_mp_zalign(njs_mp_t *mp,
     size_t alignment, size_t size)
     NJS_MALLOC_LIKE;
+NJS_EXPORT njs_mp_cleanup_t *njs_mp_cleanup_add(njs_mp_t *mp, size_t size);
 NJS_EXPORT void njs_mp_free(njs_mp_t *mp, void *p);
 
 


More information about the nginx-devel mailing list