diff --git a/ide/vs2022/mimalloc.vcxproj b/ide/vs2022/mimalloc.vcxproj index 616c226c..9811aa55 100644 --- a/ide/vs2022/mimalloc.vcxproj +++ b/ide/vs2022/mimalloc.vcxproj @@ -116,7 +116,7 @@ true true ../../include - MI_DEBUG=4;%(PreprocessorDefinitions); + MI_DEBUG=4;MI_SECURE=0;%(PreprocessorDefinitions); CompileAsCpp false stdcpp20 diff --git a/src/segment.c b/src/segment.c index 0a5ac3c7..22b9ccd0 100644 --- a/src/segment.c +++ b/src/segment.c @@ -336,12 +336,14 @@ static size_t mi_segment_calculate_slices(size_t required, size_t* pre_size, siz size_t page_size = _mi_os_page_size(); size_t isize = _mi_align_up(sizeof(mi_segment_t), page_size); size_t guardsize = 0; - + if (MI_SECURE>0) { // in secure mode, we set up a protected page in between the segment info // and the page data (and one at the end of the segment) - guardsize = page_size; - required = _mi_align_up(required, page_size); + guardsize = page_size; + if (required > 0) { + required = _mi_align_up(required, MI_SEGMENT_SLICE_SIZE) + page_size; + } } if (pre_size != NULL) *pre_size = isize; @@ -802,21 +804,27 @@ static mi_segment_t* mi_segment_init(mi_segment_t* segment, size_t required, siz size_t memid = 0; size_t align_offset = 0; size_t alignment = MI_SEGMENT_SIZE; - size_t segment_size = segment_slices * MI_SEGMENT_SLICE_SIZE; - + if (page_alignment > 0) { mi_assert_internal(huge_page != NULL); mi_assert_internal(page_alignment >= MI_SEGMENT_ALIGN); alignment = page_alignment; const size_t info_size = info_slices * MI_SEGMENT_SLICE_SIZE; align_offset = _mi_align_up( info_size, MI_SEGMENT_ALIGN ); - segment_size += _mi_align_up(align_offset - info_size, MI_SEGMENT_SLICE_SIZE); - segment_slices = segment_size / MI_SEGMENT_SLICE_SIZE; + const size_t extra = align_offset - info_size; + // recalculate due to potential guard pages + segment_slices = mi_segment_calculate_slices(required + extra, &pre_size, &info_slices); + //segment_size += _mi_align_up(align_offset - info_size, MI_SEGMENT_SLICE_SIZE); + //segment_slices = segment_size / MI_SEGMENT_SLICE_SIZE; } - else { + const size_t segment_size = segment_slices * MI_SEGMENT_SLICE_SIZE; + + // get from cache + if (page_alignment == 0) { segment = (mi_segment_t*)_mi_segment_cache_pop(segment_size, &commit_mask, &decommit_mask, &mem_large, &is_pinned, &is_zero, req_arena_id, &memid, os_tld); } + // get from OS if (segment==NULL) { segment = (mi_segment_t*)_mi_arena_alloc_aligned(segment_size, alignment, align_offset, &commit, &mem_large, &is_pinned, &is_zero, req_arena_id, &memid, os_tld); if (segment == NULL) return NULL; // failed to allocate diff --git a/test/test-api.c b/test/test-api.c index 01ef98bd..e7f3a4ed 100644 --- a/test/test-api.c +++ b/test/test-api.c @@ -149,7 +149,7 @@ int main(void) { for (size_t align = 1; align <= MI_ALIGNMENT_MAX && ok; align *= 2) { void* ps[8]; for (int i = 0; i < 8 && ok; i++) { - ps[i] = mi_malloc_aligned(align*13 /*size*/, align); + ps[i] = mi_malloc_aligned(align*5 /*size*/, align); if (ps[i] == NULL || (uintptr_t)(ps[i]) % align != 0) { ok = false; }