diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-04-12 21:35:58 +0200 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-04-12 21:36:31 +0200 |
| commit | 0df03d19ce07e6104008e55e4adef101e954071b (patch) | |
| tree | 0c7790868672c9e4b8c5b56aba57f474fade34f1 /cheesemap.c | |
| parent | 4b15348415a9c6b2ba719c65eafdc008bf5c20e0 (diff) | |
helper for calc'ing the layout
Diffstat (limited to 'cheesemap.c')
| -rw-r--r-- | cheesemap.c | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/cheesemap.c b/cheesemap.c index d17ecf4..ef98dc8 100644 --- a/cheesemap.c +++ b/cheesemap.c @@ -204,6 +204,24 @@ static inline cm_usize cm_ctrl_offset(cm_usize buckets, return cm_align_up(offset, ctrl_align); } +static inline void cm_raw_layout(const struct cm_type* type, cm_usize capacity, + cm_usize* out_buckets, + cm_usize* out_ctrl_offset, + cm_usize* out_size) { + cm_assert(type != NULL && out_buckets != NULL); + cm_assert(out_ctrl_offset != NULL && out_size != NULL); + + cm_usize buckets = cm_capacity_to_buckets(capacity); + cm_usize ctrl_offset = cm_ctrl_offset(buckets, type); + + cm_usize size = ctrl_offset + buckets + CM_GROUP_SIZE; + size = cm_align_up(size, cm_alloc_align(type)); + + *out_buckets = buckets; + *out_ctrl_offset = ctrl_offset; + *out_size = size; +} + static inline cm_u8* cm_raw_elem_at(const struct cheesemap_raw* map, cm_usize index, const struct cm_type* type) { @@ -373,19 +391,15 @@ static bool cm_raw_resize(struct cheesemap_raw* map, cm_hash_fn hash, } bool cm_raw_init_with(struct cheesemap_raw* map, cm_alloc_fn alloc, cm_u8* user, - const struct cm_type* type, cm_usize initial_capacity) { + const struct cm_type* type, cm_usize init_cap) { cm_assert(map != NULL); cm_assert(alloc != NULL); memset(map, 0, sizeof(*map)); - cm_usize buckets = cm_capacity_to_buckets(initial_capacity); - cm_usize ctrl_offset = cm_ctrl_offset(buckets, type); - - cm_usize align = cm_alloc_align(type); - cm_usize size = ctrl_offset + buckets + CM_GROUP_SIZE; - size = cm_align_up(size, align); + cm_usize buckets, ctrl_offset, size; + cm_raw_layout(type, init_cap, &buckets, &ctrl_offset, &size); - cm_u8* ptr = alloc(size, align, user); + cm_u8* ptr = alloc(size, cm_alloc_align(type), user); if (ptr == NULL) return false; cm_u8* ctrl = ptr + ctrl_offset; |
