[njs] Allowing to use custom allocator in njs_chb_t.
Dmitry Volyntsev
xeioex at nginx.com
Mon Apr 29 21:24:58 UTC 2024
details: https://hg.nginx.org/njs/rev/622387b5b612
branches:
changeset: 2322:622387b5b612
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Fri Apr 26 16:27:52 2024 -0700
description:
Allowing to use custom allocator in njs_chb_t.
diffstat:
external/njs_query_string_module.c | 6 +++---
external/njs_xml_module.c | 2 +-
external/njs_zlib_module.c | 4 ++--
nginx/ngx_js_fetch.c | 6 +++---
src/njs_array.c | 2 +-
src/njs_chb.c | 21 +++++++++++++++++----
src/njs_chb.h | 27 ++++++++++++++++-----------
src/njs_error.c | 2 +-
src/njs_function.c | 2 +-
src/njs_json.c | 4 ++--
src/njs_regexp.c | 2 +-
src/njs_string.c | 8 ++++----
src/njs_typed_array.c | 2 +-
src/njs_vm.c | 2 +-
src/test/njs_unit_test.c | 2 +-
15 files changed, 55 insertions(+), 37 deletions(-)
diffs (347 lines):
diff -r a97a9f3b3cea -r 622387b5b612 external/njs_query_string_module.c
--- a/external/njs_query_string_module.c Mon Apr 22 18:59:03 2024 -0700
+++ b/external/njs_query_string_module.c Fri Apr 26 16:27:52 2024 -0700
@@ -152,7 +152,7 @@ njs_query_string_decode(njs_vm_t *vm, nj
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
- njs_chb_init(&chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&chain, vm);
njs_utf8_decode_init(&ctx);
cp = 0;
@@ -749,7 +749,7 @@ njs_query_string_stringify(njs_vm_t *vm,
encode = njs_value_function(val);
}
- njs_chb_init(&chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&chain, vm);
keys = njs_vm_object_keys(vm, object, njs_value_arg(&value));
if (njs_slow_path(keys == NULL)) {
@@ -841,7 +841,7 @@ njs_query_string_escape(njs_vm_t *vm, nj
njs_value_string_get(string, &str);
- njs_chb_init(&chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&chain, vm);
ret = njs_query_string_encode(&chain, &str);
if (njs_slow_path(ret != NJS_OK)) {
diff -r a97a9f3b3cea -r 622387b5b612 external/njs_xml_module.c
--- a/external/njs_xml_module.c Mon Apr 22 18:59:03 2024 -0700
+++ b/external/njs_xml_module.c Fri Apr 26 16:27:52 2024 -0700
@@ -1788,7 +1788,7 @@ njs_xml_ext_canonicalization(njs_vm_t *v
}
}
- njs_chb_init(&chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&chain, vm);
buf = xmlOutputBufferCreateIO(njs_xml_buf_write_cb, NULL, &chain, NULL);
if (njs_slow_path(buf == NULL)) {
diff -r a97a9f3b3cea -r 622387b5b612 external/njs_zlib_module.c
--- a/external/njs_zlib_module.c Mon Apr 22 18:59:03 2024 -0700
+++ b/external/njs_zlib_module.c Fri Apr 26 16:27:52 2024 -0700
@@ -320,7 +320,7 @@ njs_zlib_ext_deflate(njs_vm_t *vm, njs_v
}
}
- njs_chb_init(&chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&chain, vm);
do {
stream.next_out = njs_chb_reserve(&chain, chunk_size);
@@ -461,7 +461,7 @@ njs_zlib_ext_inflate(njs_vm_t *vm, njs_v
}
}
- njs_chb_init(&chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&chain, vm);
while (rc != Z_STREAM_END) {
stream.next_out = njs_chb_reserve(&chain, chunk_size);
diff -r a97a9f3b3cea -r 622387b5b612 nginx/ngx_js_fetch.c
--- a/nginx/ngx_js_fetch.c Mon Apr 22 18:59:03 2024 -0700
+++ b/nginx/ngx_js_fetch.c Fri Apr 26 16:27:52 2024 -0700
@@ -740,7 +740,7 @@ ngx_js_ext_fetch(njs_vm_t *vm, njs_value
http->header_only = njs_strstr_eq(&request.method, &njs_str_value("HEAD"));
- njs_chb_init(&http->chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&http->chain, vm);
njs_chb_append(&http->chain, request.method.start, request.method.length);
njs_chb_append_literal(&http->chain, " ");
@@ -1051,7 +1051,7 @@ ngx_js_ext_response_constructor(njs_vm_t
}
}
- njs_chb_init(&response->chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&response->chain, vm);
body = njs_arg(args, nargs, 1);
@@ -2468,7 +2468,7 @@ ngx_js_http_process_headers(ngx_js_http_
njs_chb_destroy(&http->chain);
- njs_chb_init(&http->response.chain, njs_vm_memory_pool(http->vm));
+ NJS_CHB_MP_INIT(&http->response.chain, http->vm);
http->process = ngx_js_http_process_body;
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_array.c
--- a/src/njs_array.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_array.c Fri Apr 26 16:27:52 2024 -0700
@@ -1698,7 +1698,7 @@ njs_array_prototype_join(njs_vm_t *vm, n
value = &entry;
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
for (i = 0; i < len; i++) {
ret = njs_value_property_i64(vm, this, i, value);
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_chb.c
--- a/src/njs_chb.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_chb.c Fri Apr 26 16:27:52 2024 -0700
@@ -12,6 +12,19 @@
void
+njs_chb_init(njs_chb_t *chain, void *pool, njs_chb_alloc_t alloc,
+ njs_chb_free_t free)
+{
+ chain->error = 0;
+ chain->pool = pool;
+ chain->alloc = alloc;
+ chain->free = free;
+ chain->nodes = NULL;
+ chain->last = NULL;
+}
+
+
+void
njs_chb_append0(njs_chb_t *chain, const char *msg, size_t len)
{
u_char *p;
@@ -44,7 +57,7 @@ njs_chb_reserve(njs_chb_t *chain, size_t
size = NJS_CHB_MIN_SIZE;
}
- n = njs_mp_alloc(chain->pool, sizeof(njs_chb_node_t) + size);
+ n = chain->alloc(chain->pool, sizeof(njs_chb_node_t) + size);
if (njs_slow_path(n == NULL)) {
chain->error = 1;
return NULL;
@@ -149,7 +162,7 @@ njs_chb_drop(njs_chb_t *chain, size_t dr
if (drop >= size) {
njs_chb_destroy(chain);
- njs_chb_init(chain, chain->pool);
+ njs_chb_init(chain, chain->pool, chain->alloc, chain->free);
return;
}
@@ -201,7 +214,7 @@ njs_chb_join(njs_chb_t *chain, njs_str_t
return NJS_ERROR;
}
- start = njs_mp_alloc(chain->pool, size);
+ start = chain->alloc(chain->pool, size);
if (njs_slow_path(start == NULL)) {
return NJS_ERROR;
}
@@ -238,7 +251,7 @@ njs_chb_destroy(njs_chb_t *chain)
while (n != NULL) {
next = n->next;
- njs_mp_free(chain->pool, n);
+ chain->free(chain->pool, n);
n = next;
}
}
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_chb.h
--- a/src/njs_chb.h Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_chb.h Fri Apr 26 16:27:52 2024 -0700
@@ -17,14 +17,29 @@ struct njs_chb_node_s {
u_char *end;
};
+typedef void *(*njs_chb_alloc_t)(void *pool, size_t size);
+typedef void (*njs_chb_free_t)(void *pool, void *p);
+
typedef struct {
njs_bool_t error;
- njs_mp_t *pool;
+
+ void *pool;
+ njs_chb_alloc_t alloc;
+ njs_chb_free_t free;
+
njs_chb_node_t *nodes;
njs_chb_node_t *last;
} njs_chb_t;
+void njs_chb_init(njs_chb_t *chain, void *pool, njs_chb_alloc_t alloc,
+ njs_chb_free_t free);
+#define NJS_CHB_MP_INIT(chain, vm) \
+ njs_chb_init(chain, njs_vm_memory_pool(vm), (njs_chb_alloc_t) njs_mp_alloc,\
+ (njs_chb_free_t) njs_mp_free)
+#define NJS_CHB_CTX_INIT(chain, ctx) \
+ njs_chb_init(chain, ctx, (njs_chb_alloc_t) js_malloc, \
+ (njs_chb_free_t) js_free)
void njs_chb_append0(njs_chb_t *chain, const char *msg, size_t len);
void njs_chb_vsprintf(njs_chb_t *chain, size_t size, const char *fmt,
va_list args);
@@ -49,16 +64,6 @@ void njs_chb_destroy(njs_chb_t *chain);
njs_inline void
-njs_chb_init(njs_chb_t *chain, njs_mp_t *pool)
-{
- chain->error = 0;
- chain->pool = pool;
- chain->nodes = NULL;
- chain->last = NULL;
-}
-
-
-njs_inline void
njs_chb_append_str(njs_chb_t *chain, njs_str_t *str)
{
njs_chb_append0(chain, (const char *) str->start, str->length);
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_error.c
--- a/src/njs_error.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_error.c Fri Apr 26 16:27:52 2024 -0700
@@ -1069,7 +1069,7 @@ njs_backtrace_to_string(njs_vm_t *vm, nj
return NJS_OK;
}
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
njs_chb_append_str(&chain, dst);
njs_chb_append(&chain, "\n", 1);
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_function.c
--- a/src/njs_function.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_function.c Fri Apr 26 16:27:52 2024 -0700
@@ -1032,7 +1032,7 @@ njs_function_constructor(njs_vm_t *vm, n
goto fail;
}
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
if (async) {
njs_chb_append_literal(&chain, "(async function(");
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_json.c
--- a/src/njs_json.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_json.c Fri Apr 26 16:27:52 2024 -0700
@@ -1106,7 +1106,7 @@ njs_json_stringify_iterator(njs_json_str
goto memory_error;
}
- njs_chb_init(&chain, stringify->vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, stringify->vm);
for ( ;; ) {
if (state->index == 0) {
@@ -1990,7 +1990,7 @@ njs_vm_value_dump(njs_vm_t *vm, njs_str_
value = &exception;
}
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
if (!njs_dump_is_recursive(value)) {
ret = njs_dump_terminal(stringify, &chain, value, console);
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_regexp.c
--- a/src/njs_regexp.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_regexp.c Fri Apr 26 16:27:52 2024 -0700
@@ -1398,7 +1398,7 @@ njs_regexp_prototype_symbol_replace(njs_
}
}
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
results.separate = 0;
results.pointer = 0;
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_string.c
--- a/src/njs_string.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_string.c Fri Apr 26 16:27:52 2024 -0700
@@ -3081,7 +3081,7 @@ njs_string_get_substitution(njs_vm_t *vm
p = rep.start;
end = rep.start + rep.length;
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
while (p < end) {
r = njs_strlchr(p, end, '$');
@@ -3379,7 +3379,7 @@ njs_string_prototype_replace(njs_vm_t *v
return NJS_OK;
}
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
p_start = string.start;
increment = s.length != 0 ? s.length : 1;
@@ -4033,7 +4033,7 @@ njs_string_decode_uri(njs_vm_t *vm, njs_
src = string.start;
end = string.start + string.size;
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
njs_utf8_decode_init(&ctx);
@@ -4307,7 +4307,7 @@ njs_string_atob(njs_vm_t *vm, njs_value_
len = njs_base64_decoded_length(len, pad);
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
dst = njs_chb_reserve(&chain, len * 2);
if (njs_slow_path(dst == NULL)) {
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_typed_array.c
--- a/src/njs_typed_array.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_typed_array.c Fri Apr 26 16:27:52 2024 -0700
@@ -2143,7 +2143,7 @@ njs_typed_array_prototype_join(njs_vm_t
return NJS_ERROR;
}
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
length = njs_typed_array_to_chain(vm, &chain, array, separator);
size = njs_chb_size(&chain);
diff -r a97a9f3b3cea -r 622387b5b612 src/njs_vm.c
--- a/src/njs_vm.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/njs_vm.c Fri Apr 26 16:27:52 2024 -0700
@@ -233,7 +233,7 @@ njs_vm_compile(njs_vm_t *vm, u_char **st
}
if (njs_slow_path(vm->options.ast)) {
- njs_chb_init(&chain, vm->mem_pool);
+ NJS_CHB_MP_INIT(&chain, vm);
ret = njs_parser_serialize_ast(parser.node, &chain);
if (njs_slow_path(ret == NJS_ERROR)) {
return ret;
diff -r a97a9f3b3cea -r 622387b5b612 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Mon Apr 22 18:59:03 2024 -0700
+++ b/src/test/njs_unit_test.c Fri Apr 26 16:27:52 2024 -0700
@@ -24477,7 +24477,7 @@ njs_chb_test(njs_vm_t *vm, njs_opts_t *o
static const njs_str_t expected = njs_str("arg: \"XYZ\" -5");
- njs_chb_init(&chain, njs_vm_memory_pool(vm));
+ NJS_CHB_MP_INIT(&chain, vm);
p = njs_chb_reserve(&chain, 513);
if (p == NULL) {
More information about the nginx-devel
mailing list