mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Add allocator that always fails
This commit is contained in:
parent
76c62e509b
commit
ae518dcb41
1 changed files with 72 additions and 0 deletions
|
|
@ -81,6 +81,19 @@ pub const VTable = struct {
|
||||||
free: *const fn (*anyopaque, memory: []u8, alignment: Alignment, ret_addr: usize) void,
|
free: *const fn (*anyopaque, memory: []u8, alignment: Alignment, ret_addr: usize) void,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn noAlloc(
|
||||||
|
self: *anyopaque,
|
||||||
|
len: usize,
|
||||||
|
alignment: Alignment,
|
||||||
|
ret_addr: usize,
|
||||||
|
) ?[*]u8 {
|
||||||
|
_ = self;
|
||||||
|
_ = len;
|
||||||
|
_ = alignment;
|
||||||
|
_ = ret_addr;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn noResize(
|
pub fn noResize(
|
||||||
self: *anyopaque,
|
self: *anyopaque,
|
||||||
memory: []u8,
|
memory: []u8,
|
||||||
|
|
@ -445,3 +458,62 @@ pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) Error![:0]T {
|
||||||
new_buf[m.len] = 0;
|
new_buf[m.len] = 0;
|
||||||
return new_buf[0..m.len :0];
|
return new_buf[0..m.len :0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An allocator that always fails to allocate.
|
||||||
|
pub const failing: Allocator = .{
|
||||||
|
.ptr = undefined,
|
||||||
|
.vtable = &.{
|
||||||
|
.alloc = noAlloc,
|
||||||
|
.resize = unreachableResize,
|
||||||
|
.remap = unreachableRemap,
|
||||||
|
.free = unreachableFree,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
fn unreachableResize(
|
||||||
|
self: *anyopaque,
|
||||||
|
memory: []u8,
|
||||||
|
alignment: Alignment,
|
||||||
|
new_len: usize,
|
||||||
|
ret_addr: usize,
|
||||||
|
) bool {
|
||||||
|
_ = self;
|
||||||
|
_ = memory;
|
||||||
|
_ = alignment;
|
||||||
|
_ = new_len;
|
||||||
|
_ = ret_addr;
|
||||||
|
unreachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unreachableRemap(
|
||||||
|
self: *anyopaque,
|
||||||
|
memory: []u8,
|
||||||
|
alignment: Alignment,
|
||||||
|
new_len: usize,
|
||||||
|
ret_addr: usize,
|
||||||
|
) ?[*]u8 {
|
||||||
|
_ = self;
|
||||||
|
_ = memory;
|
||||||
|
_ = alignment;
|
||||||
|
_ = new_len;
|
||||||
|
_ = ret_addr;
|
||||||
|
unreachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unreachableFree(
|
||||||
|
self: *anyopaque,
|
||||||
|
memory: []u8,
|
||||||
|
alignment: Alignment,
|
||||||
|
ret_addr: usize,
|
||||||
|
) void {
|
||||||
|
_ = self;
|
||||||
|
_ = memory;
|
||||||
|
_ = alignment;
|
||||||
|
_ = ret_addr;
|
||||||
|
unreachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
test failing {
|
||||||
|
const f: Allocator = .failing;
|
||||||
|
try std.testing.expectError(error.OutOfMemory, f.alloc(u8, 123));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue