From e266346bf26643ecaf6a36b4d846f0d484476683 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Fri, 18 Aug 2023 10:42:29 -0400 Subject: [PATCH] Insert new buckets into linked list for for linkmap --- linkmap.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/linkmap.c b/linkmap.c index 5fa9d04..215779d 100644 --- a/linkmap.c +++ b/linkmap.c @@ -48,6 +48,7 @@ haggis_bucket* haggis_bucket_init(ino_t inode, uint64_t hash, char * path) { bucket->key.val = inode; bucket->hash = hash; bucket->path = path; + bucket->next = NULL; return bucket; } @@ -110,7 +111,7 @@ char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char * path) { } key; char * target = NULL; size_t idx, hash; - int i; + haggis_bucket *b; pthread_mutex_lock(&map->mutex); if (map->len >= map->capacity) @@ -118,23 +119,19 @@ char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char * path) { key.val = inode; hash = hash_fnv1a_64(key.bytes, sizeof(ino_t)); idx = map->capacity % hash; - for (i = 0; i < map->capacity; i++) { - if (map->buckets[idx].key.val == 0) { - map->buckets[idx].key.val = inode; - map->buckets[idx].hash = hash; - map->buckets[idx].path = path; - map->len++; - pthread_mutex_unlock(&map->mutex); - break; - } else if (map->buckets[idx].key.val == inode) { - target = strndup(target, PATH_MAX - 1); - pthread_mutex_unlock(&map->mutex); - break; - } - if (idx == map->capacity - 1) - idx = 0; - else - idx++; + if (map->buckets[idx].key.val == inode) { + target = strndup(target, PATH_MAX - 1); + pthread_mutex_unlock(&map->mutex); + } else if (map->buckets[idx].key.val == 0) { + map->buckets[idx].key.val = inode; + map->buckets[idx].hash = hash; + map->buckets[idx].path = path; + map->len++; + pthread_mutex_unlock(&map->mutex); + } else { + b = haggis_bucket_init(key.val, hash, path); + if (b == NULL) return NULL; + haggis_bucket_append(&map->buckets[idx], b); } return target; }