mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Use uintptr where applicable in mem.Rollback_Stack
This commit is contained in:
@@ -38,19 +38,19 @@ ROLLBACK_STACK_DEFAULT_BLOCK_SIZE :: 4 * Megabyte
|
|||||||
//
|
//
|
||||||
// This is because allocations over the block size are not split up if the item
|
// This is because allocations over the block size are not split up if the item
|
||||||
// within is freed; they are immediately returned to the block allocator.
|
// within is freed; they are immediately returned to the block allocator.
|
||||||
ROLLBACK_STACK_MAX_HEAD_BLOCK_SIZE :: 1 * Gigabyte
|
ROLLBACK_STACK_MAX_HEAD_BLOCK_SIZE :: 2 * Gigabyte
|
||||||
|
|
||||||
|
|
||||||
Rollback_Stack_Header :: bit_field u64 {
|
Rollback_Stack_Header :: bit_field u64 {
|
||||||
prev_offset: int | 32,
|
prev_offset: uintptr | 32,
|
||||||
is_free: bool | 1,
|
is_free: bool | 1,
|
||||||
prev_ptr: int | 31,
|
prev_ptr: uintptr | 31,
|
||||||
}
|
}
|
||||||
|
|
||||||
Rollback_Stack_Block :: struct {
|
Rollback_Stack_Block :: struct {
|
||||||
next_block: ^Rollback_Stack_Block,
|
next_block: ^Rollback_Stack_Block,
|
||||||
last_alloc: rawptr,
|
last_alloc: rawptr,
|
||||||
offset: int,
|
offset: uintptr,
|
||||||
buffer: []byte,
|
buffer: []byte,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ Rollback_Stack :: struct {
|
|||||||
@(require_results)
|
@(require_results)
|
||||||
rb_ptr_in_bounds :: proc(block: ^Rollback_Stack_Block, ptr: rawptr) -> bool {
|
rb_ptr_in_bounds :: proc(block: ^Rollback_Stack_Block, ptr: rawptr) -> bool {
|
||||||
start := cast(uintptr)raw_data(block.buffer)
|
start := cast(uintptr)raw_data(block.buffer)
|
||||||
end := cast(uintptr)raw_data(block.buffer) + cast(uintptr)block.offset
|
end := cast(uintptr)raw_data(block.buffer) + block.offset
|
||||||
return start < cast(uintptr)ptr && cast(uintptr)ptr <= end
|
return start < cast(uintptr)ptr && cast(uintptr)ptr <= end
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,8 +150,8 @@ rb_free_all :: proc(stack: ^Rollback_Stack) {
|
|||||||
rb_resize :: proc(stack: ^Rollback_Stack, ptr: rawptr, old_size, size, alignment: int) -> (result: []byte, err: Allocator_Error) {
|
rb_resize :: proc(stack: ^Rollback_Stack, ptr: rawptr, old_size, size, alignment: int) -> (result: []byte, err: Allocator_Error) {
|
||||||
if ptr != nil {
|
if ptr != nil {
|
||||||
if block, _, ok := rb_find_last_alloc(stack, ptr); ok {
|
if block, _, ok := rb_find_last_alloc(stack, ptr); ok {
|
||||||
if block.offset + (size - old_size) < len(block.buffer) {
|
if block.offset + cast(uintptr)size - cast(uintptr)old_size < cast(uintptr)len(block.buffer) {
|
||||||
block.offset += (size - old_size)
|
block.offset += cast(uintptr)size - cast(uintptr)old_size
|
||||||
#no_bounds_check return (cast([^]byte)ptr)[:size], nil
|
#no_bounds_check return (cast([^]byte)ptr)[:size], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,10 +180,10 @@ rb_alloc :: proc(stack: ^Rollback_Stack, size, alignment: int) -> (result: []byt
|
|||||||
parent.next_block = block
|
parent.next_block = block
|
||||||
}
|
}
|
||||||
|
|
||||||
start := cast(uintptr)raw_data(block.buffer) + cast(uintptr)block.offset
|
start := cast(uintptr)raw_data(block.buffer) + block.offset
|
||||||
padding := calc_padding_with_header(start, cast(uintptr)alignment, size_of(Rollback_Stack_Header))
|
padding := cast(uintptr)calc_padding_with_header(start, cast(uintptr)alignment, size_of(Rollback_Stack_Header))
|
||||||
|
|
||||||
if block.offset + padding + size > len(block.buffer) {
|
if block.offset + padding + cast(uintptr)size > cast(uintptr)len(block.buffer) {
|
||||||
parent = block
|
parent = block
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -193,17 +193,17 @@ rb_alloc :: proc(stack: ^Rollback_Stack, size, alignment: int) -> (result: []byt
|
|||||||
|
|
||||||
header^ = {
|
header^ = {
|
||||||
prev_offset = block.offset,
|
prev_offset = block.offset,
|
||||||
prev_ptr = max(0, cast(int)(cast(uintptr)block.last_alloc - cast(uintptr)raw_data(block.buffer))),
|
prev_ptr = uintptr(0) if block.last_alloc == nil else cast(uintptr)block.last_alloc - cast(uintptr)raw_data(block.buffer),
|
||||||
is_free = false,
|
is_free = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
block.last_alloc = ptr
|
block.last_alloc = ptr
|
||||||
block.offset += padding + size
|
block.offset += padding + cast(uintptr)size
|
||||||
|
|
||||||
if len(block.buffer) > stack.block_size {
|
if len(block.buffer) > stack.block_size {
|
||||||
// This block exceeds the allocator's standard block size and is considered a singleton.
|
// This block exceeds the allocator's standard block size and is considered a singleton.
|
||||||
// Prevent any further allocations on it.
|
// Prevent any further allocations on it.
|
||||||
block.offset = len(block.buffer)
|
block.offset = cast(uintptr)len(block.buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
#no_bounds_check return ptr[:size], nil
|
#no_bounds_check return ptr[:size], nil
|
||||||
@@ -242,7 +242,7 @@ rollback_stack_init_dynamic :: proc(
|
|||||||
block_allocator := context.allocator,
|
block_allocator := context.allocator,
|
||||||
) -> Allocator_Error {
|
) -> Allocator_Error {
|
||||||
assert(block_size >= size_of(Rollback_Stack_Header) + size_of(rawptr), "Rollback Stack Allocator block size is too small.")
|
assert(block_size >= size_of(Rollback_Stack_Header) + size_of(rawptr), "Rollback Stack Allocator block size is too small.")
|
||||||
assert(block_size <= ROLLBACK_STACK_MAX_HEAD_BLOCK_SIZE, "Rollback Stack Allocators cannot support head blocks larger than 1 gigabyte.")
|
assert(block_size <= ROLLBACK_STACK_MAX_HEAD_BLOCK_SIZE, "Rollback Stack Allocators cannot support head blocks larger than 2 gigabytes.")
|
||||||
|
|
||||||
block := rb_make_block(block_size, block_allocator) or_return
|
block := rb_make_block(block_size, block_allocator) or_return
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user