From f1ddc12b68b4e4c8087962de25260470e6df2bea Mon Sep 17 00:00:00 2001 From: Connor Thomson Date: Tue, 8 Sep 2026 18:27:39 -0700 Subject: Add tuxanci2 files --- src/base/idManager.c | 198 --------------------------------------------------- 1 file changed, 198 deletions(-) delete mode 100644 src/base/idManager.c (limited to 'src/base/idManager.c') diff --git a/src/base/idManager.c b/src/base/idManager.c deleted file mode 100644 index 87042bb..0000000 --- a/src/base/idManager.c +++ /dev/null @@ -1,198 +0,0 @@ -#include -#include -#include - -#include "main.h" -#include "list.h" -#include "idManager.h" - -static list_t *listID; -static int lastID; - -typedef struct id_item_struct { - int id; - int count; -} id_item_t; - -static id_item_t *newIdItem(int id, int count) -{ - id_item_t *new; - - new = malloc(sizeof(id_item_t)); - new->id = id; - new->count = count; - - return new; -} - -static void destroyIdItem(id_item_t *p) -{ - assert(p != NULL); - - free(p); -} - -void id_init_list() -{ - debug("Initializing ID manager"); - - listID = list_new(); - lastID = 0; - -} - -int id_is_register(int id) -{ - int i; - - assert(listID != NULL); - - for (i = 0; i < listID->count; i++) { - id_item_t *this; - - this = listID->list[i]; - - if (this->id == id) { - return i; - } - } - - return -1; -} - -static int findNewID() -{ - int ret; - - assert(listID != NULL); - - if (listID->count >= MAX_ID - 1) { - fatal("There is no free ID left"); - } - - do { - /*ret = (random() % (listID->count + 8 )) + 1;*/ - ret = random() % MAX_ID + 1; - } while (id_is_register(ret) != -1); - - /*printf("new ID %d\n", ret);*/ - - return ret; -} - -int id_get_newcount(int count) -{ - int id; - - id = findNewID(); - - list_add(listID, newIdItem(id, count)); - - return id; -} - -int id_get_new() -{ - return id_get_newcount(1); -} - -void id_inc(int id) -{ - id_item_t *this; - int index; - - assert(listID != NULL); - - index = id_is_register(id); - - if (index == -1) { - error("Trying to increment counter of never registered ID [%d]", id); - assert(0); - return; /* ha ha ha */ - } - - this = listID->list[index]; - - this->count++; - - /*printf("inc ID %d %d\n", this->id, this->count);*/ - return; -} - -void id_del(int id) -{ - id_item_t *this; - int index; - - assert(listID != NULL); - - index = id_is_register(id); - - if (index == -1) { - error("Trying to delete never registered ID [%d]", id); - assert(0); - return; /* ha ha ha */ - } - - this = listID->list[index]; - - this->count--; - /*printf("dec ID %d %d\n", this->id, this->count);*/ - - if (this->count <= 0) { - list_del_item(listID, index, free); - /*printf("listID->count = %d\n", listID->count);*/ - } - - return; -} - -void id_replace(int old_id, int new_id) -{ - int index_old_id; - int index_new_id; - id_item_t *this; - - if (old_id == new_id) { - return; - } - - index_old_id = id_is_register(old_id); - assert(index_old_id != -1); - - index_new_id = id_is_register(new_id); - assert(index_new_id == -1); - - this = listID->list[index_old_id]; - this->id = new_id; -} - -void infoID(int id) -{ - id_item_t *this; - int index; - - assert(listID != NULL); - - index = id_is_register(id); - - if (index == -1) { - debug("Getting information of nonexistent ID [%d]", id); - - return; - } - - this = listID->list[index]; - - debug("ID information [%d]: used %d times", this->id, this->count); - - return; -} - -void id_quit_list() -{ - debug("Shutting down ID manager"); - - assert(listID != NULL); - list_destroy_item(listID, destroyIdItem); -} -- cgit v1.2.3