From fc94efcc46815e074ac2036146dc623cef70ec23 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Mon, 2 Jan 2023 22:49:20 +0000 Subject: [PATCH] Fixed bugs with the add_object_to_store and remove_object_from_store functions --- src/ObjectStore.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ObjectStore.c b/src/ObjectStore.c index cf17cdb..9cf72d8 100644 --- a/src/ObjectStore.c +++ b/src/ObjectStore.c @@ -64,7 +64,10 @@ ObjectID add_object_to_store(ObjectStore *os, void *object, EventHandlerPtr eh, if (os->deleted_count > 0) { id = (ObjectID)reuse_id((i32 *)os->reusable_ids, os->deleted_count); os->deleted_count -= 1; - os->count += 1; + + if (id == os->count) { + os->count += 1; + } } else { id = (os->count)++; } @@ -98,7 +101,20 @@ void remove_object_from_store(ObjectStore *os, ObjectID *id_ptr) { // count shouldn't be decremented since the deleted object would be in the // middle, which means that the functions that loop over all objects in the // store will not actually loop over everything - os->count -= 1; + + u64 new_count = 0; + + // Figure out how much we need to decrement the count. Since, a lot of + // objects may have been removed, the count needs to equal the last active + // object. + for (size_t i = id - 1; i >= 0; --i) { + if (os->object_ptrs[i]) { + new_count = i + 1; + break; + } + } + + os->count = new_count; } }