diff options
author | Yorhel <git@yorhel.nl> | 2013-06-30 17:23:12 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2013-06-30 17:23:12 +0200 |
commit | 2f9820ba2b70d254323c851e8c8ea2dee9aff181 (patch) | |
tree | 57bb2c49561281bd686ef4d82fab7ad97b64e43b | |
parent | 263c5ff5bea2e30cef25ae49252b090ce97fd8e7 (diff) |
share/hash: Add share-local (de)initialization
-rw-r--r-- | src/share/hash.c | 25 | ||||
-rw-r--r-- | src/share/hash.h | 5 | ||||
-rw-r--r-- | src/share/share.c | 2 |
3 files changed, 30 insertions, 2 deletions
diff --git a/src/share/hash.c b/src/share/hash.c index 9c533ad..a1f1c50 100644 --- a/src/share/hash.c +++ b/src/share/hash.c @@ -72,6 +72,7 @@ typedef struct { */ typedef struct { share_fl_t *fl; /* NULL when the fl item has been removed from the hash queue while hashing. */ + share_t *s; t_res *chunks; uint64_t filesize; uint64_t chunksize; @@ -105,8 +106,9 @@ __KHASH_IMPL(hashfiles, static kh_inline, share_fl_t *, char, 0, share_hash_inde static obj_t obj[1]; -static share_hash_fn_t *share_hash_fn_create(share_fl_t *fl, const char *path) { +static share_hash_fn_t *share_hash_fn_create(share_t *s, share_fl_t *fl, const char *path) { share_hash_fn_t *fn = malloc(offsetof(share_hash_fn_t, fn) + strlen(path)+1); + fn->s = s; fn->fl = fl; fn->chunks = NULL; fn->fd = -1; @@ -179,7 +181,7 @@ static void share_hash_queue_process() { kstring_t path = {}; share_share_fl_path(s, fl, &path); - obj->fns[slot] = share_hash_fn_create(fl, path.s); + obj->fns[slot] = share_hash_fn_create(s, fl, path.s); free(path.s); /* Process the next item in the queue while there's still some stuff to do */ @@ -234,4 +236,23 @@ void share_hash_remove(share_t *s, share_fl_t *fl) { } +void share_hash_init(share_t *s) { + s->h.hashfiles = kh_init(hashfiles); +} + + +/* share/share.c will take care of actually freeing all share_fl_t objects, so + * we only need to free the hash-related memory. */ +void share_hash_destroy(share_t *s) { + size_t i; + for(i=0; i<HASH_JOBS; i++) + if(obj->fns[i] && obj->fns[i]->s == s) { + /* TODO: Cancel hashing and free fn object */ + obj->fns[i] = NULL; + } + + kh_destroy(hashfiles, s->h.hashfiles); +} + + /* vim: set noet sw=4 ts=4: */ diff --git a/src/share/hash.h b/src/share/hash.h index ffe9339..670a4ce 100644 --- a/src/share/hash.h +++ b/src/share/hash.h @@ -42,6 +42,11 @@ typedef struct { } share_hash_t; +void share_hash_init(share_t *s); + +void share_hash_destroy(share_t *s); + + /* Should be called whenever a new fl item has been added to the file list. If * the item already has a hash, it is added to the in-memory hash index, * otherwise it is added to the hash queue. When a file has been hashed, it is diff --git a/src/share/share.c b/src/share/share.c index 8ffc978..1f1e98e 100644 --- a/src/share/share.c +++ b/src/share/share.c @@ -31,6 +31,7 @@ share_t *share_share_create() { s->root = share_fl_create(true, "", &buf); free(buf.s); + share_hash_init(s); return s; } @@ -94,6 +95,7 @@ void share_share_destroy(share_t *s) { if(s->sc) share_scan_cancel(s->sc); + share_hash_destroy(s); share_fl_free(s->root); free(s); } |