From ea6137a5017a407ffedafd2757ee6d4a840668fc Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Sun, 24 Mar 2024 09:01:58 -0700 Subject: [PATCH] use MI_MAX_ALIGN_SIZE to adjust block_offset_adj --- include/mimalloc/types.h | 4 ++-- src/page.c | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/mimalloc/types.h b/include/mimalloc/types.h index ad0aabe9..5bc49aa0 100644 --- a/include/mimalloc/types.h +++ b/include/mimalloc/types.h @@ -297,8 +297,8 @@ typedef struct mi_page_s { mi_block_t* free; // list of available free blocks (`malloc` allocates from this list) mi_block_t* local_free; // list of deferred free blocks by this thread (migrates to `free`) uint16_t used; // number of blocks in use (including blocks in `thread_free`) - uint8_t block_size_shift; // if not zero, then `(1 << block_size_shift == block_size)` (used for fast path in `free.c:_mi_page_ptr_unalign`) - uint8_t block_offset_adj; // if not zero, then `(page_start - (uint8_t*)page - 8*(block_offset_adj-1)) % block_size == 0)` (used for fast path in `free.c:_mi_page_ptr_unalign`) + uint8_t block_size_shift; // if not zero, then `(1 << block_size_shift) == block_size` (only used for fast path in `free.c:_mi_page_ptr_unalign`) + uint8_t block_offset_adj; // if not zero, then `(mi_page_start(_,page,_) - (uint8_t*)page - MI_MAX_ALIGN_SIZE*(block_offset_adj-1)) % block_size == 0)` (only used for fast path in `free.c:_mi_page_ptr_unalign`) uint32_t xblock_size; // size available in each block (always `>0`) #if (MI_ENCODE_FREELIST || MI_PADDING) diff --git a/src/page.c b/src/page.c index d9e416b2..912f969a 100644 --- a/src/page.c +++ b/src/page.c @@ -682,8 +682,10 @@ static void mi_page_init(mi_heap_t* heap, mi_page_t* page, size_t block_size, mi if (block_size > 0) { const ptrdiff_t start_offset = (uint8_t*)page_start - (uint8_t*)page; const ptrdiff_t start_adjust = start_offset % block_size; - if (start_offset >= 0 && (start_adjust % 8) == 0 && (start_adjust/8) < 255) { - page->block_offset_adj = (uint8_t)((start_adjust/8) + 1); + if (start_offset >= 0 && (start_adjust % MI_MAX_ALIGN_SIZE) == 0 && (start_adjust / MI_MAX_ALIGN_SIZE) < 255) { + const ptrdiff_t adjust = (start_adjust / MI_MAX_ALIGN_SIZE); + mi_assert_internal(adjust + 1 == (uint8_t)(adjust + 1)); + page->block_offset_adj = (uint8_t)(adjust + 1); } } @@ -700,7 +702,7 @@ static void mi_page_init(mi_heap_t* heap, mi_page_t* page, size_t block_size, mi mi_assert_internal(page->keys[1] != 0); #endif mi_assert_internal(page->block_size_shift == 0 || (block_size == (1UL << page->block_size_shift))); - mi_assert_internal(page->block_offset_adj == 0 || (((uint8_t*)page_start - (uint8_t*)page - 8*(page->block_offset_adj-1))) % block_size == 0); + mi_assert_internal(page->block_offset_adj == 0 || (((uint8_t*)page_start - (uint8_t*)page - MI_MAX_ALIGN_SIZE*(page->block_offset_adj-1))) % block_size == 0); mi_assert_expensive(mi_page_is_valid_init(page)); // initialize an initial free list