authorgravatar for schteven.codes@gmail.comschtvn <schteven.codes@gmail.com> 2025-02-17 06:37:19-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-02-17 15:37:19+01:00
log1b62469ec93f78dbcebc90187eb9be795986d66f
treeb01e12ee0a9141e01024667a0a1d6ba63ce2354e
parentd7b93c78769360c954a364613c1c1b382020da0a
signaturebadge-check Signed by PGP key B5690EEEBB952194

Fix build failure in sbrk allocator, caused by #20511


2 files changed, 26 insertions(+), 7 deletions(-)

lib/std/heap.zig+1
......@@ -989,6 +989,7 @@ test {
989989 _ = GeneralPurposeAllocator;
990990 _ = FixedBufferAllocator;
991991 _ = ThreadSafeAllocator;
992 _ = SbrkAllocator;
992993 if (builtin.target.isWasm()) {
993994 _ = WasmAllocator;
994995 }
lib/std/heap/sbrk_allocator.zig+25-7
......@@ -11,6 +11,7 @@ pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
1111 pub const vtable: Allocator.VTable = .{
1212 .alloc = alloc,
1313 .resize = resize,
14 .remap = remap,
1415 .free = free,
1516 };
1617
......@@ -39,14 +40,13 @@ pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
3940
4041 // TODO don't do the naive locking strategy
4142 var lock: std.Thread.Mutex = .{};
42 fn alloc(ctx: *anyopaque, len: usize, log2_align: u8, return_address: usize) ?[*]u8 {
43 fn alloc(ctx: *anyopaque, len: usize, alignment: mem.Alignment, return_address: usize) ?[*]u8 {
4344 _ = ctx;
4445 _ = return_address;
4546 lock.lock();
4647 defer lock.unlock();
4748 // Make room for the freelist next pointer.
48 const alignment = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_align));
49 const actual_len = @max(len +| @sizeOf(usize), alignment);
49 const actual_len = @max(len +| @sizeOf(usize), alignment.toByteUnits());
5050 const slot_size = math.ceilPowerOfTwo(usize, actual_len) catch return null;
5151 const class = math.log2(slot_size) - min_class;
5252 if (class < size_class_count) {
......@@ -82,7 +82,7 @@ pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
8282 fn resize(
8383 ctx: *anyopaque,
8484 buf: []u8,
85 log2_buf_align: u8,
85 alignment: mem.Alignment,
8686 new_len: usize,
8787 return_address: usize,
8888 ) bool {
......@@ -92,7 +92,7 @@ pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
9292 defer lock.unlock();
9393 // We don't want to move anything from one size class to another, but we
9494 // can recover bytes in between powers of two.
95 const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
95 const buf_align = alignment.toByteUnits();
9696 const old_actual_len = @max(buf.len + @sizeOf(usize), buf_align);
9797 const new_actual_len = @max(new_len +| @sizeOf(usize), buf_align);
9898 const old_small_slot_size = math.ceilPowerOfTwoAssert(usize, old_actual_len);
......@@ -109,17 +109,27 @@ pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
109109 }
110110 }
111111
112 fn remap(
113 context: *anyopaque,
114 memory: []u8,
115 alignment: mem.Alignment,
116 new_len: usize,
117 return_address: usize,
118 ) ?[*]u8 {
119 return if (resize(context, memory, alignment, new_len, return_address)) memory.ptr else null;
120 }
121
112122 fn free(
113123 ctx: *anyopaque,
114124 buf: []u8,
115 log2_buf_align: u8,
125 alignment: mem.Alignment,
116126 return_address: usize,
117127 ) void {
118128 _ = ctx;
119129 _ = return_address;
120130 lock.lock();
121131 defer lock.unlock();
122 const buf_align = @as(usize, 1) << @as(Allocator.Log2Align, @intCast(log2_buf_align));
132 const buf_align = alignment.toByteUnits();
123133 const actual_len = @max(buf.len + @sizeOf(usize), buf_align);
124134 const slot_size = math.ceilPowerOfTwoAssert(usize, actual_len);
125135 const class = math.log2(slot_size) - min_class;
......@@ -158,3 +168,11 @@ pub fn SbrkAllocator(comptime sbrk: *const fn (n: usize) usize) type {
158168 }
159169 };
160170}
171
172test SbrkAllocator {
173 _ = SbrkAllocator(struct {
174 fn sbrk(_: usize) usize {
175 return 0;
176 }
177 }.sbrk);
178}