1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchpatch,
6 autogen,
7 autoconf,
8 automake,
9 # By default, jemalloc puts a je_ prefix onto all its symbols on OSX, which
10 # then stops downstream builds (mariadb in particular) from detecting it. This
11 # option should remove the prefix and give us a working jemalloc.
12 # Causes segfaults with some software (ex. rustc), but defaults to true for backward
13 # compatibility.
14 stripPrefix ? stdenv.hostPlatform.isDarwin,
15 disableInitExecTls ? false,
16}:
17
18stdenv.mkDerivation rec {
19 pname = "jemalloc";
20 version = "5.3.0";
21
22 src = fetchFromGitHub {
23 owner = "jemalloc";
24 repo = "jemalloc";
25 tag = version;
26 hash = "sha256-bb0OhZVXyvN+hf9BpPSykn5cGm87a0C+Y/iXKt9wTSs=";
27 };
28
29 patches = [
30 # fix tests under --with-jemalloc-prefix=, see https://github.com/jemalloc/jemalloc/pull/2340
31 (fetchpatch {
32 url = "https://github.com/jemalloc/jemalloc/commit/d00ecee6a8dfa90afcb1bbc0858985c17bef6559.patch";
33 hash = "sha256-N5i4IxGJ4SSAgFiq5oGRnrNeegdk2flw9Sh2mP0yl4c=";
34 })
35 # fix linking with libc++, can be removed in the next update (after 5.3.0).
36 # https://github.com/jemalloc/jemalloc/pull/2348
37 (fetchpatch {
38 url = "https://github.com/jemalloc/jemalloc/commit/4422f88d17404944a312825a1aec96cd9dc6c165.patch";
39 hash = "sha256-dunkE7XHzltn5bOb/rSHqzpRniAFuGubBStJeCxh0xo=";
40 })
41 # -O3 appears to introduce an unreproducibility where
42 # `rtree_read.constprop.0` shows up in some builds but
43 # not others, so we fall back to O2:
44 ./o3-to-o2.patch
45 ];
46
47 nativeBuildInputs = [
48 autogen
49 autoconf
50 automake
51 ];
52
53 # TODO: switch to autoreconfHook when updating beyond 5.3.0
54 # https://github.com/jemalloc/jemalloc/issues/2346
55 configureScript = "./autogen.sh";
56
57 configureFlags = [
58 "--with-version=${version}-0-g0000000000000000000000000000000000000000"
59 "--with-lg-vaddr=${with stdenv.hostPlatform; toString (if isILP32 then 32 else parsed.cpu.bits)}"
60 ]
61 # see the comment on stripPrefix
62 ++ lib.optional stripPrefix "--with-jemalloc-prefix="
63 ++ lib.optional disableInitExecTls "--disable-initial-exec-tls"
64 # jemalloc is unable to correctly detect transparent hugepage support on
65 # ARM (https://github.com/jemalloc/jemalloc/issues/526), and the default
66 # kernel ARMv6/7 kernel does not enable it, so we explicitly disable support
67 ++ lib.optionals (stdenv.hostPlatform.isAarch32 && lib.versionOlder version "5") [
68 "--disable-thp"
69 "je_cv_thp=no"
70 ]
71 # The upstream default is dependent on the builders' page size
72 # https://github.com/jemalloc/jemalloc/issues/467
73 # https://sources.debian.org/src/jemalloc/5.3.0-3/debian/rules/
74 ++ [
75 (
76 if (stdenv.hostPlatform.isAarch64 || stdenv.hostPlatform.isLoongArch64) then
77 "--with-lg-page=16"
78 else
79 "--with-lg-page=12"
80 )
81 ]
82 # See https://github.com/jemalloc/jemalloc/issues/1997
83 # Using a value of 48 should work on both emulated and native x86_64-darwin.
84 ++ lib.optional (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) "--with-lg-vaddr=48";
85
86 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.hostPlatform.isDarwin "-Wno-error=array-bounds";
87
88 # Tries to link test binaries binaries dynamically and fails
89 doCheck = !stdenv.hostPlatform.isStatic;
90
91 doInstallCheck = true;
92 installCheckPhase = ''
93 ! grep missing_version_try_git_fetch_tags $out/include/jemalloc/jemalloc.h
94 '';
95
96 # Parallel builds break reproducibility.
97 enableParallelBuilding = false;
98
99 meta = with lib; {
100 homepage = "https://jemalloc.net/";
101 description = "General purpose malloc(3) implementation";
102 longDescription = ''
103 malloc(3)-compatible memory allocator that emphasizes fragmentation
104 avoidance and scalable concurrency support.
105 '';
106 license = licenses.bsd2;
107 platforms = platforms.all;
108 };
109}