authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-05 16:21:21-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-06 14:23:23-08:00
log6aab1ea2562806124d4add8d95fb758edf914893
treef972fc5081a7769cbb20b51250e2866d564ba732
parent86261915497fe6b9e0802b2d1e23b5f49c1d2757

std.heap: update Windows HeapAllocator


1 files changed, 22 insertions(+), 1 deletions(-)

lib/std/heap.zig+22-1
......@@ -146,7 +146,7 @@ const CAllocator = struct {
146146 };
147147
148148 fn getHeader(ptr: [*]u8) *[*]u8 {
149 return @ptrCast(ptr - @sizeOf(usize));
149 return @alignCast(@ptrCast(ptr - @sizeOf(usize)));
150150 }
151151
152152 fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 {
......@@ -381,6 +381,7 @@ pub const HeapAllocator = switch (builtin.os.tag) {
381381 .vtable = &.{
382382 .alloc = alloc,
383383 .resize = resize,
384 .remap = remap,
384385 .free = free,
385386 },
386387 };
......@@ -448,6 +449,26 @@ pub const HeapAllocator = switch (builtin.os.tag) {
448449 return true;
449450 }
450451
452 fn remap(
453 ctx: *anyopaque,
454 buf: []u8,
455 alignment: mem.Alignment,
456 new_size: usize,
457 return_address: usize,
458 ) ?[*]u8 {
459 _ = alignment;
460 _ = return_address;
461 const self: *HeapAllocator = @ptrCast(@alignCast(ctx));
462
463 const root_addr = getRecordPtr(buf).*;
464 const align_offset = @intFromPtr(buf.ptr) - root_addr;
465 const amt = align_offset + new_size + @sizeOf(usize);
466 const new_ptr = windows.kernel32.HeapReAlloc(self.heap_handle.?, 0, @ptrFromInt(root_addr), amt) orelse return null;
467 assert(new_ptr == @as(*anyopaque, @ptrFromInt(root_addr)));
468 getRecordPtr(buf.ptr[0..new_size]).* = root_addr;
469 return @ptrCast(new_ptr);
470 }
471
451472 fn free(
452473 ctx: *anyopaque,
453474 buf: []u8,