diff options
| author | Fabrice <fabrice@schaub-dev.xyz> | 2026-04-12 21:17:13 +0200 |
|---|---|---|
| committer | Fabrice <fabrice@schaub-dev.xyz> | 2026-04-12 21:17:13 +0200 |
| commit | 4b15348415a9c6b2ba719c65eafdc008bf5c20e0 (patch) | |
| tree | d8b9dc8a485e6f434955005c3c7cc7a54c60cdcd /cheesemap.c | |
| parent | d513ca9634142bf3ec681cfd561ff7cb005304d1 (diff) | |
fix alignment UB
Diffstat (limited to 'cheesemap.c')
| -rw-r--r-- | cheesemap.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/cheesemap.c b/cheesemap.c index c6e7077..d17ecf4 100644 --- a/cheesemap.c +++ b/cheesemap.c @@ -193,10 +193,15 @@ static inline cm_usize cm_capacity_to_buckets(cm_usize capacity) { return cm_max(buckets, CM_GROUP_SIZE); } +static inline cm_usize cm_alloc_align(const struct cm_type* type) { + return cm_max(type->entry_align, _Alignof(max_align_t)); +} + static inline cm_usize cm_ctrl_offset(cm_usize buckets, const struct cm_type* type) { cm_usize offset = type->entry_size * buckets; - return cm_align_up(offset, CM_GROUP_SIZE); + cm_usize ctrl_align = cm_max(type->entry_align, CM_GROUP_SIZE); + return cm_align_up(offset, ctrl_align); } static inline cm_u8* cm_raw_elem_at(const struct cheesemap_raw* map, @@ -375,9 +380,12 @@ bool cm_raw_init_with(struct cheesemap_raw* map, cm_alloc_fn alloc, cm_u8* user, 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_u8* ptr = alloc(size, user); + cm_u8* ptr = alloc(size, align, user); if (ptr == NULL) return false; cm_u8* ctrl = ptr + ctrl_offset; @@ -502,11 +510,11 @@ void cm_init(struct cheesemap* map, cm_usize key_size, cm_usize key_align, cm_assert(alloc != NULL && dealloc != NULL); cm_usize value_offset = cm_align_up(key_size, value_align); - cm_usize max_align = cm_max(key_align, value_align); - cm_usize entry_size = cm_align_up(value_offset + value_size, max_align); + cm_usize entry_align = cm_max(key_align, value_align); + cm_usize entry_size = cm_align_up(value_offset + value_size, entry_align); struct cm_type type = - cm_type_new(key_size, value_size, value_offset, entry_size); + cm_type_new(key_size, value_size, value_offset, entry_size, entry_align); *map = cm_init_inner(type, user, hash, compare, alloc, dealloc, cm_raw_new()); } |
