Skip to content

Commit a27ec22

Browse files
committed
mimalloc: offer a build-time option to enable it
By defining `USE_MIMALLOC`, Git can now be compiled with that nicely-fast and small allocator. Note that we have to disable a couple `DEVELOPER` options to build mimalloc's source code, as it makes heavy use of declarations after statements, among other things that disagree with Git's conventions. We even have to silence some GCC warnings in non-DEVELOPER mode. For example, the `-Wno-array-bounds` flag is needed because in `-O2` builds, trying to call `NtCurrentTeb()` (which `_mi_thread_id()` does on Windows) causes the bogus warning about a system header, likely related to https://sourceforge.net/p/mingw-w64/mailman/message/37674519/ and to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578: C:/git-sdk-64-minimal/mingw64/include/psdk_inc/intrin-impl.h:838:1: error: array subscript 0 is outside array bounds of 'long long unsigned int[0]' [-Werror=array-bounds] 838 | __buildreadseg(__readgsqword, unsigned __int64, "gs", "q") | ^~~~~~~~~~~~~~ Also: The `mimalloc` library uses C11-style atomics, therefore we must require that standard when compiling with GCC if we want to use `mimalloc` (instead of requiring "only" C99). This is what we do in the CMake definition already, therefore this commit does not need to touch `contrib/buildsystems/`. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 0c0209d commit a27ec22

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

Makefile

+37
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,43 @@ ifdef USE_NED_ALLOCATOR
21242124
OVERRIDE_STRDUP = YesPlease
21252125
endif
21262126

2127+
ifdef USE_MIMALLOC
2128+
MIMALLOC_OBJS = \
2129+
compat/mimalloc/alloc-aligned.o \
2130+
compat/mimalloc/alloc.o \
2131+
compat/mimalloc/arena.o \
2132+
compat/mimalloc/bitmap.o \
2133+
compat/mimalloc/heap.o \
2134+
compat/mimalloc/init.o \
2135+
compat/mimalloc/options.o \
2136+
compat/mimalloc/os.o \
2137+
compat/mimalloc/page.o \
2138+
compat/mimalloc/random.o \
2139+
compat/mimalloc/prim/windows/prim.o \
2140+
compat/mimalloc/segment.o \
2141+
compat/mimalloc/segment-cache.o \
2142+
compat/mimalloc/segment-map.o \
2143+
compat/mimalloc/stats.o
2144+
2145+
COMPAT_CFLAGS += -Icompat/mimalloc -DMI_DEBUG=0 -DUSE_MIMALLOC --std=gnu11
2146+
COMPAT_OBJS += $(MIMALLOC_OBJS)
2147+
2148+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += -DBANNED_H
2149+
2150+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
2151+
-Wno-attributes \
2152+
-Wno-unknown-pragmas \
2153+
-Wno-array-bounds
2154+
2155+
ifdef DEVELOPER
2156+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
2157+
-Wno-pedantic \
2158+
-Wno-declaration-after-statement \
2159+
-Wno-old-style-definition \
2160+
-Wno-missing-prototypes
2161+
endif
2162+
endif
2163+
21272164
ifdef OVERRIDE_STRDUP
21282165
COMPAT_CFLAGS += -DOVERRIDE_STRDUP
21292166
COMPAT_OBJS += compat/strdup.o

config.mak.dev

+2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ endif
2222

2323
ifneq ($(uname_S),FreeBSD)
2424
ifneq ($(or $(filter gcc6,$(COMPILER_FEATURES)),$(filter clang7,$(COMPILER_FEATURES))),)
25+
ifndef USE_MIMALLOC
2526
DEVELOPER_CFLAGS += -std=gnu99
2627
endif
28+
endif
2729
else
2830
# FreeBSD cannot limit to C99 because its system headers unconditionally
2931
# rely on C11 features.

config.mak.uname

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ endif
494494
CC = compat/vcbuild/scripts/clink.pl
495495
AR = compat/vcbuild/scripts/lib.pl
496496
CFLAGS =
497-
BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
497+
BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -MP -std:c11
498498
COMPAT_OBJS = compat/msvc.o compat/winansi.o \
499499
compat/win32/flush.o \
500500
compat/win32/path-utils.o \

git-compat-util.h

+10
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,16 @@ char *gitdirname(char *);
443443
# include <sys/sysinfo.h>
444444
#endif
445445

446+
#ifdef USE_MIMALLOC
447+
#include "mimalloc.h"
448+
#define malloc mi_malloc
449+
#define calloc mi_calloc
450+
#define realloc mi_realloc
451+
#define free mi_free
452+
#define strdup mi_strdup
453+
#define strndup mi_strndup
454+
#endif
455+
446456
/* On most systems <netdb.h> would have given us this, but
447457
* not on some systems (e.g. z/OS).
448458
*/

0 commit comments

Comments
 (0)