mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
glue mimalloc with netsurf C libs
This commit is contained in:
@@ -152,6 +152,10 @@ fn common(
|
|||||||
) !void {
|
) !void {
|
||||||
try jsruntime_pkgs.add(step, options);
|
try jsruntime_pkgs.add(step, options);
|
||||||
linkNetSurf(step);
|
linkNetSurf(step);
|
||||||
|
|
||||||
|
// link mimalloc
|
||||||
|
step.addObjectFile(.{ .path = "vendor/mimalloc/out/libmimalloc.a" });
|
||||||
|
step.addIncludePath(.{ .path = "vendor/mimalloc/include" });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn linkNetSurf(step: *std.build.LibExeObjStep) void {
|
fn linkNetSurf(step: *std.build.LibExeObjStep) void {
|
||||||
|
|||||||
48
src/mimalloc.zig
Normal file
48
src/mimalloc.zig
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
// This file makes the glue between mimalloc heap allocation and libdom memory
|
||||||
|
// management.
|
||||||
|
// We replace the libdom default usage of allocations with mimalloc heap
|
||||||
|
// allocation to be able to free all memory used at once, like an arena usage.
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const c = @cImport({
|
||||||
|
@cInclude("mimalloc.h");
|
||||||
|
});
|
||||||
|
|
||||||
|
const Error = error{
|
||||||
|
HeapNotNull,
|
||||||
|
HeapNull,
|
||||||
|
};
|
||||||
|
|
||||||
|
var heap: ?*c.mi_heap_t = null;
|
||||||
|
|
||||||
|
pub fn create() Error!void {
|
||||||
|
if (heap != null) return Error.HeapNotNull;
|
||||||
|
heap = c.mi_heap_new();
|
||||||
|
if (heap == null) return Error.HeapNull;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn destroy() void {
|
||||||
|
if (heap == null) return;
|
||||||
|
c.mi_heap_destroy(heap.?);
|
||||||
|
heap = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub export fn m_alloc(size: usize) callconv(.C) ?*anyopaque {
|
||||||
|
if (heap == null) return null;
|
||||||
|
return c.mi_heap_malloc(heap.?, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub export fn re_alloc(ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque {
|
||||||
|
if (heap == null) return null;
|
||||||
|
return c.mi_heap_realloc(heap.?, ptr, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub export fn c_alloc(nmemb: usize, size: usize) callconv(.C) ?*anyopaque {
|
||||||
|
if (heap == null) return null;
|
||||||
|
return c.mi_heap_calloc(heap.?, nmemb, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOOP, use destroy to clear all the memory allocated at once.
|
||||||
|
pub export fn f_ree(_: ?*anyopaque) callconv(.C) void {
|
||||||
|
return;
|
||||||
|
}
|
||||||
@@ -8,9 +8,29 @@ const c = @cImport({
|
|||||||
@cInclude("events/event.h");
|
@cInclude("events/event.h");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const mimalloc = @import("mimalloc.zig");
|
||||||
|
|
||||||
const Callback = @import("jsruntime").Callback;
|
const Callback = @import("jsruntime").Callback;
|
||||||
const EventToInterface = @import("events/event.zig").Event.toInterface;
|
const EventToInterface = @import("events/event.zig").Event.toInterface;
|
||||||
|
|
||||||
|
// init initializes netsurf lib.
|
||||||
|
// init starts a mimalloc heap arena for the netsurf session. The caller must
|
||||||
|
// call deinit() to free the arena memory.
|
||||||
|
pub fn init() !void {
|
||||||
|
try mimalloc.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
// deinit frees the mimalloc heap arena memory.
|
||||||
|
// It also clean dom namespaces and lwc strings.
|
||||||
|
pub fn deinit() void {
|
||||||
|
_ = c.dom_namespace_finalise();
|
||||||
|
|
||||||
|
// destroy all lwc strings.
|
||||||
|
c.lwc_deinit_strings();
|
||||||
|
|
||||||
|
mimalloc.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
// Vtable
|
// Vtable
|
||||||
// ------
|
// ------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user