Skip to content

Commit 148036d

Browse files
committed
add mimalloc allocator based on mimalloc
1 parent 489ba13 commit 148036d

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/browser/mimalloc.zig

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ pub fn getRSS() i64 {
6262
const data = writer.written();
6363
const index = std.mem.indexOf(u8, data, "rss: ") orelse return -1;
6464
const sep = std.mem.indexOfScalarPos(u8, data, index + 5, ' ') orelse return -2;
65-
const value = std.fmt.parseFloat(f64, data[index+5..sep]) catch return -3;
66-
const unit = data[sep+1..];
65+
const value = std.fmt.parseFloat(f64, data[index + 5 .. sep]) catch return -3;
66+
const unit = data[sep + 1 ..];
6767
if (std.mem.startsWith(u8, unit, "KiB,")) {
6868
return @as(i64, @intFromFloat(value)) * 1024;
6969
}
@@ -108,3 +108,36 @@ pub export fn strn_dup(s: [*c]const u8, size: usize) callconv(.c) [*c]u8 {
108108
pub export fn f_ree(_: ?*anyopaque) callconv(.c) void {
109109
return;
110110
}
111+
112+
/// An allocator that use mimalloc to manage memory.
113+
pub const allocator = std.mem.Allocator{
114+
.ptr = undefined,
115+
.vtable = Allocator.vtable,
116+
};
117+
118+
const Allocator = struct {
119+
fn alloc(_: *anyopaque, len: usize, alignment: std.mem.Alignment, _: usize) ?[*]u8 {
120+
const ptr = c.mi_malloc_aligned(len, alignment.toByteUnits());
121+
return @ptrCast(ptr);
122+
}
123+
124+
fn resize(_: *anyopaque, memory: []u8, _: std.mem.Alignment, new_len: usize, _: usize) bool {
125+
return c.mi_expand(memory.ptr, new_len) != null;
126+
}
127+
128+
fn remap(_: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, _: usize) ?[*]u8 {
129+
const ptr = c.mi_realloc_aligned(memory.ptr, new_len, alignment.toByteUnits());
130+
return @ptrCast(ptr);
131+
}
132+
133+
fn free(_: *anyopaque, memory: []u8, alignment: std.mem.Alignment, _: usize) void {
134+
c.mi_free_size_aligned(memory.ptr, memory.len, alignment.toByteUnits());
135+
}
136+
137+
pub const vtable = &std.mem.Allocator.VTable{
138+
.alloc = Allocator.alloc,
139+
.resize = Allocator.resize,
140+
.remap = Allocator.remap,
141+
.free = Allocator.free,
142+
};
143+
};

src/main.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const App = @import("app.zig").App;
2525
const Server = @import("server.zig").Server;
2626
const Browser = @import("browser/browser.zig").Browser;
2727
const DumpStripMode = @import("browser/dump.zig").Opts.StripMode;
28+
const mimalloc = @import("browser/mimalloc.zig");
2829

2930
const build_config = @import("build_config");
3031

@@ -34,9 +35,9 @@ var _server: ?Server = null;
3435
pub fn main() !void {
3536
// allocator
3637
// - in Debug mode we use the General Purpose Allocator to detect memory leaks
37-
// - in Release mode we use the c allocator
38+
// - in Release mode we use the mimalloc allocator
3839
var gpa: std.heap.DebugAllocator(.{}) = .init;
39-
const alloc = if (builtin.mode == .Debug) gpa.allocator() else std.heap.c_allocator;
40+
const alloc = if (builtin.mode == .Debug) gpa.allocator() else mimalloc.allocator;
4041

4142
defer if (builtin.mode == .Debug) {
4243
if (gpa.detectLeaks()) std.posix.exit(1);

0 commit comments

Comments
 (0)