Skip to content

Instantly share code, notes, and snippets.

@mikdusan
Created April 19, 2020 13:36
Show Gist options
  • Save mikdusan/c86d1195e3de05b35e31d18fecf3edff to your computer and use it in GitHub Desktop.
Save mikdusan/c86d1195e3de05b35e31d18fecf3edff to your computer and use it in GitHub Desktop.
const std = @import("std");
const mem = std.mem;
const io = std.io;
const Bar = struct {
const Self = @This();
arena: std.heap.ArenaAllocator,
raw_frame_reader: RawFrameReader,
pub fn init(self: *Bar, allocator: *std.mem.Allocator) void {
self.arena = std.heap.ArenaAllocator.init(allocator);
self.raw_frame_reader = RawFrameReader.init(&self.arena.allocator);
}
pub fn doBar(self: *Self) !void {
const buf = try self.raw_frame_reader.allocator.alloc(u8, 4096);
std.debug.warn("allocator: {x}\n", .{self.raw_frame_reader.allocator.reallocFn});
// var f: [2]u8 = undefined;
std.debug.warn("allocator after f: {x}\n", .{self.raw_frame_reader.allocator.reallocFn});
const buf2 = try self.raw_frame_reader.allocator.alloc(u8, 4096);
std.debug.warn("allocator: {x}\n", .{self.raw_frame_reader.allocator.reallocFn});
const buf3 = try self.raw_frame_reader.allocator.alloc(u8, 4096);
}
};
const RawFrameReader = struct {
allocator: *mem.Allocator,
pub fn init(allocator: *mem.Allocator) RawFrameReader {
return RawFrameReader{
.allocator = allocator,
};
}
};
pub fn main() anyerror!void {
var bar: Bar = undefined;
bar.init(std.testing.allocator);
_ = try bar.doBar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment