@@ -62,8 +62,8 @@ pub fn getRSS() i64 {
6262 const data = writer .written ();
6363 const index = std .mem .indexOf (u8 , data , "rss: " ) orelse return -1 ;
6464 const sep = std .mem .indexOfScalarPos (u8 , data , index + 5 , ' ' ) orelse return -2 ;
65- const value = std .fmt .parseFloat (f64 , data [index + 5 .. sep ]) catch return -3 ;
66- const unit = data [sep + 1 .. ];
65+ const value = std .fmt .parseFloat (f64 , data [index + 5 .. sep ]) catch return -3 ;
66+ const unit = data [sep + 1 .. ];
6767 if (std .mem .startsWith (u8 , unit , "KiB," )) {
6868 return @as (i64 , @intFromFloat (value )) * 1024 ;
6969 }
@@ -108,3 +108,36 @@ pub export fn strn_dup(s: [*c]const u8, size: usize) callconv(.c) [*c]u8 {
108108pub export fn f_ree (_ : ? * anyopaque ) callconv (.c ) void {
109109 return ;
110110}
111+
112+ /// An allocator that use mimalloc to manage memory.
113+ pub const allocator = std.mem.Allocator {
114+ .ptr = undefined ,
115+ .vtable = Allocator .vtable ,
116+ };
117+
118+ const Allocator = struct {
119+ fn alloc (_ : * anyopaque , len : usize , alignment : std.mem.Alignment , _ : usize ) ? [* ]u8 {
120+ const ptr = c .mi_malloc_aligned (len , alignment .toByteUnits ());
121+ return @ptrCast (ptr );
122+ }
123+
124+ fn resize (_ : * anyopaque , memory : []u8 , _ : std.mem.Alignment , new_len : usize , _ : usize ) bool {
125+ return c .mi_expand (memory .ptr , new_len ) != null ;
126+ }
127+
128+ fn remap (_ : * anyopaque , memory : []u8 , alignment : std.mem.Alignment , new_len : usize , _ : usize ) ? [* ]u8 {
129+ const ptr = c .mi_realloc_aligned (memory .ptr , new_len , alignment .toByteUnits ());
130+ return @ptrCast (ptr );
131+ }
132+
133+ fn free (_ : * anyopaque , memory : []u8 , alignment : std.mem.Alignment , _ : usize ) void {
134+ c .mi_free_size_aligned (memory .ptr , memory .len , alignment .toByteUnits ());
135+ }
136+
137+ pub const vtable = & std.mem.Allocator.VTable {
138+ .alloc = Allocator .alloc ,
139+ .resize = Allocator .resize ,
140+ .remap = Allocator .remap ,
141+ .free = Allocator .free ,
142+ };
143+ };
0 commit comments