diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a5d2f60..eb679771 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,12 +4,13 @@ include("cmake/mimalloc-config-version.cmake") set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) -option(MI_OVERRIDE "Override the standard malloc interface" ON) -option(MI_INTERPOSE "Use interpose to override standard malloc on macOS" ON) -option(MI_SEE_ASM "Generate assembly files" OFF) -option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode" OFF) -option(MI_USE_CXX "Use the C++ compiler to compile the library" OFF) -option(MI_SECURE "Use security mitigations (like guard pages and randomization)" OFF) +option(MI_OVERRIDE "Override the standard malloc interface" ON) +option(MI_INTERPOSE "Use interpose to override standard malloc on macOS" ON) +option(MI_SEE_ASM "Generate assembly files" OFF) +option(MI_CHECK_FULL "Use full internal invariant checking in DEBUG mode" OFF) +option(MI_USE_CXX "Use the C++ compiler to compile the library" OFF) +option(MI_SECURE "Use security mitigations (like guard pages and randomization)" OFF) +option(MI_BUILD_TESTS "Build test executables" ON) set(mi_install_dir "lib/mimalloc-${mi_version}") @@ -191,21 +192,24 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/mimalloc-obj.dir/src/static # ----------------------------------------------------------------------------- # API surface testing # ----------------------------------------------------------------------------- -add_executable(mimalloc-test-api test/test-api.c) -target_compile_definitions(mimalloc-test-api PRIVATE ${mi_defines}) -target_compile_options(mimalloc-test-api PRIVATE ${mi_cflags}) -target_include_directories(mimalloc-test-api PRIVATE include) -target_link_libraries(mimalloc-test-api PRIVATE mimalloc-static) -add_executable(mimalloc-test-stress test/test-stress.c) -target_compile_definitions(mimalloc-test-stress PRIVATE ${mi_defines}) -target_compile_options(mimalloc-test-stress PRIVATE ${mi_cflags}) -target_include_directories(mimalloc-test-stress PRIVATE include) -target_link_libraries(mimalloc-test-stress PRIVATE mimalloc-static) +if (MI_BUILD_TESTS MATCHES "ON") + add_executable(mimalloc-test-api test/test-api.c) + target_compile_definitions(mimalloc-test-api PRIVATE ${mi_defines}) + target_compile_options(mimalloc-test-api PRIVATE ${mi_cflags}) + target_include_directories(mimalloc-test-api PRIVATE include) + target_link_libraries(mimalloc-test-api PRIVATE mimalloc-static ${mi_libraries}) -enable_testing() -add_test(test_api, mimalloc-test-api) -add_test(test_stress, mimalloc-test-stress) + add_executable(mimalloc-test-stress test/test-stress.c) + target_compile_definitions(mimalloc-test-stress PRIVATE ${mi_defines}) + target_compile_options(mimalloc-test-stress PRIVATE ${mi_cflags}) + target_include_directories(mimalloc-test-stress PRIVATE include) + target_link_libraries(mimalloc-test-stress PRIVATE mimalloc-static ${mi_libraries}) + + enable_testing() + add_test(test_api, mimalloc-test-api) + add_test(test_stress, mimalloc-test-stress) +endif() # ----------------------------------------------------------------------------- # Set override properties diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h index 3ddb734d..8b6d4124 100644 --- a/include/mimalloc-internal.h +++ b/include/mimalloc-internal.h @@ -10,7 +10,7 @@ terms of the MIT license. A copy of the license can be found in the file #include "mimalloc-types.h" -#if defined(MI_MALLOC_OVERRIDE) && defined(__APPLE__) +#if defined(MI_MALLOC_OVERRIDE) && (defined(__APPLE__) || defined(__OpenBSD__)) #define MI_TLS_RECURSE_GUARD #endif diff --git a/readme.md b/readme.md index c2efcc19..9352199d 100644 --- a/readme.md +++ b/readme.md @@ -13,7 +13,7 @@ Initially developed by Daan Leijen for the run-time systems of the [Koka](https://github.com/koka-lang/koka) and [Lean](https://github.com/leanprover/lean) languages. It is a drop-in replacement for `malloc` and can be used in other programs -without code changes, for example, on Unix you can use it as: +without code changes, for example, on dynamically linked ELF-based systems (Linux, BSD, etc.) you can use it as: ``` > LD_PRELOAD=/usr/bin/libmimalloc.so myprogram ``` @@ -117,7 +117,7 @@ Notes: The preferred usage is including ``, linking with the shared- or static library, and using the `mi_malloc` API exclusively for allocation. For example, ``` -gcc -o myprogram -lmimalloc myfile.c +> gcc -o myprogram -lmimalloc myfile.c ``` mimalloc uses only safe OS calls (`mmap` and `VirtualAlloc`) and can co-exist @@ -207,20 +207,21 @@ This is the recommended way to override the standard malloc interface. ### Linux, BSD -On these systems we preload the mimalloc shared +On these ELF-based systems we preload the mimalloc shared library so all calls to the standard `malloc` interface are resolved to the _mimalloc_ library. - -- `env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram` +``` +> env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram +``` You can set extra environment variables to check that mimalloc is running, like: ``` -env MIMALLOC_VERBOSE=1 LD_PRELOAD=/usr/lib/libmimalloc.so myprogram +> env MIMALLOC_VERBOSE=1 LD_PRELOAD=/usr/lib/libmimalloc.so myprogram ``` or run with the debug version to get detailed statistics: ``` -env MIMALLOC_SHOW_STATS=1 LD_PRELOAD=/usr/lib/libmimalloc-debug.so myprogram +> env MIMALLOC_SHOW_STATS=1 LD_PRELOAD=/usr/lib/libmimalloc-debug.so myprogram ``` ### MacOS @@ -228,8 +229,9 @@ env MIMALLOC_SHOW_STATS=1 LD_PRELOAD=/usr/lib/libmimalloc-debug.so myprogram On macOS we can also preload the mimalloc shared library so all calls to the standard `malloc` interface are resolved to the _mimalloc_ library. - -- `env DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram` +``` +> env DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram +``` Note that certain security restrictions may apply when doing this from the [shell](https://stackoverflow.com/questions/43941322/dyld-insert-libraries-ignored-when-calling-application-through-bash). @@ -257,16 +259,15 @@ robust; try this out if you experience troubles. ## Static override -On Unix systems, you can also statically link with _mimalloc_ to override the standard +On Unix-like systems, you can also statically link with _mimalloc_ to override the standard malloc interface. The recommended way is to link the final program with the _mimalloc_ single object file (`mimalloc-override.o`). We use an object file instead of a library file as linkers give preference to that over archives to resolve symbols. To ensure that the standard malloc interface resolves to the _mimalloc_ library, link it as the first object file. For example: - ``` -gcc -o myprogram mimalloc-override.o myfile1.c ... +> gcc -o myprogram mimalloc-override.o myfile1.c ... ```