seahag/linkmap.c

107 lines
3.5 KiB
C
Raw Normal View History

/* _,.---._ .-._ .--.-. ,--.--------.
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
* |==|,| | -|==| , '=' |==| - _ | |==|- |
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
* _ __ ,---. .-._ .=-.-. _,.----.
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
* `--`---' `--` `--`./ `--``--`-`
*
* @(#)Copyright (c) 2023, Nathan D. Fisher.
*
* This is free software. It comes with NO WARRANTY.
* Permission to use, modify and distribute this source code
* is granted subject to the following conditions.
* 1/ that the above copyright notice and this notice
* are preserved in all copies and that due credit be given
* to the author.
* 2/ that any changes to this code are clearly commented
* as such so that the author does not get blamed for bugs
* other than his own.
*/
#include "linkmap.h"
#include "haggis_private.h"
#include <stddef.h>
#include <stdlib.h>
#include <sys/stat.h>
haggis_link* haggis_link_init(char* path) {
haggis_link *link;
link = calloc(1, sizeof(haggis_link));
if (link == NULL) return NULL;
link->path = path;
return link;
}
void haggis_link_deinit(haggis_link *link) {
if (link == NULL)
return;
if (link->path != NULL)
free(link->path);
free(link);
}
int haggis_link_append(haggis_link *head, char *path) {
haggis_link *tail;
tail = haggis_link_init(path);
if (tail == NULL) return 2;
while (head->next != NULL) {
head++;
}
head->next = tail;
return 0;
}
void haggis_linkmap_node_deinit(haggis_linkmap_node *nod) {
haggis_link *current;
haggis_link *previous;
current = nod->links;
while (current != NULL) {
previous = current;
current = current->next;
free(previous);
}
if (nod->key != NULL)
free(nod->key);
if (nod->target != NULL)
free(nod->target);
free(nod);
}
int haggis_linkmap_expand(haggis_linkmap *map) {
haggis_linkmap_node *buckets_new;
haggis_linkmap_node *buckets_old;
size_t i, hash, idx;
buckets_new = calloc(map->capacity + HAGGIS_BUCKETS_BASE, sizeof(haggis_linkmap_node));
if (buckets_new == NULL)
return 2;
for (i = 0; i < map->capacity; i++) {
if (&map->buckets[i] != NULL) {
hash = hash_fnv1a_64(map->buckets[i].key->bytes, sizeof(ino_t));
map ->capacity += HAGGIS_BUCKETS_BASE;
idx = map->capacity % hash;
buckets_new[idx] = map->buckets[i];
}
}
buckets_old = map->buckets;
map->buckets = buckets_new;
free(buckets_old);
map->capacity += HAGGIS_BUCKETS_BASE;
return 0;
}