Skip to content

Commit 1d06192

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 86808e3 commit 1d06192

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

Makefile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ BUILTIN_OBJS += builtin/write-tree.o
13461346
# upstream unnecessarily (making merging in future changes easier).
13471347
THIRD_PARTY_SOURCES += compat/inet_ntop.c
13481348
THIRD_PARTY_SOURCES += compat/inet_pton.c
1349+
THIRD_PARTY_SOURCES += compat/mimalloc/%
13491350
THIRD_PARTY_SOURCES += compat/nedmalloc/%
13501351
THIRD_PARTY_SOURCES += compat/obstack.%
13511352
THIRD_PARTY_SOURCES += compat/poll/%
@@ -2148,6 +2149,46 @@ ifdef USE_NED_ALLOCATOR
21482149
OVERRIDE_STRDUP = YesPlease
21492150
endif
21502151

2152+
ifdef USE_MIMALLOC
2153+
MIMALLOC_OBJS = \
2154+
compat/mimalloc/alloc-aligned.o \
2155+
compat/mimalloc/alloc.o \
2156+
compat/mimalloc/arena.o \
2157+
compat/mimalloc/bitmap.o \
2158+
compat/mimalloc/heap.o \
2159+
compat/mimalloc/init.o \
2160+
compat/mimalloc/libc.o \
2161+
compat/mimalloc/options.o \
2162+
compat/mimalloc/os.o \
2163+
compat/mimalloc/page.o \
2164+
compat/mimalloc/random.o \
2165+
compat/mimalloc/prim/prim.o \
2166+
compat/mimalloc/segment.o \
2167+
compat/mimalloc/segment-map.o \
2168+
compat/mimalloc/stats.o
2169+
2170+
COMPAT_CFLAGS += -Icompat/mimalloc -DMI_DEBUG=0 -DUSE_MIMALLOC --std=gnu11
2171+
COMPAT_OBJS += $(MIMALLOC_OBJS)
2172+
2173+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += -DBANNED_H
2174+
2175+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
2176+
-DMI_WIN_USE_FLS \
2177+
-Wno-attributes \
2178+
-Wno-unknown-pragmas \
2179+
-Wno-unused-function \
2180+
-Wno-array-bounds
2181+
2182+
ifdef DEVELOPER
2183+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
2184+
-Wno-pedantic \
2185+
-Wno-declaration-after-statement \
2186+
-Wno-old-style-definition \
2187+
-Wno-missing-prototypes \
2188+
-Wno-implicit-function-declaration
2189+
endif
2190+
endif
2191+
21512192
ifdef OVERRIDE_STRDUP
21522193
COMPAT_CFLAGS += -DOVERRIDE_STRDUP
21532194
COMPAT_OBJS += compat/strdup.o

compat/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/zlib-uncompress2.c whitespace=-indent-with-non-tab,-trailing-space
2+
/mimalloc/**/* whitespace=-trailing-space

compat/posix.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ typedef unsigned long uintptr_t;
176176
#define _ALL_SOURCE 1
177177
#endif
178178

179+
#ifdef USE_MIMALLOC
180+
#include "mimalloc.h"
181+
#define malloc mi_malloc
182+
#define calloc mi_calloc
183+
#define realloc mi_realloc
184+
#define free mi_free
185+
#define strdup mi_strdup
186+
#define strndup mi_strndup
187+
#endif
188+
179189
#ifdef MKDIR_WO_TRAILING_SLASH
180190
#define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
181191
int compat_mkdir_wo_trailing_slash(const char*, mode_t);

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
@@ -518,7 +518,7 @@ endif
518518
CC = compat/vcbuild/scripts/clink.pl
519519
AR = compat/vcbuild/scripts/lib.pl
520520
CFLAGS =
521-
BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
521+
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
522522
COMPAT_OBJS = compat/msvc.o compat/winansi.o \
523523
compat/win32/flush.o \
524524
compat/win32/path-utils.o \

0 commit comments

Comments
 (0)