Skip to content

Commit 4a6bd4b

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 <johannes.schindelin@gmx.de>
1 parent 8a69e1c commit 4a6bd4b

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,41 @@ ifdef USE_NED_ALLOCATOR
20522052
OVERRIDE_STRDUP = YesPlease
20532053
endif
20542054

2055+
ifdef USE_MIMALLOC
2056+
MIMALLOC_OBJS = \
2057+
compat/mimalloc/alloc-aligned.o \
2058+
compat/mimalloc/alloc.o \
2059+
compat/mimalloc/arena.o \
2060+
compat/mimalloc/bitmap.o \
2061+
compat/mimalloc/heap.o \
2062+
compat/mimalloc/init.o \
2063+
compat/mimalloc/options.o \
2064+
compat/mimalloc/os.o \
2065+
compat/mimalloc/page.o \
2066+
compat/mimalloc/random.o \
2067+
compat/mimalloc/segment.o \
2068+
compat/mimalloc/segment-cache.o \
2069+
compat/mimalloc/stats.o
2070+
2071+
COMPAT_CFLAGS += -Icompat/mimalloc -DMI_DEBUG=0 -DUSE_MIMALLOC --std=gnu11
2072+
COMPAT_OBJS += $(MIMALLOC_OBJS)
2073+
2074+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += -DBANNED_H
2075+
2076+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
2077+
-Wno-attributes \
2078+
-Wno-unknown-pragmas \
2079+
-Wno-array-bounds
2080+
2081+
ifdef DEVELOPER
2082+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
2083+
-Wno-pedantic \
2084+
-Wno-declaration-after-statement \
2085+
-Wno-old-style-definition \
2086+
-Wno-missing-prototypes
2087+
endif
2088+
endif
2089+
20552090
ifdef OVERRIDE_STRDUP
20562091
COMPAT_CFLAGS += -DOVERRIDE_STRDUP
20572092
COMPAT_OBJS += compat/strdup.o

config.mak.dev

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ endif
480480
CC = compat/vcbuild/scripts/clink.pl
481481
AR = compat/vcbuild/scripts/lib.pl
482482
CFLAGS =
483-
BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
483+
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
484484
COMPAT_OBJS = compat/msvc.o compat/winansi.o \
485485
compat/win32/flush.o \
486486
compat/win32/path-utils.o \

git-compat-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,16 @@ char *gitdirname(char *);
379379
# include <sys/sysinfo.h>
380380
#endif
381381

382+
#ifdef USE_MIMALLOC
383+
#include "mimalloc.h"
384+
#define malloc mi_malloc
385+
#define calloc mi_calloc
386+
#define realloc mi_realloc
387+
#define free mi_free
388+
#define strdup mi_strdup
389+
#define strndup mi_strndup
390+
#endif
391+
382392
/* On most systems <netdb.h> would have given us this, but
383393
* not on some systems (e.g. z/OS).
384394
*/

0 commit comments

Comments
 (0)