Skip to content

Commit

Permalink
Add support for compiling with mimalloc
Browse files Browse the repository at this point in the history
  • Loading branch information
Sher Sun committed Apr 24, 2024
1 parent d09a59c commit 6ef673d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "deps/mimalloc"]
path = deps/mimalloc
url = https://github.com/microsoft/mimalloc.git
13 changes: 13 additions & 0 deletions deps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions deps/mimalloc
Submodule mimalloc added at 77eb3a
13 changes: 13 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 $$?)
Expand Down
15 changes: 14 additions & 1 deletion src/zmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 <mimalloc.h>
#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 <malloc/malloc.h>
#define HAVE_MALLOC_SIZE 1
Expand Down Expand Up @@ -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 */

0 comments on commit 6ef673d

Please sign in to comment.