@@ -50,13 +50,13 @@ struct HNode
50
50
{
51
51
struct HNode * next ;
52
52
AtomString key ;
53
- long index ;
53
+ atom_index_t index ;
54
54
};
55
55
56
56
struct HNodeGroup
57
57
{
58
58
struct HNodeGroup * next ;
59
- long first_index ;
59
+ atom_index_t first_index ;
60
60
uint16_t len ;
61
61
62
62
struct HNode nodes [];
@@ -188,29 +188,16 @@ static inline struct HNode *get_node(const struct AtomTable *hash_table, AtomStr
188
188
return get_node_with_hash (hash_table , string , hash );
189
189
}
190
190
191
- long atom_table_get_index (struct AtomTable * table , AtomString string )
192
- {
193
- unsigned long hash = sdbm_hash (string , atom_string_len (string ));
194
-
195
- SMP_RDLOCK (table );
196
-
197
- struct HNode * node = get_node_with_hash (table , string , hash );
198
- long result = (node != NULL ) ? node -> index : ATOM_TABLE_NOT_FOUND ;
199
-
200
- SMP_UNLOCK (table );
201
- return result ;
202
- }
203
-
204
191
// 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 )
192
+ static struct HNode * get_node_using_index (struct AtomTable * table , atom_index_t index )
206
193
{
207
194
if (UNLIKELY (((size_t ) index ) >= table -> count )) {
208
195
return NULL ;
209
196
}
210
197
211
198
struct HNodeGroup * node_group = table -> first_node_group ;
212
199
while (node_group ) {
213
- long first_index = node_group -> first_index ;
200
+ atom_index_t first_index = node_group -> first_index ;
214
201
if (first_index + node_group -> len > index ) {
215
202
return & node_group -> nodes [index - first_index ];
216
203
}
@@ -221,7 +208,7 @@ static struct HNode *get_node_using_index(struct AtomTable *table, long index)
221
208
return NULL ;
222
209
}
223
210
224
- AtomString atom_table_get_atom_string (struct AtomTable * table , long index )
211
+ AtomString atom_table_get_atom_string (struct AtomTable * table , atom_index_t index )
225
212
{
226
213
SMP_RDLOCK (table );
227
214
@@ -264,7 +251,7 @@ int atom_table_cmp_using_atom_index(struct AtomTable *table, int t_atom_index, i
264
251
return memcmp_result ;
265
252
}
266
253
267
- atom_ref_t atom_table_get_atom_ptr_and_len (struct AtomTable * table , long index , size_t * out_len )
254
+ atom_ref_t atom_table_get_atom_ptr_and_len (struct AtomTable * table , atom_index_t index , size_t * out_len )
268
255
{
269
256
SMP_RDLOCK (table );
270
257
@@ -334,10 +321,10 @@ static inline void insert_node_into_bucket(
334
321
node -> next = maybe_existing_node ;
335
322
}
336
323
337
- static inline long insert_node (struct AtomTable * table , struct HNodeGroup * node_group ,
324
+ static inline atom_index_t insert_node (struct AtomTable * table , struct HNodeGroup * node_group ,
338
325
unsigned long bucket_index , AtomString string )
339
326
{
340
- long new_index = table -> count ;
327
+ atom_index_t new_index = table -> count ;
341
328
table -> count ++ ;
342
329
343
330
struct HNode * node = & node_group -> nodes [new_index - node_group -> first_index ];
@@ -398,7 +385,7 @@ static inline bool maybe_rehash(struct AtomTable *table, int new_entries)
398
385
return do_rehash (table , new_capacity );
399
386
}
400
387
401
- long atom_table_ensure_atom (struct AtomTable * table , AtomString string , enum AtomTableCopyOpt opts )
388
+ enum AtomTableEnsureAtomResult atom_table_ensure_atom (struct AtomTable * table , AtomString string , enum AtomTableCopyOpt opts , atom_index_t * result )
402
389
{
403
390
unsigned long hash = sdbm_hash (string , atom_string_len (string ));
404
391
SMP_WRLOCK (table );
@@ -407,19 +394,20 @@ long atom_table_ensure_atom(struct AtomTable *table, AtomString string, enum Ato
407
394
struct HNode * node = get_node_from_bucket (table , bucket_index , string );
408
395
if (node ) {
409
396
SMP_UNLOCK (table );
410
- return node -> index ;
397
+ * result = node -> index ;
398
+ return AtomTableEnsureAtomOk ;
411
399
}
412
400
if (opts & AtomTableAlreadyExisting ) {
413
401
SMP_UNLOCK (table );
414
- return ATOM_TABLE_NOT_FOUND ;
402
+ return AtomTableEnsureAtomNotFound ;
415
403
}
416
404
417
405
struct HNodeGroup * node_group = table -> last_node_group ;
418
406
if (!table -> last_node_group_avail ) {
419
407
node_group = new_node_group (table , DEFAULT_SIZE );
420
408
if (IS_NULL_PTR (node_group )) {
421
409
SMP_UNLOCK (table );
422
- return ATOM_TABLE_ALLOC_FAIL ;
410
+ return AtomTableEnsureAtomAllocFail ;
423
411
}
424
412
}
425
413
@@ -429,7 +417,7 @@ long atom_table_ensure_atom(struct AtomTable *table, AtomString string, enum Ato
429
417
uint8_t * buf = malloc (1 + len );
430
418
if (IS_NULL_PTR (buf )) {
431
419
SMP_UNLOCK (table );
432
- return ATOM_TABLE_ALLOC_FAIL ;
420
+ return AtomTableEnsureAtomAllocFail ;
433
421
}
434
422
memcpy (buf , string , 1 + len );
435
423
maybe_copied = buf ;
@@ -439,10 +427,10 @@ long atom_table_ensure_atom(struct AtomTable *table, AtomString string, enum Ato
439
427
bucket_index = hash % table -> capacity ;
440
428
}
441
429
442
- long new_index = insert_node (table , node_group , bucket_index , maybe_copied );
430
+ * result = insert_node (table , node_group , bucket_index , maybe_copied );
443
431
444
432
SMP_UNLOCK (table );
445
- return new_index ;
433
+ return AtomTableEnsureAtomOk ;
446
434
}
447
435
448
436
static inline int read_encoded_len (const uint8_t * * len_bytes )
@@ -463,8 +451,11 @@ static inline int read_encoded_len(const uint8_t **len_bytes)
463
451
}
464
452
}
465
453
466
- int atom_table_ensure_atoms (struct AtomTable * table , const void * atoms , int count ,
467
- int * translate_table , enum EnsureAtomsOpt opt )
454
+ // -1 is not a valid atom index as we're limited to 2^20
455
+ #define ATOM_TABLE_NOT_FOUND_MARKER ((atom_index_t) -1)
456
+
457
+ enum AtomTableEnsureAtomResult atom_table_ensure_atoms (struct AtomTable * table , const void * atoms , int count ,
458
+ atom_index_t * translate_table , enum EnsureAtomsOpt opt )
468
459
{
469
460
bool is_long_format = (opt & EnsureLongEncoding ) != 0 ;
470
461
@@ -481,7 +472,7 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
481
472
if (UNLIKELY (atom_len < 0 )) {
482
473
fprintf (stderr , "Found invalid atom len." );
483
474
SMP_UNLOCK (table );
484
- return ATOM_TABLE_INVALID_LEN ;
475
+ return AtomTableEnsureAtomInvalidLen ;
485
476
} else if (UNLIKELY (atom_len > 255 )) {
486
477
fprintf (stderr ,
487
478
"Unsupported atom length %i bytes.\n"
@@ -491,7 +482,7 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
491
482
"https://github.com/atomvm/AtomVM/issues\n" ,
492
483
atom_len );
493
484
SMP_UNLOCK (table );
494
- return ATOM_TABLE_INVALID_LEN ;
485
+ return AtomTableEnsureAtomInvalidLen ;
495
486
}
496
487
char tmp_old_fmt [256 ];
497
488
tmp_old_fmt [0 ] = atom_len ;
@@ -508,7 +499,7 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
508
499
translate_table [i ] = node -> index ;
509
500
} else {
510
501
new_atoms_count ++ ;
511
- translate_table [i ] = ATOM_TABLE_NOT_FOUND ;
502
+ translate_table [i ] = ATOM_TABLE_NOT_FOUND_MARKER ;
512
503
}
513
504
}
514
505
@@ -531,12 +522,12 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
531
522
next_atom += 1 + atom_len ;
532
523
}
533
524
534
- if (translate_table [i ] == ATOM_TABLE_NOT_FOUND ) {
525
+ if (translate_table [i ] == ATOM_TABLE_NOT_FOUND_MARKER ) {
535
526
if (!table -> last_node_group_avail ) {
536
527
node_group = new_node_group (table , remaining_atoms );
537
528
if (IS_NULL_PTR (node_group )) {
538
529
SMP_UNLOCK (table );
539
- return ATOM_TABLE_ALLOC_FAIL ;
530
+ return AtomTableEnsureAtomAllocFail ;
540
531
}
541
532
}
542
533
@@ -545,7 +536,7 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
545
536
if (IS_NULL_PTR (atom_copy )) {
546
537
// we are not going to remove atoms that have already been added up to this one
547
538
SMP_UNLOCK (table );
548
- return ATOM_TABLE_ALLOC_FAIL ;
539
+ return AtomTableEnsureAtomAllocFail ;
549
540
}
550
541
atom_copy [0 ] = atom_len ;
551
542
memcpy (atom_copy + 1 , to_be_copied , atom_len );
@@ -566,5 +557,5 @@ int atom_table_ensure_atoms(struct AtomTable *table, const void *atoms, int coun
566
557
567
558
SMP_UNLOCK (table );
568
559
569
- return 0 ;
560
+ return AtomTableEnsureAtomOk ;
570
561
}
0 commit comments