From 3c906bde8b0612bac5cec5207432f5dedc32da1b Mon Sep 17 00:00:00 2001 From: Daan Date: Sat, 4 Mar 2023 15:17:13 -0800 Subject: [PATCH 1/2] better track_free_size --- include/mimalloc-track.h | 13 +++++++++---- src/alloc-aligned.c | 4 +--- src/alloc.c | 4 ++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/mimalloc-track.h b/include/mimalloc-track.h index 35fb786a..3a2c2741 100644 --- a/include/mimalloc-track.h +++ b/include/mimalloc-track.h @@ -8,11 +8,16 @@ terms of the MIT license. A copy of the license can be found in the file #ifndef MIMALLOC_TRACK_H #define MIMALLOC_TRACK_H -// ------------------------------------------------------ -// Track memory ranges with macros for tools like Valgrind -// address sanitizer, or other memory checkers. -// ------------------------------------------------------ +/* ------------------------------------------------------------------------------------------------------ +Track memory ranges with macros for tools like Valgrind +address sanitizer, or other memory checkers. +The macros are set up such that the size passed to `mi_track_free_size` +matches the size of the allocation, or the newsize of a `mi_track_resize` (currently unused though). + +The `size` is either byte precise (and what the user requested) if `MI_PADDING` is enabled, +or otherwise it is the full block size which may be larger than the original request. +-------------------------------------------------------------------------------------------------------*/ #if MI_VALGRIND diff --git a/src/alloc-aligned.c b/src/alloc-aligned.c index 8059f4b5..a75c9947 100644 --- a/src/alloc-aligned.c +++ b/src/alloc-aligned.c @@ -65,6 +65,7 @@ static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t* mi_page_set_has_aligned(page, true); _mi_padding_shrink(page, (mi_block_t*)p, adjust + size); } + // todo: expand padding if overallocated and p==aligned_p ? mi_assert_internal(mi_page_usable_block_size(_mi_ptr_page(p)) >= adjust + size); mi_assert_internal(p == _mi_page_ptr_unalign(_mi_ptr_segment(aligned_p), _mi_ptr_page(aligned_p), aligned_p)); @@ -85,9 +86,6 @@ static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t* mi_track_free(p); mi_track_malloc(aligned_p, size, zero); } - else { - mi_track_resize(aligned_p, oversize, size); - } #endif return aligned_p; } diff --git a/src/alloc.c b/src/alloc.c index 9a91cc14..8ef78339 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -498,7 +498,7 @@ mi_block_t* _mi_page_ptr_unalign(const mi_segment_t* segment, const mi_page_t* p void mi_decl_noinline _mi_free_generic(const mi_segment_t* segment, mi_page_t* page, bool is_local, void* p) mi_attr_noexcept { mi_block_t* const block = (mi_page_has_aligned(page) ? _mi_page_ptr_unalign(segment, page, p) : (mi_block_t*)p); - mi_stat_free(page, block); // stat_free may access the padding + mi_stat_free(page, block); // stat_free may access the padding mi_track_free(p); _mi_free_block(page, is_local, block); } @@ -559,7 +559,7 @@ void mi_free(void* p) mi_attr_noexcept #if (MI_DEBUG!=0) && !MI_TRACK_ENABLED memset(block, MI_DEBUG_FREED, mi_page_block_size(page)); #endif - mi_track_free(p); + mi_track_free_size(p, mi_page_usable_size_of(page,block)); // faster then mi_usable_size as we already now the page and that p is unaligned mi_block_set_next(page, block, page->local_free); page->local_free = block; if mi_unlikely(--page->used == 0) { // using this expression generates better code than: page->used--; if (mi_page_all_free(page)) From 20ae35a1d4ef475631acc3674906f1051ced2c4f Mon Sep 17 00:00:00 2001 From: Daan Date: Sat, 4 Mar 2023 16:03:14 -0800 Subject: [PATCH 2/2] remove accidental -fsanitize --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 46bbc109..2e7b81b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -370,7 +370,7 @@ if(MI_BUILD_SHARED) set_target_properties(mimalloc PROPERTIES VERSION ${mi_version} SOVERSION ${mi_version_major} OUTPUT_NAME ${mi_basename} ) target_compile_definitions(mimalloc PRIVATE ${mi_defines} MI_SHARED_LIB MI_SHARED_LIB_EXPORT) target_compile_options(mimalloc PRIVATE ${mi_cflags}) - target_link_libraries(mimalloc PRIVATE ${mi_libraries} -fsanitize=address) + target_link_libraries(mimalloc PRIVATE ${mi_libraries}) target_include_directories(mimalloc PUBLIC $ $