khash: clarify that allocations never fail

We use our standard allocation functions and macros (xcalloc, ALLOC_ARRAY, REALLOC_ARRAY) in our version of khash.h. They terminate the program on error instead, so code that's using them doesn't have to handle allocation failures. Make this behavior explicit by turning kh_resize_ into a void function and removing the related unreachable error handling code. Helped-by: Jeff King <peff@peff.net> Signed-off-by: René Scharfe <l.s.r@web.de> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jul 3, 2021 at 14:57 UTC 5632e838f8fa73abfce3f66d2781b8e6d7b14001
1 file changed +5 -9
khash.h
+5 -9
@@ -74,7 +74,7 @@ static const double __ac_HASH_UPPER = 0.77;
74 void kh_destroy_##name(kh_##name##_t *h); \
75 void kh_clear_##name(kh_##name##_t *h); \
76 khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \
77 - int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
77 + void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
78 khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \
79 void kh_del_##name(kh_##name##_t *h, khint_t x);
80
@@ -116,7 +116,7 @@ static const double __ac_HASH_UPPER = 0.77;
116 return __ac_iseither(h->flags, i)? h->n_buckets : i; \
117 } else return 0; \
118 } \
119 - SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
119 + SCOPE void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
120 { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \
121 khint32_t *new_flags = NULL; \
122 khint_t j = 1; \
@@ -126,7 +126,6 @@ static const double __ac_HASH_UPPER = 0.77;
126 if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \
127 else { /* hash table size to be changed (shrink or expand); rehash */ \
128 ALLOC_ARRAY(new_flags, __ac_fsize(new_n_buckets)); \
129 - if (!new_flags) return -1; \
129 memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \
130 if (h->n_buckets < new_n_buckets) { /* expand */ \
131 REALLOC_ARRAY(h->keys, new_n_buckets); \
@@ -173,18 +172,15 @@ static const double __ac_HASH_UPPER = 0.77;
172 h->n_occupied = h->size; \
173 h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \
174 } \
176 - return 0; \
175 } \
176 SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \
177 { \
178 khint_t x; \
179 if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \
180 if (h->n_buckets > (h->size<<1)) { \
183 - if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \
184 - *ret = -1; return h->n_buckets; \
185 - } \
186 - } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \
187 - *ret = -1; return h->n_buckets; \
181 + kh_resize_##name(h, h->n_buckets - 1); /* clear "deleted" elements */ \
182 + } else { \
183 + kh_resize_##name(h, h->n_buckets + 1); /* expand the hash table */ \
184 } \
185 } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \
186 { \