diff --git a/include/mimalloc/prim.h b/include/mimalloc/prim.h index 606b7199..bddd66e9 100644 --- a/include/mimalloc/prim.h +++ b/include/mimalloc/prim.h @@ -22,14 +22,14 @@ terms of the MIT license. A copy of the license can be found in the file // OS memory configuration typedef struct mi_os_mem_config_s { - size_t page_size; // default to 4KiB - size_t large_page_size; // 0 if not supported, usually 2MiB (4MiB on Windows) - size_t alloc_granularity; // smallest allocation size (usually 4KiB, on Windows 64KiB) - size_t physical_memory; // physical memory size - size_t virtual_address_bits; // usually 48 or 56 bits on 64-bit systems. (used to determine secure randomization) - bool has_overcommit; // can we reserve more memory than can be actually committed? - bool has_partial_free; // can allocated blocks be freed partially? (true for mmap, false for VirtualAlloc) - bool has_virtual_reserve; // supports virtual address space reservation? (if true we can reserve virtual address space without using commit or physical memory) + size_t page_size; // default to 4KiB + size_t large_page_size; // 0 if not supported, usually 2MiB (4MiB on Windows) + size_t alloc_granularity; // smallest allocation size (usually 4KiB, on Windows 64KiB) + size_t physical_memory_in_kib; // physical memory size in KiB + size_t virtual_address_bits; // usually 48 or 56 bits on 64-bit systems. (used to determine secure randomization) + bool has_overcommit; // can we reserve more memory than can be actually committed? + bool has_partial_free; // can allocated blocks be freed partially? (true for mmap, false for VirtualAlloc) + bool has_virtual_reserve; // supports virtual address space reservation? (if true we can reserve virtual address space without using commit or physical memory) } mi_os_mem_config_t; // Initialize @@ -124,7 +124,7 @@ void _mi_prim_thread_associate_default_heap(mi_heap_t* heap); //------------------------------------------------------------------- // Access to TLS (thread local storage) slots. // We need fast access to both a unique thread id (in `free.c:mi_free`) and -// to a thread-local heap pointer (in `alloc.c:mi_malloc`). +// to a thread-local heap pointer (in `alloc.c:mi_malloc`). // To achieve this we use specialized code for various platforms. //------------------------------------------------------------------- diff --git a/src/os.c b/src/os.c index 61c9eebf..2472b803 100644 --- a/src/os.c +++ b/src/os.c @@ -18,17 +18,17 @@ terms of the MIT license. A copy of the license can be found in the file ----------------------------------------------------------- */ #ifndef MI_DEFAULT_VIRTUAL_ADDRESS_BITS #if MI_INTPTR_SIZE < 8 -#define MI_DEFAULT_VIRTUAL_ADDRESS_BITS 32 +#define MI_DEFAULT_VIRTUAL_ADDRESS_BITS 32 #else -#define MI_DEFAULT_VIRTUAL_ADDRESS_BITS 48 +#define MI_DEFAULT_VIRTUAL_ADDRESS_BITS 48 #endif #endif -#ifndef MI_DEFAULT_PHYSICAL_MEMORY +#ifndef MI_DEFAULT_PHYSICAL_MEMORY_IN_KIB #if MI_INTPTR_SIZE < 8 -#define MI_DEFAULT_PHYSICAL_MEMORY 4*MI_GiB +#define MI_DEFAULT_PHYSICAL_MEMORY_IN_KIB 4*MI_MiB // 4 GiB #else -#define MI_DEFAULT_PHYSICAL_MEMORY 32*MI_GiB +#define MI_DEFAULT_PHYSICAL_MEMORY_IN_KIB 32*MI_MiB // 32 GiB #endif #endif @@ -36,7 +36,7 @@ static mi_os_mem_config_t mi_os_mem_config = { 4096, // page size 0, // large page size (usually 2MiB) 4096, // allocation granularity - MI_DEFAULT_PHYSICAL_MEMORY, + MI_DEFAULT_PHYSICAL_MEMORY_IN_KIB, MI_DEFAULT_VIRTUAL_ADDRESS_BITS, true, // has overcommit? (if true we use MAP_NORESERVE on mmap systems) false, // can we partially free allocated blocks? (on mmap systems we can free anywhere in a mapped range, but on Windows we must free the entire span) diff --git a/src/prim/unix/prim.c b/src/prim/unix/prim.c index 46869c86..6103b4e2 100644 --- a/src/prim/unix/prim.c +++ b/src/prim/unix/prim.c @@ -142,8 +142,9 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) config->alloc_granularity = (size_t)psize; #if defined(_SC_PHYS_PAGES) long pphys = sysconf(_SC_PHYS_PAGES); - if (pphys > 0 && (size_t)pphys < (SIZE_MAX/(size_t)psize)) { - config->physical_memory = (size_t)pphys * (size_t)psize; + const size_t psize_in_kib = (size_t)psize / MI_KiB; + if (psize_in_kib > 0 && pphys > 0 && (size_t)pphys <= (SIZE_MAX/psize_in_kib)) { + config->physical_memory_in_kib = (size_t)pphys * psize_in_kib; } #endif } diff --git a/src/prim/windows/prim.c b/src/prim/windows/prim.c index aa79eb91..20f833bd 100644 --- a/src/prim/windows/prim.c +++ b/src/prim/windows/prim.c @@ -154,8 +154,8 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) if (pGetPhysicallyInstalledSystemMemory != NULL) { ULONGLONG memInKiB = 0; if ((*pGetPhysicallyInstalledSystemMemory)(&memInKiB)) { - if (memInKiB > 0 && memInKiB < (SIZE_MAX / MI_KiB)) { - config->physical_memory = (size_t)memInKiB * MI_KiB; + if (memInKiB > 0 && memInKiB <= SIZE_MAX) { + config->physical_memory_in_kib = (size_t)memInKiB; } } } @@ -643,7 +643,7 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) { } else if (reason==DLL_THREAD_DETACH && !_mi_is_redirected()) { _mi_thread_done(NULL); - } + } }