Update `khash.h` to pull request 42 https://github.com/attractivechaos/klib/pull/42/files introduces `kreallocarray`. Hook that up as our `git__reallocarray`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
diff --git a/src/khash.h b/src/khash.h
index e5789c4..818ac83 100644
--- a/src/khash.h
+++ b/src/khash.h
@@ -177,6 +177,9 @@ typedef khint_t khiter_t;
#ifndef krealloc
#define krealloc(P,Z) realloc(P,Z)
#endif
+#ifndef kreallocarray
+#define kreallocarray(P,N,Z) ((SIZE_MAX - N < Z) ? NULL : krealloc(P, (N*Z)))
+#endif
#ifndef kfree
#define kfree(P) free(P)
#endif
@@ -242,15 +245,15 @@ static const double __ac_HASH_UPPER = 0.77;
if (new_n_buckets < 4) new_n_buckets = 4; \
if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
else { /* hash table size to be changed (shrink or expand); rehash */ \
- new_flags = (khint32_t*)kmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
+ new_flags = (khint32_t*)kreallocarray(NULL, __ac_fsize(new_n_buckets), sizeof(khint32_t)); \
if (!new_flags) return -1; \
memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
if (h->n_buckets < new_n_buckets) { /* expand */ \
- khkey_t *new_keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
+ khkey_t *new_keys = (khkey_t*)kreallocarray((void *)h->keys, new_n_buckets, sizeof(khkey_t)); \
if (!new_keys) { kfree(new_flags); return -1; } \
h->keys = new_keys; \
if (kh_is_map) { \
- khval_t *new_vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
+ khval_t *new_vals = (khval_t*)kreallocarray((void *)h->vals, new_n_buckets, sizeof(khval_t)); \
if (!new_vals) { kfree(new_flags); return -1; } \
h->vals = new_vals; \
} \
@@ -285,8 +288,8 @@ static const double __ac_HASH_UPPER = 0.77;
} \
} \
if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \
- h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \
- if (kh_is_map) h->vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \
+ h->keys = (khkey_t*)kreallocarray((void *)h->keys, new_n_buckets, sizeof(khkey_t)); \
+ if (kh_is_map) h->vals = (khval_t*)kreallocarray((void *)h->vals, new_n_buckets, sizeof(khval_t)); \
} \
kfree(h->flags); /* free the working space */ \
h->flags = new_flags; \
diff --git a/src/offmap.h b/src/offmap.h
index cd46fd6..9471e7b 100644
--- a/src/offmap.h
+++ b/src/offmap.h
@@ -13,6 +13,7 @@
#define kmalloc git__malloc
#define kcalloc git__calloc
#define krealloc git__realloc
+#define kreallocarray git__reallocarray
#define kfree git__free
#include "khash.h"
diff --git a/src/oidmap.h b/src/oidmap.h
index b871a79..5e3b44b 100644
--- a/src/oidmap.h
+++ b/src/oidmap.h
@@ -13,6 +13,7 @@
#define kmalloc git__malloc
#define kcalloc git__calloc
#define krealloc git__realloc
+#define kreallocarray git__reallocarray
#define kfree git__free
#include "khash.h"
diff --git a/src/strmap.h b/src/strmap.h
index 8985aaf..dfbe563 100644
--- a/src/strmap.h
+++ b/src/strmap.h
@@ -12,6 +12,7 @@
#define kmalloc git__malloc
#define kcalloc git__calloc
#define krealloc git__realloc
+#define kreallocarray git__reallocarray
#define kfree git__free
#include "khash.h"