Skip to content

Commit d791640

Browse files
committed
ets: rename ets_table->keypos to key_index
Additionally, unifies tuple arity checks (no change in behavior). Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
1 parent 64f2bb2 commit d791640

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/libAtomVM/ets.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct EtsTable
5757
term name;
5858
bool is_named;
5959
int32_t owner_process_id;
60-
size_t keypos;
60+
size_t key_index;
6161
EtsTableType table_type;
6262
// In the future, we might support rb-trees for sorted sets
6363
// For this MVP, we only support unsorted sets
@@ -171,7 +171,7 @@ EtsErrorCode ets_create_table_maybe_gc(term name, bool is_named, EtsTableType ta
171171
uint64_t ref_ticks = globalcontext_get_ref_ticks(ctx->global);
172172
ets_table->ref_ticks = ref_ticks;
173173

174-
ets_table->keypos = keypos;
174+
ets_table->key_index = keypos;
175175

176176
#ifndef AVM_NO_SMP
177177
ets_table->lock = smp_rwlock_create();
@@ -249,9 +249,9 @@ static void ets_delete_all_tables(struct Ets *ets, GlobalContext *global)
249249

250250
static EtsErrorCode insert(struct EtsTable *ets_table, term entry, Context *ctx)
251251
{
252-
size_t keypos = ets_table->keypos;
252+
size_t keypos = ets_table->key_index;
253253

254-
if ((size_t) term_get_tuple_arity(entry) < keypos + 1) {
254+
if ((size_t) term_get_tuple_arity(entry) <= keypos) {
255255
return EtsBadEntry;
256256
}
257257

@@ -276,7 +276,7 @@ static EtsErrorCode insert_multiple(struct EtsTable *ets_table, term list, Conte
276276
while (term_is_nonempty_list(iter)) {
277277
term tuple = term_get_list_head(iter);
278278
iter = term_get_list_tail(iter);
279-
if (!term_is_tuple(tuple) || (size_t) term_get_tuple_arity(tuple) < (ets_table->keypos + 1)) {
279+
if (!term_is_tuple(tuple) || (size_t) term_get_tuple_arity(tuple) <= ets_table->key_index) {
280280
return EtsBadEntry;
281281
}
282282
++size;
@@ -293,7 +293,7 @@ static EtsErrorCode insert_multiple(struct EtsTable *ets_table, term list, Conte
293293
size_t i = 0;
294294
while (term_is_nonempty_list(list)) {
295295
term tuple = term_get_list_head(list);
296-
nodes[i] = ets_hashtable_new_node(tuple, ets_table->keypos);
296+
nodes[i] = ets_hashtable_new_node(tuple, ets_table->key_index);
297297
if (IS_NULL_PTR(nodes[i])) {
298298
for (size_t it = 0; it < i; ++it) {
299299
ets_hashtable_free_node(nodes[it], ctx->global);
@@ -337,7 +337,7 @@ EtsErrorCode ets_insert(term name_or_ref, term entry, Context *ctx)
337337

338338
static EtsErrorCode ets_table_lookup_maybe_gc(struct EtsTable *ets_table, term key, term *ret, Context *ctx, int num_roots, term *roots)
339339
{
340-
term res = ets_hashtable_lookup(ets_table->hashtable, key, ets_table->keypos, ctx->global);
340+
term res = ets_hashtable_lookup(ets_table->hashtable, key, ets_table->key_index, ctx->global);
341341

342342
if (term_is_nil(res)) {
343343
*ret = term_nil();
@@ -375,7 +375,7 @@ EtsErrorCode ets_lookup_element_maybe_gc(term name_or_ref, term key, size_t key_
375375
return EtsBadAccess;
376376
}
377377

378-
term entry = ets_hashtable_lookup(ets_table->hashtable, key, ets_table->keypos, ctx->global);
378+
term entry = ets_hashtable_lookup(ets_table->hashtable, key, ets_table->key_index, ctx->global);
379379
if (!term_is_tuple(entry)) {
380380
SMP_UNLOCK(ets_table);
381381
return EtsEntryNotFound;
@@ -424,7 +424,7 @@ EtsErrorCode ets_delete(term name_or_ref, term key, term *ret, Context *ctx)
424424
return EtsBadAccess;
425425
}
426426

427-
bool _found = ets_hashtable_remove(ets_table->hashtable, key, ets_table->keypos, ctx->global);
427+
bool _found = ets_hashtable_remove(ets_table->hashtable, key, ets_table->key_index, ctx->global);
428428
UNUSED(_found);
429429
SMP_UNLOCK(ets_table);
430430

@@ -519,15 +519,15 @@ EtsErrorCode ets_update_counter_maybe_gc(term ref, term key, term operation, ter
519519
term threshold_term;
520520
term set_value_term;
521521
// +1 for index -> position, +1 for targeting elem after key
522-
size_t default_pos = (ets_table->keypos + 1) + 1;
522+
size_t default_pos = (ets_table->key_index + 1) + 1;
523523

524524
if (UNLIKELY(!operation_to_tuple4(operation, default_pos, &position_term, &increment_term, &threshold_term, &set_value_term))) {
525525
SMP_UNLOCK(ets_table);
526526
return EtsBadEntry;
527527
}
528528
size_t arity = term_get_tuple_arity(to_insert);
529529
size_t elem_index = term_to_int(position_term) - 1;
530-
if (UNLIKELY(arity <= elem_index || elem_index == ets_table->keypos)) {
530+
if (UNLIKELY(arity <= elem_index || elem_index == ets_table->key_index)) {
531531
SMP_UNLOCK(ets_table);
532532
return EtsBadEntry;
533533
}

0 commit comments

Comments
 (0)