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