Skip to content

Commit

Permalink
fix(hash_table): fix list free bug
Browse files Browse the repository at this point in the history
  • Loading branch information
exbotanical committed Dec 29, 2024
1 parent b3f79ab commit 971577f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 52 deletions.
2 changes: 1 addition & 1 deletion clib.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libhash",
"version": "1.1.0",
"version": "1.1.1",
"author": "Matthew Zito",
"repo": "exbotanical/libhash",
"license": "MIT",
Expand Down
15 changes: 10 additions & 5 deletions src/hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,23 @@ static void ht_resize(hash_table *ht, int base_capacity) {
}

ht->base_capacity = new_ht->base_capacity;
ht->count = new_ht->count;

const int tmp_capacity = ht->capacity;
ht->capacity = new_ht->capacity;
ht->count = new_ht->count;

// Cannot free values - we may still be using them.
ht_entry **tmp_entries = ht->entries;
ht->entries = new_ht->entries;
free(tmp_entries);

list_free(ht->occupied_buckets);
// TODO: Figure out why list_free doesnt work but this does
node_t *head = ht->occupied_buckets;
node_t *tmp;
while (head != &LIST_SENTINEL_NODE) {
tmp = head;
head = head->next;
free(tmp);
}

ht->occupied_buckets = new_ht->occupied_buckets;

free(new_ht);
Expand Down Expand Up @@ -183,7 +189,6 @@ static void __ht_delete_table(hash_table *ht) {
}
}

// list_free(ht->occupied_buckets);
free(ht->entries);
free(ht);
}
Expand Down
46 changes: 46 additions & 0 deletions src/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "list.h"

node_t *list_node_create(const int value) {
node_t *n = (node_t *)malloc(sizeof(node_t));
n->value = value;
return n;
}

void list_prepend(node_t **head, int value) {
// TODO: xmalloc
node_t *new_node = (node_t *)malloc(sizeof(node_t));
new_node->value = value;

node_t *tmp = *head;
*head = new_node;
new_node->next = tmp;
}

void list_remove(node_t **head, int value) {
node_t *current = *head;
node_t *prev = NULL;

while (current != NULL) {
if (current->value == value) {
if (prev == NULL) {
*head = current->next;
} else {
prev->next = current->next;
}
free(current);
return;
}
prev = current;
current = current->next;
}
}

void list_free(node_t *head) {
node_t *headp = head;
node_t *tmp;
while (headp != &LIST_SENTINEL_NODE) {
tmp = headp;
headp = headp->next;
free(tmp);
}
}
56 changes: 10 additions & 46 deletions src/list.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef LIBHASH_LIST_H
#define LIBHASH_LIST_H

#include <stdio.h>
#include <stdlib.h>

typedef struct node node_t;
Expand All @@ -10,53 +9,18 @@ struct node {
node_t *next;
};

static node_t LIST_SENTINEL_NODE = {0, NULL};

static node_t *list_node_create_head(void) { return &LIST_SENTINEL_NODE; }

static node_t *list_node_create(const int value) {
node_t *n = (node_t *)malloc(sizeof(node_t));
n->value = value;
return n;
}

static void list_prepend(node_t **head, int value) {
// TODO: xmalloc
node_t *new_node = (node_t *)malloc(sizeof(node_t));
new_node->value = value;

node_t *tmp = *head;
*head = new_node;
new_node->next = tmp;
}

static void list_remove(node_t **head, int value) {
node_t *current = *head;
node_t *prev = NULL;
static node_t LIST_SENTINEL_NODE = {
.value = 0,
.next = NULL,
};

while (current != NULL) {
if (current->value == value) {
if (prev == NULL) {
*head = current->next;
} else {
prev->next = current->next;
}
free(current);
return;
}
prev = current;
current = current->next;
}
static inline node_t *list_node_create_head(void) {
return &LIST_SENTINEL_NODE;
}

// TODO: Use and test
static void list_free(node_t *head) {
node_t *tmp;
while (head != &LIST_SENTINEL_NODE) {
tmp = head;
head = head->next;
free(tmp);
}
}
node_t *list_node_create(const int value);
void list_prepend(node_t **head, int value);
void list_remove(node_t **head, int value);
void list_free(node_t *head);

#endif /* LIBHASH_LIST_H */

0 comments on commit 971577f

Please sign in to comment.