summaryrefslogtreecommitdiff
path: root/src/khash_str.h
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-04-25 10:36:01 -0700
committerRussell Belfer <rb@github.com>2012-04-25 11:18:08 -0700
commit01fed0a8f9b80e80c8f76cde29fc0d66cb77fff7 (patch)
tree817dbac7a66529c1a25d26cc256b819564b6cb03 /src/khash_str.h
parentada488bfe720d0df8187b5b58e326a13b7bdc678 (diff)
downloadlibgit2-01fed0a8f9b80e80c8f76cde29fc0d66cb77fff7.tar.gz
Convert hashtable usage over to khash
This updates khash.h with some extra features (like error checking on allocations, ability to use wrapped malloc, foreach calls, etc), creates two high-level wrappers around khash: `git_khash_str` and `git_khash_oid` for string-to-void-ptr and oid-to-void-ptr tables, then converts all of the old usage of `git_hashtable` over to use these new hashtables. For `git_khash_str`, I've tried to create a set of macros that yield an API not too unlike the old `git_hashtable` API. Since the oid hashtable is only used in one file, I haven't bother to set up all those macros and just use the khash APIs directly for now.
Diffstat (limited to 'src/khash_str.h')
-rw-r--r--src/khash_str.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/khash_str.h b/src/khash_str.h
new file mode 100644
index 000000000..0b840d836
--- /dev/null
+++ b/src/khash_str.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_khash_str_h__
+#define INCLUDE_khash_str_h__
+
+#include "common.h"
+
+#define kmalloc git__malloc
+#define kcalloc git__calloc
+#define krealloc git__realloc
+#define kfree git__free
+#include "khash.h"
+
+__KHASH_TYPE(str, const char *, void *);
+typedef khash_t(str) git_khash_str;
+
+#define GIT_KHASH_STR__IMPLEMENTATION \
+ __KHASH_IMPL(str, static inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
+
+#define git_khash_str_alloc() kh_init(str)
+#define git_khash_str_free(h) kh_destroy(str, h), h = NULL
+#define git_khash_str_clear(h) kh_clear(str, h)
+
+#define git_khash_str_num_entries(h) kh_size(h)
+
+#define git_khash_str_lookup_index(h, k) kh_get(str, h, k)
+#define git_khash_str_valid_index(h, idx) (idx != kh_end(h))
+
+#define git_khash_str_exists(h, k) (kh_get(str, h, k) != kh_end(h))
+
+#define git_khash_str_value_at(h, idx) kh_val(h, idx)
+#define git_khash_str_set_value_at(h, idx, v) kh_val(h, idx) = v
+#define git_khash_str_delete_at(h, idx) kh_del(str, h, idx)
+
+#define git_khash_str_insert(h, key, val, err) do { \
+ khiter_t __pos = kh_put(str, h, key, &err); \
+ if (err >= 0) kh_val(h, __pos) = val; \
+ } while (0)
+
+#define git_khash_str_insert2(h, key, val, old, err) do { \
+ khiter_t __pos = kh_put(str, h, key, &err); \
+ if (err >= 0) { \
+ old = (err == 0) ? kh_val(h, __pos) : NULL; \
+ kh_val(h, __pos) = val; \
+ } } while (0)
+
+#define git_khash_str_foreach kh_foreach
+#define git_khash_str_foreach_value kh_foreach_value
+
+#endif