Fixed arena

This commit is contained in:
Talha Qamar 2025-02-01 20:52:02 +05:00
parent fc6e1c095e
commit 6572ebeca2
2 changed files with 6 additions and 4 deletions
src/lily
alloc
collections

View file

@ -33,7 +33,9 @@ ArenaAllocator := struct {
self.size = self.size * 2
}
allocation := self.ptr + self.allocated
self.allocations.push(.(allocation, count * @sizeof(T)))
self.allocated = self.allocated + count * @sizeof(T)
log.debug("allocated")
return @bitcast(allocation)

View file

@ -32,10 +32,10 @@ Vec := fn($T: type, $Allocator: type): type return struct {
push := fn(self: ^Self, value: T): void {
if self.slice.len == self.cap {
if self.cap == 0 {
self.cap = 1
// ! (libc) (compiler) bug: null check broken, so unwrapping (unsafe!)
new_alloc := @unwrap(self.allocator.alloc(T, self.cap))
self.slice.ptr = new_alloc
self.cap = 1
} else {
self.cap *= 2
// ! (libc) (compiler) bug: null check broken, so unwrapping (unsafe!)
@ -43,8 +43,8 @@ Vec := fn($T: type, $Allocator: type): type return struct {
self.slice.ptr = new_alloc
}
}
self.slice[self.slice.len] = value
self.slice.len += 1
// self.slice[self.slice.len] = value
// self.slice.len += 1
}
get := fn(self: ^Self, n: uint): ?T {
if n >= self.slice.len return null
@ -92,4 +92,4 @@ Vec := fn($T: type, $Allocator: type): type return struct {
$sort := fn(self: ^Self): void self.sort_with(compare)
$len := fn(self: ^Self): uint return self.slice.len
$capacity := fn(self: ^Self): uint return self.cap
}
}