2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const mem = std.mem;
|
|
|
|
|
|
|
|
/// Allocator that fails after N allocations, useful for making sure out of
|
|
|
|
/// memory conditions are handled correctly.
|
2018-10-15 06:51:15 -07:00
|
|
|
pub const FailingAllocator = struct.{
|
2017-12-23 19:08:53 -08:00
|
|
|
allocator: mem.Allocator,
|
|
|
|
index: usize,
|
|
|
|
fail_index: usize,
|
2018-05-31 07:56:59 -07:00
|
|
|
internal_allocator: *mem.Allocator,
|
2017-12-23 19:08:53 -08:00
|
|
|
allocated_bytes: usize,
|
|
|
|
freed_bytes: usize,
|
|
|
|
deallocations: usize,
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
pub fn init(allocator: *mem.Allocator, fail_index: usize) FailingAllocator {
|
2018-10-15 06:51:15 -07:00
|
|
|
return FailingAllocator.{
|
2017-12-23 19:08:53 -08:00
|
|
|
.internal_allocator = allocator,
|
|
|
|
.fail_index = fail_index,
|
|
|
|
.index = 0,
|
|
|
|
.allocated_bytes = 0,
|
|
|
|
.freed_bytes = 0,
|
|
|
|
.deallocations = 0,
|
2018-10-15 06:51:15 -07:00
|
|
|
.allocator = mem.Allocator.{
|
2017-12-23 19:08:53 -08:00
|
|
|
.allocFn = alloc,
|
|
|
|
.reallocFn = realloc,
|
|
|
|
.freeFn = free,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn alloc(allocator: *mem.Allocator, n: usize, alignment: u29) ![]u8 {
|
2017-12-23 19:08:53 -08:00
|
|
|
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
|
|
|
|
if (self.index == self.fail_index) {
|
|
|
|
return error.OutOfMemory;
|
|
|
|
}
|
2018-01-07 13:51:46 -08:00
|
|
|
const result = try self.internal_allocator.allocFn(self.internal_allocator, n, alignment);
|
2017-12-23 19:08:53 -08:00
|
|
|
self.allocated_bytes += result.len;
|
|
|
|
self.index += 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn realloc(allocator: *mem.Allocator, old_mem: []u8, new_size: usize, alignment: u29) ![]u8 {
|
2017-12-23 19:08:53 -08:00
|
|
|
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
|
|
|
|
if (new_size <= old_mem.len) {
|
|
|
|
self.freed_bytes += old_mem.len - new_size;
|
|
|
|
return self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
|
|
|
|
}
|
|
|
|
if (self.index == self.fail_index) {
|
|
|
|
return error.OutOfMemory;
|
|
|
|
}
|
2018-01-07 13:51:46 -08:00
|
|
|
const result = try self.internal_allocator.reallocFn(self.internal_allocator, old_mem, new_size, alignment);
|
2017-12-23 19:08:53 -08:00
|
|
|
self.allocated_bytes += new_size - old_mem.len;
|
|
|
|
self.deallocations += 1;
|
|
|
|
self.index += 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
fn free(allocator: *mem.Allocator, bytes: []u8) void {
|
2017-12-23 19:08:53 -08:00
|
|
|
const self = @fieldParentPtr(FailingAllocator, "allocator", allocator);
|
|
|
|
self.freed_bytes += bytes.len;
|
|
|
|
self.deallocations += 1;
|
|
|
|
return self.internal_allocator.freeFn(self.internal_allocator, bytes);
|
|
|
|
}
|
|
|
|
};
|