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 {...@@ -156,9 +156,8 @@ pub const DirectAllocator = struct {
156 const root_addr = @intToPtr(*align(1) usize, old_record_addr).*;156 const root_addr = @intToPtr(*align(1) usize, old_record_addr).*;
157 const old_ptr = @intToPtr(*c_void, root_addr);157 const old_ptr = @intToPtr(*c_void, root_addr);
158 158
159 if(new_size == 0)159 if(new_size == 0) {
160 {160 if (os.windows.HeapFree(self.heap_handle.?, 0, old_ptr) == 0) unreachable;
161 if(os.windows.HeapFree(self.heap_handle.?, 0, old_ptr) == 0) unreachable;
162 return old_mem[0..0];161 return old_mem[0..0];
163 }162 }
164 163
...@@ -171,8 +170,17 @@ pub const DirectAllocator = struct {...@@ -171,8 +170,17 @@ pub const DirectAllocator = struct {
171 ) orelse return error.OutOfMemory;170 ) orelse return error.OutOfMemory;
172 const offset = old_adjusted_addr - root_addr;171 const offset = old_adjusted_addr - root_addr;
173 const new_root_addr = @ptrToInt(new_ptr);172 const new_root_addr = @ptrToInt(new_ptr);
174 const new_adjusted_addr = new_root_addr + offset;173 const adjusted_addr = new_root_addr + offset;
175 assert(new_adjusted_addr % new_align == 0);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 }
176 const new_record_addr = new_adjusted_addr + new_size;184 const new_record_addr = new_adjusted_addr + new_size;
177 @intToPtr(*align(1) usize, new_record_addr).* = new_root_addr;185 @intToPtr(*align(1) usize, new_record_addr).* = new_root_addr;
178 return @intToPtr([*]u8, new_adjusted_addr)[0..new_size];186 return @intToPtr([*]u8, new_adjusted_addr)[0..new_size];