Ensure we find a bucket for storing link info after hash collisions; Fix
`install` make target by adding path to `haggis.h`
This commit is contained in:
parent
4149c4275f
commit
6a58dd2a63
3 changed files with 39 additions and 16 deletions
2
Makefile
2
Makefile
|
@ -71,7 +71,7 @@ libhaggis.a: $(objs)
|
||||||
libhaggis.so: $(objs)
|
libhaggis.so: $(objs)
|
||||||
$(CC) -shared -o $@ $? $(LIBS)
|
$(CC) -shared -o $@ $? $(LIBS)
|
||||||
|
|
||||||
install: libhaggis.a libhaggis.so haggis.h
|
install: libhaggis.a libhaggis.so include/haggis.h
|
||||||
[ -d $(includedir) ] || install -d $(includedir)
|
[ -d $(includedir) ] || install -d $(includedir)
|
||||||
[ -d $(libdir) ] || install -d $(libdir)
|
[ -d $(libdir) ] || install -d $(libdir)
|
||||||
install -m755 libhaggis.so $(libdir)/
|
install -m755 libhaggis.so $(libdir)/
|
||||||
|
|
|
@ -53,7 +53,7 @@ typedef struct __bucket {
|
||||||
} haggis_bucket;
|
} haggis_bucket;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
pthread_mutex_t *mutex;
|
pthread_mutex_t mutex;
|
||||||
size_t len;
|
size_t len;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
haggis_bucket *buckets;
|
haggis_bucket *buckets;
|
||||||
|
|
39
linkmap.c
39
linkmap.c
|
@ -65,6 +65,21 @@ void haggis_bucket_append(haggis_bucket *head, haggis_bucket *tail) {
|
||||||
head->next = tail;
|
head->next = tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
haggis_linkmap* haggis_linkmap_init() {
|
||||||
|
haggis_linkmap *map;
|
||||||
|
|
||||||
|
map = calloc(1, sizeof(haggis_linkmap));
|
||||||
|
if (map == NULL)
|
||||||
|
return NULL;
|
||||||
|
map->buckets = calloc(HAGGIS_BUCKETS_BASE, sizeof(size_t));
|
||||||
|
if (map->buckets == NULL) {
|
||||||
|
free(map);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
map->capacity = HAGGIS_BUCKETS_BASE;
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
int haggis_linkmap_expand(haggis_linkmap *map) {
|
int haggis_linkmap_expand(haggis_linkmap *map) {
|
||||||
haggis_bucket *buckets_new;
|
haggis_bucket *buckets_new;
|
||||||
haggis_bucket *buckets_old;
|
haggis_bucket *buckets_old;
|
||||||
|
@ -95,23 +110,31 @@ char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char * path) {
|
||||||
} key;
|
} key;
|
||||||
char * target = NULL;
|
char * target = NULL;
|
||||||
size_t idx, hash;
|
size_t idx, hash;
|
||||||
|
int i;
|
||||||
|
|
||||||
pthread_mutex_lock(map->mutex);
|
pthread_mutex_lock(&map->mutex);
|
||||||
|
if (map->len >= map->capacity)
|
||||||
|
haggis_linkmap_expand(map);
|
||||||
key.val = inode;
|
key.val = inode;
|
||||||
hash = hash_fnv1a_64(key.bytes, sizeof(ino_t));
|
hash = hash_fnv1a_64(key.bytes, sizeof(ino_t));
|
||||||
idx = map->capacity % hash;
|
idx = map->capacity % hash;
|
||||||
|
for (i = 0; i < map->capacity; i++) {
|
||||||
if (map->buckets[idx].key.val == 0) {
|
if (map->buckets[idx].key.val == 0) {
|
||||||
map->buckets[idx].key.val = inode;
|
map->buckets[idx].key.val = inode;
|
||||||
map->buckets[idx].hash = hash;
|
map->buckets[idx].hash = hash;
|
||||||
map->buckets[idx].path = path;
|
map->buckets[idx].path = path;
|
||||||
pthread_mutex_unlock(map->mutex);
|
map->len++;
|
||||||
return NULL;
|
pthread_mutex_unlock(&map->mutex);
|
||||||
|
break;
|
||||||
} else if (map->buckets[idx].key.val == inode) {
|
} else if (map->buckets[idx].key.val == inode) {
|
||||||
target = strndup(target, PATH_MAX - 1);
|
target = strndup(target, PATH_MAX - 1);
|
||||||
pthread_mutex_unlock(map->mutex);
|
pthread_mutex_unlock(&map->mutex);
|
||||||
return target;
|
break;
|
||||||
}
|
}
|
||||||
// todo: if buckets[idx] has a different value stored, loop until we find an
|
if (idx == map->capacity - 1)
|
||||||
// empty slot or a slot with the correct value (whichever is first)
|
idx = 0;
|
||||||
return NULL;
|
else
|
||||||
|
idx++;
|
||||||
|
}
|
||||||
|
return target;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue