Skip to content

Commit 11a5645

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 f26daf0 commit 11a5645

14 files changed

+74
-365
lines changed

src/libAtomVM/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
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
@@ -74,7 +73,6 @@ set(HEADER_FILES
7473

7574
set(SOURCE_FILES
7675
atom.c
77-
atomshashtable.c
7876
atom_table.c
7977
avmpack.c
8078
bif.c

src/libAtomVM/atom_table.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,23 @@ 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)
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+
276+
char *result = malloc(atom_len + 1);
277+
if (IS_NULL_PTR(result)) {
278+
return NULL;
279+
}
280+
281+
memcpy(result, atom_data, atom_len);
282+
result[atom_len] = 0;
283+
284+
return result;
285+
}
286+
270287
bool atom_table_is_atom_ref_ascii(struct AtomTable *table, atom_ref_t atom)
271288
{
272289
SMP_RDLOCK(table);

src/libAtomVM/atom_table.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ 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+
* @return a newly allocated string with the representation of the atom or
87+
* NULL if allocation failed or atom index doesn't exist
88+
*/
89+
char *atom_table_atom_to_new_cstring(struct AtomTable *table, atom_index_t atom_index);
90+
8091
#ifdef __cplusplus
8192
}
8293
#endif

src/libAtomVM/atomshashtable.c

Lines changed: 0 additions & 167 deletions
This file was deleted.

src/libAtomVM/atomshashtable.h

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/libAtomVM/globalcontext.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "globalcontext.h"
2525

2626
#include "atom_table.h"
27-
#include "atomshashtable.h"
2827
#include "avmpack.h"
2928
#include "context.h"
3029
#include "defaultatoms.h"
@@ -96,7 +95,7 @@ GlobalContext *globalcontext_new()
9695

9796
glb->modules_by_index = NULL;
9897
glb->loaded_modules_count = 0;
99-
glb->modules_table = atomshashtable_new();
98+
glb->modules_table = valueshashtable_new();
10099
if (IS_NULL_PTR(glb->modules_table)) {
101100
atom_table_destroy(glb->atom_table);
102101
free(glb);
@@ -601,9 +600,9 @@ term globalcontext_existing_term_from_atom_string(GlobalContext *glb, AtomString
601600

602601
int globalcontext_insert_module(GlobalContext *global, Module *module)
603602
{
603+
term module_name = module_get_name(module);
604604
SMP_RWLOCK_WRLOCK(global->modules_lock);
605-
AtomString module_name_atom = module_get_atom_string_by_id(module, 1, global);
606-
if (!atomshashtable_insert(global->modules_table, module_name_atom, TO_ATOMSHASHTABLE_VALUE(module))) {
605+
if (!valueshashtable_insert(global->modules_table, term_to_atom_index(module_name), TO_VALUESHASHTABLE_VALUE(module))) {
607606
SMP_RWLOCK_UNLOCK(global->modules_lock);
608607
return -1;
609608
}
@@ -659,17 +658,15 @@ Module *globalcontext_load_module_from_avm(GlobalContext *global, const char *mo
659658
return new_module;
660659
}
661660

662-
Module *globalcontext_get_module(GlobalContext *global, AtomString module_name_atom)
661+
Module *globalcontext_get_module(GlobalContext *global, atom_index_t module_name_atom)
663662
{
664-
Module *found_module = (Module *) atomshashtable_get_value(global->modules_table, module_name_atom, (unsigned long) NULL);
663+
Module *found_module = (Module *) valueshashtable_get_value(global->modules_table, module_name_atom, TO_VALUESHASHTABLE_VALUE(NULL));
665664

666665
if (!found_module) {
667-
char *module_name = malloc(256 + 5);
666+
char *module_name = atom_table_atom_to_new_cstring(global->atom_table, module_name_atom);
668667
if (IS_NULL_PTR(module_name)) {
669668
return NULL;
670669
}
671-
672-
atom_string_to_c(module_name_atom, module_name, 256);
673670
strcat(module_name, ".beam");
674671
Module *loaded_module = globalcontext_load_module_from_avm(global, module_name);
675672
if (IS_NULL_PTR(loaded_module)) {

src/libAtomVM/globalcontext.h

Lines changed: 3 additions & 2 deletions
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" {
@@ -112,7 +113,7 @@ struct GlobalContext
112113
int32_t last_process_id;
113114

114115
struct AtomTable *atom_table;
115-
struct AtomsHashTable *modules_table;
116+
struct ValuesHashTable *modules_table;
116117

117118
#ifndef AVM_NO_SMP
118119
RWLock *modules_lock;
@@ -469,7 +470,7 @@ Module *globalcontext_get_module_by_index(GlobalContext *global, int index);
469470
* @param module_name_atom the module name.
470471
* @returns a pointer to a Module struct.
471472
*/
472-
Module *globalcontext_get_module(GlobalContext *global, AtomString module_name_atom);
473+
Module *globalcontext_get_module(GlobalContext *global, atom_index_t module_name_atom);
473474

474475
/**
475476
* @brief Load a given module from registered AVM packs

src/libAtomVM/module.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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)