My goal was to make a buffer that I can flush/clear when it is getting full, but I guess I don’t fully understand how ArrayList
and/or std.heap.FixedBufferAllocator
work.
Here’s what I tried:
const std = @import("std");
const ArrayList = std.ArrayList;
pub fn main() !void {
var buf: [64]u8 = undefined;
@memset(buf[0..], 0);
var FBA = std.heap.FixedBufferAllocator.init(&buf);
defer FBA.reset();
var list = ArrayList(u8).init(FBA.allocator());
defer list.deinit();
// FBA.reset();
try list.append('H');
try list.append('e');
try list.append('l');
try list.append('l');
try list.append('o');
try list.append(' ');
try list.appendSlice("é");
try list.appendSlice("€");
try list.appendSlice(" World!");
std.debug.print("{s}\n", .{list.items});
std.debug.print("{}\n", .{list.capacity});
std.debug.print("{s}\n", .{buf});
std.debug.print("{any}\n", .{FBA});
//FBA.reset();
@memset(buf[0..], 0);
//FBA = FBA{.end_index=0, .buffer=&buf};
try list.append('H');
try list.append('e');
try list.append('l');
try list.append('l');
try list.append('o');
try list.append(' ');
try list.appendSlice("é");
try list.appendSlice("€");
try list.appendSlice(" World!");
std.debug.print("{s}\n", .{list.items});
std.debug.print("{}\n", .{list.capacity});
std.debug.print("{s}\n", .{buf});
std.debug.print("{any}\n", .{FBA});
}
I commented out some of what I tried and what absolutely will crash the build.
Here’s the output of running the code:
Hello é€ World!
20
Hello é€ World!
heap.FixedBufferAllocator{ .end_index = 20, .buffer = { 72, 101, 108, 108, 111, 32, 195, 169, 226, 130, 172, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
Hello é€ World!
38
Hello é€ World!
heap.FixedBufferAllocator{ .end_index = 38, .buffer = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72, 101, 108, 108, 111, 32, 195, 169, 226, 130, 172, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
Is there a way to reset the .end_index
? Because if I zero the buffer string and then append again, it obviously starts from where it stopped before and not from beginning.
Any thoughts?
What should I do instead?