From cfcd05773b00700c34b12b04ccbbf3a7248351ec Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Wed, 9 Oct 2024 08:47:33 -0700 Subject: [PATCH] refactor(*) generalize ngx_str_node_t and ngx_str_t extensions To use them in IPC code. --- src/ngx_wasmx.h | 52 ++++++++++++++++++++++++++++++++++++++++++ src/wasm/ngx_wasm.h | 45 +++--------------------------------- src/wasm/vm/ngx_wavm.c | 20 ++++++++-------- 3 files changed, 65 insertions(+), 52 deletions(-) diff --git a/src/ngx_wasmx.h b/src/ngx_wasmx.h index 2abe681b0..58fe1cfc7 100644 --- a/src/ngx_wasmx.h +++ b/src/ngx_wasmx.h @@ -24,6 +24,10 @@ #define NGX_WA_METRICS_DEFAULT_MAX_NAME_LEN 256 #define NGX_WA_METRICS_DEFAULT_SLAB_SIZE 1024 * 1024 * 5 /* 5 MiB */ + +/* ngx_wasmx_module */ + + #define ngx_wa_cycle_get_conf(cycle) \ (ngx_wa_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_wasmx_module) @@ -54,4 +58,52 @@ extern ngx_uint_t ngx_wasm_max_module; extern ngx_uint_t ngx_ipc_max_module; +/* ngx_str_node_t extensions */ + + +#define ngx_wa_sn_insert(rbtree, sn) \ + ngx_rbtree_insert((rbtree), &(sn)->node) + +#define ngx_wa_sn_init(sn, s) \ + (sn)->node.key = ngx_crc32_long((s)->data, (s)->len); \ + (sn)->str.len = (s)->len; \ + (sn)->str.data = (s)->data + +#define ngx_wa_sn_n2sn(n) \ + (ngx_str_node_t *) ((u_char *) (n) - offsetof(ngx_str_node_t, node)) + + +static ngx_inline ngx_str_node_t * +ngx_wa_sn_lookup(ngx_rbtree_t *rbtree, ngx_str_t *str) +{ + uint32_t hash; + + hash = ngx_crc32_long(str->data, str->len); + + return ngx_str_rbtree_lookup(rbtree, str, hash); +} + + +/* ngx_str_t extensions */ + + +static ngx_inline unsigned +ngx_str_eq(const void *s1, ngx_int_t s1_len, const void *s2, ngx_int_t s2_len) +{ + if (s1_len < 0) { + s1_len = ngx_strlen((const char *) s1); + } + + if (s2_len < 0) { + s2_len = ngx_strlen((const char *) s2); + } + + if (s1_len != s2_len) { + return 0; + } + + return ngx_memcmp(s1, s2, s2_len) == 0; +} + + #endif /* _NGX_WA_H_INCLUDED_ */ diff --git a/src/wasm/ngx_wasm.h b/src/wasm/ngx_wasm.h index 01554203a..db8d2986f 100644 --- a/src/wasm/ngx_wasm.h +++ b/src/wasm/ngx_wasm.h @@ -45,12 +45,15 @@ #define ngx_wasm_vec_set_i32(vec, i, v) \ (((wasm_val_vec_t *) (vec))->data[i].kind = WASM_I32); \ ((wasm_val_vec_t *) (vec))->data[i].of.i32 = v + #define ngx_wasm_vec_set_i64(vec, i, v) \ (((wasm_val_vec_t *) (vec))->data[i].kind = WASM_I64); \ ((wasm_val_vec_t *) (vec))->data[i].of.i64 = v + #define ngx_wasm_vec_set_f32(vec, i, v) \ (((wasm_val_vec_t *) (vec))->data[i].kind = WASM_F32); \ ((wasm_val_vec_t *) (vec))->data[i].of.f32 = v + #define ngx_wasm_vec_set_f64(vec, i, v) \ (((wasm_val_vec_t *) (vec))->data[i].kind = WASM_F64); \ ((wasm_val_vec_t *) (vec))->data[i].of.f64 = v @@ -155,48 +158,6 @@ char *ngx_wasm_core_pwm_lua_resolver_directive(ngx_conf_t *cf, extern ngx_module_t ngx_wasm_core_module; -/* ngx_str_node_t extensions */ -#define ngx_wasm_sn_insert(rbtree, sn) \ - ngx_rbtree_insert((rbtree), &(sn)->node) -#define ngx_wasm_sn_init(sn, s) \ - (sn)->node.key = ngx_crc32_long((s)->data, (s)->len); \ - (sn)->str.len = (s)->len; \ - (sn)->str.data = (s)->data -#define ngx_wasm_sn_n2sn(n) \ - (ngx_str_node_t *) ((u_char *) (n) - offsetof(ngx_str_node_t, node)) - - -static ngx_inline ngx_str_node_t * -ngx_wasm_sn_lookup(ngx_rbtree_t *rbtree, ngx_str_t *str) -{ - uint32_t hash; - - hash = ngx_crc32_long(str->data, str->len); - - return ngx_str_rbtree_lookup(rbtree, str, hash); -} - - -/* ngx_str_t extensions */ -static ngx_inline unsigned -ngx_str_eq(const void *s1, ngx_int_t s1_len, const void *s2, ngx_int_t s2_len) -{ - if (s1_len < 0) { - s1_len = ngx_strlen((const char *) s1); - } - - if (s2_len < 0) { - s2_len = ngx_strlen((const char *) s2); - } - - if (s1_len != s2_len) { - return 0; - } - - return ngx_memcmp(s1, s2, s2_len) == 0; -} - - /* subsystems & phases */ typedef enum { NGX_WASM_SUBSYS_HTTP = 1, diff --git a/src/wasm/vm/ngx_wavm.c b/src/wasm/vm/ngx_wavm.c index a2a62a1b7..8c576b701 100644 --- a/src/wasm/vm/ngx_wavm.c +++ b/src/wasm/vm/ngx_wavm.c @@ -193,7 +193,7 @@ ngx_wavm_init(ngx_wavm_t *vm) node; node = ngx_rbtree_next(&vm->modules_tree, node)) { - sn = ngx_wasm_sn_n2sn(node); + sn = ngx_wa_sn_n2sn(node); module = ngx_rbtree_data(&sn->node, ngx_wavm_module_t, sn); rc = ngx_wavm_module_load_bytes(module); @@ -243,7 +243,7 @@ ngx_wavm_load(ngx_wavm_t *vm) node; node = ngx_rbtree_next(&vm->modules_tree, node)) { - sn = ngx_wasm_sn_n2sn(node); + sn = ngx_wa_sn_n2sn(node); module = ngx_rbtree_data(&sn->node, ngx_wavm_module_t, sn); if (ngx_wavm_module_load(module) != NGX_OK) { @@ -307,7 +307,7 @@ ngx_wavm_destroy(ngx_wavm_t *vm) while (*root != *sentinel) { node = ngx_rbtree_min(*root, *sentinel); - sn = ngx_wasm_sn_n2sn(node); + sn = ngx_wa_sn_n2sn(node); module = ngx_rbtree_data(&sn->node, ngx_wavm_module_t, sn); ngx_rbtree_delete(&vm->modules_tree, node); @@ -330,7 +330,7 @@ ngx_wavm_module_lookup(ngx_wavm_t *vm, ngx_str_t *name) { ngx_str_node_t *sn; - sn = ngx_wasm_sn_lookup(&vm->modules_tree, name); + sn = ngx_wa_sn_lookup(&vm->modules_tree, name); if (sn == NULL) { return NULL; } @@ -407,8 +407,8 @@ ngx_wavm_module_add(ngx_wavm_t *vm, ngx_str_t *name, ngx_str_t *path, } } - ngx_wasm_sn_init(&module->sn, &module->name); - ngx_wasm_sn_insert(&vm->modules_tree, &module->sn); + ngx_wa_sn_init(&module->sn, &module->name); + ngx_wa_sn_insert(&vm->modules_tree, &module->sn); return NGX_OK; @@ -603,8 +603,8 @@ ngx_wavm_module_load(ngx_wavm_module_t *module) funcref->name.len); *p = '\0'; - ngx_wasm_sn_init(&funcref->sn, &funcref->name); - ngx_wasm_sn_insert(&module->funcs_tree, &funcref->sn); + ngx_wa_sn_init(&funcref->sn, &funcref->name); + ngx_wa_sn_insert(&module->funcs_tree, &funcref->sn); if (module->f_start == NULL && (ngx_str_eq(exportname->data, exportname->size, "_start", -1) @@ -812,7 +812,7 @@ ngx_wavm_module_destroy(ngx_wavm_module_t *module) while (*root != *sentinel) { node = ngx_rbtree_min(*root, *sentinel); - sn = ngx_wasm_sn_n2sn(node); + sn = ngx_wa_sn_n2sn(node); funcref = ngx_rbtree_data(&sn->node, ngx_wavm_funcref_t, sn); ngx_rbtree_delete(&module->funcs_tree, node); @@ -863,7 +863,7 @@ ngx_wavm_module_func_lookup(ngx_wavm_module_t *module, ngx_str_t *name) return NULL; } - sn = ngx_wasm_sn_lookup(&module->funcs_tree, name); + sn = ngx_wa_sn_lookup(&module->funcs_tree, name); if (sn == NULL) { return NULL; }