From cf89fc6338945fb9e1be4532cc9e98c58b77f121 Mon Sep 17 00:00:00 2001 From: Biswapriyo Nath Date: Fri, 18 Feb 2022 12:06:08 +0530 Subject: [PATCH 1/4] Fix strict function prototype warnings Fix warning: function declaration isn't a prototype [-Wstrict-prototypes] In C int foo() and int foo(void) are different functions. --- src/os.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/os.c b/src/os.c index 757e8cab..e458946c 100644 --- a/src/os.c +++ b/src/os.c @@ -107,7 +107,7 @@ bool _mi_os_has_overcommit(void) { } // OS (small) page size -size_t _mi_os_page_size() { +size_t _mi_os_page_size(void) { return os_page_size; } @@ -159,7 +159,7 @@ static PGetCurrentProcessorNumberEx pGetCurrentProcessorNumberEx = NULL; static PGetNumaProcessorNodeEx pGetNumaProcessorNodeEx = NULL; static PGetNumaNodeProcessorMaskEx pGetNumaNodeProcessorMaskEx = NULL; -static bool mi_win_enable_large_os_pages() +static bool mi_win_enable_large_os_pages(void) { if (large_os_page_size > 0) return true; @@ -230,7 +230,7 @@ void _mi_os_init(void) } } #elif defined(__wasi__) -void _mi_os_init() { +void _mi_os_init(void) { os_overcommit = false; os_page_size = 64*MI_KiB; // WebAssembly has a fixed page size: 64KiB os_alloc_granularity = 16; @@ -261,7 +261,7 @@ static void os_detect_overcommit(void) { #endif } -void _mi_os_init() { +void _mi_os_init(void) { // get the page size long result = sysconf(_SC_PAGESIZE); if (result > 0) { @@ -1302,7 +1302,7 @@ void _mi_os_free_huge_pages(void* p, size_t size, mi_stats_t* stats) { Support NUMA aware allocation -----------------------------------------------------------------------------*/ #ifdef _WIN32 -static size_t mi_os_numa_nodex() { +static size_t mi_os_numa_nodex(void) { USHORT numa_node = 0; if (pGetCurrentProcessorNumberEx != NULL && pGetNumaProcessorNodeEx != NULL) { // Extended API is supported From 1f8138a4dc08c906e34b047f4e462d1c292a35ee Mon Sep 17 00:00:00 2001 From: Roman Gershman Date: Mon, 28 Feb 2022 09:25:56 +0200 Subject: [PATCH 2/4] Fix wrong reporting of area used bytes Fixes #552 --- src/heap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/heap.c b/src/heap.c index d560fbc6..7cb4e5d1 100644 --- a/src/heap.c +++ b/src/heap.c @@ -530,7 +530,7 @@ static bool mi_heap_visit_areas_page(mi_heap_t* heap, mi_page_queue_t* pq, mi_pa xarea.area.reserved = page->reserved * bsize; xarea.area.committed = page->capacity * bsize; xarea.area.blocks = _mi_page_start(_mi_page_segment(page), page, NULL); - xarea.area.used = page->used; + xarea.area.used = page->used * bsize; xarea.area.block_size = bsize; return fun(heap, &xarea, arg); } From c027c27c0aad8d0389e1b0f1fbc01c789b3835ae Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 30 Mar 2022 18:45:22 +0100 Subject: [PATCH 3/4] update the docs to clarify the proper C++ override usage. --- docs/overrides.html | 2 +- docs/using.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/overrides.html b/docs/overrides.html index 0e7fd0ec..fc0ad591 100644 --- a/docs/overrides.html +++ b/docs/overrides.html @@ -118,7 +118,7 @@ $(document).ready(function(){initNavTree('overrides.html',''); initResizable();

Windows

Overriding on Windows is robust and has the particular advantage to be able to redirect all malloc/free calls that go through the (dynamic) C runtime allocator, including those from other DLL's or libraries.

The overriding on Windows requires that you link your program explicitly with the mimalloc DLL and use the C-runtime library as a DLL (using the /MD or /MDd switch). Also, the mimalloc-redirect.dll (or mimalloc-redirect32.dll) must be available in the same folder as the main mimalloc-override.dll at runtime (as it is a dependency). The redirection DLL ensures that all calls to the C runtime malloc API get redirected to mimalloc (in mimalloc-override.dll).

-

To ensure the mimalloc DLL is loaded at run-time it is easiest to insert some call to the mimalloc API in the main function, like mi_version() (or use the /INCLUDE:mi_version switch on the linker). See the mimalloc-override-test project for an example on how to use this. For best performance on Windows with C++, it is also recommended to also override the new/delete operations (by including mimalloc-new-delete.h a single(!) source file in your project).

+

To ensure the mimalloc DLL is loaded at run-time it is easiest to insert some call to the mimalloc API in the main function, like mi_version() (or use the /INCLUDE:mi_version switch on the linker). See the mimalloc-override-test project for an example on how to use this. For best performance on Windows with C++, it is also recommended to also override the new/delete operations (by including mimalloc-new-delete.h a single(!) source file in your project without linking to the mimalloc library).

The environment variable MIMALLOC_DISABLE_REDIRECT=1 can be used to disable dynamic overriding at run-time. Use MIMALLOC_VERBOSE=1 to check if mimalloc was successfully redirected.

(Note: in principle, it is possible to even patch existing executables without any recompilation if they are linked with the dynamic C runtime (ucrtbase.dll) – just put the mimalloc-override.dll into the import table (and put mimalloc-redirect.dll in the same folder) Such patching can be done for example with CFF Explorer).

Static override

diff --git a/docs/using.html b/docs/using.html index e6aad1a2..140f0c5c 100644 --- a/docs/using.html +++ b/docs/using.html @@ -105,7 +105,7 @@ $(document).ready(function(){initNavTree('using.html',''); initResizable(); });

to link with the shared (dynamic) library, or:

target_link_libraries(myapp PUBLIC mimalloc-static)

to link with the static library. See test\CMakeLists.txt for an example.

C++

-

For best performance in C++ programs, it is also recommended to override the global new and delete operators. For convience, mimalloc provides mimalloc-new-delete.h which does this for you – just include it in a single(!) source file in your project.

+

For best performance in C++ programs, it is also recommended to override the global new and delete operators. For convience, mimalloc provides mimalloc-new-delete.h which does this for you – just include it in a single(!) source file in your project without linking to the mimalloc's library.

In C++, mimalloc also provides the mi_stl_allocator struct which implements the std::allocator interface. For example:

std::vector<some_struct, mi_stl_allocator<some_struct>> vec;
vec.push_back(some_struct());

Statistics

From b7677b6f8482daad8374b2cf5430be2156308063 Mon Sep 17 00:00:00 2001 From: Daan Date: Fri, 8 Apr 2022 14:09:38 -0700 Subject: [PATCH 4/4] fix atomic warnings on clang14 (issue #571) --- include/mimalloc-atomic.h | 8 +++++++- src/init.c | 10 +++++----- src/os.c | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/mimalloc-atomic.h b/include/mimalloc-atomic.h index e07df84d..7ad5da58 100644 --- a/include/mimalloc-atomic.h +++ b/include/mimalloc-atomic.h @@ -23,10 +23,15 @@ terms of the MIT license. A copy of the license can be found in the file #define _Atomic(tp) std::atomic #define mi_atomic(name) std::atomic_##name #define mi_memory_order(name) std::memory_order_##name +#if !defined(ATOMIC_VAR_INIT) || (__cplusplus >= 202002L) // c++20, see issue #571 + #define MI_ATOMIC_VAR_INIT(x) x +#else + #define MI_ATOMIC_VAR_INIT(x) ATOMIC_VAR_INIT(x) +#endif #elif defined(_MSC_VER) // Use MSVC C wrapper for C11 atomics #define _Atomic(tp) tp -#define ATOMIC_VAR_INIT(x) x +#define MI_ATOMIC_VAR_INIT(x) x #define mi_atomic(name) mi_atomic_##name #define mi_memory_order(name) mi_memory_order_##name #else @@ -34,6 +39,7 @@ terms of the MIT license. A copy of the license can be found in the file #include #define mi_atomic(name) atomic_##name #define mi_memory_order(name) memory_order_##name +#define MI_ATOMIC_VAR_INIT(x) ATOMIC_VAR_INIT(x) #endif // Various defines for all used memory orders in mimalloc diff --git a/src/init.c b/src/init.c index 854a2228..ad1e4d45 100644 --- a/src/init.c +++ b/src/init.c @@ -25,8 +25,8 @@ const mi_page_t _mi_page_empty = { 0, // used 0, // xblock_size NULL, // local_free - ATOMIC_VAR_INIT(0), // xthread_free - ATOMIC_VAR_INIT(0), // xheap + MI_ATOMIC_VAR_INIT(0), // xthread_free + MI_ATOMIC_VAR_INIT(0), // xheap NULL, NULL }; @@ -91,7 +91,7 @@ mi_decl_cache_align const mi_heap_t _mi_heap_empty = { NULL, MI_SMALL_PAGES_EMPTY, MI_PAGE_QUEUES_EMPTY, - ATOMIC_VAR_INIT(NULL), + MI_ATOMIC_VAR_INIT(NULL), 0, // tid 0, // cookie { 0, 0 }, // keys @@ -123,7 +123,7 @@ mi_heap_t _mi_heap_main = { &tld_main, MI_SMALL_PAGES_EMPTY, MI_PAGE_QUEUES_EMPTY, - ATOMIC_VAR_INIT(NULL), + MI_ATOMIC_VAR_INIT(NULL), 0, // thread id 0, // initial cookie { 0, 0 }, // the key of the main heap can be fixed (unlike page keys that need to be secure!) @@ -325,7 +325,7 @@ bool _mi_is_main_thread(void) { return (_mi_heap_main.thread_id==0 || _mi_heap_main.thread_id == _mi_thread_id()); } -static _Atomic(size_t) thread_count = ATOMIC_VAR_INIT(1); +static _Atomic(size_t) thread_count = MI_ATOMIC_VAR_INIT(1); size_t _mi_current_thread_count(void) { return mi_atomic_load_relaxed(&thread_count); diff --git a/src/os.c b/src/os.c index 52939faa..f36f8480 100644 --- a/src/os.c +++ b/src/os.c @@ -983,7 +983,7 @@ static bool mi_os_resetx(void* addr, size_t size, bool reset, mi_stats_t* stats) if (p != start) return false; #else #if defined(MADV_FREE) - static _Atomic(size_t) advice = ATOMIC_VAR_INIT(MADV_FREE); + static _Atomic(size_t) advice = MI_ATOMIC_VAR_INIT(MADV_FREE); int oadvice = (int)mi_atomic_load_relaxed(&advice); int err; while ((err = mi_madvise(start, csize, oadvice)) != 0 && errno == EAGAIN) { errno = 0; };