Allocator that fails after N allocations, useful for making sure out of memory conditions are handled correctly.

To use this, first initialize it and get an allocator with

const failing_allocator = &FailingAllocator.init(<allocator>, <fail_index>).allocator;

Then use failing_allocator anywhere you would have used a different allocator.

Fields

index: usize,
fail_index: usize,
internal_allocator: mem.Allocator,
allocated_bytes: usize,
freed_bytes: usize,
allocations: usize,
deallocations: usize,
stack_addresses: [num_stack_frames]usize,
has_induced_failure: bool,

Functions

fn allocator(self: *FailingAllocator) mem.Allocator

No documentation provided.

fn getStackTrace(self: *FailingAllocator) std.builtin.StackTrace

Only valid once has_induced_failure == true

fn init(internal_allocator: mem.Allocator, fail_index: usize) FailingAllocator

fail_index is the number of successful allocations you can expect from this a…

fail_index is the number of successful allocations you can expect from this allocator. The next allocation will fail. For example, if this is called with fail_index equal to 2, the following test will pass:

var a = try failing_alloc.create(i32); var b = try failing_alloc.create(i32); testing.expectError(error.OutOfMemory, failing_alloc.create(i32));