testAllocatorLargeAlignment
Function parameters
Parameters
- base_allocator:mem.Allocator
Type definitions in this namespace
Types
- GeneralPurposeAllocatorConfig
- Deprecated; to be removed after 0.14.0 is tagged.
Functions in this namespace
Functions
- GeneralPurposeAllocator
- Deprecated; to be removed after 0.14.0 is tagged.
- pageSize
- If the page size is comptime-known, return value is comptime.
- defaultQueryPageSize
- The default implementation of `std.options.queryPageSize`.
- stackFallback
- Returns a `StackFallbackAllocator` allocating using either a
- StackFallbackAllocator
- An allocator that attempts to allocate using a
- testAllocator
- This one should not try alignments that exceed what C malloc can handle.
TODO Utilize this on Windows.
Values
- next_mmap_addr_hint
- TODO Utilize this on Windows.
- page_size_min
- comptime-known minimum page size of the target.
- page_size_max
- comptime-known maximum page size of the target.
- c_allocator
- Supports the full Allocator interface, including alignment, and exploiting
- raw_c_allocator
- Asserts allocations are within `@alignOf(std.c.max_align_t)` and directly
- page_allocator
- On operating systems that support memory mapping, this allocator makes a
- smp_allocator
- = .{ .ptr = undefined, .vtable = &SmpAllocator.vtable, }
- wasm_allocator
- This allocator is fast, small, and specific to WebAssembly.
Source
Implementation
pub fn testAllocatorLargeAlignment(base_allocator: mem.Allocator) !void {
var validationAllocator = mem.validationWrap(base_allocator);
const allocator = validationAllocator.allocator();
const large_align: usize = page_size_min / 2;
var align_mask: usize = undefined;
align_mask = @shlWithOverflow(~@as(usize, 0), @as(Allocator.Log2Align, @ctz(large_align)))[0];
var slice = try allocator.alignedAlloc(u8, .fromByteUnits(large_align), 500);
try testing.expect(@intFromPtr(slice.ptr) & align_mask == @intFromPtr(slice.ptr));
if (allocator.resize(slice, 100)) {
slice = slice[0..100];
}
slice = try allocator.realloc(slice, 5000);
try testing.expect(@intFromPtr(slice.ptr) & align_mask == @intFromPtr(slice.ptr));
if (allocator.resize(slice, 10)) {
slice = slice[0..10];
}
slice = try allocator.realloc(slice, 20000);
try testing.expect(@intFromPtr(slice.ptr) & align_mask == @intFromPtr(slice.ptr));
allocator.free(slice);
}