From b3fe6b4de85dfb5a52b6c94c991dc3c1eda058ca Mon Sep 17 00:00:00 2001 From: Talha Qamar <qamartalha@proton.me> Date: Fri, 31 Jan 2025 22:28:13 +0500 Subject: [PATCH] More work on this --- src/lily/alloc/arena.hb | 16 ++++++---------- src/lily/alloc/lib.hb | 3 ++- src/test/lily/lily.alloc.arena.hb | 12 ++++++++++++ 3 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 src/test/lily/lily.alloc.arena.hb diff --git a/src/lily/alloc/arena.hb b/src/lily/alloc/arena.hb index 908a059..fa32fc0 100644 --- a/src/lily/alloc/arena.hb +++ b/src/lily/alloc/arena.hb @@ -10,15 +10,11 @@ ArenaAllocator := struct { size: uint, allocated: uint, - $new := fn(size: uint): Self { - allocated := idk - if size == 0 { - allocated = Target.page_size() - } else { - allocated = size - } - ptr := Target.alloc_zeroed(size) - return .(ptr, size, 0) + $new := fn(): Self { + allocated := 0 + allocated = Target.page_size() + ptr := Target.alloc_zeroed(allocated) + return .(ptr, allocated, 0) } deinit := fn(self: ^Self): void { match Target.current() { @@ -37,7 +33,7 @@ ArenaAllocator := struct { log.debug("allocated") return @bitcast(allocation) } - alloc_zeroed := fn(self: ^Self, $T: type, count: uint): ?^T { + $alloc_zeroed := fn(self: ^Self, $T: type, count: uint): ?^T { return self.alloc(T, count) } realloc := fn(self: ^Self, $T: type, ptr: ^T, count: uint): ?^T { diff --git a/src/lily/alloc/lib.hb b/src/lily/alloc/lib.hb index 69e7ba4..03fb0a1 100644 --- a/src/lily/alloc/lib.hb +++ b/src/lily/alloc/lib.hb @@ -1,2 +1,3 @@ .{RawAllocator} := @use("raw.hb"); -.{SimpleAllocator} := @use("simple.hb") \ No newline at end of file +.{SimpleAllocator} := @use("simple.hb"); +.{ArenaAllocator} := @use("arena.hb"); diff --git a/src/test/lily/lily.alloc.arena.hb b/src/test/lily/lily.alloc.arena.hb new file mode 100644 index 0000000..fd66485 --- /dev/null +++ b/src/test/lily/lily.alloc.arena.hb @@ -0,0 +1,12 @@ +/* + * exit: 0 + */ +lily := @use("../../lily/lib.hb") + +main := fn(argc: int, argv: [][]u8): u8 { + alloc := lily.alloc.ArenaAllocator.new() + ptr_one := alloc.alloc(u8, 6) + ptr_two := alloc.alloc(u8, 179) + alloc.deinit() + return 0 +}