Implemented DHT
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
#include "lookup.h"
|
||||
|
||||
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 && current->request->hash_id != hash_id)
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user