Skip to content

Commit

Permalink
define vm->atom_hash* and manipualtion of them.
Browse files Browse the repository at this point in the history
-- added njs_flathsh_alloc_copy(mp, to, from);
-- added atom_hash_* fields in njs_vm_t;
-- added njs_flathsh_alloc_copy(mp, to, from);
-- added vm_parent for using atom_hash from parent_vm for preload_object case;
-- added code for using correct atom_hash and atom_hash_shared in vm and cloned vm.
  • Loading branch information
VadimZhestikov committed Oct 5, 2024
1 parent 398acbd commit 36a797e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/njs.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,14 @@ typedef njs_int_t (*njs_iterator_handler_t)(njs_vm_t *vm,


NJS_EXPORT void njs_vm_opt_init(njs_vm_opt_t *options);
NJS_EXPORT njs_vm_t *njs_vm_create_parent(njs_vm_opt_t *options,
njs_vm_t *vm_parent);
NJS_EXPORT njs_vm_t *njs_vm_create(njs_vm_opt_t *options);
NJS_EXPORT void njs_vm_destroy(njs_vm_t *vm);

NJS_EXPORT njs_int_t njs_vm_compile(njs_vm_t *vm, u_char **start, u_char *end);
NJS_EXPORT void njs_vm_set_module_loader(njs_vm_t *vm,
njs_module_loader_t module_loader, void *opaque);
njs_module_loader_t module_loader, void *opaque);
NJS_EXPORT njs_mod_t *njs_vm_add_module(njs_vm_t *vm, njs_str_t *name,
njs_value_t *value);
NJS_EXPORT njs_mod_t *njs_vm_compile_module(njs_vm_t *vm, njs_str_t *name,
Expand All @@ -314,7 +316,7 @@ NJS_EXPORT njs_int_t njs_vm_execute_pending_job(njs_vm_t *vm);
NJS_EXPORT njs_int_t njs_vm_pending(njs_vm_t *vm);

NJS_EXPORT void njs_vm_set_rejection_tracker(njs_vm_t *vm,
njs_rejection_tracker_t rejection_tracker, void *opaque);
njs_rejection_tracker_t rejection_tracker, void *opaque);

/*
* Runs the specified function with provided arguments.
Expand Down
1 change: 1 addition & 0 deletions src/njs_atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ void njs_atom_hash_init(void);

extern njs_atom_values_t njs_atom;
extern njs_flathsh_t njs_atom_hash;
extern uint32_t njs_atom_hash_atom_id;

#endif /* _NJS_ATOM_H_INCLUDED_ */
23 changes: 22 additions & 1 deletion src/njs_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ njs_object_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash,


njs_int_t
njs_builtin_objects_create(njs_vm_t *vm)
njs_builtin_objects_create(njs_vm_t *vm, njs_vm_t *vm_parent)
{
njs_int_t ret, index;
njs_uint_t i;
Expand All @@ -127,10 +127,31 @@ njs_builtin_objects_create(njs_vm_t *vm)
vm->shared = shared;

njs_lvlhsh_init(&shared->keywords_hash);

njs_lvlhsh_init(&shared->values_hash);

njs_atom_hash_init();

if (vm_parent == NULL) {
/* njs_lvlhsh_init(&vm->atom_hash_shared); // done by zalign */

ret = njs_flathsh_alloc_copy(vm->mem_pool, &vm->atom_hash,
&njs_atom_hash);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
vm->atom_hash_mem_pool = vm->mem_pool;
vm->atom_hash_atom_id = njs_atom_hash_atom_id;

} else {
vm->atom_hash_shared = vm_parent->atom_hash_shared;

vm->atom_hash = vm_parent->atom_hash;
vm->atom_hash_mem_pool = vm_parent->mem_pool;
vm->atom_hash_atom_id = vm_parent->atom_hash_atom_id;

}

pattern = njs_regexp_pattern_create(vm, (u_char *) "(?:)",
njs_length("(?:)"), 0);
if (njs_slow_path(pattern == NULL)) {
Expand Down
27 changes: 27 additions & 0 deletions src/njs_flathsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,30 @@ njs_flathsh_each(const njs_flathsh_t *fh, njs_flathsh_each_t *fhe)

return NULL;
}


njs_int_t
njs_flathsh_alloc_copy(njs_mp_t *mp, njs_flathsh_t *to, njs_flathsh_t *from)
{
void *from_chunk, *to_chunk;
uint32_t from_size;
njs_flathsh_descr_t *from_descr;

from_descr = from->slot;

from_size = (from_descr->hash_mask + 1ul) + sizeof(njs_flathsh_descr_t) +
sizeof(uint32_t) * from_descr->elts_size;

to_chunk = njs_mp_alloc(mp, from_size);
if (njs_slow_path(to_chunk == NULL)) {
return NJS_ERROR;
}

from_chunk = njs_flathsh_chunk(from_descr);

memcpy(to_chunk, from_chunk, from_size);

to->slot = (char *)to_chunk + ((char *)from_descr - (char *)from_chunk);

return NJS_OK;
}
3 changes: 3 additions & 0 deletions src/njs_flathsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ NJS_EXPORT njs_flathsh_elt_t *njs_flathsh_add_elt(njs_flathsh_t *fh,
NJS_EXPORT njs_flathsh_descr_t *njs_flathsh_new(njs_flathsh_query_t *fhq);
NJS_EXPORT void njs_flathsh_destroy(njs_flathsh_t *fh, njs_flathsh_query_t *fhq);

NJS_EXPORT njs_int_t njs_flathsh_alloc_copy(njs_mp_t *mp, njs_flathsh_t *to,
njs_flathsh_t *from);


/* Temporary backward compatibility .*/

Expand Down
2 changes: 1 addition & 1 deletion src/njs_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
#include <njs_murmur_hash.h>
#include <njs_trace.h>
#include <njs_queue.h>
#include <njs_flathsh.h>
#include <njs_random.h>
#include <njs_malloc.h>
#include <njs_rbtree.h>
#include <njs_mp.h>
#include <njs_flathsh.h>
#include <njs_arr.h>
#include <njs_chb.h>
#include <njs_utils.h>
Expand Down
20 changes: 18 additions & 2 deletions src/njs_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ njs_vm_opt_init(njs_vm_opt_t *options)


njs_vm_t *
njs_vm_create(njs_vm_opt_t *options)
njs_vm_create_parent(njs_vm_opt_t *options, njs_vm_t *vm_parent)
{
njs_mp_t *mp;
njs_vm_t *vm;
Expand Down Expand Up @@ -62,12 +62,14 @@ njs_vm_create(njs_vm_opt_t *options)
vm->shared = options->shared;

} else {
ret = njs_builtin_objects_create(vm);
ret = njs_builtin_objects_create(vm, vm_parent);
if (njs_slow_path(ret != NJS_OK)) {
return NULL;
}
}

//vm->is_cloned = 0; //??

vm->external = options->external;

vm->spare_stack_size = options->max_stack_size;
Expand Down Expand Up @@ -148,6 +150,13 @@ njs_vm_create(njs_vm_opt_t *options)
}


njs_vm_t *
njs_vm_create(njs_vm_opt_t *options)
{
return njs_vm_create_parent(options, NULL);
}


njs_int_t
njs_vm_ctor_push(njs_vm_t *vm)
{
Expand Down Expand Up @@ -408,6 +417,13 @@ njs_vm_clone(njs_vm_t *vm, njs_external_ptr_t external)
nvm->trace.data = nvm;
nvm->external = external;

nvm->atom_hash_shared = vm->atom_hash;

njs_lvlhsh_init(&nvm->atom_hash);
nvm->atom_hash_mem_pool = nvm->mem_pool;
nvm->atom_hash_atom_id = vm->atom_hash_atom_id;
//?? nvm->is_clone = 1;

ret = njs_vm_runtime_init(nvm);
if (njs_slow_path(ret != NJS_OK)) {
goto fail;
Expand Down
7 changes: 6 additions & 1 deletion src/njs_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ struct njs_vm_s {
njs_native_frame_t *top_frame;
njs_frame_t *active_frame;

njs_lvlhsh_t atom_hash_shared;
njs_lvlhsh_t atom_hash;
njs_mp_t *atom_hash_mem_pool;
uint32_t atom_hash_atom_id;

njs_lvlhsh_t keywords_hash;
njs_lvlhsh_t values_hash;

Expand Down Expand Up @@ -243,7 +248,7 @@ void njs_vm_constructors_init(njs_vm_t *vm);
njs_value_t njs_vm_exception(njs_vm_t *vm);
void njs_vm_scopes_restore(njs_vm_t *vm, njs_native_frame_t *frame);

njs_int_t njs_builtin_objects_create(njs_vm_t *vm);
njs_int_t njs_builtin_objects_create(njs_vm_t *vm, njs_vm_t *vm_parent);
njs_int_t njs_builtin_match_native_function(njs_vm_t *vm,
njs_function_t *function, njs_str_t *name);

Expand Down

0 comments on commit 36a797e

Please sign in to comment.