Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/browser/mimalloc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ pub fn getRSS() i64 {
const data = writer.written();
const index = std.mem.indexOf(u8, data, "rss: ") orelse return -1;
const sep = std.mem.indexOfScalarPos(u8, data, index + 5, ' ') orelse return -2;
const value = std.fmt.parseFloat(f64, data[index+5..sep]) catch return -3;
const unit = data[sep+1..];
const value = std.fmt.parseFloat(f64, data[index + 5 .. sep]) catch return -3;
const unit = data[sep + 1 ..];
if (std.mem.startsWith(u8, unit, "KiB,")) {
return @as(i64, @intFromFloat(value)) * 1024;
}
Expand Down Expand Up @@ -108,3 +108,36 @@ pub export fn strn_dup(s: [*c]const u8, size: usize) callconv(.c) [*c]u8 {
pub export fn f_ree(_: ?*anyopaque) callconv(.c) void {
return;
}

/// An allocator that use mimalloc to manage memory.
pub const allocator = std.mem.Allocator{
.ptr = undefined,
.vtable = Allocator.vtable,
};

const Allocator = struct {
fn alloc(_: *anyopaque, len: usize, alignment: std.mem.Alignment, _: usize) ?[*]u8 {
const ptr = c.mi_malloc_aligned(len, alignment.toByteUnits());
return @ptrCast(ptr);
}

fn resize(_: *anyopaque, memory: []u8, _: std.mem.Alignment, new_len: usize, _: usize) bool {
return c.mi_expand(memory.ptr, new_len) != null;
}

fn remap(_: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, _: usize) ?[*]u8 {
const ptr = c.mi_realloc_aligned(memory.ptr, new_len, alignment.toByteUnits());
return @ptrCast(ptr);
}

fn free(_: *anyopaque, memory: []u8, alignment: std.mem.Alignment, _: usize) void {
c.mi_free_size_aligned(memory.ptr, memory.len, alignment.toByteUnits());
}

pub const vtable = &std.mem.Allocator.VTable{
.alloc = Allocator.alloc,
.resize = Allocator.resize,
.remap = Allocator.remap,
.free = Allocator.free,
};
};
5 changes: 3 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const App = @import("app.zig").App;
const Server = @import("server.zig").Server;
const Browser = @import("browser/browser.zig").Browser;
const DumpStripMode = @import("browser/dump.zig").Opts.StripMode;
const mimalloc = @import("browser/mimalloc.zig");

const build_config = @import("build_config");

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

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