Skip to content

Commit

Permalink
refactor(*) generalize ngx_str_node_t and ngx_str_t extensions
Browse files Browse the repository at this point in the history
To use them in IPC code.
  • Loading branch information
thibaultcha committed Oct 9, 2024
1 parent c46ff1c commit e7d2577
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 52 deletions.
52 changes: 52 additions & 0 deletions src/ngx_wasmx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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_ */
45 changes: 3 additions & 42 deletions src/wasm/ngx_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 10 additions & 10 deletions src/wasm/vm/ngx_wavm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit e7d2577

Please sign in to comment.