| ... | @@ -247,7 +247,7 @@ const PageAllocator = struct { | ... | @@ -247,7 +247,7 @@ const PageAllocator = struct { |
| 247 | ) catch return error.OutOfMemory; | 247 | ) catch return error.OutOfMemory; |
| 248 | | 248 | |
| 249 | // If the allocation is sufficiently aligned, use it. | 249 | // If the allocation is sufficiently aligned, use it. |
| 250 | if (@ptrToInt(addr) & (alignment - 1) == 0) { | 250 | if (mem.isAligned(@ptrToInt(addr), alignment)) { |
| 251 | return @ptrCast([*]u8, addr)[0..alignPageAllocLen(aligned_len, n, len_align)]; | 251 | return @ptrCast([*]u8, addr)[0..alignPageAllocLen(aligned_len, n, len_align)]; |
| 252 | } | 252 | } |
| 253 | | 253 | |
| ... | @@ -300,13 +300,13 @@ const PageAllocator = struct { | ... | @@ -300,13 +300,13 @@ const PageAllocator = struct { |
| 300 | ) catch return error.OutOfMemory; | 300 | ) catch return error.OutOfMemory; |
| 301 | assert(mem.isAligned(@ptrToInt(slice.ptr), mem.page_size)); | 301 | assert(mem.isAligned(@ptrToInt(slice.ptr), mem.page_size)); |
| 302 | | 302 | |
| 303 | const aligned_addr = mem.alignForward(@ptrToInt(slice.ptr), alignment); | 303 | const result_ptr = mem.alignPointer(slice.ptr, alignment) orelse |
| 304 | const result_ptr = @alignCast(mem.page_size, @intToPtr([*]u8, aligned_addr)); | 304 | return error.OutOfMemory; |
| 305 | | 305 | |
| 306 | // Unmap the extra bytes that were only requested in order to guarantee | 306 | // Unmap the extra bytes that were only requested in order to guarantee |
| 307 | // that the range of memory we were provided had a proper alignment in | 307 | // that the range of memory we were provided had a proper alignment in |
| 308 | // it somewhere. The extra bytes could be at the beginning, or end, or both. | 308 | // it somewhere. The extra bytes could be at the beginning, or end, or both. |
| 309 | const drop_len = aligned_addr - @ptrToInt(slice.ptr); | 309 | const drop_len = @ptrToInt(result_ptr) - @ptrToInt(slice.ptr); |
| 310 | if (drop_len != 0) { | 310 | if (drop_len != 0) { |
| 311 | os.munmap(slice[0..drop_len]); | 311 | os.munmap(slice[0..drop_len]); |
| 312 | } | 312 | } |
| ... | @@ -372,7 +372,7 @@ const PageAllocator = struct { | ... | @@ -372,7 +372,7 @@ const PageAllocator = struct { |
| 372 | return alignPageAllocLen(new_size_aligned, new_size, len_align); | 372 | return alignPageAllocLen(new_size_aligned, new_size, len_align); |
| 373 | | 373 | |
| 374 | if (new_size_aligned < buf_aligned_len) { | 374 | if (new_size_aligned < buf_aligned_len) { |
| 375 | const ptr = @intToPtr([*]align(mem.page_size) u8, @ptrToInt(buf_unaligned.ptr) + new_size_aligned); | 375 | const ptr = @alignCast(mem.page_size, buf_unaligned.ptr + new_size_aligned); |
| 376 | // TODO: if the next_mmap_addr_hint is within the unmapped range, update it | 376 | // TODO: if the next_mmap_addr_hint is within the unmapped range, update it |
| 377 | os.munmap(ptr[0 .. buf_aligned_len - new_size_aligned]); | 377 | os.munmap(ptr[0 .. buf_aligned_len - new_size_aligned]); |
| 378 | if (new_size_aligned == 0) | 378 | if (new_size_aligned == 0) |
| ... | @@ -692,8 +692,9 @@ pub const FixedBufferAllocator = struct { | ... | @@ -692,8 +692,9 @@ pub const FixedBufferAllocator = struct { |
| 692 | | 692 | |
| 693 | fn alloc(allocator: *Allocator, n: usize, ptr_align: u29, len_align: u29, ra: usize) ![]u8 { | 693 | fn alloc(allocator: *Allocator, n: usize, ptr_align: u29, len_align: u29, ra: usize) ![]u8 { |
| 694 | const self = @fieldParentPtr(FixedBufferAllocator, "allocator", allocator); | 694 | const self = @fieldParentPtr(FixedBufferAllocator, "allocator", allocator); |
| 695 | const aligned_addr = mem.alignForward(@ptrToInt(self.buffer.ptr) + self.end_index, ptr_align); | 695 | const adjust_off = mem.alignPointerOffset(self.buffer.ptr + self.end_index, ptr_align) orelse |
| 696 | const adjusted_index = aligned_addr - @ptrToInt(self.buffer.ptr); | 696 | return error.OutOfMemory; |
| | 697 | const adjusted_index = self.end_index + adjust_off; |
| 697 | const new_end_index = adjusted_index + n; | 698 | const new_end_index = adjusted_index + n; |
| 698 | if (new_end_index > self.buffer.len) { | 699 | if (new_end_index > self.buffer.len) { |
| 699 | return error.OutOfMemory; | 700 | return error.OutOfMemory; |
| ... | @@ -765,9 +766,9 @@ pub const ThreadSafeFixedBufferAllocator = blk: { | ... | @@ -765,9 +766,9 @@ pub const ThreadSafeFixedBufferAllocator = blk: { |
| 765 | const self = @fieldParentPtr(ThreadSafeFixedBufferAllocator, "allocator", allocator); | 766 | const self = @fieldParentPtr(ThreadSafeFixedBufferAllocator, "allocator", allocator); |
| 766 | var end_index = @atomicLoad(usize, &self.end_index, builtin.AtomicOrder.SeqCst); | 767 | var end_index = @atomicLoad(usize, &self.end_index, builtin.AtomicOrder.SeqCst); |
| 767 | while (true) { | 768 | while (true) { |
| 768 | const addr = @ptrToInt(self.buffer.ptr) + end_index; | 769 | const adjust_off = mem.alignPointerOffset(self.buffer.ptr + end_index, ptr_align) orelse |
| 769 | const adjusted_addr = mem.alignForward(addr, ptr_align); | 770 | return error.OutOfMemory; |
| 770 | const adjusted_index = end_index + (adjusted_addr - addr); | 771 | const adjusted_index = end_index + adjust_off; |
| 771 | const new_end_index = adjusted_index + n; | 772 | const new_end_index = adjusted_index + n; |
| 772 | if (new_end_index > self.buffer.len) { | 773 | if (new_end_index > self.buffer.len) { |
| 773 | return error.OutOfMemory; | 774 | return error.OutOfMemory; |