From 4dabdeb0fed3fd0de08bd66078742043626c49bb Mon Sep 17 00:00:00 2001 From: dam Date: Tue, 30 Aug 2022 23:53:55 +0000 Subject: Testing possibility to address all tasks using pointers instead of uint32_t indexes. --- main.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 4ce277b..a030447 100644 --- a/main.c +++ b/main.c @@ -34,8 +34,9 @@ typedef struct /*__attribute__((__packed__))*/ { uint64_t modified_on; uint32_t count; uint32_t capacity; - task_t* active_task; task_t* tasks; + task_t* active_task; + task_t* selected_task; // TODO Maybe use this instead of indexes? } database_t; const char* DB_BIN_FILE_SIGNATURE = "TTT:B:01"; @@ -140,6 +141,7 @@ bool create_task(database_t* db, task_t** task) { // If necessary, expand database capacity. uint32_t current_capacity = db->capacity; if((db->count + 1) > current_capacity) { + ptrdiff_t selected_task_offset = db->tasks - db->active_task; uint32_t new_capacity = current_capacity == 0 ? 2 : current_capacity >= UINT32_MAX >> 2 ? UINT32_MAX : current_capacity << 1; @@ -152,6 +154,8 @@ bool create_task(database_t* db, task_t** task) { db->capacity = new_capacity; db->tasks = new_tasks; + db->active_task = db->tasks + selected_task_offset; + // TODO Validate selected_task (may now be above count. } // Prepare new task. @@ -181,6 +185,7 @@ bool remove_task(database_t* db, uint32_t index) { // If possible, shrink database capacity. uint32_t current_capacity = db->capacity; if (db->count <= (current_capacity >> 2)) { + ptrdiff_t selected_task_offset = db->tasks - db->active_task; uint32_t new_capacity = current_capacity >> 1; task_t* new_tasks = realloc(db->tasks, new_capacity * SIZEOF_TASK_T); if (new_tasks == NULL) { @@ -189,6 +194,8 @@ bool remove_task(database_t* db, uint32_t index) { } db->capacity = new_capacity; db->tasks = new_tasks; + db->active_task = db->tasks + selected_task_offset; + // TODO Validate selected_task (may now be above count. } return true; -- cgit v1.2.3