authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-07 00:58:01-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-07 14:41:49-08:00
log88e2e60e88ebaeb46b2642830cd8cd369dc737c0
tree35a64b4fea199d59b4216f3ca23a63f753614306
parent1ffae59fec60816ff364b4ade93c6fc2fc1571cf

std.heap.SmpAllocator: simplify by putting freelist node at start


1 files changed, 5 insertions(+), 10 deletions(-)

lib/std/heap/SmpAllocator.zig+5-10
......@@ -51,7 +51,7 @@ threadlocal var thread_index: u32 = 0;
5151const max_thread_count = 128;
5252const slab_len: usize = @max(std.heap.page_size_max, 256 * 1024);
5353/// Because of storing free list pointers, the minimum size class is 3.
54const min_class = math.log2(math.ceilPowerOfTwoAssert(usize, 1 + @sizeOf(usize)));
54const min_class = math.log2(@sizeOf(usize));
5555const size_class_count = math.log2(slab_len) - min_class;
5656
5757const Thread = struct {
......@@ -132,7 +132,7 @@ fn alloc(context: *anyopaque, len: usize, alignment: mem.Alignment, ra: usize) ?
132132 if (top_free_ptr != 0) {
133133 @branchHint(.likely);
134134 defer t.unlock();
135 const node: *usize = @ptrFromInt(top_free_ptr + (slot_size - @sizeOf(usize)));
135 const node: *usize = @ptrFromInt(top_free_ptr);
136136 t.frees[class] = node.*;
137137 return @ptrFromInt(top_free_ptr);
138138 }
......@@ -202,22 +202,17 @@ fn free(context: *anyopaque, memory: []u8, alignment: mem.Alignment, ra: usize)
202202 return PageAllocator.unmap(@alignCast(memory));
203203 }
204204
205 const slot_size = slotSize(class);
206 const addr = @intFromPtr(memory.ptr);
207 const node: *usize = @ptrFromInt(addr + (slot_size - @sizeOf(usize)));
205 const node: *usize = @alignCast(@ptrCast(memory.ptr));
208206
209207 const t = Thread.lock();
210208 defer t.unlock();
211209
212210 node.* = t.frees[class];
213 t.frees[class] = addr;
211 t.frees[class] = @intFromPtr(node);
214212}
215213
216214fn sizeClassIndex(len: usize, alignment: mem.Alignment) usize {
217 return @max(
218 @bitSizeOf(usize) - @clz(len + (@sizeOf(usize) - 1)),
219 @intFromEnum(alignment) + 1,
220 ) - min_class;
215 return @max(@bitSizeOf(usize) - @clz(len - 1), @intFromEnum(alignment), min_class) - min_class;
221216}
222217
223218fn slotSize(class: usize) usize {