Skip to content

Commit b2cf5df

Browse files
committed
Switch mimalloc guards to assertions
The thin mimalloc API is currently defensive around incorrect setup/teardown by guarding against using/destroying the arena when the heap is null, or creating an arena when it already exists. The only time these checks will fail is when the code is wrong, e.g. trying to use libdom before or after freeing the arena. The current behavior can mask these errors, plus add runtime overhead.
1 parent f66f4d9 commit b2cf5df

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

src/browser/mimalloc.zig

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// management.
2121
// We replace the libdom default usage of allocations with mimalloc heap
2222
// allocation to be able to free all memory used at once, like an arena usage.
23+
const std = @import("std");
2324

2425
const c = @cImport({
2526
@cInclude("mimalloc.h");
@@ -33,39 +34,39 @@ const Error = error{
3334
var heap: ?*c.mi_heap_t = null;
3435

3536
pub fn create() Error!void {
36-
if (heap != null) return Error.HeapNotNull;
37+
std.debug.assert(heap == null);
3738
heap = c.mi_heap_new();
38-
if (heap == null) return Error.HeapNull;
39+
std.debug.assert(heap != null);
3940
}
4041

4142
pub fn destroy() void {
42-
if (heap == null) return;
43+
std.debug.assert(heap != null);
4344
c.mi_heap_destroy(heap.?);
4445
heap = null;
4546
}
4647

4748
pub export fn m_alloc(size: usize) callconv(.c) ?*anyopaque {
48-
if (heap == null) return null;
49+
std.debug.assert(heap != null);
4950
return c.mi_heap_malloc(heap.?, size);
5051
}
5152

5253
pub export fn re_alloc(ptr: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque {
53-
if (heap == null) return null;
54+
std.debug.assert(heap != null);
5455
return c.mi_heap_realloc(heap.?, ptr, size);
5556
}
5657

5758
pub export fn c_alloc(nmemb: usize, size: usize) callconv(.c) ?*anyopaque {
58-
if (heap == null) return null;
59+
std.debug.assert(heap != null);
5960
return c.mi_heap_calloc(heap.?, nmemb, size);
6061
}
6162

6263
pub export fn str_dup(s: [*c]const u8) callconv(.c) [*c]u8 {
63-
if (heap == null) return null;
64+
std.debug.assert(heap != null);
6465
return c.mi_heap_strdup(heap.?, s);
6566
}
6667

6768
pub export fn strn_dup(s: [*c]const u8, size: usize) callconv(.c) [*c]u8 {
68-
if (heap == null) return null;
69+
std.debug.assert(heap != null);
6970
return c.mi_heap_strndup(heap.?, s, size);
7071
}
7172

src/cdp/Node.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ pub const Writer = struct {
331331

332332
const testing = @import("testing.zig");
333333
test "cdp Node: Registry register" {
334+
try parser.init();
335+
defer parser.deinit();
336+
334337
var registry = Registry.init(testing.allocator);
335338
defer registry.deinit();
336339

@@ -366,6 +369,9 @@ test "cdp Node: Registry register" {
366369
}
367370

368371
test "cdp Node: search list" {
372+
try parser.init();
373+
defer parser.deinit();
374+
369375
var registry = Registry.init(testing.allocator);
370376
defer registry.deinit();
371377

@@ -417,6 +423,9 @@ test "cdp Node: search list" {
417423
}
418424

419425
test "cdp Node: Writer" {
426+
try parser.init();
427+
defer parser.deinit();
428+
420429
var registry = Registry.init(testing.allocator);
421430
defer registry.deinit();
422431

src/cdp/testing.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ const TestContext = struct {
121121

122122
if (opts.html) |html| {
123123
if (bc.session_id == null) bc.session_id = "SID-X";
124-
parser.deinit();
125124
const page = try bc.session.createPage();
126125
page.window.document = (try Document.init(html)).doc;
127126
}

src/testing.zig

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,6 @@ pub const Document = struct {
211211
arena: std.heap.ArenaAllocator,
212212

213213
pub fn init(html: []const u8) !Document {
214-
parser.deinit();
215-
try parser.init();
216-
217214
var fbs = std.io.fixedBufferStream(html);
218215
const html_doc = try parser.documentHTMLParse(fbs.reader(), "utf-8");
219216

@@ -224,7 +221,6 @@ pub const Document = struct {
224221
}
225222

226223
pub fn deinit(self: *Document) void {
227-
parser.deinit();
228224
self.arena.deinit();
229225
}
230226

@@ -375,8 +371,6 @@ pub const JsRunner = struct {
375371
allocator: Allocator,
376372

377373
fn init(alloc: Allocator, opts: RunnerOpts) !JsRunner {
378-
parser.deinit();
379-
380374
const browser = try alloc.create(Browser);
381375
errdefer alloc.destroy(browser);
382376

@@ -493,14 +487,11 @@ var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
493487
pub var test_app: *App = undefined;
494488

495489
pub fn setup() !void {
496-
try parser.init();
497-
498490
test_app = try App.init(gpa.allocator(), .{
499491
.run_mode = .serve,
500492
.tls_verify_host = false,
501493
});
502494
}
503495
pub fn shutdown() void {
504-
parser.deinit();
505496
test_app.deinit();
506497
}

0 commit comments

Comments
 (0)