FULL ARENA
This commit is contained in:
parent
089be867b2
commit
fa7a0506f8
src
|
@ -21,22 +21,16 @@ ArenaAllocator := struct {
|
|||
return .(ptr, size, 0, vec, raw)
|
||||
}
|
||||
deinit := fn(self: ^Self): void {
|
||||
match Target.current() {
|
||||
.LibC => Target.dealloc(self.ptr),
|
||||
.AbleOS => Target.dealloc(self.ptr, self.size),
|
||||
}
|
||||
self.vec.deinit()
|
||||
Target.dealloc(self.ptr, self.size)
|
||||
self.allocations.deinit()
|
||||
self.raw.deinit()
|
||||
log.debug("deinit: allocator")
|
||||
}
|
||||
alloc := fn(self: ^Self, $T: type, count: uint): ?^T {
|
||||
if self.allocated + count * @sizeof(T) > self.size {
|
||||
ptr := Target.realloc(self.ptr, self.size, self.size * 2)
|
||||
if ptr == null {
|
||||
log.error("Failed to grow arena");
|
||||
die
|
||||
}
|
||||
self.ptr = @unwrap(ptr)
|
||||
// ! (libc) (compiler) bug: null check broken. unwrapping.
|
||||
self.ptr = @unwrap(Target.realloc(self.ptr, self.size, self.size * 2))
|
||||
self.size = self.size * 2
|
||||
}
|
||||
allocation := self.ptr + self.allocated
|
||||
self.allocations.push(.(allocation, count * @sizeof(T)))
|
||||
|
@ -49,17 +43,16 @@ ArenaAllocator := struct {
|
|||
}
|
||||
realloc := fn(self: ^Self, $T: type, ptr: ^T, count: uint): ?^T {
|
||||
old_size := self._find_size(ptr)
|
||||
if old_size == null {
|
||||
return null
|
||||
}
|
||||
if old_size == null return null
|
||||
|
||||
if old_size > @sizeof(T) * count {
|
||||
if Config.debug_assertions() {
|
||||
log.warn("arena allocator: new_size is smaller than old_size")
|
||||
}
|
||||
return ptr
|
||||
}
|
||||
new_ptr := self.alloc(T, count)
|
||||
Target.memcpy(new_ptr, ptr, old_size)
|
||||
new_ptr := @unwrap(self.alloc(T, count))
|
||||
_ = Target.memcpy_w(new_ptr, ptr, old_size)
|
||||
return new_ptr
|
||||
}
|
||||
dealloc := fn(self: ^Self, $T: type, ptr: ^T): void {
|
||||
|
|
|
@ -30,12 +30,11 @@ RawAllocator := struct {
|
|||
realloc := fn(self: ^Self, $T: type, ptr: ^T, count: uint): ?^T {
|
||||
if self.size == 0 return null
|
||||
log.debug("reallocated raw")
|
||||
new_ptr := Target.realloc(self.ptr, self.size, count * @sizeof(T))
|
||||
if new_ptr != null {
|
||||
self.ptr = new_ptr
|
||||
self.size = count * @sizeof(T)
|
||||
}
|
||||
return @bitcast(new_ptr)
|
||||
// ! (libc) (compiler) bug: null check broken. unwrapping.
|
||||
new_ptr := @unwrap(Target.realloc(self.ptr, self.size, count * @sizeof(T)))
|
||||
self.ptr = new_ptr
|
||||
self.size = count * @sizeof(T)
|
||||
return @bitcast(new_ptr)
|
||||
}
|
||||
// ! INLINING THIS FUNCTION CAUSES MISCOMPILATION!! DO NOT INLINE IT!! :) :) :)
|
||||
dealloc := fn(self: ^Self, $T: type, ptr: ^T): void {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
alloc := fn(size: uint): ?^u8 @import("malloc")
|
||||
alloc_zeroed := fn(size: uint): ?^u8 @import("calloc")
|
||||
realloc_c := fn(ptr: ^u8, size: uint): ?^u8 @import()
|
||||
realloc_c := fn(ptr: ^u8, size: uint): ?^u8 @import("realloc")
|
||||
dealloc_c := fn(ptr: ^u8): void @import("free")
|
||||
memmove := fn(dest: ^u8, src: ^u8, size: uint): void @import()
|
||||
memcpy := fn(dest: ^u8, src: ^u8, size: uint): void @import()
|
||||
|
|
|
@ -6,7 +6,24 @@ lily := @use("../../lily/lib.hb")
|
|||
main := fn(argc: int, argv: [][]u8): u8 {
|
||||
alloc := lily.alloc.ArenaAllocator.new()
|
||||
defer alloc.deinit()
|
||||
|
||||
vec := lily.collections.Vec(u8, lily.alloc.ArenaAllocator).new(&alloc)
|
||||
|
||||
vec.push(69)
|
||||
vec.push(69)
|
||||
vec.push(69)
|
||||
|
||||
vec2 := lily.collections.Vec(u8, lily.alloc.ArenaAllocator).new(&alloc)
|
||||
|
||||
vec2.push(69)
|
||||
vec2.push(69)
|
||||
vec2.push(69)
|
||||
|
||||
vec.push(69)
|
||||
vec.push(69)
|
||||
vec.push(69)
|
||||
|
||||
ptr_one := alloc.alloc(u8, 6)
|
||||
ptr_two := alloc.alloc(u8, 179)
|
||||
ptr_two := alloc.alloc(u8, 18)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue