#+TITLE: zig-jemalloc #+DATE: 2024-04-03T16:31:20+0800 #+LASTMOD: 2024-10-16T08:45:54+0800 #+AUTHOR: Jiacai Liu
[[https://github.com/jiacai2050/zig-jemalloc/actions/workflows/CI.yml][https://github.com/jiacai2050/zig-jemalloc/actions/workflows/CI.yml/badge.svg]] [[https://img.shields.io/badge/zig%20version-0.13.0-blue.svg]] [[https://img.shields.io/badge/zig%20version-master-blue.svg]]
Zig allocator backed by [[https://jemalloc.net/][jemalloc]].
Replace ${COMMIT} with a real one, then in your =build.zig=, import the module like this:
#+begin_src zig
const dep_jemalloc = b.dependency(“jemalloc”, .{});
exe.root_module.addImport(“jemalloc”, dep_jemalloc.module(“jemalloc”));
exe.linkLibC();
#+end_src
This library will link to vendored [[https://github.com/jemalloc/jemalloc/releases/tag/5.3.0][jemalloc(5.3.0)]] by default, you can disable this feature and link to the system-wide version using: #+begin_src zig const dep_jemalloc = b.dependency(“jemalloc”, .{ .link_vendor = false }); // Install jemalloc with system package manager, such as // brew install jemalloc // sudo apt-get install libjemalloc-dev exe.linkSystemLibrary(“jemalloc”); exe.linkLibC(); #+end_src
Then in you =main.zig=, initialize like this:
#+begin_src zig const jemalloc = @import(“jemalloc”); const allocator = jemalloc.allocator; #+end_src