Improve owned pointer types (#21908)

(For internal tracking: fixes STAB-1005, STAB-1006, STAB-1007,
STAB-1008, STAB-1009)
This commit is contained in:
taylor.fish
2025-08-15 19:05:25 -07:00
committed by GitHub
parent 599947de28
commit ecd74ac14c
11 changed files with 365 additions and 146 deletions

View File

@@ -3,7 +3,7 @@ const log = bun.Output.scoped(.mimalloc, .hidden);
fn mimalloc_free(
_: *anyopaque,
buf: []u8,
alignment: mem.Alignment,
alignment: Alignment,
_: usize,
) void {
if (comptime Environment.enable_logs)
@@ -23,7 +23,7 @@ fn mimalloc_free(
}
const MimallocAllocator = struct {
fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 {
fn alignedAlloc(len: usize, alignment: Alignment) ?[*]u8 {
if (comptime Environment.enable_logs)
log("mi_alloc({d}, {d})", .{ len, alignment.toByteUnits() });
@@ -48,15 +48,15 @@ const MimallocAllocator = struct {
return mimalloc.mi_malloc_size(ptr);
}
fn alloc_with_default_allocator(_: *anyopaque, len: usize, alignment: mem.Alignment, _: usize) ?[*]u8 {
fn alloc_with_default_allocator(_: *anyopaque, len: usize, alignment: Alignment, _: usize) ?[*]u8 {
return alignedAlloc(len, alignment);
}
fn resize_with_default_allocator(_: *anyopaque, buf: []u8, _: mem.Alignment, new_len: usize, _: usize) bool {
fn resize_with_default_allocator(_: *anyopaque, buf: []u8, _: Alignment, new_len: usize, _: usize) bool {
return mimalloc.mi_expand(buf.ptr, new_len) != null;
}
fn remap_with_default_allocator(_: *anyopaque, buf: []u8, alignment: mem.Alignment, new_len: usize, _: usize) ?[*]u8 {
fn remap_with_default_allocator(_: *anyopaque, buf: []u8, alignment: Alignment, new_len: usize, _: usize) ?[*]u8 {
return @ptrCast(mimalloc.mi_realloc_aligned(buf.ptr, new_len, alignment.toByteUnits()));
}
@@ -76,7 +76,7 @@ const c_allocator_vtable = &Allocator.VTable{
};
const ZAllocator = struct {
fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 {
fn alignedAlloc(len: usize, alignment: Alignment) ?[*]u8 {
log("ZAllocator.alignedAlloc: {d}\n", .{len});
const ptr = if (mimalloc.mustUseAlignedAlloc(alignment))
@@ -100,11 +100,11 @@ const ZAllocator = struct {
return mimalloc.mi_malloc_size(ptr);
}
fn alloc_with_z_allocator(_: *anyopaque, len: usize, alignment: mem.Alignment, _: usize) ?[*]u8 {
fn alloc_with_z_allocator(_: *anyopaque, len: usize, alignment: Alignment, _: usize) ?[*]u8 {
return alignedAlloc(len, alignment);
}
fn resize_with_z_allocator(_: *anyopaque, buf: []u8, _: mem.Alignment, new_len: usize, _: usize) bool {
fn resize_with_z_allocator(_: *anyopaque, buf: []u8, _: Alignment, new_len: usize, _: usize) bool {
if (new_len <= buf.len) {
return true;
}
@@ -135,7 +135,7 @@ pub const z_allocator = Allocator{
const z_allocator_vtable = Allocator.VTable{
.alloc = &ZAllocator.alloc_with_z_allocator,
.resize = &ZAllocator.resize_with_z_allocator,
.remap = &std.mem.Allocator.noRemap,
.remap = &Allocator.noRemap,
.free = &ZAllocator.free_with_z_allocator,
};
@@ -150,5 +150,5 @@ const std = @import("std");
const bun = @import("bun");
const mimalloc = bun.mimalloc;
const mem = @import("std").mem;
const Allocator = mem.Allocator;
const Alignment = std.mem.Alignment;
const Allocator = std.mem.Allocator;