Skip to content

Commit

Permalink
mimalloc: offer a build-time option to enable it
Browse files Browse the repository at this point in the history
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.

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>
  • Loading branch information
dscho committed Sep 22, 2022
1 parent 64f09f3 commit c5d12c1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Makefile
Expand Up @@ -1942,6 +1942,39 @@ ifdef USE_NED_ALLOCATOR
OVERRIDE_STRDUP = YesPlease
endif

ifdef USE_MIMALLOC
MIMALLOC_OBJS = \
compat/mimalloc/alloc-aligned.o \
compat/mimalloc/alloc.o \
compat/mimalloc/arena.o \
compat/mimalloc/bitmap.o \
compat/mimalloc/heap.o \
compat/mimalloc/init.o \
compat/mimalloc/options.o \
compat/mimalloc/os.o \
compat/mimalloc/page.o \
compat/mimalloc/random.o \
compat/mimalloc/segment.o \
compat/mimalloc/segment-cache.o \
compat/mimalloc/stats.o

COMPAT_CFLAGS += -Icompat/mimalloc -DMI_DEBUG=0 -DUSE_MIMALLOC --std=gnu11
COMPAT_OBJS += $(MIMALLOC_OBJS)

$(MIMALLOC_OBJS): COMPAT_CFLAGS += -DBANNED_H

ifdef DEVELOPER
$(MIMALLOC_OBJS): COMPAT_CFLAGS += \
-Wno-attributes \
-Wno-pedantic \
-Wno-unknown-pragmas \
-Wno-declaration-after-statement \
-Wno-old-style-definition \
-Wno-missing-prototypes \
-Wno-array-bounds
endif
endif

ifdef OVERRIDE_STRDUP
COMPAT_CFLAGS += -DOVERRIDE_STRDUP
COMPAT_OBJS += compat/strdup.o
Expand Down
2 changes: 2 additions & 0 deletions config.mak.dev
Expand Up @@ -22,8 +22,10 @@ endif

ifneq ($(uname_S),FreeBSD)
ifneq ($(or $(filter gcc6,$(COMPILER_FEATURES)),$(filter clang7,$(COMPILER_FEATURES))),)
ifndef USE_MIMALLOC
DEVELOPER_CFLAGS += -std=gnu99
endif
endif
else
# FreeBSD cannot limit to C99 because its system headers unconditionally
# rely on C11 features.
Expand Down
2 changes: 1 addition & 1 deletion config.mak.uname
Expand Up @@ -480,7 +480,7 @@ endif
CC = compat/vcbuild/scripts/clink.pl
AR = compat/vcbuild/scripts/lib.pl
CFLAGS =
BASIC_CFLAGS = -nologo -I. -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE
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
COMPAT_OBJS = compat/msvc.o compat/winansi.o \
compat/win32/flush.o \
compat/win32/path-utils.o \
Expand Down
10 changes: 10 additions & 0 deletions git-compat-util.h
Expand Up @@ -371,6 +371,16 @@ char *gitdirname(char *);
# include <sys/sysinfo.h>
#endif

#ifdef USE_MIMALLOC
#include "mimalloc.h"
#define malloc mi_malloc
#define calloc mi_calloc
#define realloc mi_realloc
#define free mi_free
#define strdup mi_strdup
#define strndup mi_strndup
#endif

/* On most systems <netdb.h> would have given us this, but
* not on some systems (e.g. z/OS).
*/
Expand Down

0 comments on commit c5d12c1

Please sign in to comment.