[njs] Removing njs_mem_proto_t from njs_mp_t.

Dmitry Volyntsev xeioex at nginx.com
Thu Aug 1 18:01:32 UTC 2019


details:   https://hg.nginx.org/njs/rev/84f4da1e7d96
branches:  
changeset: 1092:84f4da1e7d96
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Aug 01 21:01:16 2019 +0300
description:
Removing njs_mem_proto_t from njs_mp_t.

diffstat:

 src/njs.h                   |   2 -
 src/njs_malloc.c            |  18 +++++++++
 src/njs_malloc.h            |   3 +-
 src/njs_mp.c                |  82 ++++++++++++++++----------------------------
 src/njs_mp.h                |  22 ++++++++---
 src/njs_vm.c                |  53 +---------------------------
 src/test/lvlhsh_unit_test.c |  65 +----------------------------------
 7 files changed, 69 insertions(+), 176 deletions(-)

diffs (479 lines):

diff -r 7356d868d2cd -r 84f4da1e7d96 src/njs.h
--- a/src/njs.h	Thu Aug 01 18:49:29 2019 +0300
+++ b/src/njs.h	Thu Aug 01 21:01:16 2019 +0300
@@ -295,6 +295,4 @@ NJS_EXPORT njs_int_t njs_vm_json_parse(n
 NJS_EXPORT njs_int_t njs_vm_json_stringify(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs);
 
-extern const njs_mem_proto_t  njs_vm_mp_proto;
-
 #endif /* _NJS_H_INCLUDED_ */
diff -r 7356d868d2cd -r 84f4da1e7d96 src/njs_malloc.c
--- a/src/njs_malloc.c	Thu Aug 01 18:49:29 2019 +0300
+++ b/src/njs_malloc.c	Thu Aug 01 21:01:16 2019 +0300
@@ -7,8 +7,26 @@
 #include <njs_auto_config.h>
 #include <njs_types.h>
 #include <njs_clang.h>
+#include <njs_str.h>
 #include <njs_malloc.h>
 
+#include <string.h>
+
+
+void *
+njs_zalloc(size_t size)
+{
+    void  *p;
+
+    p = njs_malloc(size);
+
+    if (njs_fast_path(p != NULL)) {
+        njs_memzero(p, size);
+    }
+
+    return p;
+}
+
 
 #if (NJS_HAVE_POSIX_MEMALIGN)
 
diff -r 7356d868d2cd -r 84f4da1e7d96 src/njs_malloc.h
--- a/src/njs_malloc.h	Thu Aug 01 18:49:29 2019 +0300
+++ b/src/njs_malloc.h	Thu Aug 01 21:01:16 2019 +0300
@@ -22,7 +22,8 @@
 #define njs_free(p)        free(p)
 
 
-NJS_EXPORT void *njs_memalign(size_t alignment, size_t size);
+NJS_EXPORT void *njs_zalloc(size_t size) NJS_MALLOC_LIKE;
+NJS_EXPORT void *njs_memalign(size_t alignment, size_t size) NJS_MALLOC_LIKE;
 
 
 #endif /* _NJS_MALLOC_H_INCLUDED_ */
diff -r 7356d868d2cd -r 84f4da1e7d96 src/njs_mp.c
--- a/src/njs_mp.c	Thu Aug 01 18:49:29 2019 +0300
+++ b/src/njs_mp.c	Thu Aug 01 21:01:16 2019 +0300
@@ -9,6 +9,8 @@
 #include <njs_clang.h>
 #include <njs_stub.h>
 #include <njs_str.h>
+#include <njs_sprintf.h>
+#include <njs_malloc.h>
 #include <njs_queue.h>
 #include <njs_rbtree.h>
 #include <njs_mp.h>
@@ -114,10 +116,6 @@ struct njs_mp_s {
     uint32_t                    page_alignment;
     uint32_t                    cluster_size;
 
-    const njs_mem_proto_t       *proto;
-    void                        *mem;
-    void                        *trace;
-
     njs_mp_slot_t               slots[];
 };
 
@@ -155,8 +153,7 @@ static const char *njs_mp_chunk_free(njs
 
 
 njs_mp_t *
-njs_mp_create(const njs_mem_proto_t *proto, void *mem,
-    void *trace, size_t cluster_size, size_t page_alignment, size_t page_size,
+njs_mp_create(size_t cluster_size, size_t page_alignment, size_t page_size,
     size_t min_chunk_size)
 {
     /* Alignment and sizes must be a power of 2. */
@@ -181,14 +178,13 @@ njs_mp_create(const njs_mem_proto_t *pro
         return NULL;
     }
 
-    return njs_mp_fast_create(proto, mem, trace, cluster_size, page_alignment,
-                              page_size, min_chunk_size);
+    return njs_mp_fast_create(cluster_size, page_alignment, page_size,
+                              min_chunk_size);
 }
 
 
 njs_mp_t *
-njs_mp_fast_create(const njs_mem_proto_t *proto, void *mem,
-    void *trace, size_t cluster_size, size_t page_alignment, size_t page_size,
+njs_mp_fast_create(size_t cluster_size, size_t page_alignment, size_t page_size,
     size_t min_chunk_size)
 {
     njs_mp_t       *mp;
@@ -203,13 +199,9 @@ njs_mp_fast_create(const njs_mem_proto_t
         chunk_size /= 2;
     } while (chunk_size > min_chunk_size);
 
-    mp = proto->zalloc(mem, sizeof(njs_mp_t) + slots * sizeof(njs_mp_slot_t));
+    mp = njs_zalloc(sizeof(njs_mp_t) + slots * sizeof(njs_mp_slot_t));
 
     if (njs_fast_path(mp != NULL)) {
-        mp->proto = proto;
-        mp->mem = mem;
-        mp->trace = trace;
-
         mp->page_size = page_size;
         mp->page_alignment = njs_max(page_alignment, NJS_MAX_ALIGNMENT);
         mp->cluster_size = cluster_size;
@@ -271,6 +263,8 @@ njs_mp_destroy(njs_mp_t *mp)
     njs_mp_block_t     *block;
     njs_rbtree_node_t  *node, *next;
 
+    njs_debug_alloc("mp destroy\n");
+
     next = njs_rbtree_root(&mp->blocks);
 
     while (next != njs_rbtree_sentinel(&mp->blocks)) {
@@ -281,22 +275,20 @@ njs_mp_destroy(njs_mp_t *mp)
         p = block->start;
 
         if (block->type != NJS_MP_EMBEDDED_BLOCK) {
-            mp->proto->free(mp->mem, block);
+            njs_free(block);
         }
 
-        mp->proto->free(mp->mem, p);
+        njs_free(p);
     }
 
-    mp->proto->free(mp->mem, mp);
+    njs_free(mp);
 }
 
 
 void *
 njs_mp_alloc(njs_mp_t *mp, size_t size)
 {
-    if (mp->proto->trace != NULL) {
-        mp->proto->trace(mp->trace, "mem cache alloc: %zd", size);
-    }
+    njs_debug_alloc("mp alloc: %uz\n", size);
 
 #if !(NJS_DEBUG_MEMORY)
 
@@ -328,10 +320,7 @@ njs_mp_zalloc(njs_mp_t *mp, size_t size)
 void *
 njs_mp_align(njs_mp_t *mp, size_t alignment, size_t size)
 {
-    if (mp->proto->trace != NULL) {
-        mp->proto->trace(mp->trace,
-                         "mem cache align: @%zd:%zd", alignment, size);
-    }
+    njs_debug_alloc("mp align: @%uz:%uz\n", alignment, size);
 
     /* Alignment must be a power of 2. */
 
@@ -456,9 +445,7 @@ njs_mp_alloc_small(njs_mp_t *mp, size_t 
 #endif
     }
 
-    if (mp->proto->trace != NULL) {
-        mp->proto->trace(mp->trace, "mem cache chunk:%uz alloc: %p", size, p);
-    }
+    njs_debug_alloc("mp chunk:%uz alloc: %p\n", size, p);
 
     return p;
 }
@@ -533,9 +520,7 @@ njs_mp_alloc_cluster(njs_mp_t *mp)
 
     n = mp->cluster_size >> mp->page_size_shift;
 
-    cluster = mp->proto->zalloc(mp->mem,
-                                sizeof(njs_mp_block_t)
-                                + n * sizeof(njs_mp_page_t));
+    cluster = njs_zalloc(sizeof(njs_mp_block_t) + n * sizeof(njs_mp_page_t));
 
     if (njs_slow_path(cluster == NULL)) {
         return NULL;
@@ -545,10 +530,9 @@ njs_mp_alloc_cluster(njs_mp_t *mp)
 
     cluster->size = mp->cluster_size;
 
-    cluster->start = mp->proto->align(mp->mem, mp->page_alignment,
-                                      mp->cluster_size);
+    cluster->start = njs_memalign(mp->page_alignment, mp->cluster_size);
     if (njs_slow_path(cluster->start == NULL)) {
-        mp->proto->free(mp->mem, cluster);
+        njs_free(cluster);
         return NULL;
     }
 
@@ -585,14 +569,14 @@ njs_mp_alloc_large(njs_mp_t *mp, size_t 
     }
 
     if (njs_is_power_of_two(size)) {
-        block = mp->proto->alloc(mp->mem, sizeof(njs_mp_block_t));
+        block = njs_malloc(sizeof(njs_mp_block_t));
         if (njs_slow_path(block == NULL)) {
             return NULL;
         }
 
-        p = mp->proto->align(mp->mem, alignment, size);
+        p = njs_memalign(alignment, size);
         if (njs_slow_path(p == NULL)) {
-            mp->proto->free(mp->mem, block);
+            njs_free(block);
             return NULL;
         }
 
@@ -601,9 +585,7 @@ njs_mp_alloc_large(njs_mp_t *mp, size_t 
     } else {
         aligned_size = njs_align_size(size, sizeof(uintptr_t));
 
-        p = mp->proto->align(mp->mem, alignment,
-                             aligned_size + sizeof(njs_mp_block_t));
-
+        p = njs_memalign(alignment, aligned_size + sizeof(njs_mp_block_t));
         if (njs_slow_path(p == NULL)) {
             return NULL;
         }
@@ -640,9 +622,7 @@ njs_mp_free(njs_mp_t *mp, void *p)
     const char      *err;
     njs_mp_block_t  *block;
 
-    if (mp->proto->trace != NULL) {
-        mp->proto->trace(mp->trace, "mem cache free %p", p);
-    }
+    njs_debug_alloc("mp free: @%p\n", p);
 
     block = njs_mp_find_block(&mp->blocks, p);
 
@@ -659,24 +639,22 @@ njs_mp_free(njs_mp_t *mp, void *p)
             njs_rbtree_delete(&mp->blocks, &block->node);
 
             if (block->type == NJS_MP_DISCRETE_BLOCK) {
-                mp->proto->free(mp->mem, block);
+                njs_free(block);
             }
 
-            mp->proto->free(mp->mem, p);
+            njs_free(p);
 
             return;
 
         } else {
-            err = "freed pointer points to middle of block: %p";
+            err = "freed pointer points to middle of block: %p\n";
         }
 
     } else {
-        err = "freed pointer is out of mp: %p";
+        err = "freed pointer is out of mp: %p\n";
     }
 
-    if (mp->proto->alert != NULL) {
-        mp->proto->alert(mp->trace, err, p);
-    }
+    njs_debug_alloc(err, p);
 }
 
 
@@ -810,8 +788,8 @@ njs_mp_chunk_free(njs_mp_t *mp, njs_mp_b
 
     p = cluster->start;
 
-    mp->proto->free(mp->mem, cluster);
-    mp->proto->free(mp->mem, p);
+    njs_free(cluster);
+    njs_free(p);
 
     return NULL;
 }
diff -r 7356d868d2cd -r 84f4da1e7d96 src/njs_mp.h
--- a/src/njs_mp.h	Thu Aug 01 18:49:29 2019 +0300
+++ b/src/njs_mp.h	Thu Aug 01 21:01:16 2019 +0300
@@ -11,13 +11,10 @@
 typedef struct njs_mp_s  njs_mp_t;
 
 
-NJS_EXPORT njs_mp_t *njs_mp_create(const njs_mem_proto_t *proto, void *mem,
-    void *trace, size_t cluster_size, size_t page_alignment, size_t page_size,
-    size_t min_chunk_size)
-    NJS_MALLOC_LIKE;
-NJS_EXPORT njs_mp_t * njs_mp_fast_create(const njs_mem_proto_t *proto,
-    void *mem, void *trace, size_t cluster_size, size_t page_alignment,
-    size_t page_size, size_t min_chunk_size)
+NJS_EXPORT njs_mp_t *njs_mp_create(size_t cluster_size, size_t page_alignment,
+    size_t page_size, size_t min_chunk_size) NJS_MALLOC_LIKE;
+NJS_EXPORT njs_mp_t * njs_mp_fast_create(size_t cluster_size,
+    size_t page_alignment, size_t page_size, size_t min_chunk_size)
     NJS_MALLOC_LIKE;
 NJS_EXPORT njs_bool_t njs_mp_is_empty(njs_mp_t *mp);
 NJS_EXPORT void njs_mp_destroy(njs_mp_t *mp);
@@ -34,4 +31,15 @@ NJS_EXPORT void *njs_mp_zalign(njs_mp_t 
 NJS_EXPORT void njs_mp_free(njs_mp_t *mp, void *p);
 
 
+#if (NJS_ALLOC_DEBUG)
+#define njs_debug_alloc(...)                                                  \
+    njs_stderror(__VA_ARGS__)
+
+#else
+
+#define njs_debug_alloc(...)
+
+#endif
+
+
 #endif /* _NJS_MP_H_INCLUDED_ */
diff -r 7356d868d2cd -r 84f4da1e7d96 src/njs_vm.c
--- a/src/njs_vm.c	Thu Aug 01 18:49:29 2019 +0300
+++ b/src/njs_vm.c	Thu Aug 01 21:01:16 2019 +0300
@@ -21,53 +21,6 @@ const njs_str_t  njs_entry_anonymous =  
 
 
 static void *
-njs_alloc(void *mem, size_t size)
-{
-    return njs_malloc(size);
-}
-
-
-static void *
-njs_zalloc(void *mem, size_t size)
-{
-    void  *p;
-
-    p = njs_malloc(size);
-
-    if (p != NULL) {
-        njs_memzero(p, size);
-    }
-
-    return p;
-}
-
-
-static void *
-njs_align(void *mem, size_t alignment, size_t size)
-{
-    return njs_memalign(alignment, size);
-}
-
-
-static void
-njs_free2(void *mem, void *p)
-{
-    njs_free(p);
-}
-
-
-const njs_mem_proto_t  njs_vm_mp_proto = {
-    njs_alloc,
-    njs_zalloc,
-    njs_align,
-    NULL,
-    njs_free2,
-    NULL,
-    NULL,
-};
-
-
-static void *
 njs_array_mem_alloc(void *mem, size_t size)
 {
     return njs_mp_alloc(mem, size);
@@ -101,8 +54,7 @@ njs_vm_create(njs_vm_opt_t *options)
     njs_arr_t             *debug;
     njs_regexp_pattern_t  *pattern;
 
-    mp = njs_mp_create(&njs_vm_mp_proto, NULL, NULL, 2 * njs_pagesize(),
-                       128, 512, 16);
+    mp = njs_mp_fast_create(2 * njs_pagesize(), 128, 512, 16);
     if (njs_slow_path(mp == NULL)) {
         return NULL;
     }
@@ -325,8 +277,7 @@ njs_vm_clone(njs_vm_t *vm, njs_external_
         return NULL;
     }
 
-    nmp = njs_mp_create(&njs_vm_mp_proto, NULL, NULL, 2 * njs_pagesize(),
-                        128, 512, 16);
+    nmp = njs_mp_fast_create(2 * njs_pagesize(), 128, 512, 16);
     if (njs_slow_path(nmp == NULL)) {
         return NULL;
     }
diff -r 7356d868d2cd -r 84f4da1e7d96 src/test/lvlhsh_unit_test.c
--- a/src/test/lvlhsh_unit_test.c	Thu Aug 01 18:49:29 2019 +0300
+++ b/src/test/lvlhsh_unit_test.c	Thu Aug 01 21:01:16 2019 +0300
@@ -129,67 +129,6 @@ lvlhsh_unit_test_delete(njs_lvlhsh_t *lh
 }
 
 
-static void *
-lvlhsh_malloc(void *mem, size_t size)
-{
-    return njs_malloc(size);
-}
-
-
-static void *
-lvlhsh_zalloc(void *mem, size_t size)
-{
-    void  *p;
-
-    p = njs_malloc(size);
-
-    if (p != NULL) {
-        njs_memzero(p, size);
-    }
-
-    return p;
-}
-
-
-static void *
-lvlhsh_align(void *mem, size_t alignment, size_t size)
-{
-    return njs_memalign(alignment, size);
-}
-
-
-static void
-lvlhsh_free(void *mem, void *p)
-{
-    njs_free(p);
-}
-
-
-static void
-lvlhsh_alert(void *mem, const char *fmt, ...)
-{
-    u_char   buf[1024], *p;
-    va_list  args;
-
-    va_start(args, fmt);
-    p = njs_sprintf(buf, buf + sizeof(buf), fmt, args);
-    va_end(args);
-
-    (void) njs_stderror("alert: \"%*s\"\n", p - buf, buf);
-}
-
-
-static const njs_mem_proto_t  lvl_mp_proto = {
-    lvlhsh_malloc,
-    lvlhsh_zalloc,
-    lvlhsh_align,
-    NULL,
-    lvlhsh_free,
-    lvlhsh_alert,
-    NULL,
-};
-
-
 static njs_int_t
 lvlhsh_unit_test(njs_uint_t n)
 {
@@ -204,8 +143,8 @@ lvlhsh_unit_test(njs_uint_t n)
     const size_t       page_alignment = 128;
     const size_t       cluster_size = 4096;
 
-    pool = njs_mp_create(&lvl_mp_proto, NULL, NULL, cluster_size,
-                         page_alignment, page_size, min_chunk_size);
+    pool = njs_mp_create(cluster_size, page_alignment, page_size,
+                         min_chunk_size);
     if (pool == NULL) {
         return NJS_ERROR;
     }


More information about the nginx-devel mailing list