Skip to content

Commit ef6886f

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 <johannes.schindelin@gmx.de>
1 parent bb2da73 commit ef6886f

5 files changed

Lines changed: 64 additions & 1 deletion

File tree

Makefile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,8 @@ BUILTIN_OBJS += builtin/write-tree.o
15151515
# upstream unnecessarily (making merging in future changes easier).
15161516
THIRD_PARTY_SOURCES += compat/inet_ntop.c
15171517
THIRD_PARTY_SOURCES += compat/inet_pton.c
1518+
THIRD_PARTY_SOURCES += compat/mimalloc/%
1519+
THIRD_PARTY_SOURCES += compat/nedmalloc/%
15181520
THIRD_PARTY_SOURCES += compat/obstack.%
15191521
THIRD_PARTY_SOURCES += compat/poll/%
15201522
THIRD_PARTY_SOURCES += compat/regex/%
@@ -2271,6 +2273,53 @@ ifdef NATIVE_CRLF
22712273
BASIC_CFLAGS += -DNATIVE_CRLF
22722274
endif
22732275

2276+
ifdef USE_NED_ALLOCATOR
2277+
COMPAT_CFLAGS += -Icompat/nedmalloc
2278+
COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
2279+
OVERRIDE_STRDUP = YesPlease
2280+
endif
2281+
2282+
ifdef USE_MIMALLOC
2283+
MIMALLOC_OBJS = \
2284+
compat/mimalloc/alloc-aligned.o \
2285+
compat/mimalloc/alloc.o \
2286+
compat/mimalloc/arena.o \
2287+
compat/mimalloc/bitmap.o \
2288+
compat/mimalloc/heap.o \
2289+
compat/mimalloc/init.o \
2290+
compat/mimalloc/libc.o \
2291+
compat/mimalloc/options.o \
2292+
compat/mimalloc/os.o \
2293+
compat/mimalloc/page.o \
2294+
compat/mimalloc/random.o \
2295+
compat/mimalloc/prim/prim.o \
2296+
compat/mimalloc/segment.o \
2297+
compat/mimalloc/segment-map.o \
2298+
compat/mimalloc/stats.o
2299+
2300+
COMPAT_CFLAGS += -Icompat/mimalloc -DMI_DEBUG=0 -DUSE_MIMALLOC --std=gnu11
2301+
COMPAT_OBJS += $(MIMALLOC_OBJS)
2302+
2303+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += -DBANNED_H
2304+
2305+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
2306+
-DMI_WIN_USE_FLS \
2307+
-Wno-attributes \
2308+
-Wno-unknown-pragmas \
2309+
-Wno-unused-function \
2310+
-Wno-array-bounds
2311+
2312+
ifdef DEVELOPER
2313+
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
2314+
-Wno-pedantic \
2315+
-Wno-declaration-after-statement \
2316+
-Wno-old-style-definition \
2317+
-Wno-missing-prototypes \
2318+
-Wno-implicit-function-declaration
2319+
endif
2320+
endif
2321+
2322+
22742323
ifdef OVERRIDE_STRDUP
22752324
COMPAT_CFLAGS += -DOVERRIDE_STRDUP
22762325
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ 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
29+
endif
2730
else
2831
# FreeBSD cannot limit to C99 because its system headers unconditionally
2932
# rely on C11 features.

config.mak.uname

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ endif
526526
CC = compat/vcbuild/scripts/clink.pl
527527
AR = compat/vcbuild/scripts/lib.pl
528528
CFLAGS =
529-
BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
529+
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
530530
COMPAT_OBJS = compat/msvc.o compat/winansi.o \
531531
compat/win32/flush.o \
532532
compat/win32/path-utils.o \

0 commit comments

Comments
 (0)