From 5c181d1c3d5eebcc771f70e1d2e085338c6c6981 Mon Sep 17 00:00:00 2001 From: rscharfe Date: Wed, 3 Oct 2018 14:24:28 +0200 Subject: [PATCH] khash: add kh_release Add a function for releasing only the internal memory of the khash structure, but not the structure itself. This allows keeping that struct on the stack instead of allocating it using kh_init_*. --- khash.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/khash.h b/khash.h index f75f3474..ad1d805f 100644 --- a/khash.h +++ b/khash.h @@ -212,11 +212,16 @@ static const double __ac_HASH_UPPER = 0.77; SCOPE kh_##name##_t *kh_init_##name(void) { \ return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \ } \ + SCOPE void kh_release_##name(kh_##name##_t *h) \ + { \ + kfree(h->flags); \ + kfree((void *)h->keys); \ + kfree((void *)h->vals); \ + } \ SCOPE void kh_destroy_##name(kh_##name##_t *h) \ { \ if (h) { \ - kfree((void *)h->keys); kfree(h->flags); \ - kfree((void *)h->vals); \ + kh_release_##name(h); \ kfree(h); \ } \ } \ @@ -445,6 +450,13 @@ static kh_inline khint_t __ac_Wang_hash(khint_t key) */ #define kh_destroy(name, h) kh_destroy_##name(h) +/*! @function + @abstract Free the memory of a hash table, keep the [khash_t(name)*]. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + */ +#define kh_release(name, h) kh_release_##name(h) + /*! @function @abstract Reset a hash table without deallocating memory. @param name Name of the hash table [symbol]