mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 19:08:50 +00:00
* @min and @max * builtins and some trivial ones * Most of them * more * more! * More Progress * wip * Update tagged_pointer.zig * Update http_client_async.zig * Most of the iterable dir changes * alright * Remove usages of deprecated formatters * 📷 * fmt * Update shimmer.zig * wip * wip * wip * progress * more * Latest * stuck on error * latest * workaround stage2 * wip * Update string_immutable.zig * wip * Migrate `Dirent` and `require("fs')` to use JSC<>Zig bindings * Fix build errors * Fixup most of the test failures * Fix `make headers` * Fix "outside package path" error * Fixup aligned alloc * Add missing file * linux * More linux fixes * use latest peechy * Fix transpiler test failure * Forgot about these * Fixup test failure * Update node-timers.test.ts * [node:htt] Fix `undefined is not an object` error Fixes https://github.com/oven-sh/bun/issues/1618 * Update http.exports.js * Make this test less flaky * fix hashes * Fix hex formatting and zls issues * Download zig version * Update Dockerfile * Update Dockerfile * Update uws * Update Dockerfile * Set llvm version * Update README.md * Update uws * Update Dockerfile * Update io_linux.zig * Update bun.zig * Log output * workaround strange @cInclude error * Make ffi tests better * Don't use cImport * Update c.zig * Update c-bindings.cpp * call setOutputDir * Update Dockerfile * Use a longer name * latest * Update serve.test.ts Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: Veikka Tuominen <git@vexu.eu>
212 lines
6.3 KiB
Zig
212 lines
6.3 KiB
Zig
const mem = @import("std").mem;
|
|
const builtin = @import("std").builtin;
|
|
const std = @import("std");
|
|
|
|
const mimalloc = @import("./allocators/mimalloc.zig");
|
|
const Environment = @import("./env.zig");
|
|
const FeatureFlags = @import("./feature_flags.zig");
|
|
const Allocator = mem.Allocator;
|
|
const assert = std.debug.assert;
|
|
const bun = @import("bun");
|
|
|
|
pub const GlobalArena = struct {
|
|
arena: Arena,
|
|
fallback_allocator: std.mem.Allocator,
|
|
|
|
pub fn initWithCapacity(capacity: usize, fallback: std.mem.Allocator) error{OutOfMemory}!GlobalArena {
|
|
const arena = try Arena.initWithCapacity(capacity);
|
|
|
|
return GlobalArena{
|
|
.arena = arena,
|
|
.fallback_allocator = fallback,
|
|
};
|
|
}
|
|
|
|
pub fn allocator(this: *GlobalArena) Allocator {
|
|
return .{
|
|
.ptr = this,
|
|
.vtable = &.{
|
|
.alloc = alloc,
|
|
.resize = resize,
|
|
.free = free,
|
|
},
|
|
};
|
|
}
|
|
|
|
fn alloc(
|
|
self: *GlobalArena,
|
|
len: usize,
|
|
ptr_align: u29,
|
|
len_align: u29,
|
|
return_address: usize,
|
|
) error{OutOfMemory}![]u8 {
|
|
return self.arena.alloc(len, ptr_align, len_align, return_address) catch
|
|
return self.fallback_allocator.rawAlloc(len, ptr_align, return_address) orelse return error.OutOfMemory;
|
|
}
|
|
|
|
fn resize(
|
|
self: *GlobalArena,
|
|
buf: []u8,
|
|
buf_align: u29,
|
|
new_len: usize,
|
|
len_align: u29,
|
|
return_address: usize,
|
|
) ?usize {
|
|
if (self.arena.ownsPtr(buf.ptr)) {
|
|
return self.arena.resize(buf, buf_align, new_len, len_align, return_address);
|
|
} else {
|
|
return self.fallback_allocator.rawResize(buf, buf_align, new_len, len_align, return_address);
|
|
}
|
|
}
|
|
|
|
fn free(
|
|
self: *GlobalArena,
|
|
buf: []u8,
|
|
buf_align: u29,
|
|
return_address: usize,
|
|
) void {
|
|
if (self.arena.ownsPtr(buf.ptr)) {
|
|
return self.arena.free(buf, buf_align, return_address);
|
|
} else {
|
|
return self.fallback_allocator.rawFree(buf, buf_align, return_address);
|
|
}
|
|
}
|
|
};
|
|
|
|
pub const Arena = struct {
|
|
heap: ?*mimalloc.Heap = null,
|
|
|
|
/// Internally, mimalloc calls mi_heap_get_default()
|
|
/// to get the default heap.
|
|
/// It uses pthread_getspecific to do that.
|
|
/// We can save those extra calls if we just do it once in here
|
|
pub fn getThreadlocalDefault() Allocator {
|
|
return Allocator{ .ptr = mimalloc.mi_heap_get_default(), .vtable = &c_allocator_vtable };
|
|
}
|
|
|
|
pub fn backingAllocator(this: Arena) Allocator {
|
|
var arena = Arena{ .heap = this.heap.?.backing() };
|
|
return arena.allocator();
|
|
}
|
|
|
|
pub fn allocator(this: Arena) Allocator {
|
|
@setRuntimeSafety(false);
|
|
return Allocator{ .ptr = this.heap.?, .vtable = &c_allocator_vtable };
|
|
}
|
|
|
|
pub fn deinit(this: *Arena) void {
|
|
mimalloc.mi_heap_destroy(this.heap.?);
|
|
|
|
this.heap = null;
|
|
}
|
|
|
|
pub fn dumpThreadStats(_: *Arena) void {
|
|
const dump_fn = struct {
|
|
pub fn dump(textZ: [*:0]const u8, _: ?*anyopaque) callconv(.C) void {
|
|
const text = bun.span(textZ);
|
|
bun.Output.errorWriter().writeAll(text) catch {};
|
|
}
|
|
}.dump;
|
|
mimalloc.mi_thread_stats_print_out(dump_fn, null);
|
|
bun.Output.flush();
|
|
}
|
|
|
|
pub fn dumpStats(_: *Arena) void {
|
|
const dump_fn = struct {
|
|
pub fn dump(textZ: [*:0]const u8, _: ?*anyopaque) callconv(.C) void {
|
|
const text = bun.span(textZ);
|
|
bun.Output.errorWriter().writeAll(text) catch {};
|
|
}
|
|
}.dump;
|
|
mimalloc.mi_stats_print_out(dump_fn, null);
|
|
bun.Output.flush();
|
|
}
|
|
|
|
pub fn reset(this: *Arena) void {
|
|
this.deinit();
|
|
this.* = init() catch unreachable;
|
|
}
|
|
|
|
pub fn init() !Arena {
|
|
return Arena{ .heap = mimalloc.mi_heap_new() orelse return error.OutOfMemory };
|
|
}
|
|
|
|
pub fn gc(this: Arena, force: bool) void {
|
|
mimalloc.mi_heap_collect(this.heap orelse return, force);
|
|
}
|
|
|
|
pub fn ownsPtr(this: Arena, ptr: *const anyopaque) bool {
|
|
return mimalloc.mi_heap_check_owned(this.heap.?, ptr);
|
|
}
|
|
pub const supports_posix_memalign = true;
|
|
|
|
fn alignedAlloc(heap: *mimalloc.Heap, len: usize, alignment: usize) ?[*]u8 {
|
|
if (comptime FeatureFlags.log_allocations) std.debug.print("Malloc: {d}\n", .{len});
|
|
|
|
var ptr: ?*anyopaque = if (mimalloc.canUseAlignedAlloc(len, alignment))
|
|
mimalloc.mi_heap_malloc_aligned(heap, len, alignment)
|
|
else
|
|
mimalloc.mi_heap_malloc(heap, len);
|
|
|
|
if (comptime Environment.allow_assert) {
|
|
const usable = mimalloc.mi_malloc_usable_size(ptr);
|
|
if (usable < len) {
|
|
std.debug.panic("mimalloc: allocated size is too small: {d} < {d}", .{ usable, len });
|
|
}
|
|
}
|
|
|
|
return if (ptr) |p|
|
|
@ptrCast([*]u8, p)
|
|
else
|
|
null;
|
|
}
|
|
|
|
fn alignedAllocSize(ptr: [*]u8) usize {
|
|
return mimalloc.mi_malloc_usable_size(ptr);
|
|
}
|
|
|
|
fn alloc(arena: *anyopaque, len: usize, ptr_align: u8, _: usize) ?[*]u8 {
|
|
var this = bun.cast(*mimalloc.Heap, arena);
|
|
return alignedAlloc(this, len, ptr_align);
|
|
}
|
|
|
|
fn resize(_: *anyopaque, buf: []u8, _: u8, new_len: usize, _: usize) bool {
|
|
if (new_len <= buf.len) {
|
|
return true;
|
|
}
|
|
|
|
const full_len = alignedAllocSize(buf.ptr);
|
|
if (new_len <= full_len) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
fn free(
|
|
_: *anyopaque,
|
|
buf: []u8,
|
|
buf_align: u8,
|
|
_: usize,
|
|
) void {
|
|
// mi_free_size internally just asserts the size
|
|
// so it's faster if we don't pass that value through
|
|
// but its good to have that assertion
|
|
if (comptime Environment.allow_assert) {
|
|
assert(mimalloc.mi_is_in_heap_region(buf.ptr));
|
|
if (mimalloc.canUseAlignedAlloc(buf.len, buf_align))
|
|
mimalloc.mi_free_size_aligned(buf.ptr, buf.len, buf_align)
|
|
else
|
|
mimalloc.mi_free_size(buf.ptr, buf.len);
|
|
} else {
|
|
mimalloc.mi_free(buf.ptr);
|
|
}
|
|
}
|
|
};
|
|
|
|
const c_allocator_vtable = Allocator.VTable{
|
|
.alloc = &Arena.alloc,
|
|
.resize = &Arena.resize,
|
|
.free = &Arena.free,
|
|
};
|