diff --git a/modules/arena.kl b/modules/arena.kl new file mode 100644 index 0000000..001fcb1 --- /dev/null +++ b/modules/arena.kl @@ -0,0 +1,45 @@ +OS :: #import "os_windows.kl" +Base :: #import "base.kl" +ArenaDI: U64 + +ADDITIONAL_COMMIT_SIZE :: 1024*1024 +DEFAULT_RESERVE_SIZE :: 1024*1024*1024 +DEFAULT_ALIGNMENT :: 8 + +Arena :: struct + di: U64 // @debug_id + memory: OS.Memory + alignment: U64 + len: U64 + +Init :: (a: *Arena) + a.memory = OS.Reserve(DEFAULT_RESERVE_SIZE) + a.alignment = DEFAULT_ALIGNMENT + a.di = ArenaDI++ + // a.allocator.proc = arena_allocator_proc + +PushSize :: (a: *Arena, size: Base.SizeU): *void + generous_size := size + a.alignment + if a.len + generous_size > a.memory.commit + if a.memory.reserve == 0 + Init(a) + result := OS.Commit(&a.memory, generous_size + ADDITIONAL_COMMIT_SIZE) + Assert(result == true) + a.len = Base.AlignUp(a.len, a.alignment) + Assert(a.memory.reserve > a.len + a.memory.commit) + result: *void = a.memory.data + a.len + a.len += size + return result + +// @todo: Make this compile time thing!!! +// This probably will wait till polymorphism stuff +// something like this: +// PushType :: (a: *Arena, type: $T): *T +// +PushType :: (a: *Arena, type: Type): *void + type_info := get_type_info(type) + Assert(type_info != 0) + return PushSize(a, type_info.size->Base.SizeU) + +Release :: (a: *Arena) + OS.Release(&a.memory)