| ... | @@ -170,16 +170,20 @@ pub const DirectAllocator = struct { | ... | @@ -170,16 +170,20 @@ pub const DirectAllocator = struct { |
| 170 | ) orelse return error.OutOfMemory; | 170 | ) orelse return error.OutOfMemory; |
| 171 | const offset = old_adjusted_addr - root_addr; | 171 | const offset = old_adjusted_addr - root_addr; |
| 172 | const new_root_addr = @ptrToInt(new_ptr); | 172 | const new_root_addr = @ptrToInt(new_ptr); |
| 173 | const adjusted_addr = new_root_addr + offset; | 173 | var new_adjusted_addr = new_root_addr + offset; |
| 174 | const new_adjusted_addr = mem.alignForward(new_root_addr, new_align); | 174 | const offset_is_valid = new_adjusted_addr + new_size + @sizeOf(usize) <= new_root_addr + amt; |
| 175 | // If HeapReAlloc didn't happen to move the memory to the new alignment | 175 | const offset_is_aligned = new_adjusted_addr % new_align == 0; |
| 176 | // then we need to copy it | 176 | if (!offset_is_valid or !offset_is_aligned) { |
| 177 | if (new_adjusted_addr != adjusted_addr) { | 177 | // If HeapReAlloc didn't happen to move the memory to the new alignment, |
| | 178 | // or the memory starting at the old offset would be outside of the new allocation, |
| | 179 | // then we need to copy the memory to a valid aligned address and use that |
| | 180 | const new_aligned_addr = mem.alignForward(new_root_addr, new_align); |
| 178 | @memcpy( | 181 | @memcpy( |
| | 182 | @intToPtr([*]u8, new_aligned_addr), |
| 179 | @intToPtr([*]u8, new_adjusted_addr), | 183 | @intToPtr([*]u8, new_adjusted_addr), |
| 180 | @intToPtr([*]u8, adjusted_addr), | | |
| 181 | std.math.min(old_mem.len, new_size), | 184 | std.math.min(old_mem.len, new_size), |
| 182 | ); | 185 | ); |
| | 186 | new_adjusted_addr = new_aligned_addr; |
| 183 | } | 187 | } |
| 184 | const new_record_addr = new_adjusted_addr + new_size; | 188 | const new_record_addr = new_adjusted_addr + new_size; |
| 185 | @intToPtr(*align(1) usize, new_record_addr).* = new_root_addr; | 189 | @intToPtr(*align(1) usize, new_record_addr).* = new_root_addr; |