2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("../std.zig");
|
2017-12-24 03:08:53 +00:00
|
|
|
const mem = std.mem;
|
|
|
|
|
|
|
|
/// Allocator that fails after N allocations, useful for making sure out of
|
|
|
|
/// memory conditions are handled correctly.
|
2019-10-28 07:57:23 +00:00
|
|
|
///
|
|
|
|
/// To use this, first initialize it and get an allocator with
|
2019-11-25 18:51:09 +00:00
|
|
|
///
|
2019-10-28 07:57:23 +00:00
|
|
|
/// `const failing_allocator = &FailingAllocator.init(<allocator>,
|
|
|
|
/// <fail_index>).allocator;`
|
2019-11-25 18:51:09 +00:00
|
|
|
///
|
2019-10-28 07:57:23 +00:00
|
|
|
/// Then use `failing_allocator` anywhere you would have used a
|
|
|
|
/// different allocator.
|
2018-11-13 13:08:37 +00:00
|
|
|
pub const FailingAllocator = struct {
|
2017-12-24 03:08:53 +00:00
|
|
|
allocator: mem.Allocator,
|
|
|
|
index: usize,
|
|
|
|
fail_index: usize,
|
2018-05-31 15:56:59 +01:00
|
|
|
internal_allocator: *mem.Allocator,
|
2017-12-24 03:08:53 +00:00
|
|
|
allocated_bytes: usize,
|
|
|
|
freed_bytes: usize,
|
2019-05-05 14:17:23 +01:00
|
|
|
allocations: usize,
|
2017-12-24 03:08:53 +00:00
|
|
|
deallocations: usize,
|
|
|
|
|
2019-10-28 07:57:23 +00:00
|
|
|
/// `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));
|
2018-05-31 15:56:59 +01:00
|
|
|
pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator {
|
2018-11-13 13:08:37 +00:00
|
|
|
return FailingAllocator{
|
2017-12-24 03:08:53 +00:00
|
|
|
.internal_allocator = allocator,
|
|
|
|
.fail_index = fail_index,
|
|
|
|
.index = 0,
|
|
|
|
.allocated_bytes = 0,
|
|
|
|
.freed_bytes = 0,
|
2019-05-05 14:17:23 +01:00
|
|
|
.allocations = 0,
|
2017-12-24 03:08:53 +00:00
|
|
|
.deallocations = 0,
|
2018-11-13 13:08:37 +00:00
|
|
|
.allocator = mem.Allocator{
|
2020-04-17 21:15:36 +01:00
|
|
|
.allocFn = alloc,
|
|
|
|
.resizeFn = resize,
|
2017-12-24 03:08:53 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-08 08:34:13 +01:00
|
|
|
fn alloc(
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
len: usize,
|
|
|
|
ptr_align: u29,
|
|
|
|
len_align: u29,
|
|
|
|
return_address: usize,
|
|
|
|
) error{OutOfMemory}![]u8 {
|
2017-12-24 03:08:53 +00:00
|
|
|
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
|
|
|
|
if (self.index == self.fail_index) {
|
|
|
|
return error.OutOfMemory;
|
|
|
|
}
|
2020-08-08 08:34:13 +01:00
|
|
|
const result = try self.internal_allocator.allocFn(self.internal_allocator, len, ptr_align, len_align, return_address);
|
2020-04-17 21:15:36 +01:00
|
|
|
self.allocated_bytes += result.len;
|
|
|
|
self.allocations += 1;
|
2017-12-24 03:08:53 +00:00
|
|
|
self.index += 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-08 06:35:15 +01:00
|
|
|
fn resize(
|
|
|
|
allocator: *std.mem.Allocator,
|
|
|
|
old_mem: []u8,
|
|
|
|
old_align: u29,
|
|
|
|
new_len: usize,
|
|
|
|
len_align: u29,
|
2020-08-08 08:34:13 +01:00
|
|
|
ra: usize,
|
2020-08-08 06:35:15 +01:00
|
|
|
) error{OutOfMemory}!usize {
|
2017-12-24 03:08:53 +00:00
|
|
|
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
|
2020-08-08 08:34:13 +01:00
|
|
|
const r = self.internal_allocator.resizeFn(self.internal_allocator, old_mem, old_align, new_len, len_align, ra) catch |e| {
|
2020-04-17 21:15:36 +01:00
|
|
|
std.debug.assert(new_len > old_mem.len);
|
|
|
|
return e;
|
|
|
|
};
|
|
|
|
if (new_len == 0) {
|
2019-05-05 14:17:23 +01:00
|
|
|
self.deallocations += 1;
|
2020-04-17 21:15:36 +01:00
|
|
|
self.freed_bytes += old_mem.len;
|
|
|
|
} else if (r < old_mem.len) {
|
|
|
|
self.freed_bytes += old_mem.len - r;
|
|
|
|
} else {
|
|
|
|
self.allocated_bytes += r - old_mem.len;
|
|
|
|
}
|
2019-05-05 14:17:23 +01:00
|
|
|
return r;
|
2017-12-24 03:08:53 +00:00
|
|
|
}
|
|
|
|
};
|