From 1ab9bba9da02884206a30bd710cc546be913702d Mon Sep 17 00:00:00 2001 From: kayomn Date: Sun, 7 May 2023 14:51:22 +0100 Subject: [PATCH] Implement null allocator --- source/coral/io.zig | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/source/coral/io.zig b/source/coral/io.zig index 58fb24b..d52284f 100755 --- a/source/coral/io.zig +++ b/source/coral/io.zig @@ -164,13 +164,22 @@ pub fn equals(this: []const u8, that: []const u8) bool { var null_context = @as(usize, 0); +pub const null_allocator = Allocator.bind(&null_context, struct { + fn reallocate(context: *usize, options: AllocationOptions) ?[]u8 { + debug.assert(context.* == 0); + debug.assert(options.allocation == null); + + return null; + } +}); + pub const null_writer = Writer.bind(&null_context, struct { - pub fn write(context: *usize, buffer: []const u8) usize { + fn write(context: *usize, buffer: []const u8) usize { debug.assert(context.* == 0); return buffer.len; } -}); +}.write); pub fn overlaps(pointer: [*]u8, memory_range: []u8) bool { return (pointer >= memory_range.ptr) and (pointer < (memory_range.ptr + memory_range.len));