const std = @import("std");
const mem = std.mem;

const Scratch = @This();

fixedBuffer: std.heap.FixedBufferAllocator,
buffer: []u8,

pub fn create(a: mem.Allocator, max: usize) !*Scratch {
    const s = try a.create(Scratch);
    errdefer a.destroy(s);

    const buffer = try a.alloc(u8, max);

    s.* = .{
        .fixedBuffer = std.heap.FixedBufferAllocator.init(buffer),
        .buffer = buffer,
    };

    return s;
}

pub fn destroy(scratch: *Scratch, a: mem.Allocator) void {
    a.free(scratch.fixedBuffer.buffer);
    a.destroy(scratch);
}

pub fn allocator(scratch: *Scratch) mem.Allocator {
    return scratch.fixedBuffer.allocator();
}
