mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
77 lines
2.3 KiB
Zig
77 lines
2.3 KiB
Zig
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
|
|
//
|
|
// Francis Bouvier <francis@lightpanda.io>
|
|
// Pierre Tachoire <pierre@lightpanda.io>
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
// License, or (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
// 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);
|
|
}
|
|
|
|
pub export fn str_dup(s: [*c]const u8) callconv(.C) [*c]u8 {
|
|
if (heap == null) return null;
|
|
return c.mi_heap_strdup(heap.?, s);
|
|
}
|
|
|
|
pub export fn strn_dup(s: [*c]const u8, size: usize) callconv(.C) [*c]u8 {
|
|
if (heap == null) return null;
|
|
return c.mi_heap_strndup(heap.?, s, size);
|
|
}
|
|
|
|
// NOOP, use destroy to clear all the memory allocated at once.
|
|
pub export fn f_ree(_: ?*anyopaque) callconv(.C) void {
|
|
return;
|
|
}
|