| ... | ... | @@ -20,6 +20,7 @@ comptime { |
| 20 | 20 | pub const vtable: Allocator.VTable = .{ |
| 21 | 21 | .alloc = alloc, |
| 22 | 22 | .resize = resize, |
| 23 | .remap = remap, |
| 23 | 24 | .free = free, |
| 24 | 25 | }; |
| 25 | 26 | |
| ... | ... | @@ -46,12 +47,11 @@ var frees: [size_class_count]usize = @splat(0); |
| 46 | 47 | /// For each big size class, points to the freed pointer. |
| 47 | 48 | var big_frees: [big_size_class_count]usize = @splat(0); |
| 48 | 49 | |
| 49 | | fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*]u8 { |
| 50 | fn alloc(ctx: *anyopaque, len: usize, alignment: mem.Alignment, return_address: usize) ?[*]u8 { |
| 50 | 51 | _ = ctx; |
| 51 | 52 | _ = return_address; |
| 52 | 53 | // Make room for the freelist next pointer. |
| 53 | | const alignment = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_align)); |
| 54 | | const actual_len = @max(len +| @sizeOf(usize), alignment); |
| 54 | const actual_len = @max(len +| @sizeOf(usize), alignment.toByteUnits()); |
| 55 | 55 | const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null; |
| 56 | 56 | const class = math.log2(slot_size) - min_class; |
| 57 | 57 | if (class < size_class_count) { |
| ... | ... | @@ -86,7 +86,7 @@ fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[* |
| 86 | 86 | fn resize( |
| 87 | 87 | ctx: *anyopaque, |
| 88 | 88 | buf: []u8, |
| 89 | | log2_buf_align: u8, |
| 89 | alignment: mem.Alignment, |
| 90 | 90 | new_len: usize, |
| 91 | 91 | return_address: usize, |
| 92 | 92 | ) bool { |
| ... | ... | @@ -94,7 +94,7 @@ fn resize( |
| 94 | 94 | _ = return_address; |
| 95 | 95 | // We don't want to move anything from one size class to another, but we |
| 96 | 96 | // can recover bytes in between powers of two. |
| 97 | | const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align)); |
| 97 | const buf_align = alignment.toByteUnits(); |
| 98 | 98 | const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align); |
| 99 | 99 | const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align); |
| 100 | 100 | const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len); |
| ... | ... | @@ -111,15 +111,25 @@ fn resize( |
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | fn remap( |
| 115 | context: *anyopaque, |
| 116 | memory: []u8, |
| 117 | alignment: mem.Alignment, |
| 118 | new_len: usize, |
| 119 | return_address: usize, |
| 120 | ) ?[*]u8 { |
| 121 | return if (resize(context, memory, alignment, new_len, return_address)) memory.ptr else null; |
| 122 | } |
| 123 | |
| 114 | 124 | fn free( |
| 115 | 125 | ctx: *anyopaque, |
| 116 | 126 | buf: []u8, |
| 117 | | log2_buf_align: u8, |
| 127 | alignment: mem.Alignment, |
| 118 | 128 | return_address: usize, |
| 119 | 129 | ) void { |
| 120 | 130 | _ = ctx; |
| 121 | 131 | _ = return_address; |
| 122 | | const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align)); |
| 132 | const buf_align = alignment.toByteUnits(); |
| 123 | 133 | const actual_len = @max(buf.len + @sizeOf(usize), buf_align); |
| 124 | 134 | const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len); |
| 125 | 135 | const class = math.log2(slot_size) - min_class; |