mirror of
https://github.com/odin-lang/Odin.git
synced 2025-12-31 02:12:04 +00:00
Change to init from create
This commit is contained in:
@@ -54,45 +54,45 @@ allocator :: proc(t: ^Allocator) -> runtime.Allocator {
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
create_from_buf :: proc(buf: []byte) -> (control: ^Allocator, err: Error) {
|
||||
init_from_buffer :: proc(control: ^Allocator, buf: []byte) -> Error {
|
||||
assert(control != nil)
|
||||
if uintptr(raw_data(buf)) % ALIGN_SIZE != 0 {
|
||||
return nil, .Invalid_Alignment
|
||||
return .Invalid_Alignment
|
||||
}
|
||||
|
||||
pool_bytes := align_down(len(buf) - POOL_OVERHEAD - size_of(Allocator), ALIGN_SIZE)
|
||||
pool_bytes := align_down(len(buf) - POOL_OVERHEAD, ALIGN_SIZE)
|
||||
if pool_bytes < BLOCK_SIZE_MIN {
|
||||
return nil, .Backing_Buffer_Too_Small
|
||||
return .Backing_Buffer_Too_Small
|
||||
} else if pool_bytes > BLOCK_SIZE_MAX {
|
||||
return nil, .Backing_Buffer_Too_Large
|
||||
return .Backing_Buffer_Too_Large
|
||||
}
|
||||
|
||||
control = (^Allocator)(raw_data(buf))
|
||||
clear(control)
|
||||
pool_add(control, buf[size_of(Allocator):]) or_return
|
||||
return
|
||||
return pool_add(control, buf[:])
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
create_from_allocator :: proc(backing: runtime.Allocator, initial_pool_size: int, new_pool_size := 0) -> (control: ^Allocator, err: Error) {
|
||||
pool_bytes := align_up(uint(initial_pool_size) + POOL_OVERHEAD + size_of(Allocator), ALIGN_SIZE)
|
||||
init_from_allocator :: proc(control: ^Allocator, backing: runtime.Allocator, initial_pool_size: int, new_pool_size := 0) -> Error {
|
||||
assert(control != nil)
|
||||
pool_bytes := align_up(uint(initial_pool_size) + POOL_OVERHEAD, ALIGN_SIZE)
|
||||
if pool_bytes < BLOCK_SIZE_MIN {
|
||||
return nil, .Backing_Buffer_Too_Small
|
||||
return .Backing_Buffer_Too_Small
|
||||
} else if pool_bytes > BLOCK_SIZE_MAX {
|
||||
return nil, .Backing_Buffer_Too_Large
|
||||
return .Backing_Buffer_Too_Large
|
||||
}
|
||||
|
||||
if buf, backing_err := runtime.make_aligned([]byte, pool_bytes, ALIGN_SIZE, backing); backing_err != nil {
|
||||
return nil, .Backing_Allocator_Error
|
||||
} else {
|
||||
control, err = create_from_buf(buf)
|
||||
control.pool = Pool{
|
||||
data = buf,
|
||||
allocator = backing,
|
||||
}
|
||||
buf, backing_err := runtime.make_aligned([]byte, pool_bytes, ALIGN_SIZE, backing)
|
||||
if backing_err != nil {
|
||||
return .Backing_Allocator_Error
|
||||
}
|
||||
return
|
||||
err := init_from_buffer(control, buf)
|
||||
control.pool = Pool{
|
||||
data = buf,
|
||||
allocator = backing,
|
||||
}
|
||||
return err
|
||||
}
|
||||
create :: proc{create_from_buf, create_from_allocator}
|
||||
init :: proc{init_from_buffer, init_from_allocator}
|
||||
|
||||
destroy :: proc(control: ^Allocator) {
|
||||
if control == nil { return }
|
||||
|
||||
@@ -522,10 +522,10 @@ block_locate_free :: proc(control: ^Allocator, size: uint) -> (block: ^Block_Hea
|
||||
@(require_results)
|
||||
block_prepare_used :: proc(control: ^Allocator, block: ^Block_Header, size: uint) -> (res: []byte, err: runtime.Allocator_Error) {
|
||||
if block != nil {
|
||||
assert(size != 0, "Size must be non-zero")
|
||||
block_trim_free(control, block, size)
|
||||
block_mark_as_used(block)
|
||||
res = ([^]byte)(block_to_ptr(block))[:size]
|
||||
assert(size != 0, "Size must be non-zero")
|
||||
block_trim_free(control, block, size)
|
||||
block_mark_as_used(block)
|
||||
res = ([^]byte)(block_to_ptr(block))[:size]
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -588,6 +588,7 @@ pool_remove :: proc(control: ^Allocator, pool: []u8) {
|
||||
|
||||
@(require_results)
|
||||
alloc_bytes_non_zeroed :: proc(control: ^Allocator, size: uint, align: uint) -> (res: []byte, err: runtime.Allocator_Error) {
|
||||
assert(control != nil)
|
||||
adjust := adjust_request_size(size, ALIGN_SIZE)
|
||||
|
||||
GAP_MINIMUM :: size_of(Block_Header)
|
||||
@@ -635,6 +636,7 @@ alloc_bytes :: proc(control: ^Allocator, size: uint, align: uint) -> (res: []byt
|
||||
|
||||
|
||||
free_with_size :: proc(control: ^Allocator, ptr: rawptr, size: uint) {
|
||||
assert(control != nil)
|
||||
// `size` is currently ignored
|
||||
if ptr == nil {
|
||||
return
|
||||
@@ -651,8 +653,7 @@ free_with_size :: proc(control: ^Allocator, ptr: rawptr, size: uint) {
|
||||
|
||||
@(require_results)
|
||||
resize :: proc(control: ^Allocator, ptr: rawptr, old_size, new_size: uint, alignment: uint) -> (res: []byte, err: runtime.Allocator_Error) {
|
||||
// `size` is currently ignored
|
||||
|
||||
assert(control != nil)
|
||||
if ptr != nil && new_size == 0 {
|
||||
free_with_size(control, ptr, old_size)
|
||||
return
|
||||
@@ -696,8 +697,7 @@ resize :: proc(control: ^Allocator, ptr: rawptr, old_size, new_size: uint, align
|
||||
|
||||
@(require_results)
|
||||
resize_non_zeroed :: proc(control: ^Allocator, ptr: rawptr, old_size, new_size: uint, alignment: uint) -> (res: []byte, err: runtime.Allocator_Error) {
|
||||
// `size` is currently ignored
|
||||
|
||||
assert(control != nil)
|
||||
if ptr != nil && new_size == 0 {
|
||||
free_with_size(control, ptr, old_size)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user