31 lines
833 B
Zig
31 lines
833 B
Zig
const physMemory = @import("mem/phys.zig");
|
|
const log = @import("debug.zig").log;
|
|
const mem = @import("mem.zig");
|
|
|
|
pub const Arena = struct {
|
|
physBase: mem.PhysicalAddress,
|
|
capacity: usize,
|
|
len: usize,
|
|
|
|
pub fn setup(cap: usize) ?Arena {
|
|
const base = physMemory.allocateChunk(cap / 0x1000) orelse return null;
|
|
return .{
|
|
.physBase = .{ .raw = base },
|
|
.capacity = cap,
|
|
.len = 0,
|
|
};
|
|
}
|
|
|
|
pub fn create(self: *@This(), comptime T: type) *T {
|
|
if (self.len + @sizeOf(T) > self.capacity) {
|
|
log.panic("Out of memory. Cannot allocate {} bytes", .{ @sizeOf(T) });
|
|
}
|
|
|
|
const v = self.physBase.add(self.len).virtualize();
|
|
const ptr = @as(*T, @ptrFromInt(v));
|
|
self.len += @sizeOf(T);
|
|
|
|
return ptr;
|
|
}
|
|
};
|