Skip to content

Commit b395a39

Browse files
committed
Use valueshashtable for modules and remove atomshashtable
Change signature of `globalcontext_get_module` to get an `atom_index_t` to refer to a given module. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent a7ee8b4 commit b395a39

14 files changed

+82
-366
lines changed

src/libAtomVM/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ project (libAtomVM)
2323

2424
set(HEADER_FILES
2525
atom.h
26-
atomshashtable.h
2726
atom_table.h
2827
avmpack.h
2928
bif.h
@@ -75,7 +74,6 @@ set(HEADER_FILES
7574

7675
set(SOURCE_FILES
7776
atom.c
78-
atomshashtable.c
7977
atom_table.c
8078
avmpack.c
8179
bif.c

src/libAtomVM/atom_table.c

+24
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,30 @@ atom_ref_t atom_table_get_atom_ptr_and_len(struct AtomTable *table, atom_index_t
267267
return node;
268268
}
269269

270+
char *atom_table_atom_to_new_cstring(struct AtomTable *table, atom_index_t atom_index, const char *suffix)
271+
{
272+
AtomString atom_string = atom_table_get_atom_string(table, atom_index);
273+
size_t atom_len = atom_string_len(atom_string);
274+
const uint8_t *atom_data = atom_string_data(atom_string);
275+
size_t suffix_len = 0;
276+
if (suffix) {
277+
suffix_len = strlen(suffix);
278+
}
279+
280+
char *result = malloc(atom_len + suffix_len + 1);
281+
if (IS_NULL_PTR(result)) {
282+
return NULL;
283+
}
284+
285+
memcpy(result, atom_data, atom_len);
286+
if (suffix) {
287+
memcpy(result + atom_len, suffix, suffix_len);
288+
}
289+
result[atom_len + suffix_len] = 0;
290+
291+
return result;
292+
}
293+
270294
bool atom_table_is_atom_ref_ascii(struct AtomTable *table, atom_ref_t atom)
271295
{
272296
SMP_RDLOCK(table);

src/libAtomVM/atom_table.h

+12
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ void atom_table_write_bytes(struct AtomTable *table, atom_ref_t atom, size_t buf
7777
void atom_table_write_cstring(
7878
struct AtomTable *table, atom_ref_t atom, size_t buf_len, char *outbuf);
7979

80+
/**
81+
* @brief Allocate a new C string with malloc with the representation of
82+
* an atom
83+
*
84+
* @param table atom table
85+
* @param atom_index index of the atom to get the representation of
86+
* @param suffix suffix to append to atom name or NULL of none
87+
* @return a newly allocated string with the representation of the atom or
88+
* NULL if allocation failed or atom index doesn't exist
89+
*/
90+
char *atom_table_atom_to_new_cstring(struct AtomTable *table, atom_index_t atom_index, const char *suffix);
91+
8092
#ifdef __cplusplus
8193
}
8294
#endif

src/libAtomVM/atomshashtable.c

-167
This file was deleted.

src/libAtomVM/atomshashtable.h

-58
This file was deleted.

src/libAtomVM/globalcontext.c

+6-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "globalcontext.h"
2626

2727
#include "atom_table.h"
28-
#include "atomshashtable.h"
2928
#include "avmpack.h"
3029
#include "context.h"
3130
#include "defaultatoms.h"
@@ -98,7 +97,7 @@ GlobalContext *globalcontext_new()
9897

9998
glb->modules_by_index = NULL;
10099
glb->loaded_modules_count = 0;
101-
glb->modules_table = atomshashtable_new();
100+
glb->modules_table = valueshashtable_new();
102101
if (IS_NULL_PTR(glb->modules_table)) {
103102
atom_table_destroy(glb->atom_table);
104103
free(glb);
@@ -637,9 +636,9 @@ term globalcontext_existing_term_from_atom_string(GlobalContext *glb, AtomString
637636

638637
int globalcontext_insert_module(GlobalContext *global, Module *module)
639638
{
639+
term module_name = module_get_name(module);
640640
SMP_RWLOCK_WRLOCK(global->modules_lock);
641-
AtomString module_name_atom = module_get_atom_string_by_id(module, 1, global);
642-
if (!atomshashtable_insert(global->modules_table, module_name_atom, TO_ATOMSHASHTABLE_VALUE(module))) {
641+
if (!valueshashtable_insert(global->modules_table, term_to_atom_index(module_name), TO_VALUESHASHTABLE_VALUE(module))) {
643642
SMP_RWLOCK_UNLOCK(global->modules_lock);
644643
return -1;
645644
}
@@ -695,18 +694,15 @@ Module *globalcontext_load_module_from_avm(GlobalContext *global, const char *mo
695694
return new_module;
696695
}
697696

698-
Module *globalcontext_get_module(GlobalContext *global, AtomString module_name_atom)
697+
Module *globalcontext_get_module(GlobalContext *global, atom_index_t module_name_atom)
699698
{
700-
Module *found_module = (Module *) atomshashtable_get_value(global->modules_table, module_name_atom, (unsigned long) NULL);
699+
Module *found_module = (Module *) valueshashtable_get_value(global->modules_table, module_name_atom, TO_VALUESHASHTABLE_VALUE(NULL));
701700

702701
if (!found_module) {
703-
char *module_name = malloc(256 + 5);
702+
char *module_name = atom_table_atom_to_new_cstring(global->atom_table, module_name_atom, ".beam");
704703
if (IS_NULL_PTR(module_name)) {
705704
return NULL;
706705
}
707-
708-
atom_string_to_c(module_name_atom, module_name, 256);
709-
strcat(module_name, ".beam");
710706
Module *loaded_module = globalcontext_load_module_from_avm(global, module_name);
711707
if (IS_NULL_PTR(loaded_module)) {
712708
// Platform may implement sys_load_module_from_file

src/libAtomVM/globalcontext.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "synclist.h"
4141
#include "term.h"
4242
#include "timer_list.h"
43+
#include "valueshashtable.h"
4344

4445
#ifdef __cplusplus
4546
extern "C" {
@@ -117,7 +118,7 @@ struct GlobalContext
117118
int32_t last_process_id;
118119

119120
struct AtomTable *atom_table;
120-
struct AtomsHashTable *modules_table;
121+
struct ValuesHashTable *modules_table;
121122

122123
#ifndef AVM_NO_SMP
123124
RWLock *modules_lock;
@@ -510,7 +511,7 @@ Module *globalcontext_get_module_by_index(GlobalContext *global, int index);
510511
* @param module_name_atom the module name.
511512
* @returns a pointer to a Module struct.
512513
*/
513-
Module *globalcontext_get_module(GlobalContext *global, AtomString module_name_atom);
514+
Module *globalcontext_get_module(GlobalContext *global, atom_index_t module_name_atom);
514515

515516
/**
516517
* @brief Load a given module from registered AVM packs

src/libAtomVM/module.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ const struct ExportedFunction *module_resolve_function0(Module *mod, int import_
432432
AtomString function_name_atom = atom_table_get_atom_string(glb->atom_table, unresolved->function_atom_index);
433433
int arity = unresolved->arity;
434434

435-
Module *found_module = globalcontext_get_module(glb, module_name_atom);
435+
Module *found_module = globalcontext_get_module(glb, unresolved->module_atom_index);
436436

437437
if (LIKELY(found_module != NULL)) {
438438
int exported_label = module_search_exported_function(found_module, function_name_atom, arity, glb);

src/libAtomVM/module.h

-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@
3333

3434
#include "atom.h"
3535
#include "atom_table.h"
36-
#include "atomshashtable.h"
3736
#include "context.h"
3837
#include "exportedfunction.h"
3938
#include "globalcontext.h"
4039
#include "term.h"
41-
#include "valueshashtable.h"
4240

4341
#ifdef __cplusplus
4442
extern "C" {

0 commit comments

Comments
 (0)