This commit is contained in:
Schrodinger ZHU Yifan 2025-05-13 17:44:05 +01:00 committed by GitHub
commit 5a1939dece
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 87 additions and 1 deletions

6
.gitmodules vendored Normal file
View File

@ -0,0 +1,6 @@
[submodule "deps/mimalloc"]
path = deps/mimalloc
url = https://github.com/microsoft/mimalloc
[submodule "snmalloc"]
path = deps/snmalloc
url = https://github.com/microsoft/snmalloc

18
deps/Makefile vendored
View File

@ -142,3 +142,21 @@ jemalloc: .make-prerequisites
cd jemalloc && $(MAKE) lib/libjemalloc.a
.PHONY: jemalloc
mimalloc: .make-prerequisites
@printf '%b %b\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)
cd mimalloc && cmake . -DCMAKE_BUILD_TYPE=Release -DMI_OVERRIDE=OFF && cmake --build . --target mimalloc-static
.PHONY: mimalloc
snmalloc: .make-prerequisites
@printf '%b %b\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)
cd snmalloc && cmake . && cmake --build . --target snmallocshim-static
.PHONY: snmalloc
snmalloc-1mib: .make-prerequisites
@printf '%b %b\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)
cd snmalloc && cmake . && cmake --build . --target snmallocshim-1mib-static
.PHONY: snmalloc-1mib

1
deps/mimalloc vendored Submodule

@ -0,0 +1 @@
Subproject commit a09a64e29b5535f6f4ea70fc58ac41c91c73a440

1
deps/snmalloc vendored Submodule

@ -0,0 +1 @@
Subproject commit 61afa77898d233586acf8567fd2dcc24caa611ab

View File

@ -88,6 +88,18 @@ ifneq (,$(filter aarch64 armv%,$(uname_M)))
endif
# Backwards compatibility for selecting an allocator
ifeq ($(USE_MIMALLOC),yes)
MALLOC=mimalloc
endif
ifeq ($(USE_SNMALLOC),yes)
MALLOC=snmalloc
endif
ifeq ($(USE_SNMALLOC_1MIB),yes)
MALLOC=snmalloc-1mib
endif
ifeq ($(USE_TCMALLOC),yes)
MALLOC=tcmalloc
endif
@ -279,6 +291,27 @@ ifeq ($(BUILD_WITH_SYSTEMD),yes)
FINAL_CFLAGS+= -DHAVE_LIBSYSTEMD
endif
ifeq ($(MALLOC),snmalloc)
__version := $(shell sh -c 'git submodule status | grep "snmalloc" | grep -oP "(?<=\()(.+)(?=\))"')
DEPENDENCY_TARGETS+= snmalloc
FINAL_CFLAGS+= -DUSE_SNMALLOC -DSNMALLOC_VERSION=\"$(__version)\"
FINAL_LIBS := ../deps/snmalloc/libsnmallocshim-static.a -lstdc++ -latomic $(FINAL_LIBS)
endif
ifeq ($(MALLOC),snmalloc-1mib)
__version := $(shell sh -c 'git submodule status | grep "snmalloc" | grep -oP "(?<=\()(.+)(?=\))"')
DEPENDENCY_TARGETS+= snmalloc-1mib
FINAL_CFLAGS+= -DUSE_SNMALLOC -DSNMALLOC_VERSION='"$(__version) (1mib)"'
FINAL_LIBS := ../deps/snmalloc/libsnmallocshim-1mib-static.a -lstdc++ -latomic $(FINAL_LIBS)
endif
ifeq ($(MALLOC),mimalloc)
__version := $(shell sh -c 'git submodule status | grep "mimalloc" | grep -oP "(?<=\()(.+)(?=\))"')
DEPENDENCY_TARGETS+= mimalloc
FINAL_CFLAGS+= -DUSE_MIMALLOC -I../deps/mimalloc/include -DMIMALLOC_VERSION=\"$(__version)\"
FINAL_LIBS := ../deps/mimalloc/libmimalloc.a $(FINAL_LIBS)
endif
ifeq ($(MALLOC),tcmalloc)
FINAL_CFLAGS+= -DUSE_TCMALLOC
FINAL_LIBS+= -ltcmalloc

View File

@ -53,6 +53,7 @@ void zlibc_free(void *ptr) {
#define MALLOC_MIN_SIZE(x) ((x) > 0 ? (x) : sizeof(long))
/* Explicitly override malloc/free etc when using tcmalloc. */
/* Not overriding the malloc here if using snmalloc */
#if defined(USE_TCMALLOC)
#define malloc(size) tc_malloc(size)
#define calloc(count,size) tc_calloc(count,size)
@ -67,6 +68,16 @@ void zlibc_free(void *ptr) {
#define mallocx(size,flags) je_mallocx(size,flags)
#define rallocx(ptr,size,flags) je_rallocx(ptr,size,flags)
#define dallocx(ptr,flags) je_dallocx(ptr,flags)
#elif defined(USE_MIMALLOC)
#define malloc(size) mi_malloc(size)
#define calloc(count, size) mi_calloc(count, size)
#define realloc(ptr, size) mi_realloc(ptr, size)
#define free(ptr) mi_free(ptr)
#elif defined(USE_SNMALLOC)
#define malloc(size) sn_malloc(size)
#define calloc(count, size) sn_calloc(count, size)
#define realloc(ptr, size) sn_realloc(ptr, size)
#define free(ptr) sn_free(ptr)
#endif
#define MAX_THREADS 16 /* Keep it a power of 2 so we can use '&' instead of '%'. */

View File

@ -15,7 +15,23 @@
#define __xstr(s) __str(s)
#define __str(s) #s
#if defined(USE_TCMALLOC)
#if defined(USE_MIMALLOC)
#include <mimalloc.h>
#define ZMALLOC_LIB ("mimalloc-" MIMALLOC_VERSION)
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) mi_malloc_usable_size(p)
#elif defined(USE_SNMALLOC)
#define ZMALLOC_LIB ("snmalloc-" SNMALLOC_VERSION)
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) sn_malloc_usable_size(p)
extern size_t sn_malloc_usable_size(void *);
extern void * sn_malloc(size_t);
extern void sn_free(void *);
extern void * sn_realloc(void *, size_t);
extern void * sn_calloc(size_t, size_t);
#elif defined(USE_TCMALLOC)
#define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR))
#include <google/tcmalloc.h>
#if (TC_VERSION_MAJOR == 1 && TC_VERSION_MINOR >= 6) || (TC_VERSION_MAJOR > 1)