From 5eb8c752f7dc2aaef5080831b1f8ff8092bc25af Mon Sep 17 00:00:00 2001 From: Daan Date: Tue, 13 Aug 2024 16:36:53 -0700 Subject: [PATCH 1/3] fix UINT32_MAX constant (see issue #913) --- src/segment-map.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/segment-map.c b/src/segment-map.c index 8927a8bd..2c3964fe 100644 --- a/src/segment-map.c +++ b/src/segment-map.c @@ -22,7 +22,7 @@ terms of the MIT license. A copy of the license can be found in the file #elif (MI_INTPTR_SIZE > 4) #define MI_SEGMENT_MAP_MAX_ADDRESS (48*1024ULL*MI_GiB) // 48 TiB #else -#define MI_SEGMENT_MAP_MAX_ADDRESS (MAX_UINT32) +#define MI_SEGMENT_MAP_MAX_ADDRESS (UINT32_MAX) #endif #define MI_SEGMENT_MAP_PART_SIZE (MI_INTPTR_SIZE*MI_KiB - 128) // 128 > sizeof(mi_memid_t) ! From b5c6495f69fa0d642aa40211df61274b801c8915 Mon Sep 17 00:00:00 2001 From: daanx Date: Tue, 20 Aug 2024 15:58:36 -0700 Subject: [PATCH 2/3] don't consider memory as large OS pages if only madvise'd --- ide/vs2022/mimalloc-override.vcxproj | 8 +++--- src/prim/unix/prim.c | 9 ++++--- test/main-override-static.c | 39 +++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/ide/vs2022/mimalloc-override.vcxproj b/ide/vs2022/mimalloc-override.vcxproj index e895fa3c..4383d886 100644 --- a/ide/vs2022/mimalloc-override.vcxproj +++ b/ide/vs2022/mimalloc-override.vcxproj @@ -98,7 +98,7 @@ MI_SHARED_LIB;MI_SHARED_LIB_EXPORT;MI_MALLOC_OVERRIDE;%(PreprocessorDefinitions); MultiThreadedDebugDLL false - Default + CompileAsCpp $(ProjectDir)\..\..\bin\mimalloc-redirect32.lib;%(AdditionalDependencies) @@ -126,7 +126,7 @@ MI_DEBUG=4;MI_SHARED_LIB;MI_SHARED_LIB_EXPORT;MI_MALLOC_OVERRIDE;%(PreprocessorDefinitions); MultiThreadedDebugDLL false - Default + CompileAsCpp $(ProjectDir)\..\..\bin\mimalloc-redirect.lib;%(AdditionalDependencies) @@ -157,7 +157,7 @@ $(IntDir) false MultiThreadedDLL - Default + CompileAsCpp false @@ -189,7 +189,7 @@ $(IntDir) false MultiThreadedDLL - Default + CompileAsCpp false diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index 63a36f25..0ea8189c 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -181,10 +181,11 @@ int _mi_prim_free(void* addr, size_t size ) { static int unix_madvise(void* addr, size_t size, int advice) { #if defined(__sun) - return madvise((caddr_t)addr, size, advice); // Solaris needs cast (issue #520) + int res = madvise((caddr_t)addr, size, advice); // Solaris needs cast (issue #520) #else - return madvise(addr, size, advice); + int res = madvise(addr, size, advice); #endif + return (res==0 ? 0 : errno); } static void* unix_mmap_prim(void* addr, size_t size, size_t try_alignment, int protect_flags, int flags, int fd) { @@ -331,7 +332,7 @@ static void* unix_mmap(void* addr, size_t size, size_t try_alignment, int protec // when large OS pages are enabled for mimalloc, we call `madvise` anyways. if (allow_large && _mi_os_use_large_page(size, try_alignment)) { if (unix_madvise(p, size, MADV_HUGEPAGE) == 0) { - *is_large = true; // possibly + // *is_large = true; // possibly }; } #elif defined(__sun) @@ -340,7 +341,7 @@ static void* unix_mmap(void* addr, size_t size, size_t try_alignment, int protec cmd.mha_pagesize = _mi_os_large_page_size(); cmd.mha_cmd = MHA_MAPSIZE_VA; if (memcntl((caddr_t)p, size, MC_HAT_ADVISE, (caddr_t)&cmd, 0, 0) == 0) { - *is_large = true; + // *is_large = true; // possibly } } #endif diff --git a/test/main-override-static.c b/test/main-override-static.c index bf1cc416..535a9aaf 100644 --- a/test/main-override-static.c +++ b/test/main-override-static.c @@ -18,11 +18,13 @@ static void test_reserved(void); static void negative_stat(void); static void alloc_huge(void); static void test_heap_walk(void); +// static void test_large_pages(void); int main() { mi_version(); mi_stats_reset(); + // test_large_pages(); // detect double frees and heap corruption // double_free1(); // double_free2(); @@ -61,7 +63,7 @@ int main() { //mi_stats_print(NULL); // test_process_info(); - + return 0; } @@ -216,6 +218,41 @@ static void test_heap_walk(void) { mi_heap_visit_blocks(heap, true, &test_visit, NULL); } +// Experiment with huge OS pages +#if 0 + +#include +#include +#include +#include + +static void test_large_pages(void) { + mi_memid_t memid; + + #if 0 + size_t pages_reserved; + size_t page_size; + uint8_t* p = (uint8_t*)_mi_os_alloc_huge_os_pages(1, -1, 30000, &pages_reserved, &page_size, &memid); + const size_t req_size = pages_reserved * page_size; + #else + const size_t req_size = 64*MI_MiB; + uint8_t* p = (uint8_t*)_mi_os_alloc(req_size,&memid,NULL); + #endif + + p[0] = 1; + + //_mi_os_protect(p, _mi_os_page_size()); + //_mi_os_unprotect(p, _mi_os_page_size()); + //_mi_os_decommit(p, _mi_os_page_size(), NULL); + if (madvise(p, req_size, MADV_HUGEPAGE) == 0) { + printf("advised huge pages\n"); + _mi_os_decommit(p, _mi_os_page_size(), NULL); + }; + _mi_os_free(p, req_size, memid, NULL); +} + +#endif + // ---------------------------- // bin size experiments // ------------------------------ From b9b529de2817ccaddbab814d5636f2569464cb1b Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Wed, 21 Aug 2024 10:45:19 -0700 Subject: [PATCH 3/3] shuffle for 128 bit --- include/mimalloc/internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mimalloc/internal.h b/include/mimalloc/internal.h index 6e87d5ae..9d1f6e51 100644 --- a/include/mimalloc/internal.h +++ b/include/mimalloc/internal.h @@ -731,7 +731,7 @@ static inline mi_memid_t _mi_memid_create_os(bool committed, bool is_zero, bool static inline uintptr_t _mi_random_shuffle(uintptr_t x) { if (x==0) { x = 17; } // ensure we don't get stuck in generating zeros -#if (MI_INTPTR_SIZE==8) +#if (MI_INTPTR_SIZE>=8) // by Sebastiano Vigna, see: x ^= x >> 30; x *= 0xbf58476d1ce4e5b9UL;