Commit f2f5ec844d96fe0007a47e4df00499ab5598b41f

Patrick Steinhardt 2018-11-23T19:27:09

khash: move khash include into implementation files The current map implementations directly include the "khash.h" headers into their own headers to make available a set of static functions, defines et cetera. Besides leaking the complete khash namespace into files wherever khashes are used, this also triggers Clang's -Wunused-function warnings when some of the static functions are not being used at all. Fix the issue by moving the includes into the respective map implementation files. Add forward declares for all the map types to make them known.

diff --git a/src/idxmap.c b/src/idxmap.c
index 45f0c22..05a7b2f 100644
--- a/src/idxmap.c
+++ b/src/idxmap.c
@@ -7,6 +7,16 @@
 
 #include "idxmap.h"
 
+#define kmalloc git__malloc
+#define kcalloc git__calloc
+#define krealloc git__realloc
+#define kreallocarray git__reallocarray
+#define kfree git__free
+#include "khash.h"
+
+__KHASH_TYPE(idx, const git_index_entry *, git_index_entry *)
+__KHASH_TYPE(idxicase, const git_index_entry *, git_index_entry *)
+
 /* This is __ac_X31_hash_string but with tolower and it takes the entry's stage into account */
 static kh_inline khint_t idxentry_hash(const git_index_entry *e)
 {
@@ -104,11 +114,21 @@ void git_idxmap_free(git_idxmap *map)
 	kh_destroy(idx, map);
 }
 
+void git_idxmap_icase_free(git_idxmap_icase *map)
+{
+	kh_destroy(idxicase, map);
+}
+
 void git_idxmap_clear(git_idxmap *map)
 {
 	kh_clear(idx, map);
 }
 
+void git_idxmap_icase_clear(git_idxmap_icase *map)
+{
+	kh_clear(idxicase, map);
+}
+
 void git_idxmap_delete_at(git_idxmap *map, size_t idx)
 {
 	kh_del(idx, map, idx);
diff --git a/src/idxmap.h b/src/idxmap.h
index 7fed8b9..215a245 100644
--- a/src/idxmap.h
+++ b/src/idxmap.h
@@ -9,23 +9,10 @@
 
 #include "common.h"
 
-#include <ctype.h>
 #include "git2/index.h"
 
-#define kmalloc git__malloc
-#define kcalloc git__calloc
-#define krealloc git__realloc
-#define kreallocarray git__reallocarray
-#define kfree git__free
-#include "khash.h"
-
-__KHASH_TYPE(idx, const git_index_entry *, git_index_entry *)
-__KHASH_TYPE(idxicase, const git_index_entry *, git_index_entry *)
-
-typedef khash_t(idx) git_idxmap;
-typedef khash_t(idxicase) git_idxmap_icase;
-
-typedef khiter_t git_idxmap_iter;
+typedef struct kh_idx_s git_idxmap;
+typedef struct kh_idxicase_s git_idxmap_icase;
 
 int git_idxmap_alloc(git_idxmap **map);
 int git_idxmap_icase_alloc(git_idxmap_icase **map);
@@ -41,7 +28,9 @@ int git_idxmap_has_data(git_idxmap *map, size_t idx);
 void git_idxmap_resize(git_idxmap *map, size_t size);
 void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size);
 void git_idxmap_free(git_idxmap *map);
+void git_idxmap_icase_free(git_idxmap_icase *map);
 void git_idxmap_clear(git_idxmap *map);
+void git_idxmap_icase_clear(git_idxmap_icase *map);
 
 void git_idxmap_delete_at(git_idxmap *map, size_t idx);
 void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx);
diff --git a/src/offmap.c b/src/offmap.c
index e6e5cfe..d0fa9f0 100644
--- a/src/offmap.c
+++ b/src/offmap.c
@@ -7,6 +7,15 @@
 
 #include "offmap.h"
 
+#define kmalloc git__malloc
+#define kcalloc git__calloc
+#define krealloc git__realloc
+#define kreallocarray git__reallocarray
+#define kfree git__free
+#include "khash.h"
+
+__KHASH_TYPE(off, git_off_t, void *)
+
 __KHASH_IMPL(off, static kh_inline, git_off_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal)
 
 git_offmap *git_offmap_alloc(void)
diff --git a/src/offmap.h b/src/offmap.h
index 82ebfb7..c688093 100644
--- a/src/offmap.h
+++ b/src/offmap.h
@@ -11,15 +11,7 @@
 
 #include "git2/types.h"
 
-#define kmalloc git__malloc
-#define kcalloc git__calloc
-#define krealloc git__realloc
-#define kreallocarray git__reallocarray
-#define kfree git__free
-#include "khash.h"
-
-__KHASH_TYPE(off, git_off_t, void *)
-typedef khash_t(off) git_offmap;
+typedef struct kh_off_s git_offmap;
 
 git_offmap *git_offmap_alloc(void);
 void git_offmap_free(git_offmap *map);
diff --git a/src/oidmap.c b/src/oidmap.c
index a2051f8..c42e5c2 100644
--- a/src/oidmap.c
+++ b/src/oidmap.c
@@ -7,6 +7,15 @@
 
 #include "oidmap.h"
 
+#define kmalloc git__malloc
+#define kcalloc git__calloc
+#define krealloc git__realloc
+#define kreallocarray git__reallocarray
+#define kfree git__free
+#include "khash.h"
+
+__KHASH_TYPE(oid, const git_oid *, void *)
+
 GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid)
 {
 	khint_t h;
diff --git a/src/oidmap.h b/src/oidmap.h
index f34c034..a417907 100644
--- a/src/oidmap.h
+++ b/src/oidmap.h
@@ -11,15 +11,7 @@
 
 #include "git2/oid.h"
 
-#define kmalloc git__malloc
-#define kcalloc git__calloc
-#define krealloc git__realloc
-#define kreallocarray git__reallocarray
-#define kfree git__free
-#include "khash.h"
-
-__KHASH_TYPE(oid, const git_oid *, void *)
-typedef khash_t(oid) git_oidmap;
+typedef struct kh_oid_s git_oidmap;
 
 git_oidmap *git_oidmap_alloc(void);
 void git_oidmap_free(git_oidmap *map);
diff --git a/src/strmap.c b/src/strmap.c
index 0c35357..cee7f69 100644
--- a/src/strmap.c
+++ b/src/strmap.c
@@ -7,6 +7,15 @@
 
 #include "strmap.h"
 
+#define kmalloc git__malloc
+#define kcalloc git__calloc
+#define krealloc git__realloc
+#define kreallocarray git__reallocarray
+#define kfree git__free
+#include "khash.h"
+
+__KHASH_TYPE(str, const char *, void *)
+
 __KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
 
 int git_strmap_alloc(git_strmap **map)
diff --git a/src/strmap.h b/src/strmap.h
index 7856494..2649acb 100644
--- a/src/strmap.h
+++ b/src/strmap.h
@@ -9,16 +9,7 @@
 
 #include "common.h"
 
-#define kmalloc git__malloc
-#define kcalloc git__calloc
-#define krealloc git__realloc
-#define kreallocarray git__reallocarray
-#define kfree git__free
-#include "khash.h"
-
-__KHASH_TYPE(str, const char *, void *)
-typedef khash_t(str) git_strmap;
-typedef khiter_t git_strmap_iter;
+typedef struct kh_str_s git_strmap;
 
 int git_strmap_alloc(git_strmap **map);
 void git_strmap_free(git_strmap *map);