aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-14 23:44:07 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-03-14 23:44:07 -0700
commit9a43eb903dbdd619e25faa9bdc9d79b4f32e055d (patch)
tree88e4edb276f7d59290f71b9276fa78f81c77a769 /src
parent5a6010deda0bc6f2f31e89c11978d9d62cc70a05 (diff)
downloadbun-9a43eb903dbdd619e25faa9bdc9d79b4f32e055d.tar.gz
bun-9a43eb903dbdd619e25faa9bdc9d79b4f32e055d.tar.zst
bun-9a43eb903dbdd619e25faa9bdc9d79b4f32e055d.zip
cleanup some code
Diffstat (limited to 'src')
-rw-r--r--src/memory_allocator.zig81
-rw-r--r--src/mimalloc_arena.zig14
2 files changed, 14 insertions, 81 deletions
diff --git a/src/memory_allocator.zig b/src/memory_allocator.zig
index 1cc6f9caf..19b738add 100644
--- a/src/memory_allocator.zig
+++ b/src/memory_allocator.zig
@@ -32,35 +32,14 @@ const c = struct {
const Allocator = mem.Allocator;
const assert = std.debug.assert;
const CAllocator = struct {
- usingnamespace if (@hasDecl(c, "malloc_size"))
- struct {
- pub const supports_malloc_size = true;
- pub const malloc_size = c.malloc_size;
- }
- else if (@hasDecl(c, "malloc_usable_size"))
- struct {
- pub const supports_malloc_size = true;
- pub const malloc_size = c.malloc_usable_size;
- }
- else if (@hasDecl(c, "_msize"))
- struct {
- pub const supports_malloc_size = true;
- pub const malloc_size = c._msize;
- }
- else
- struct {
- pub const supports_malloc_size = false;
- };
-
+ const malloc_size = c.malloc_size;
pub const supports_posix_memalign = true;
- fn getHeader(ptr: [*]u8) *[*]u8 {
- return @intToPtr(*[*]u8, @ptrToInt(ptr) - @sizeOf(usize));
- }
-
+ // This is copied from Rust's mimalloc integration
const MI_MAX_ALIGN_SIZE = 16;
inline fn mi_malloc_satisfies_alignment(alignment: usize, size: usize) bool {
- return (alignment == @sizeOf(*anyopaque) or (alignment == MI_MAX_ALIGN_SIZE and size > (MI_MAX_ALIGN_SIZE / 2)));
+ return (alignment == @sizeOf(*anyopaque) or
+ (alignment == MI_MAX_ALIGN_SIZE and size > (MI_MAX_ALIGN_SIZE / 2)));
}
fn alignedAlloc(len: usize, alignment: usize) ?[*]u8 {
@@ -74,10 +53,6 @@ const CAllocator = struct {
return @ptrCast([*]u8, ptr orelse null);
}
- fn alignedFree(ptr: [*]u8) void {
- return c.free(ptr);
- }
-
fn alignedAllocSize(ptr: [*]u8) usize {
return CAllocator.malloc_size(ptr);
}
@@ -93,22 +68,11 @@ const CAllocator = struct {
assert(len > 0);
assert(std.math.isPowerOfTwo(alignment));
- var ptr = alignedAlloc(
- len,
- alignment,
- ) orelse return error.OutOfMemory;
+ var ptr = alignedAlloc(len, alignment) orelse return error.OutOfMemory;
if (len_align == 0) {
return ptr[0..len];
}
-
- if (comptime Environment.allow_assert) {
- const size = mem.alignBackwardAnyAlign(mimalloc.mi_usable_size(ptr), len_align);
-
- assert(size >= len);
- return ptr[0..size];
- } else {
- return ptr[0..mem.alignBackwardAnyAlign(mimalloc.mi_usable_size(ptr), len_align)];
- }
+ return ptr[0..mem.alignBackwardAnyAlign(mimalloc.mi_usable_size(ptr), len_align)];
}
fn resize(
@@ -164,35 +128,14 @@ const c_allocator_vtable = Allocator.VTable{
// This is a memory allocator which always writes zero instead of undefined
const ZAllocator = struct {
- usingnamespace if (@hasDecl(c, "malloc_size"))
- struct {
- pub const supports_malloc_size = true;
- pub const malloc_size = c.malloc_size;
- }
- else if (@hasDecl(c, "malloc_usable_size"))
- struct {
- pub const supports_malloc_size = true;
- pub const malloc_size = c.malloc_usable_size;
- }
- else if (@hasDecl(c, "_msize"))
- struct {
- pub const supports_malloc_size = true;
- pub const malloc_size = c._msize;
- }
- else
- struct {
- pub const supports_malloc_size = false;
- };
-
+ const malloc_size = c.malloc_size;
pub const supports_posix_memalign = true;
- fn getHeader(ptr: [*]u8) *[*]u8 {
- return @intToPtr(*[*]u8, @ptrToInt(ptr) - @sizeOf(usize));
- }
-
+ // This is copied from Rust's mimalloc integration
const MI_MAX_ALIGN_SIZE = 16;
inline fn mi_malloc_satisfies_alignment(alignment: usize, size: usize) bool {
- return (alignment == @sizeOf(*anyopaque) or (alignment == MI_MAX_ALIGN_SIZE and size > (MI_MAX_ALIGN_SIZE / 2)));
+ return (alignment == @sizeOf(*anyopaque) or
+ (alignment == MI_MAX_ALIGN_SIZE and size > (MI_MAX_ALIGN_SIZE / 2)));
}
fn alignedAlloc(len: usize, alignment: usize) ?[*]u8 {
@@ -206,10 +149,6 @@ const ZAllocator = struct {
return @ptrCast([*]u8, ptr orelse null);
}
- fn alignedFree(ptr: [*]u8) void {
- return c.free(ptr);
- }
-
fn alignedAllocSize(ptr: [*]u8) usize {
return ZAllocator.malloc_size(ptr);
}
diff --git a/src/mimalloc_arena.zig b/src/mimalloc_arena.zig
index 21eae175b..2c2f493ce 100644
--- a/src/mimalloc_arena.zig
+++ b/src/mimalloc_arena.zig
@@ -26,24 +26,22 @@ pub const Arena = struct {
pub fn reset(this: *Arena) void {
this.deinit();
- this.* = initAssumeCapacity();
+ this.* = init() catch unreachable;
}
pub fn init() !Arena {
return Arena{ .heap = mimalloc.mi_heap_new() orelse return error.OutOfMemory };
}
- pub fn initAssumeCapacity() Arena {
- return Arena{ .heap = mimalloc.mi_heap_new().? };
- }
-
pub fn gc(this: Arena, force: bool) void {
mimalloc.mi_heap_collect(this.heap, force);
}
+ // Copied from rust
const MI_MAX_ALIGN_SIZE = 16;
inline fn mi_malloc_satisfies_alignment(alignment: usize, size: usize) bool {
- return (alignment == @sizeOf(*anyopaque) or (alignment == MI_MAX_ALIGN_SIZE and size > (MI_MAX_ALIGN_SIZE / 2)));
+ return (alignment == @sizeOf(*anyopaque) or
+ (alignment == MI_MAX_ALIGN_SIZE and size > (MI_MAX_ALIGN_SIZE / 2)));
}
fn alignedAlloc(heap: *mimalloc.mi_heap_t, len: usize, alignment: usize) ?[*]u8 {
@@ -58,10 +56,6 @@ pub const Arena = struct {
return @ptrCast([*]u8, ptr orelse null);
}
- fn alignedFree(ptr: [*]u8) void {
- return mimalloc.mi_free(ptr);
- }
-
fn alloc(
arena: *anyopaque,
len: usize,