void pointer duck typing
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
Os :: #import "os.kl"
|
||||
|
||||
Os :: #import "os.kl"
|
||||
SizeU :: #strict U64
|
||||
|
||||
arena_di: U64
|
||||
|
||||
Arena :: struct
|
||||
di: U64 // @debug_id
|
||||
memory: Os.Memory
|
||||
alignment: U64
|
||||
len: U64
|
||||
|
||||
ADDITIONAL_COMMIT_SIZE :: 1024*1024
|
||||
DEFAULT_RESERVE_SIZE :: 1024*1024*1024
|
||||
DEFAULT_ALIGNMENT :: 8
|
||||
|
||||
clamp_top_sizeu :: (val: SizeU, max: SizeU): SizeU
|
||||
if val > max
|
||||
return max
|
||||
@@ -22,3 +28,22 @@ get_align_offset :: (size: SizeU, align: SizeU): SizeU
|
||||
align_up :: (size: SizeU, align: SizeU): SizeU
|
||||
result := size + get_align_offset(size, align)
|
||||
return result
|
||||
|
||||
arena_init :: (a: *Arena)
|
||||
a.memory = Os.reserve(a.DEFAULT_RESERVE_SIZE)
|
||||
a.alignment = a.DEFAULT_ALIGNMENT
|
||||
a.di = arena_di++
|
||||
|
||||
arena_push_size :: (a: *Arena, size: SizeU): *void
|
||||
generous_size := size + a.alignment
|
||||
if a.len + generous_size > a.memory.commit
|
||||
if a.memory.reserve == 0
|
||||
arena_init(a)
|
||||
result := Os.commit(&a.memory, generous_size + a.ADDITIONAL_COMMIT_SIZE)
|
||||
assert(result == true)
|
||||
a.len = align_up(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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user