aboutsummaryrefslogtreecommitdiff
path: root/src/zlib.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zlib.zig')
-rw-r--r--src/zlib.zig46
1 files changed, 18 insertions, 28 deletions
diff --git a/src/zlib.zig b/src/zlib.zig
index b578f0ede..6b2e9dc48 100644
--- a/src/zlib.zig
+++ b/src/zlib.zig
@@ -3,6 +3,8 @@
const std = @import("std");
const bun = @import("root").bun;
+const mimalloc = @import("./allocators/mimalloc.zig");
+
pub const MAX_WBITS = 15;
test "Zlib Read" {
@@ -240,22 +242,19 @@ pub fn NewZlibReader(comptime Writer: type, comptime buffer_size: usize) type {
buf: [buffer_size]u8,
zlib: zStream_struct,
allocator: std.mem.Allocator,
- arena: @import("root").bun.ArenaAllocator,
state: State = State.Uninitialized,
- pub fn alloc(ctx: *anyopaque, items: uInt, len: uInt) callconv(.C) *anyopaque {
- var this = @as(*ZlibReader, @ptrCast(@alignCast(ctx)));
- const buf = this.arena.allocator().alloc(u8, items * len) catch unreachable;
- return buf.ptr;
+ pub fn alloc(_: *anyopaque, items: uInt, len: uInt) callconv(.C) *anyopaque {
+ return mimalloc.mi_malloc(items * len) orelse unreachable;
}
- // we free manually all at once
- pub fn free(_: *anyopaque, _: *anyopaque) callconv(.C) void {}
+ pub fn free(_: *anyopaque, data: *anyopaque) callconv(.C) void {
+ mimalloc.mi_free(data);
+ }
pub fn deinit(this: *ZlibReader) void {
var allocator = this.allocator;
this.end();
- this.arena.deinit();
allocator.destroy(this);
}
@@ -274,7 +273,6 @@ pub fn NewZlibReader(comptime Writer: type, comptime buffer_size: usize) type {
.buf = std.mem.zeroes([buffer_size]u8),
.allocator = allocator,
.zlib = undefined,
- .arena = @import("root").bun.ArenaAllocator.init(allocator),
};
zlib_reader.zlib = zStream_struct{
@@ -424,22 +422,19 @@ pub const ZlibReaderArrayList = struct {
list_ptr: *std.ArrayListUnmanaged(u8),
zlib: zStream_struct,
allocator: std.mem.Allocator,
- arena: @import("root").bun.ArenaAllocator,
state: State = State.Uninitialized,
- pub fn alloc(ctx: *anyopaque, items: uInt, len: uInt) callconv(.C) *anyopaque {
- var this = @as(*ZlibReader, @ptrCast(@alignCast(ctx)));
- const buf = this.allocator.alloc(u8, items * len) catch unreachable;
- return buf.ptr;
+ pub fn alloc(_: *anyopaque, items: uInt, len: uInt) callconv(.C) *anyopaque {
+ return mimalloc.mi_malloc(items * len) orelse unreachable;
}
- // we free manually all at once
- pub fn free(_: *anyopaque, _: *anyopaque) callconv(.C) void {}
+ pub fn free(_: *anyopaque, data: *anyopaque) callconv(.C) void {
+ mimalloc.mi_free(data);
+ }
pub fn deinit(this: *ZlibReader) void {
var allocator = this.allocator;
this.end();
- this.arena.deinit();
allocator.destroy(this);
}
@@ -475,7 +470,6 @@ pub const ZlibReaderArrayList = struct {
.list_ptr = list,
.allocator = allocator,
.zlib = undefined,
- .arena = @import("root").bun.ArenaAllocator.init(allocator),
};
zlib_reader.zlib = zStream_struct{
@@ -835,22 +829,19 @@ pub const ZlibCompressorArrayList = struct {
list_ptr: *std.ArrayListUnmanaged(u8),
zlib: zStream_struct,
allocator: std.mem.Allocator,
- arena: @import("root").bun.ArenaAllocator,
state: State = State.Uninitialized,
- pub fn alloc(ctx: *anyopaque, items: uInt, len: uInt) callconv(.C) *anyopaque {
- var this = @as(*ZlibCompressor, @ptrCast(@alignCast(ctx)));
- const buf = this.allocator.alloc(u8, items * len) catch unreachable;
- return buf.ptr;
+ pub fn alloc(_: *anyopaque, items: uInt, len: uInt) callconv(.C) *anyopaque {
+ return mimalloc.mi_malloc(items * len) orelse unreachable;
}
- // we free manually all at once
- pub fn free(_: *anyopaque, _: *anyopaque) callconv(.C) void {}
+ pub fn free(_: *anyopaque, data: *anyopaque) callconv(.C) void {
+ mimalloc.mi_free(data);
+ }
pub fn deinit(this: *ZlibCompressor) void {
var allocator = this.allocator;
this.end();
- this.arena.deinit();
allocator.destroy(this);
}
@@ -874,7 +865,6 @@ pub const ZlibCompressorArrayList = struct {
.list_allocator = list_allocator,
.allocator = allocator,
.zlib = undefined,
- .arena = @import("root").bun.ArenaAllocator.init(allocator),
};
zlib_reader.zlib = zStream_struct{
@@ -909,7 +899,7 @@ pub const ZlibCompressorArrayList = struct {
@sizeOf(zStream_struct),
)) {
ReturnCode.Ok => {
- try zlib_reader.list.ensureTotalCapacityPrecise(allocator, deflateBound(&zlib_reader.zlib, input.len));
+ try zlib_reader.list.ensureTotalCapacityPrecise(list_allocator, deflateBound(&zlib_reader.zlib, input.len));
zlib_reader.list_ptr.* = zlib_reader.list;
zlib_reader.zlib.avail_out = @as(uInt, @truncate(zlib_reader.list.capacity));
zlib_reader.zlib.next_out = zlib_reader.list.items.ptr;