Skip to content

Commit c67a045

Browse files
committed
Introduce AtomIndex type
Also introduce a type for the result of atom_table_ensure_atoms, which isn't an AtomIndex. Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent fc81cde commit c67a045

File tree

7 files changed

+47
-35
lines changed

7 files changed

+47
-35
lines changed

src/libAtomVM/atom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extern "C" {
4545
#define ATOM_STR(LENSTR, STR) (LENSTR STR)
4646

4747
typedef const void *AtomString;
48+
typedef uint32_t AtomIndex;
4849

4950
/**
5051
* @brief Gets a C string from an AtomString

src/libAtomVM/atom_table.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static inline struct HNode *get_node(const struct AtomTable *hash_table, AtomStr
188188
return get_node_with_hash(hash_table, string, hash);
189189
}
190190

191-
long atom_table_get_index(struct AtomTable *table, AtomString string)
191+
AtomIndex atom_table_get_index(struct AtomTable *table, AtomString string)
192192
{
193193
unsigned long hash = sdbm_hash(string, atom_string_len(string));
194194

@@ -202,7 +202,7 @@ long atom_table_get_index(struct AtomTable *table, AtomString string)
202202
}
203203

204204
// TODO: this function needs use an efficient structure such as a skip list
205-
static struct HNode *get_node_using_index(struct AtomTable *table, long index)
205+
static struct HNode *get_node_using_index(struct AtomTable *table, AtomIndex index)
206206
{
207207
if (UNLIKELY(((size_t) index) >= table->count)) {
208208
return NULL;
@@ -221,7 +221,7 @@ static struct HNode *get_node_using_index(struct AtomTable *table, long index)
221221
return NULL;
222222
}
223223

224-
AtomString atom_table_get_atom_string(struct AtomTable *table, long index)
224+
AtomString atom_table_get_atom_string(struct AtomTable *table, AtomIndex index)
225225
{
226226
SMP_RDLOCK(table);
227227

@@ -264,7 +264,7 @@ int atom_table_cmp_using_atom_index(struct AtomTable *table, int t_atom_index, i
264264
return memcmp_result;
265265
}
266266

267-
atom_ref_t atom_table_get_atom_ptr_and_len(struct AtomTable *table, long index, size_t *out_len)
267+
atom_ref_t atom_table_get_atom_ptr_and_len(struct AtomTable *table, AtomIndex index, size_t *out_len)
268268
{
269269
SMP_RDLOCK(table);
270270

@@ -398,7 +398,7 @@ static inline bool maybe_rehash(struct AtomTable *table, int new_entries)
398398
return do_rehash(table, new_capacity);
399399
}
400400

401-
long atom_table_ensure_atom(struct AtomTable *table, AtomString string, enum AtomTableCopyOpt opts)
401+
AtomIndex atom_table_ensure_atom(struct AtomTable *table, AtomString string, enum AtomTableCopyOpt opts)
402402
{
403403
unsigned long hash = sdbm_hash(string, atom_string_len(string));
404404
SMP_WRLOCK(table);
@@ -463,8 +463,8 @@ static inline int read_encoded_len(const uint8_t **len_bytes)
463463
}
464464
}
465465

466-
int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int count,
467-
int *translate_table, enum EnsureAtomsOpt opt)
466+
enum AtomTableEnsureAtomsResult atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int count,
467+
AtomIndex *translate_table, enum EnsureAtomsOpt opt)
468468
{
469469
bool is_long_format = (opt & EnsureLongEncoding) != 0;
470470

@@ -481,7 +481,7 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
481481
if (UNLIKELY(atom_len < 0)) {
482482
fprintf(stderr, "Found invalid atom len.");
483483
SMP_UNLOCK(table);
484-
return ATOM_TABLE_INVALID_LEN;
484+
return AtomTableEnsureAtomsInvalidLen;
485485
} else if (UNLIKELY(atom_len > 255)) {
486486
fprintf(stderr,
487487
"Unsupported atom length %i bytes.\n"
@@ -491,7 +491,7 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
491491
"https://github.com/atomvm/AtomVM/issues\n",
492492
atom_len);
493493
SMP_UNLOCK(table);
494-
return ATOM_TABLE_INVALID_LEN;
494+
return AtomTableEnsureAtomsInvalidLen;
495495
}
496496
char tmp_old_fmt[256];
497497
tmp_old_fmt[0] = atom_len;
@@ -536,7 +536,7 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
536536
node_group = new_node_group(table, remaining_atoms);
537537
if (IS_NULL_PTR(node_group)) {
538538
SMP_UNLOCK(table);
539-
return ATOM_TABLE_ALLOC_FAIL;
539+
return AtomTableEnsureAtomsAllocFail;
540540
}
541541
}
542542

@@ -545,7 +545,7 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
545545
if (IS_NULL_PTR(atom_copy)) {
546546
// we are not going to remove atoms that have already been added up to this one
547547
SMP_UNLOCK(table);
548-
return ATOM_TABLE_ALLOC_FAIL;
548+
return AtomTableEnsureAtomsAllocFail;
549549
}
550550
atom_copy[0] = atom_len;
551551
memcpy(atom_copy + 1, to_be_copied, atom_len);
@@ -566,5 +566,5 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
566566

567567
SMP_UNLOCK(table);
568568

569-
return 0;
569+
return AtomTableEnsureAtomsOk;
570570
}

src/libAtomVM/atom_table.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@
2929
extern "C" {
3030
#endif
3131

32-
#define ATOM_TABLE_NOT_FOUND -1
33-
#define ATOM_TABLE_ALLOC_FAIL -2
34-
#define ATOM_TABLE_INVALID_LEN -3
32+
#define ATOM_TABLE_NOT_FOUND ((AtomIndex) -1)
33+
#define ATOM_TABLE_ALLOC_FAIL ((AtomIndex) -2)
3534

3635
struct AtomTable;
3736

@@ -48,27 +47,34 @@ enum AtomTableCopyOpt
4847
AtomTableAlreadyExisting = 2
4948
};
5049

50+
enum AtomTableEnsureAtomsResult
51+
{
52+
AtomTableEnsureAtomsOk = 0,
53+
AtomTableEnsureAtomsAllocFail = -2,
54+
AtomTableEnsureAtomsInvalidLen = -3,
55+
};
56+
5157
typedef const void *atom_ref_t;
5258

5359
struct AtomTable *atom_table_new();
5460
void atom_table_destroy(struct AtomTable *table);
5561

5662
size_t atom_table_count(struct AtomTable *table);
5763

58-
long atom_table_ensure_atom(struct AtomTable *table, AtomString string, enum AtomTableCopyOpt opts);
64+
AtomIndex atom_table_ensure_atom(struct AtomTable *table, AtomString string, enum AtomTableCopyOpt opts);
5965

6066
// This function is deprecated and it will be removed.
6167
// atom strings should be copied to caller owned buffers.
62-
AtomString atom_table_get_atom_string(struct AtomTable *table, long index);
68+
AtomString atom_table_get_atom_string(struct AtomTable *table, AtomIndex index);
6369

64-
long atom_table_get_index(struct AtomTable *table, AtomString string);
70+
AtomIndex atom_table_get_index(struct AtomTable *table, AtomString string);
6571

66-
int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int count,
67-
int *translate_table, enum EnsureAtomsOpt opts);
72+
enum AtomTableEnsureAtomsResult atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int count,
73+
AtomIndex *translate_table, enum EnsureAtomsOpt opts);
6874

6975
int atom_table_cmp_using_atom_index(
7076
struct AtomTable *table, int t_atom_index, int other_atom_index);
71-
atom_ref_t atom_table_get_atom_ptr_and_len(struct AtomTable *table, long index, size_t *out_len);
77+
atom_ref_t atom_table_get_atom_ptr_and_len(struct AtomTable *table, AtomIndex index, size_t *out_len);
7278
bool atom_table_is_atom_ref_ascii(struct AtomTable *table, atom_ref_t atom);
7379
void atom_table_write_bytes(struct AtomTable *table, atom_ref_t atom, size_t buf_len, void *outbuf);
7480
void atom_table_write_cstring(

src/libAtomVM/module.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,20 @@ static enum ModuleLoadResult module_populate_atoms_table(Module *this_module, ui
8181
return MODULE_ERROR_FAILED_ALLOCATION;
8282
}
8383

84-
long ensure_result = atom_table_ensure_atoms(
84+
enum AtomTableEnsureAtomsResult ensure_result = atom_table_ensure_atoms(
8585
glb->atom_table, current_atom, atoms_count, this_module->local_atoms_to_global_table + 1, ensure_opts);
86-
if (UNLIKELY(ensure_result == ATOM_TABLE_ALLOC_FAIL)) {
87-
fprintf(stderr, "Cannot allocate memory while loading module (line: %i).\n", __LINE__);
88-
return MODULE_ERROR_FAILED_ALLOCATION;
89-
} else if (UNLIKELY(ensure_result == ATOM_TABLE_INVALID_LEN)) {
90-
return MODULE_ERROR_INVALID;
86+
switch (ensure_result) {
87+
case AtomTableEnsureAtomsAllocFail: {
88+
fprintf(stderr, "Cannot allocate memory while loading module (line: %i).\n", __LINE__);
89+
return MODULE_ERROR_FAILED_ALLOCATION;
90+
}
91+
case AtomTableEnsureAtomsInvalidLen: {
92+
return MODULE_ERROR_INVALID;
93+
}
94+
case AtomTableEnsureAtomsOk: {
95+
return MODULE_LOAD_OK;
96+
}
9197
}
92-
93-
return MODULE_LOAD_OK;
9498
}
9599

96100
static enum ModuleLoadResult module_build_imported_functions_table(Module *this_module, uint8_t *table_data, GlobalContext *glb)

src/libAtomVM/module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct Module
123123

124124
struct LiteralEntry *literals_table;
125125

126-
int *local_atoms_to_global_table;
126+
AtomIndex *local_atoms_to_global_table;
127127

128128
void *module_platform_data;
129129

src/libAtomVM/term.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <stdlib.h>
3838
#include <string.h>
3939

40+
#include "atom.h"
4041
#include "memory.h"
4142
#include "refc_binary.h"
4243
#include "utils.h"
@@ -599,7 +600,7 @@ static inline term term_nil()
599600
* @param t the term that will be converted to atom table index. t must be a valid atom term.
600601
* @return a global atom table index.
601602
*/
602-
static inline int term_to_atom_index(term t)
603+
static inline AtomIndex term_to_atom_index(term t)
603604
{
604605
return t >> 6;
605606
}
@@ -611,7 +612,7 @@ static inline int term_to_atom_index(term t)
611612
* @param atom_index global atoms table index.
612613
* @return a term that encapsulates the atom.
613614
*/
614-
static inline term term_from_atom_index(int atom_index)
615+
static inline term term_from_atom_index(AtomIndex atom_index)
615616
{
616617
return TERM_FROM_ATOM_INDEX(atom_index);
617618
}

tests/test-structs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ void test_valueshashtable()
274274
}
275275
}
276276

277-
int insert_atoms_into_atom_table(struct AtomTable *table)
277+
AtomIndex insert_atoms_into_atom_table(struct AtomTable *table)
278278
{
279-
int decimals_index;
279+
AtomIndex decimals_index;
280280

281281
atom_table_ensure_atom(table, false_atom, AtomTableNoOpts);
282282
atom_table_ensure_atom(table, true_atom, AtomTableNoOpts);
@@ -431,7 +431,7 @@ void test_atom_table()
431431

432432
assert(((char *) atom_table_get_atom_string(table, 0))[0] == 3);
433433

434-
int decimals_index = insert_atoms_into_atom_table(table);
434+
AtomIndex decimals_index = insert_atoms_into_atom_table(table);
435435

436436
assert(atom_table_get_index(table, "\x8" "decimals") == decimals_index);
437437
assert(atom_table_ensure_atom(table, "\x8" "decimals", AtomTableNoOpts) == decimals_index);

0 commit comments

Comments
 (0)