authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2019-04-22 02:20:01-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2019-04-22 02:20:01-07:00
logb3598163df821a3cd11470ebc4fc45e032a0183b
tree9e8358f7f0bbda5445cfb9d541ffdfc563f1056a
parent66020856b21b493d32355d58a1d4c58265abd198

std.heap.DirectAllocator: Fix aligned reallocs on Windows


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

std/heap.zig+13-5
......@@ -156,9 +156,8 @@ pub const DirectAllocator = struct {
156156 const root_addr = @intToPtr(*align(1) usize, old_record_addr).*;
157157 const old_ptr = @intToPtr(*c_void, root_addr);
158158
159 if(new_size == 0)
160 {
161 if(os.windows.HeapFree(self.heap_handle.?, 0, old_ptr) == 0) unreachable;
159 if(new_size == 0) {
160 if (os.windows.HeapFree(self.heap_handle.?, 0, old_ptr) == 0) unreachable;
162161 return old_mem[0..0];
163162 }
164163
......@@ -171,8 +170,17 @@ pub const DirectAllocator = struct {
171170 ) orelse return error.OutOfMemory;
172171 const offset = old_adjusted_addr - root_addr;
173172 const new_root_addr = @ptrToInt(new_ptr);
174 const new_adjusted_addr = new_root_addr + offset;
175 assert(new_adjusted_addr % new_align == 0);
173 const adjusted_addr = new_root_addr + offset;
174 const new_adjusted_addr = mem.alignForward(new_root_addr, new_align);
175 // If HeapReAlloc didn't happen to move the memory to the new alignment
176 // then we need to copy it
177 if (new_adjusted_addr != adjusted_addr) {
178 @memcpy(
179 @intToPtr([*]u8, new_adjusted_addr),
180 @intToPtr([*]u8, adjusted_addr),
181 std.math.min(old_mem.len, new_size),
182 );
183 }
176184 const new_record_addr = new_adjusted_addr + new_size;
177185 @intToPtr(*align(1) usize, new_record_addr).* = new_root_addr;
178186 return @intToPtr([*]u8, new_adjusted_addr)[0..new_size];