From 6ef673dd91e6f9d0f071f8dcb4a693557342ed14 Mon Sep 17 00:00:00 2001 From: Sher Sun Date: Wed, 24 Apr 2024 16:02:58 +0000 Subject: [PATCH] Add support for compiling with mimalloc --- .gitmodules | 3 +++ deps/Makefile | 13 +++++++++++++ deps/mimalloc | 1 + src/Makefile | 13 +++++++++++++ src/zmalloc.h | 15 ++++++++++++++- 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 deps/mimalloc diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000..2b3ffa4080 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "deps/mimalloc"] + path = deps/mimalloc + url = https://github.com/microsoft/mimalloc.git diff --git a/deps/Makefile b/deps/Makefile index 67b7d41026..eadd555a7b 100644 --- a/deps/Makefile +++ b/deps/Makefile @@ -12,6 +12,10 @@ BINCOLOR="\033[37;1m" MAKECOLOR="\033[32;1m" ENDCOLOR="\033[0m" +MIMALLOC_DIR := mimalloc +MIMALLOC_BUILD_DIR := $(MIMALLOC_DIR)/out/release +MIMALLOC_LIB := $(MIMALLOC_BUILD_DIR)/libmimalloc.a + default: @echo "Explicit target required" @@ -42,6 +46,7 @@ distclean: -(cd jemalloc && [ -f Makefile ] && $(MAKE) distclean) > /dev/null || true -(cd hdr_histogram && $(MAKE) clean) > /dev/null || true -(cd fpconv && $(MAKE) clean) > /dev/null || true + -rm -rf $(MIMALLOC_BUILD_DIR) -(rm -f .make-*) .PHONY: distclean @@ -116,3 +121,11 @@ jemalloc: .make-prerequisites cd jemalloc && $(MAKE) lib/libjemalloc.a .PHONY: jemalloc + +.PHONY: mimalloc + +mimalloc: .make-prerequisites + @printf '%b %b\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) + @[ -d $(MIMALLOC_BUILD_DIR) ] || mkdir -p $(MIMALLOC_BUILD_DIR) + cd $(MIMALLOC_BUILD_DIR) && cmake ../.. -DCMAKE_BUILD_TYPE=Release + $(MAKE) -C $(MIMALLOC_BUILD_DIR) \ No newline at end of file diff --git a/deps/mimalloc b/deps/mimalloc new file mode 160000 index 0000000000..77eb3a366b --- /dev/null +++ b/deps/mimalloc @@ -0,0 +1 @@ +Subproject commit 77eb3a366b0355a0a90e2a0a8b6ac03757b4a1f6 diff --git a/src/Makefile b/src/Makefile index 47b961862a..4a11836f54 100644 --- a/src/Makefile +++ b/src/Makefile @@ -98,6 +98,10 @@ ifeq ($(USE_JEMALLOC),no) MALLOC=libc endif +ifeq ($(USE_MIMALLOC),yes) + MALLOC=mimalloc +endif + ifdef SANITIZER ifeq ($(SANITIZER),address) MALLOC=libc @@ -285,6 +289,15 @@ ifeq ($(MALLOC),jemalloc) FINAL_LIBS := ../deps/jemalloc/lib/libjemalloc.a $(FINAL_LIBS) endif +ifeq ($(MALLOC),mimalloc) + DEPENDENCY_TARGETS+= mimalloc + MIMALLOC_DIR= ../deps/mimalloc + MIMALLOC_LIB= $(MIMALLOC_DIR)/out/release/libmimalloc.a + FINAL_CFLAGS+= -DUSE_MIMALLOC -I$(MIMALLOC_DIR)/include + FINAL_LIBS := $(MIMALLOC_LIB) $(FINAL_LIBS) +endif + + # LIBSSL & LIBCRYPTO LIBSSL_LIBS= LIBSSL_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libssl && echo $$?) diff --git a/src/zmalloc.h b/src/zmalloc.h index 1cf4af96c4..82a8440a2d 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -34,6 +34,9 @@ /* Double expansion needed for stringification of macro values. */ #define __xstr(s) __str(s) #define __str(s) #s +#define MI_VERSION_MAJOR 1 +#define MI_VERSION_MINOR 8 +#define MI_VERSION_PATCH 5 #if defined(USE_TCMALLOC) #define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR)) @@ -55,6 +58,16 @@ #error "Newer version of jemalloc required" #endif +#elif defined(USE_MIMALLOC) +#define ZMALLOC_LIB ("mimalloc-" __xstr(MI_VERSION_MAJOR) "." __xstr(MI_VERSION_MINOR) "." __xstr(MI_VERSION_PATCH)) +#include +#if (MI_VERSION_MAJOR == 1 && MI_VERSION_MINOR >= 8) || (MI_VERSION_MAJOR > 1) +#define HAVE_MALLOC_SIZE 1 +#define zmalloc_size(p) mi_usable_size(p) +#else +#error "Newer version of mimalloc required" +#endif + #elif defined(__APPLE__) #include #define HAVE_MALLOC_SIZE 1 @@ -172,4 +185,4 @@ int get_proc_stat_ll(int i, long long *res); int zmalloc_test(int argc, char **argv, int flags); #endif -#endif /* __ZMALLOC_H */ +#endif /* __ZMALLOC_H */ \ No newline at end of file