| ... | @@ -146,7 +146,7 @@ const CAllocator = struct { | ... | @@ -146,7 +146,7 @@ const CAllocator = struct { |
| 146 | }; | 146 | }; |
| 147 | | 147 | |
| 148 | fn getHeader(ptr: [*]u8) *[*]u8 { | 148 | fn getHeader(ptr: [*]u8) *[*]u8 { |
| 149 | return @ptrCast(ptr - @sizeOf(usize)); | 149 | return @alignCast(@ptrCast(ptr - @sizeOf(usize))); |
| 150 | } | 150 | } |
| 151 | | 151 | |
| 152 | fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 { | 152 | fn alignedAlloc(len: usize, alignment: mem.Alignment) ?[*]u8 { |
| ... | @@ -381,6 +381,7 @@ pub const HeapAllocator = switch (builtin.os.tag) { | ... | @@ -381,6 +381,7 @@ pub const HeapAllocator = switch (builtin.os.tag) { |
| 381 | .vtable = &.{ | 381 | .vtable = &.{ |
| 382 | .alloc = alloc, | 382 | .alloc = alloc, |
| 383 | .resize = resize, | 383 | .resize = resize, |
| | 384 | .remap = remap, |
| 384 | .free = free, | 385 | .free = free, |
| 385 | }, | 386 | }, |
| 386 | }; | 387 | }; |
| ... | @@ -448,6 +449,26 @@ pub const HeapAllocator = switch (builtin.os.tag) { | ... | @@ -448,6 +449,26 @@ pub const HeapAllocator = switch (builtin.os.tag) { |
| 448 | return true; | 449 | return true; |
| 449 | } | 450 | } |
| 450 | | 451 | |
| | 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 | |
| 451 | fn free( | 472 | fn free( |
| 452 | ctx: *anyopaque, | 473 | ctx: *anyopaque, |
| 453 | buf: []u8, | 474 | buf: []u8, |