1{ lib
2, stdenv
3, fetchurl
4, fetchpatch
5# By default, jemalloc puts a je_ prefix onto all its symbols on OSX, which
6# then stops downstream builds (mariadb in particular) from detecting it. This
7# option should remove the prefix and give us a working jemalloc.
8# Causes segfaults with some software (ex. rustc), but defaults to true for backward
9# compatibility.
10, stripPrefix ? stdenv.hostPlatform.isDarwin
11, disableInitExecTls ? false
12}:
13
14stdenv.mkDerivation rec {
15 pname = "jemalloc";
16 version = "5.3.0";
17
18 src = fetchurl {
19 url = "https://github.com/jemalloc/jemalloc/releases/download/${version}/${pname}-${version}.tar.bz2";
20 sha256 = "sha256-LbgtHnEZ3z5xt2QCGbbf6EeJvAU3mDw7esT3GJrs/qo=";
21 };
22
23 patches = [
24 # fix tests under --with-jemalloc-prefix=, see https://github.com/jemalloc/jemalloc/pull/2340
25 (fetchpatch {
26 url = "https://github.com/jemalloc/jemalloc/commit/d00ecee6a8dfa90afcb1bbc0858985c17bef6559.patch";
27 hash = "sha256-N5i4IxGJ4SSAgFiq5oGRnrNeegdk2flw9Sh2mP0yl4c=";
28 })
29 ];
30
31 configureFlags =
32 # see the comment on stripPrefix
33 lib.optional stripPrefix "--with-jemalloc-prefix="
34 ++ lib.optional disableInitExecTls "--disable-initial-exec-tls"
35 # jemalloc is unable to correctly detect transparent hugepage support on
36 # ARM (https://github.com/jemalloc/jemalloc/issues/526), and the default
37 # kernel ARMv6/7 kernel does not enable it, so we explicitly disable support
38 ++ lib.optionals (stdenv.isAarch32 && lib.versionOlder version "5") [
39 "--disable-thp"
40 "je_cv_thp=no"
41 ]
42 # AArch64 has configurable page size up to 64k. The default configuration
43 # for jemalloc only supports 4k page sizes.
44 ++ lib.optional stdenv.isAarch64 "--with-lg-page=16"
45 # See https://github.com/jemalloc/jemalloc/issues/1997
46 # Using a value of 48 should work on both emulated and native x86_64-darwin.
47 ++ lib.optional (stdenv.isDarwin && stdenv.isx86_64) "--with-lg-vaddr=48"
48 ;
49
50 env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.isDarwin "-Wno-error=array-bounds";
51
52 doCheck = true;
53
54 enableParallelBuilding = true;
55
56 meta = with lib; {
57 homepage = "https://jemalloc.net/";
58 description = "General purpose malloc(3) implementation";
59 longDescription = ''
60 malloc(3)-compatible memory allocator that emphasizes fragmentation
61 avoidance and scalable concurrency support.
62 '';
63 license = licenses.bsd2;
64 platforms = platforms.all;
65 };
66}