khash: move internal helpers from KHASH_DECLARE to KHASH_DEFINE

Moved kh_data_size_##name() and kh_flags_##name() from KHASH_DECLARE
to KHASH_DEFINE section where internal implementation details belong.

This improves the separation of concerns:
- KHASH_DECLARE: public API only (struct definition, function declarations)
- KHASH_DEFINE: implementation details and internal helper functions

No functional changes, only better code organization.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-08-02 21:35:45 +09:00
parent 8bcd0d247e
commit 71c5e2c536
+8 -8
View File
@@ -78,11 +78,6 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0};
khint_t n_buckets; /* Number of buckets (power of 2) */ \
khint_t size; /* Number of elements */ \
} kh_##name##_t; \
/* Size calculation helper */ \
static inline size_t kh_data_size_##name(khint_t count) { \
return sizeof(khkey_t) * count + \
(kh_is_map ? sizeof(khval_t) * count : 0); \
} \
/* Address calculation functions for optimized memory layout */ \
static inline khkey_t* kh_keys_##name(const kh_##name##_t *h) { \
return (khkey_t*)(h)->data; \
@@ -91,9 +86,6 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0};
return kh_is_map ? \
(khval_t*)((uint8_t*)(h)->data + sizeof(khkey_t) * (h)->n_buckets) : NULL; \
} \
static inline uint8_t* kh_flags_##name(const kh_##name##_t *h) { \
return (uint8_t*)(h)->data + kh_data_size_##name((h)->n_buckets); \
} \
void kh_alloc_##name(mrb_state *mrb, kh_##name##_t *h); \
kh_##name##_t *kh_init_##name##_size(mrb_state *mrb, khint_t size); \
kh_##name##_t *kh_init_##name(mrb_state *mrb); \
@@ -119,6 +111,14 @@ static const uint8_t __m_either[] = {0x03, 0x0c, 0x30, 0xc0};
*/
#define KHASH_DEFINE(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
mrb_noreturn void mrb_raise_nomemory(mrb_state *mrb); \
/* Internal helper functions */ \
static inline size_t kh_data_size_##name(khint_t count) { \
return sizeof(khkey_t) * count + \
(kh_is_map ? sizeof(khval_t) * count : 0); \
} \
static inline uint8_t* kh_flags_##name(const kh_##name##_t *h) { \
return (uint8_t*)(h)->data + kh_data_size_##name((h)->n_buckets); \
} \
/* Small table optimization functions */ \
static inline int kh_is_small_##name(const kh_##name##_t *h) { \
return h->n_buckets == 0; /* Small table marker */ \