| author | |
| committer | |
| log | 3f3003097cbf5a6ad9e0dfc29b2cafbe2e35dded |
| tree | 2dfd831099a3f3b99039474d164f008243b3ae55 |
| parent | 9b54c9dee8a571f1c821b3eb4ee3d2c713cf63fa |
Instead of making the memory alignment functions more complicated, I
added more API documentation for their existing semantics.
closes #12118
closes #121353 files changed, 13 insertions(+), 0 deletions(-)
lib/std/heap.zig+3| ... | ... | @@ -260,6 +260,9 @@ const PageAllocator = struct { |
| 260 | 260 | fn alloc(_: *anyopaque, n: usize, alignment: u29, len_align: u29, ra: usize) error{OutOfMemory}![]u8 { |
| 261 | 261 | _ = ra; |
| 262 | 262 | assert(n > 0); |
| 263 | if (n > maxInt(usize) - (mem.page_size - 1)) { | |
| 264 | return error.OutOfMemory; | |
| 265 | } | |
| 263 | 266 | const aligned_len = mem.alignForward(n, mem.page_size); |
| 264 | 267 | |
| 265 | 268 | if (builtin.os.tag == .windows) { |
lib/std/heap/general_purpose_allocator.zig+8| ... | ... | @@ -971,6 +971,14 @@ test "large allocations" { |
| 971 | 971 | allocator.free(ptr2); |
| 972 | 972 | } |
| 973 | 973 | |
| 974 | test "very large allocation" { | |
| 975 | var gpa = GeneralPurposeAllocator(test_config){}; | |
| 976 | defer std.testing.expect(!gpa.deinit()) catch @panic("leak"); | |
| 977 | const allocator = gpa.allocator(); | |
| 978 | ||
| 979 | try std.testing.expectError(error.OutOfMemory, allocator.alloc(u8, math.maxInt(usize))); | |
| 980 | } | |
| 981 | ||
| 974 | 982 | test "realloc" { |
| 975 | 983 | var gpa = GeneralPurposeAllocator(test_config){}; |
| 976 | 984 | defer std.testing.expect(!gpa.deinit()) catch @panic("leak"); |
lib/std/mem.zig+2| ... | ... | @@ -3551,12 +3551,14 @@ test "sliceAsBytes preserves pointer attributes" { |
| 3551 | 3551 | |
| 3552 | 3552 | /// Round an address up to the next (or current) aligned address. |
| 3553 | 3553 | /// The alignment must be a power of 2 and greater than 0. |
| 3554 | /// Asserts that rounding up the address does not cause integer overflow. | |
| 3554 | 3555 | pub fn alignForward(addr: usize, alignment: usize) usize { |
| 3555 | 3556 | return alignForwardGeneric(usize, addr, alignment); |
| 3556 | 3557 | } |
| 3557 | 3558 | |
| 3558 | 3559 | /// Round an address up to the next (or current) aligned address. |
| 3559 | 3560 | /// The alignment must be a power of 2 and greater than 0. |
| 3561 | /// Asserts that rounding up the address does not cause integer overflow. | |
| 3560 | 3562 | pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T { |
| 3561 | 3563 | return alignBackwardGeneric(T, addr + (alignment - 1), alignment); |
| 3562 | 3564 | } |