From: Dmitry Volyntsev Date: Tue, 31 Aug 2021 13:16:43 +0000 (+0000) Subject: Memory: added cleanup handlers support. X-Git-Tag: 0.6.2~2 X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=8b704dc7fd83bf8c449f6acc6d54773548b5e395;p=njs.git Memory: added cleanup handlers support. --- diff --git a/src/njs_mp.c b/src/njs_mp.c index 987f052b..5c7868df 100644 --- a/src/njs_mp.c +++ b/src/njs_mp.c @@ -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 *node1, njs_rbtree_node_t *node2) } +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 --git a/src/njs_mp.h b/src/njs_mp.h index 3e38b481..3dbf989d 100644 --- a/src/njs_mp.h +++ b/src/njs_mp.h @@ -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 *mp, size_t alignment, size_t size) 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);