#include "lookup.h" static int free_space = TABLE_SIZE; static lookup_head *table; void init_lookup_table() { table = malloc(sizeof(lookup_head)); if (table == NULL) return; table->head = NULL; } void push_lookup(lookup_request *request) { if (free_space == 0) { lookup_table *infront = table->head; while (infront->next != NULL && infront->next->next != NULL) { infront = infront->next; } free(infront->next->request); free(infront->next); infront->next = NULL; // if malloc fails this does not leak space free_space++; } lookup_table *new = (lookup_table*)malloc(sizeof(lookup_table)); if (new == NULL) return; new->request = request; new->next = table->head; table->head = new; free_space--; } lookup_request *find_lookup(const uint16_t hash_id) { const lookup_table *current = table->head; while (current != NULL){ if((current->request->node_id < current->request->hash_id && (hash_id > current->request->hash_id && hash_id < 65535)) || hash_id <= current->request->node_id || (hash_id > current->request->hash_id && hash_id <= current->request->node_id)) break; current = current->next; } return current == NULL ? NULL : current->request; } void free_lookup_table() { if (table == NULL) return; lookup_table *current = table->head; while (current != NULL) { lookup_table *temp = current; current = current->next; free(temp->request); free(temp); } free(table); table = NULL; free_space = TABLE_SIZE; }