mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Improve the Allocator interface to support returning Allocator_Error to allow for safer calls
Virtually all code (except for user-written custom allocators) should work as normal. Extra features will need to be added to make the current procedures support the `Allocator_Error` return value (akin to #optional_ok)
This commit is contained in:
+95
-15
@@ -28,11 +28,19 @@ Allocator_Query_Info :: struct {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
Allocator_Error :: runtime.Allocator_Error;
|
||||||
|
/*
|
||||||
|
Allocator_Error :: enum byte {
|
||||||
|
None = 0,
|
||||||
|
Out_Of_Memory = 1,
|
||||||
|
Invalid_Pointer = 2,
|
||||||
|
}
|
||||||
|
*/
|
||||||
Allocator_Proc :: runtime.Allocator_Proc;
|
Allocator_Proc :: runtime.Allocator_Proc;
|
||||||
/*
|
/*
|
||||||
Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
|
Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, location := #caller_location) -> rawptr;
|
old_memory: rawptr, old_size: int, location: Source_Code_Location = #caller_location) -> ([]byte, Allocator_Error);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Allocator :: runtime.Allocator;
|
Allocator :: runtime.Allocator;
|
||||||
@@ -52,23 +60,49 @@ alloc :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := contex
|
|||||||
if allocator.procedure == nil {
|
if allocator.procedure == nil {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
return allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, 0, loc);
|
data, err := allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, loc);
|
||||||
|
_ = err;
|
||||||
|
return raw_data(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) {
|
alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
if ptr == nil {
|
if size == 0 {
|
||||||
return;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
if allocator.procedure == nil {
|
if allocator.procedure == nil {
|
||||||
return;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, 0, 0, loc);
|
return allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
free_all :: proc(allocator := context.allocator, loc := #caller_location) {
|
free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
if allocator.procedure != nil {
|
if ptr == nil {
|
||||||
allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc);
|
return nil;
|
||||||
}
|
}
|
||||||
|
if allocator.procedure == nil {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
_, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, 0, loc);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
free_bytes :: proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
|
if bytes == nil {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
if allocator.procedure == nil {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
_, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, raw_data(bytes), len(bytes), loc);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
free_all :: proc(allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
|
if allocator.procedure != nil {
|
||||||
|
_, err := allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, loc);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
||||||
@@ -77,18 +111,40 @@ resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_AL
|
|||||||
}
|
}
|
||||||
if new_size == 0 {
|
if new_size == 0 {
|
||||||
if ptr != nil {
|
if ptr != nil {
|
||||||
allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, 0, 0, loc);
|
allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc);
|
||||||
}
|
}
|
||||||
return nil;
|
return nil;
|
||||||
} else if ptr == nil {
|
} else if ptr == nil {
|
||||||
return allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, 0, loc);
|
_, err := allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc);
|
||||||
|
_ = err;
|
||||||
|
return nil;
|
||||||
}
|
}
|
||||||
return allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, 0, loc);
|
data, err := allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, loc);
|
||||||
|
_ = err;
|
||||||
|
return raw_data(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
resize_bytes :: proc(old_data: []byte, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
|
if allocator.procedure == nil {
|
||||||
|
return nil, nil;
|
||||||
|
}
|
||||||
|
ptr := raw_data(old_data);
|
||||||
|
old_size := len(old_data);
|
||||||
|
if new_size == 0 {
|
||||||
|
if ptr != nil {
|
||||||
|
_, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc);
|
||||||
|
return nil, err;
|
||||||
|
}
|
||||||
|
return nil, nil;
|
||||||
|
} else if ptr == nil {
|
||||||
|
return allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc);
|
||||||
|
}
|
||||||
|
return allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
query_features :: proc(allocator: Allocator, loc := #caller_location) -> (set: Allocator_Mode_Set) {
|
query_features :: proc(allocator: Allocator, loc := #caller_location) -> (set: Allocator_Mode_Set) {
|
||||||
if allocator.procedure != nil {
|
if allocator.procedure != nil {
|
||||||
allocator.procedure(allocator.data, Allocator_Mode.Query_Features, 0, 0, &set, 0, 0, loc);
|
allocator.procedure(allocator.data, Allocator_Mode.Query_Features, 0, 0, &set, 0, loc);
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
return nil;
|
return nil;
|
||||||
@@ -97,7 +153,7 @@ query_features :: proc(allocator: Allocator, loc := #caller_location) -> (set: A
|
|||||||
query_info :: proc(pointer: rawptr, allocator: Allocator, loc := #caller_location) -> (props: Allocator_Query_Info) {
|
query_info :: proc(pointer: rawptr, allocator: Allocator, loc := #caller_location) -> (props: Allocator_Query_Info) {
|
||||||
props.pointer = pointer;
|
props.pointer = pointer;
|
||||||
if allocator.procedure != nil {
|
if allocator.procedure != nil {
|
||||||
allocator.procedure(allocator.data, Allocator_Mode.Query_Info, 0, 0, &props, 0, 0, loc);
|
allocator.procedure(allocator.data, Allocator_Mode.Query_Info, 0, 0, &props, 0, loc);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -218,4 +274,28 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment:
|
|||||||
free(old_memory, allocator, loc);
|
free(old_memory, allocator, loc);
|
||||||
return new_memory;
|
return new_memory;
|
||||||
}
|
}
|
||||||
|
default_resize_bytes_align :: proc(old_data: []byte, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
|
old_memory := raw_data(old_data);
|
||||||
|
old_size := len(old_data);
|
||||||
|
if old_memory == nil {
|
||||||
|
return alloc_bytes(new_size, alignment, allocator, loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
if new_size == 0 {
|
||||||
|
err := free_bytes(old_data, allocator, loc);
|
||||||
|
return nil, err;
|
||||||
|
}
|
||||||
|
|
||||||
|
if new_size == old_size {
|
||||||
|
return old_data, .None;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_memory, err := alloc_bytes(new_size, alignment, allocator, loc);
|
||||||
|
if new_memory == nil || err != nil {
|
||||||
|
return nil, err;
|
||||||
|
}
|
||||||
|
|
||||||
|
runtime.copy(new_memory, old_data);
|
||||||
|
free_bytes(old_data, allocator, loc);
|
||||||
|
return new_memory, err;
|
||||||
|
}
|
||||||
|
|||||||
+154
-174
@@ -5,8 +5,8 @@ import "core:runtime"
|
|||||||
|
|
||||||
nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
nil_allocator :: proc() -> Allocator {
|
nil_allocator :: proc() -> Allocator {
|
||||||
@@ -47,7 +47,7 @@ arena_allocator :: proc(arena: ^Arena) -> Allocator {
|
|||||||
|
|
||||||
arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
arena := cast(^Arena)allocator_data;
|
arena := cast(^Arena)allocator_data;
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
@@ -55,7 +55,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
total_size := size + alignment;
|
total_size := size + alignment;
|
||||||
|
|
||||||
if arena.offset + total_size > len(arena.data) {
|
if arena.offset + total_size > len(arena.data) {
|
||||||
return nil;
|
return nil, .Out_Of_Memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
#no_bounds_check end := &arena.data[arena.offset];
|
#no_bounds_check end := &arena.data[arena.offset];
|
||||||
@@ -63,7 +63,8 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
ptr := align_forward(end, uintptr(alignment));
|
ptr := align_forward(end, uintptr(alignment));
|
||||||
arena.offset += total_size;
|
arena.offset += total_size;
|
||||||
arena.peak_used = max(arena.peak_used, arena.offset);
|
arena.peak_used = max(arena.peak_used, arena.offset);
|
||||||
return zero(ptr, size);
|
zero(ptr, size);
|
||||||
|
return byte_slice(ptr, size), nil;
|
||||||
|
|
||||||
case .Free:
|
case .Free:
|
||||||
// NOTE(bill): Free all at once
|
// NOTE(bill): Free all at once
|
||||||
@@ -73,20 +74,20 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
arena.offset = 0;
|
arena.offset = 0;
|
||||||
|
|
||||||
case .Resize:
|
case .Resize:
|
||||||
return default_resize_align(old_memory, old_size, size, alignment, arena_allocator(arena));
|
return default_resize_bytes_align(byte_slice(old_memory, old_size), size, alignment, arena_allocator(arena));
|
||||||
|
|
||||||
case .Query_Features:
|
case .Query_Features:
|
||||||
set := (^Allocator_Mode_Set)(old_memory);
|
set := (^Allocator_Mode_Set)(old_memory);
|
||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free_All, .Resize, .Query_Features};
|
set^ = {.Alloc, .Free_All, .Resize, .Query_Features};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
begin_arena_temp_memory :: proc(a: ^Arena) -> Arena_Temp_Memory {
|
begin_arena_temp_memory :: proc(a: ^Arena) -> Arena_Temp_Memory {
|
||||||
@@ -109,9 +110,9 @@ end_arena_temp_memory :: proc(using tmp: Arena_Temp_Memory) {
|
|||||||
Scratch_Allocator :: struct {
|
Scratch_Allocator :: struct {
|
||||||
data: []byte,
|
data: []byte,
|
||||||
curr_offset: int,
|
curr_offset: int,
|
||||||
prev_allocation: rawptr,
|
prev_allocation: rawptr,
|
||||||
backup_allocator: Allocator,
|
backup_allocator: Allocator,
|
||||||
leaked_allocations: [dynamic]rawptr,
|
leaked_allocations: [dynamic][]byte,
|
||||||
}
|
}
|
||||||
|
|
||||||
scratch_allocator_init :: proc(s: ^Scratch_Allocator, size: int, backup_allocator := context.allocator) {
|
scratch_allocator_init :: proc(s: ^Scratch_Allocator, size: int, backup_allocator := context.allocator) {
|
||||||
@@ -127,7 +128,7 @@ scratch_allocator_destroy :: proc(s: ^Scratch_Allocator) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for ptr in s.leaked_allocations {
|
for ptr in s.leaked_allocations {
|
||||||
free(ptr, s.backup_allocator);
|
free_bytes(ptr, s.backup_allocator);
|
||||||
}
|
}
|
||||||
delete(s.leaked_allocations);
|
delete(s.leaked_allocations);
|
||||||
delete(s.data, s.backup_allocator);
|
delete(s.data, s.backup_allocator);
|
||||||
@@ -136,7 +137,7 @@ scratch_allocator_destroy :: proc(s: ^Scratch_Allocator) {
|
|||||||
|
|
||||||
scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
|
|
||||||
s := (^Scratch_Allocator)(allocator_data);
|
s := (^Scratch_Allocator)(allocator_data);
|
||||||
|
|
||||||
@@ -165,7 +166,7 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
s.prev_allocation = rawptr(ptr);
|
s.prev_allocation = rawptr(ptr);
|
||||||
offset := int(ptr - start);
|
offset := int(ptr - start);
|
||||||
s.curr_offset = offset + size;
|
s.curr_offset = offset + size;
|
||||||
return rawptr(ptr);
|
return byte_slice(rawptr(ptr), size), nil;
|
||||||
|
|
||||||
case size <= len(s.data):
|
case size <= len(s.data):
|
||||||
start := uintptr(raw_data(s.data));
|
start := uintptr(raw_data(s.data));
|
||||||
@@ -175,7 +176,7 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
s.prev_allocation = rawptr(ptr);
|
s.prev_allocation = rawptr(ptr);
|
||||||
offset := int(ptr - start);
|
offset := int(ptr - start);
|
||||||
s.curr_offset = offset + size;
|
s.curr_offset = offset + size;
|
||||||
return rawptr(ptr);
|
return byte_slice(rawptr(ptr), size), nil;
|
||||||
}
|
}
|
||||||
a := s.backup_allocator;
|
a := s.backup_allocator;
|
||||||
if a.procedure == nil {
|
if a.procedure == nil {
|
||||||
@@ -183,9 +184,12 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
s.backup_allocator = a;
|
s.backup_allocator = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr := alloc(size, alignment, a, loc);
|
ptr, err := alloc_bytes(size, alignment, a, loc);
|
||||||
|
if err != nil {
|
||||||
|
return ptr, err;
|
||||||
|
}
|
||||||
if s.leaked_allocations == nil {
|
if s.leaked_allocations == nil {
|
||||||
s.leaked_allocations = make([dynamic]rawptr, a);
|
s.leaked_allocations = make([dynamic][]byte, a);
|
||||||
}
|
}
|
||||||
append(&s.leaked_allocations, ptr);
|
append(&s.leaked_allocations, ptr);
|
||||||
|
|
||||||
@@ -195,7 +199,7 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ptr;
|
return ptr, err;
|
||||||
|
|
||||||
case .Free:
|
case .Free:
|
||||||
start := uintptr(raw_data(s.data));
|
start := uintptr(raw_data(s.data));
|
||||||
@@ -205,30 +209,32 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
if s.prev_allocation == old_memory {
|
if s.prev_allocation == old_memory {
|
||||||
s.curr_offset = int(uintptr(s.prev_allocation) - start);
|
s.curr_offset = int(uintptr(s.prev_allocation) - start);
|
||||||
s.prev_allocation = nil;
|
s.prev_allocation = nil;
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
if start <= old_ptr && old_ptr < end {
|
if start <= old_ptr && old_ptr < end {
|
||||||
// NOTE(bill): Cannot free this pointer but it is valid
|
// NOTE(bill): Cannot free this pointer but it is valid
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(s.leaked_allocations) != 0 {
|
if len(s.leaked_allocations) != 0 {
|
||||||
for ptr, i in s.leaked_allocations {
|
for data, i in s.leaked_allocations {
|
||||||
|
ptr := raw_data(data);
|
||||||
if ptr == old_memory {
|
if ptr == old_memory {
|
||||||
free(ptr, s.backup_allocator);
|
free_bytes(data, s.backup_allocator);
|
||||||
ordered_remove(&s.leaked_allocations, i);
|
ordered_remove(&s.leaked_allocations, i);
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic("invalid pointer passed to default_temp_allocator");
|
return nil, .Invalid_Pointer;
|
||||||
|
// panic("invalid pointer passed to default_temp_allocator");
|
||||||
|
|
||||||
case .Free_All:
|
case .Free_All:
|
||||||
s.curr_offset = 0;
|
s.curr_offset = 0;
|
||||||
s.prev_allocation = nil;
|
s.prev_allocation = nil;
|
||||||
for ptr in s.leaked_allocations {
|
for ptr in s.leaked_allocations {
|
||||||
free(ptr, s.backup_allocator);
|
free_bytes(ptr, s.backup_allocator);
|
||||||
}
|
}
|
||||||
clear(&s.leaked_allocations);
|
clear(&s.leaked_allocations);
|
||||||
|
|
||||||
@@ -238,26 +244,28 @@ scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
old_ptr := uintptr(old_memory);
|
old_ptr := uintptr(old_memory);
|
||||||
if begin <= old_ptr && old_ptr < end && old_ptr+uintptr(size) < end {
|
if begin <= old_ptr && old_ptr < end && old_ptr+uintptr(size) < end {
|
||||||
s.curr_offset = int(old_ptr-begin)+size;
|
s.curr_offset = int(old_ptr-begin)+size;
|
||||||
return old_memory;
|
return byte_slice(old_memory, size), nil;
|
||||||
}
|
}
|
||||||
ptr := scratch_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, flags, loc);
|
data, err := scratch_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, loc);
|
||||||
copy(ptr, old_memory, old_size);
|
if err != nil {
|
||||||
scratch_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, flags, loc);
|
return data, err;
|
||||||
return ptr;
|
}
|
||||||
|
runtime.copy(data, byte_slice(old_memory, old_size));
|
||||||
|
_, err = scratch_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, loc);
|
||||||
|
return data, err;
|
||||||
|
|
||||||
case .Query_Features:
|
case .Query_Features:
|
||||||
set := (^Allocator_Mode_Set)(old_memory);
|
set := (^Allocator_Mode_Set)(old_memory);
|
||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
|
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil, nil;
|
||||||
return nil;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scratch_allocator :: proc(allocator: ^Scratch_Allocator) -> Allocator {
|
scratch_allocator :: proc(allocator: ^Scratch_Allocator) -> Allocator {
|
||||||
@@ -301,18 +309,18 @@ stack_allocator :: proc(stack: ^Stack) -> Allocator {
|
|||||||
|
|
||||||
stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
s := cast(^Stack)allocator_data;
|
s := cast(^Stack)allocator_data;
|
||||||
|
|
||||||
if s.data == nil {
|
if s.data == nil {
|
||||||
return nil;
|
return nil, .Out_Of_Memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
raw_alloc :: proc(s: ^Stack, size, alignment: int) -> rawptr {
|
raw_alloc :: proc(s: ^Stack, size, alignment: int) -> ([]byte, Allocator_Error) {
|
||||||
curr_addr := uintptr(raw_data(s.data)) + uintptr(s.curr_offset);
|
curr_addr := uintptr(raw_data(s.data)) + uintptr(s.curr_offset);
|
||||||
padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Stack_Allocation_Header));
|
padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Stack_Allocation_Header));
|
||||||
if s.curr_offset + padding + size > len(s.data) {
|
if s.curr_offset + padding + size > len(s.data) {
|
||||||
return nil;
|
return nil, .Out_Of_Memory;
|
||||||
}
|
}
|
||||||
s.prev_offset = s.curr_offset;
|
s.prev_offset = s.curr_offset;
|
||||||
s.curr_offset += padding;
|
s.curr_offset += padding;
|
||||||
@@ -326,7 +334,8 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
|
|
||||||
s.peak_used = max(s.peak_used, s.curr_offset);
|
s.peak_used = max(s.peak_used, s.curr_offset);
|
||||||
|
|
||||||
return zero(rawptr(next_addr), size);
|
zero(rawptr(next_addr), size);
|
||||||
|
return byte_slice(rawptr(next_addr), size), nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
@@ -334,7 +343,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
return raw_alloc(s, size, alignment);
|
return raw_alloc(s, size, alignment);
|
||||||
case .Free:
|
case .Free:
|
||||||
if old_memory == nil {
|
if old_memory == nil {
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
start := uintptr(raw_data(s.data));
|
start := uintptr(raw_data(s.data));
|
||||||
end := start + uintptr(len(s.data));
|
end := start + uintptr(len(s.data));
|
||||||
@@ -346,20 +355,20 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
|
|
||||||
if curr_addr >= start+uintptr(s.curr_offset) {
|
if curr_addr >= start+uintptr(s.curr_offset) {
|
||||||
// NOTE(bill): Allow double frees
|
// NOTE(bill): Allow double frees
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
header := (^Stack_Allocation_Header)(curr_addr - size_of(Stack_Allocation_Header));
|
header := (^Stack_Allocation_Header)(curr_addr - size_of(Stack_Allocation_Header));
|
||||||
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)));
|
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)));
|
||||||
|
|
||||||
if old_offset != header.prev_offset {
|
if old_offset != header.prev_offset {
|
||||||
panic("Out of order stack allocator free");
|
// panic("Out of order stack allocator free");
|
||||||
|
return nil, .Invalid_Pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
s.curr_offset = old_offset;
|
s.curr_offset = old_offset;
|
||||||
s.prev_offset = header.prev_offset;
|
s.prev_offset = header.prev_offset;
|
||||||
|
|
||||||
|
|
||||||
case .Free_All:
|
case .Free_All:
|
||||||
s.prev_offset = 0;
|
s.prev_offset = 0;
|
||||||
s.curr_offset = 0;
|
s.curr_offset = 0;
|
||||||
@@ -369,7 +378,7 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
return raw_alloc(s, size, alignment);
|
return raw_alloc(s, size, alignment);
|
||||||
}
|
}
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
start := uintptr(raw_data(s.data));
|
start := uintptr(raw_data(s.data));
|
||||||
@@ -381,20 +390,22 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
|
|
||||||
if curr_addr >= start+uintptr(s.curr_offset) {
|
if curr_addr >= start+uintptr(s.curr_offset) {
|
||||||
// NOTE(bill): Allow double frees
|
// NOTE(bill): Allow double frees
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
if old_size == size {
|
if old_size == size {
|
||||||
return old_memory;
|
return byte_slice(old_memory, size), nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
header := (^Stack_Allocation_Header)(curr_addr - size_of(Stack_Allocation_Header));
|
header := (^Stack_Allocation_Header)(curr_addr - size_of(Stack_Allocation_Header));
|
||||||
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)));
|
old_offset := int(curr_addr - uintptr(header.padding) - uintptr(raw_data(s.data)));
|
||||||
|
|
||||||
if old_offset != header.prev_offset {
|
if old_offset != header.prev_offset {
|
||||||
ptr := raw_alloc(s, size, alignment);
|
data, err := raw_alloc(s, size, alignment);
|
||||||
copy(ptr, old_memory, min(old_size, size));
|
if err == nil {
|
||||||
return ptr;
|
runtime.copy(data, byte_slice(old_memory, old_size));
|
||||||
|
}
|
||||||
|
return data, err;
|
||||||
}
|
}
|
||||||
|
|
||||||
old_memory_size := uintptr(s.curr_offset) - (curr_addr - start);
|
old_memory_size := uintptr(s.curr_offset) - (curr_addr - start);
|
||||||
@@ -406,19 +417,19 @@ stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
zero(rawptr(curr_addr + uintptr(diff)), diff);
|
zero(rawptr(curr_addr + uintptr(diff)), diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
return old_memory;
|
return byte_slice(old_memory, size), nil;
|
||||||
|
|
||||||
case .Query_Features:
|
case .Query_Features:
|
||||||
set := (^Allocator_Mode_Set)(old_memory);
|
set := (^Allocator_Mode_Set)(old_memory);
|
||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
|
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -453,20 +464,20 @@ small_stack_allocator :: proc(stack: ^Small_Stack) -> Allocator {
|
|||||||
|
|
||||||
small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, ocation := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
s := cast(^Small_Stack)allocator_data;
|
s := cast(^Small_Stack)allocator_data;
|
||||||
|
|
||||||
if s.data == nil {
|
if s.data == nil {
|
||||||
return nil;
|
return nil, .Out_Of_Memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
align := clamp(alignment, 1, 8*size_of(Stack_Allocation_Header{}.padding)/2);
|
align := clamp(alignment, 1, 8*size_of(Stack_Allocation_Header{}.padding)/2);
|
||||||
|
|
||||||
raw_alloc :: proc(s: ^Small_Stack, size, alignment: int) -> rawptr {
|
raw_alloc :: proc(s: ^Small_Stack, size, alignment: int) -> ([]byte, Allocator_Error) {
|
||||||
curr_addr := uintptr(raw_data(s.data)) + uintptr(s.offset);
|
curr_addr := uintptr(raw_data(s.data)) + uintptr(s.offset);
|
||||||
padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Small_Stack_Allocation_Header));
|
padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Small_Stack_Allocation_Header));
|
||||||
if s.offset + padding + size > len(s.data) {
|
if s.offset + padding + size > len(s.data) {
|
||||||
return nil;
|
return nil, .Out_Of_Memory;
|
||||||
}
|
}
|
||||||
s.offset += padding;
|
s.offset += padding;
|
||||||
|
|
||||||
@@ -478,7 +489,8 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
|
|
||||||
s.peak_used = max(s.peak_used, s.offset);
|
s.peak_used = max(s.peak_used, s.offset);
|
||||||
|
|
||||||
return zero(rawptr(next_addr), size);
|
zero(rawptr(next_addr), size);
|
||||||
|
return byte_slice(rawptr(next_addr), size), nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
@@ -486,19 +498,20 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
return raw_alloc(s, size, align);
|
return raw_alloc(s, size, align);
|
||||||
case .Free:
|
case .Free:
|
||||||
if old_memory == nil {
|
if old_memory == nil {
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
start := uintptr(raw_data(s.data));
|
start := uintptr(raw_data(s.data));
|
||||||
end := start + uintptr(len(s.data));
|
end := start + uintptr(len(s.data));
|
||||||
curr_addr := uintptr(old_memory);
|
curr_addr := uintptr(old_memory);
|
||||||
|
|
||||||
if !(start <= curr_addr && curr_addr < end) {
|
if !(start <= curr_addr && curr_addr < end) {
|
||||||
panic("Out of bounds memory address passed to stack allocator (free)");
|
// panic("Out of bounds memory address passed to stack allocator (free)");
|
||||||
|
return nil, .Invalid_Pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if curr_addr >= start+uintptr(s.offset) {
|
if curr_addr >= start+uintptr(s.offset) {
|
||||||
// NOTE(bill): Allow double frees
|
// NOTE(bill): Allow double frees
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
header := (^Small_Stack_Allocation_Header)(curr_addr - size_of(Small_Stack_Allocation_Header));
|
header := (^Small_Stack_Allocation_Header)(curr_addr - size_of(Small_Stack_Allocation_Header));
|
||||||
@@ -514,41 +527,44 @@ small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
return raw_alloc(s, size, align);
|
return raw_alloc(s, size, align);
|
||||||
}
|
}
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
start := uintptr(raw_data(s.data));
|
start := uintptr(raw_data(s.data));
|
||||||
end := start + uintptr(len(s.data));
|
end := start + uintptr(len(s.data));
|
||||||
curr_addr := uintptr(old_memory);
|
curr_addr := uintptr(old_memory);
|
||||||
if !(start <= curr_addr && curr_addr < end) {
|
if !(start <= curr_addr && curr_addr < end) {
|
||||||
panic("Out of bounds memory address passed to stack allocator (resize)");
|
// panic("Out of bounds memory address passed to stack allocator (resize)");
|
||||||
|
return nil, .Invalid_Pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if curr_addr >= start+uintptr(s.offset) {
|
if curr_addr >= start+uintptr(s.offset) {
|
||||||
// NOTE(bill): Treat as a double free
|
// NOTE(bill): Treat as a double free
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
if old_size == size {
|
if old_size == size {
|
||||||
return old_memory;
|
return byte_slice(old_memory, size), nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr := raw_alloc(s, size, align);
|
data, err := raw_alloc(s, size, align);
|
||||||
copy(ptr, old_memory, min(old_size, size));
|
if err == nil {
|
||||||
return ptr;
|
runtime.copy(data, byte_slice(old_memory, old_size));
|
||||||
|
}
|
||||||
|
return data, err;
|
||||||
|
|
||||||
case .Query_Features:
|
case .Query_Features:
|
||||||
set := (^Allocator_Mode_Set)(old_memory);
|
set := (^Allocator_Mode_Set)(old_memory);
|
||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
|
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -579,42 +595,44 @@ DYNAMIC_POOL_OUT_OF_BAND_SIZE_DEFAULT :: 6554;
|
|||||||
|
|
||||||
dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int,
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
flags: u64 = 0, loc := #caller_location) -> rawptr {
|
|
||||||
pool := (^Dynamic_Pool)(allocator_data);
|
pool := (^Dynamic_Pool)(allocator_data);
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
case .Alloc:
|
case .Alloc:
|
||||||
return dynamic_pool_alloc(pool, size);
|
return dynamic_pool_alloc_bytes(pool, size);
|
||||||
case .Free:
|
case .Free:
|
||||||
//
|
return nil, nil;
|
||||||
case .Free_All:
|
case .Free_All:
|
||||||
dynamic_pool_free_all(pool);
|
dynamic_pool_free_all(pool);
|
||||||
|
return nil, nil;
|
||||||
case .Resize:
|
case .Resize:
|
||||||
if old_size >= size {
|
if old_size >= size {
|
||||||
return old_memory;
|
return byte_slice(old_memory, size), nil;
|
||||||
}
|
}
|
||||||
ptr := dynamic_pool_alloc(pool, size);
|
data, err := dynamic_pool_alloc_bytes(pool, size);
|
||||||
copy(ptr, old_memory, old_size);
|
if err == nil {
|
||||||
return ptr;
|
runtime.copy(data, byte_slice(old_memory, old_size));
|
||||||
|
}
|
||||||
|
return data, err;
|
||||||
|
|
||||||
case .Query_Features:
|
case .Query_Features:
|
||||||
set := (^Allocator_Mode_Set)(old_memory);
|
set := (^Allocator_Mode_Set)(old_memory);
|
||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free_All, .Resize, .Query_Features, .Query_Info};
|
set^ = {.Alloc, .Free_All, .Resize, .Query_Features, .Query_Info};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
info := (^Allocator_Query_Info)(old_memory);
|
info := (^Allocator_Query_Info)(old_memory);
|
||||||
if info != nil && info.pointer != nil {
|
if info != nil && info.pointer != nil {
|
||||||
info.size = pool.block_size;
|
info.size = pool.block_size;
|
||||||
info.alignment = pool.alignment;
|
info.alignment = pool.alignment;
|
||||||
return info;
|
return byte_slice(info, size_of(info^)), nil;
|
||||||
}
|
}
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -649,8 +667,14 @@ dynamic_pool_destroy :: proc(using pool: ^Dynamic_Pool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
dynamic_pool_alloc :: proc(using pool: ^Dynamic_Pool, bytes: int) -> rawptr {
|
dynamic_pool_alloc :: proc(pool: ^Dynamic_Pool, bytes: int) -> rawptr {
|
||||||
cycle_new_block :: proc(using pool: ^Dynamic_Pool) {
|
data, err := dynamic_pool_alloc_bytes(pool, bytes);
|
||||||
|
assert(err == nil);
|
||||||
|
return raw_data(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic_pool_alloc_bytes :: proc(using pool: ^Dynamic_Pool, bytes: int) -> ([]byte, Allocator_Error) {
|
||||||
|
cycle_new_block :: proc(using pool: ^Dynamic_Pool) -> (err: Allocator_Error) {
|
||||||
if block_allocator.procedure == nil {
|
if block_allocator.procedure == nil {
|
||||||
panic("You must call pool_init on a Pool before using it");
|
panic("You must call pool_init on a Pool before using it");
|
||||||
}
|
}
|
||||||
@@ -663,14 +687,17 @@ dynamic_pool_alloc :: proc(using pool: ^Dynamic_Pool, bytes: int) -> rawptr {
|
|||||||
if len(unused_blocks) > 0 {
|
if len(unused_blocks) > 0 {
|
||||||
new_block = pop(&unused_blocks);
|
new_block = pop(&unused_blocks);
|
||||||
} else {
|
} else {
|
||||||
new_block = block_allocator.procedure(block_allocator.data, Allocator_Mode.Alloc,
|
data: []byte;
|
||||||
block_size, alignment,
|
data, err = block_allocator.procedure(block_allocator.data, Allocator_Mode.Alloc,
|
||||||
nil, 0);
|
block_size, alignment,
|
||||||
|
nil, 0);
|
||||||
|
new_block = raw_data(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes_left = block_size;
|
bytes_left = block_size;
|
||||||
current_pos = new_block;
|
current_pos = new_block;
|
||||||
current_block = new_block;
|
current_block = new_block;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
n := bytes;
|
n := bytes;
|
||||||
@@ -678,26 +705,29 @@ dynamic_pool_alloc :: proc(using pool: ^Dynamic_Pool, bytes: int) -> rawptr {
|
|||||||
n += extra;
|
n += extra;
|
||||||
if n >= out_band_size {
|
if n >= out_band_size {
|
||||||
assert(block_allocator.procedure != nil);
|
assert(block_allocator.procedure != nil);
|
||||||
memory := block_allocator.procedure(block_allocator.data, Allocator_Mode.Alloc,
|
memory, err := block_allocator.procedure(block_allocator.data, Allocator_Mode.Alloc,
|
||||||
block_size, alignment,
|
block_size, alignment,
|
||||||
nil, 0);
|
nil, 0);
|
||||||
if memory != nil {
|
if memory != nil {
|
||||||
append(&out_band_allocations, (^byte)(memory));
|
append(&out_band_allocations, raw_data(memory));
|
||||||
}
|
}
|
||||||
return memory;
|
return memory, err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes_left < n {
|
if bytes_left < n {
|
||||||
cycle_new_block(pool);
|
err := cycle_new_block(pool);
|
||||||
|
if err != nil {
|
||||||
|
return nil, err;
|
||||||
|
}
|
||||||
if current_block == nil {
|
if current_block == nil {
|
||||||
return nil;
|
return nil, .Out_Of_Memory;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
memory := current_pos;
|
memory := current_pos;
|
||||||
current_pos = ptr_offset((^byte)(current_pos), n);
|
current_pos = ptr_offset((^byte)(current_pos), n);
|
||||||
bytes_left -= n;
|
bytes_left -= n;
|
||||||
return memory;
|
return byte_slice(memory, bytes), nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -730,7 +760,7 @@ dynamic_pool_free_all :: proc(using pool: ^Dynamic_Pool) {
|
|||||||
|
|
||||||
panic_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
panic_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int,loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
case .Alloc:
|
case .Alloc:
|
||||||
@@ -753,13 +783,13 @@ panic_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Query_Features};
|
set^ = {.Query_Features};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
panic_allocator :: proc() -> Allocator {
|
panic_allocator :: proc() -> Allocator {
|
||||||
@@ -770,70 +800,12 @@ panic_allocator :: proc() -> Allocator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
alloca_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|
||||||
size, alignment: int,
|
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
|
||||||
switch mode {
|
|
||||||
case .Alloc:
|
|
||||||
switch alignment {
|
|
||||||
case: return intrinsics.alloca(size, 2*align_of(uintptr));
|
|
||||||
case 0: return intrinsics.alloca(size, 0);
|
|
||||||
|
|
||||||
case 1: return intrinsics.alloca(size, 1);
|
|
||||||
case 2: return intrinsics.alloca(size, 2);
|
|
||||||
case 4: return intrinsics.alloca(size, 4);
|
|
||||||
case 8: return intrinsics.alloca(size, 8);
|
|
||||||
case 16: return intrinsics.alloca(size, 16);
|
|
||||||
case 32: return intrinsics.alloca(size, 32);
|
|
||||||
case 64: return intrinsics.alloca(size, 64);
|
|
||||||
case 128: return intrinsics.alloca(size, 128);
|
|
||||||
case 256: return intrinsics.alloca(size, 256);
|
|
||||||
case 512: return intrinsics.alloca(size, 512);
|
|
||||||
case 1024: return intrinsics.alloca(size, 1024);
|
|
||||||
case 2048: return intrinsics.alloca(size, 2048);
|
|
||||||
case 4096: return intrinsics.alloca(size, 4096);
|
|
||||||
case 8192: return intrinsics.alloca(size, 8192);
|
|
||||||
case 16384: return intrinsics.alloca(size, 16384);
|
|
||||||
case 32768: return intrinsics.alloca(size, 32768);
|
|
||||||
case 65536: return intrinsics.alloca(size, 65536);
|
|
||||||
}
|
|
||||||
case .Resize:
|
|
||||||
return default_resize_align(old_memory, old_size, size, alignment, alloca_allocator());
|
|
||||||
|
|
||||||
case .Free:
|
|
||||||
// Do nothing
|
|
||||||
case .Free_All:
|
|
||||||
// Do nothing
|
|
||||||
|
|
||||||
case .Query_Features:
|
|
||||||
set := (^Allocator_Mode_Set)(old_memory);
|
|
||||||
if set != nil {
|
|
||||||
set^ = {.Alloc, .Resize, .Query_Features};
|
|
||||||
}
|
|
||||||
return set;
|
|
||||||
|
|
||||||
case .Query_Info:
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
alloca_allocator :: proc() -> Allocator {
|
|
||||||
return Allocator{
|
|
||||||
procedure = alloca_allocator_proc,
|
|
||||||
data = nil,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Tracking_Allocator_Entry :: struct {
|
Tracking_Allocator_Entry :: struct {
|
||||||
memory: rawptr,
|
memory: rawptr,
|
||||||
size: int,
|
size: int,
|
||||||
alignment: int,
|
alignment: int,
|
||||||
location: runtime.Source_Code_Location,
|
err: Allocator_Error,
|
||||||
|
location: runtime.Source_Code_Location,
|
||||||
}
|
}
|
||||||
Tracking_Allocator_Bad_Free_Entry :: struct {
|
Tracking_Allocator_Bad_Free_Entry :: struct {
|
||||||
memory: rawptr,
|
memory: rawptr,
|
||||||
@@ -864,7 +836,9 @@ tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
|
size, alignment: int,
|
||||||
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
data := (^Tracking_Allocator)(allocator_data);
|
data := (^Tracking_Allocator)(allocator_data);
|
||||||
if mode == .Query_Info {
|
if mode == .Query_Info {
|
||||||
info := (^Allocator_Query_Info)(old_memory);
|
info := (^Allocator_Query_Info)(old_memory);
|
||||||
@@ -872,23 +846,27 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, si
|
|||||||
if entry, ok := data.allocation_map[info.pointer]; ok {
|
if entry, ok := data.allocation_map[info.pointer]; ok {
|
||||||
info.size = entry.size;
|
info.size = entry.size;
|
||||||
info.alignment = entry.alignment;
|
info.alignment = entry.alignment;
|
||||||
return info;
|
|
||||||
}
|
}
|
||||||
info.pointer = nil;
|
info.pointer = nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
result: rawptr;
|
result: []byte;
|
||||||
|
err: Allocator_Error;
|
||||||
if mode == .Free && old_memory not_in data.allocation_map {
|
if mode == .Free && old_memory not_in data.allocation_map {
|
||||||
append(&data.bad_free_array, Tracking_Allocator_Bad_Free_Entry{
|
append(&data.bad_free_array, Tracking_Allocator_Bad_Free_Entry{
|
||||||
memory = old_memory,
|
memory = old_memory,
|
||||||
location = loc,
|
location = loc,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
result = data.backing.procedure(data.backing.data, mode, size, alignment, old_memory, old_size, flags, loc);
|
result, err = data.backing.procedure(data.backing.data, mode, size, alignment, old_memory, old_size, loc);
|
||||||
|
if err != nil {
|
||||||
|
return result, err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
result_ptr := raw_data(result);
|
||||||
|
|
||||||
if data.allocation_map.allocator.procedure == nil {
|
if data.allocation_map.allocator.procedure == nil {
|
||||||
data.allocation_map.allocator = context.allocator;
|
data.allocation_map.allocator = context.allocator;
|
||||||
@@ -896,22 +874,24 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, si
|
|||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
case .Alloc:
|
case .Alloc:
|
||||||
data.allocation_map[result] = Tracking_Allocator_Entry{
|
data.allocation_map[result_ptr] = Tracking_Allocator_Entry{
|
||||||
memory = result,
|
memory = result_ptr,
|
||||||
size = size,
|
size = size,
|
||||||
alignment = alignment,
|
alignment = alignment,
|
||||||
|
err = err,
|
||||||
location = loc,
|
location = loc,
|
||||||
};
|
};
|
||||||
case .Free:
|
case .Free:
|
||||||
delete_key(&data.allocation_map, old_memory);
|
delete_key(&data.allocation_map, old_memory);
|
||||||
case .Resize:
|
case .Resize:
|
||||||
if old_memory != result {
|
if old_memory != result_ptr {
|
||||||
delete_key(&data.allocation_map, old_memory);
|
delete_key(&data.allocation_map, old_memory);
|
||||||
}
|
}
|
||||||
data.allocation_map[result] = Tracking_Allocator_Entry{
|
data.allocation_map[result_ptr] = Tracking_Allocator_Entry{
|
||||||
memory = result,
|
memory = result_ptr,
|
||||||
size = size,
|
size = size,
|
||||||
alignment = alignment,
|
alignment = alignment,
|
||||||
|
err = err,
|
||||||
location = loc,
|
location = loc,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -925,13 +905,13 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, si
|
|||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features, .Query_Info};
|
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features, .Query_Info};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
unreachable();
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result, err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1021,13 +1001,13 @@ small_allocator :: proc(s: ^$S/Small_Allocator, backing := context.allocator) ->
|
|||||||
|
|
||||||
|
|
||||||
case .Query_Features:
|
case .Query_Features:
|
||||||
return nil;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, nil;
|
||||||
};
|
};
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-3
@@ -139,9 +139,11 @@ slice_ptr :: proc(ptr: ^$T, len: int) -> []T {
|
|||||||
return transmute([]T)Raw_Slice{data = ptr, len = len};
|
return transmute([]T)Raw_Slice{data = ptr, len = len};
|
||||||
}
|
}
|
||||||
|
|
||||||
slice_ptr_to_bytes :: proc(ptr: rawptr, len: int) -> []byte {
|
byte_slice :: slice_ptr_to_bytes;
|
||||||
assert(len >= 0);
|
slice_ptr_to_bytes :: #force_inline proc "contextless" (data: rawptr, len: int) -> (res: []byte) {
|
||||||
return transmute([]byte)Raw_Slice{data = ptr, len = len};
|
r := (^Raw_Slice)(&res);
|
||||||
|
r.data, r.len = data, min(len, 0);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
slice_to_bytes :: proc(slice: $E/[]$T) -> []byte {
|
slice_to_bytes :: proc(slice: $E/[]$T) -> []byte {
|
||||||
|
|||||||
+14
-9
@@ -133,7 +133,12 @@ read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) {
|
|||||||
|
|
||||||
heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, mem.Allocator_Error) {
|
||||||
|
byte_slice :: #force_inline proc "contextless" (data: rawptr, len: int) -> (res: []byte) {
|
||||||
|
r := (^mem.Raw_Slice)(&res);
|
||||||
|
r.data, r.len = data, len;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
|
// NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
|
||||||
@@ -142,7 +147,7 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
|||||||
// the pointer we return to the user.
|
// the pointer we return to the user.
|
||||||
//
|
//
|
||||||
|
|
||||||
aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> rawptr {
|
aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, mem.Allocator_Error) {
|
||||||
a := max(alignment, align_of(rawptr));
|
a := max(alignment, align_of(rawptr));
|
||||||
space := size + a - 1;
|
space := size + a - 1;
|
||||||
|
|
||||||
@@ -159,13 +164,13 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
|||||||
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a);
|
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a);
|
||||||
diff := int(aligned_ptr - ptr);
|
diff := int(aligned_ptr - ptr);
|
||||||
if (size + diff) > space {
|
if (size + diff) > space {
|
||||||
return nil;
|
return nil, .Out_Of_Memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
aligned_mem = rawptr(aligned_ptr);
|
aligned_mem = rawptr(aligned_ptr);
|
||||||
mem.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem;
|
mem.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem;
|
||||||
|
|
||||||
return aligned_mem;
|
return byte_slice(aligned_mem, size), .None;
|
||||||
}
|
}
|
||||||
|
|
||||||
aligned_free :: proc(p: rawptr) {
|
aligned_free :: proc(p: rawptr) {
|
||||||
@@ -174,9 +179,9 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> rawptr {
|
aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, mem.Allocator_Error) {
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
return aligned_alloc(new_size, new_alignment, p);
|
return aligned_alloc(new_size, new_alignment, p);
|
||||||
}
|
}
|
||||||
@@ -202,13 +207,13 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
|||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free, .Resize, .Query_Features};
|
set^ = {.Alloc, .Free, .Resize, .Query_Features};
|
||||||
}
|
}
|
||||||
return set;
|
return byte_slice(set, size_of(set^)), .None;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
heap_allocator :: proc() -> mem.Allocator {
|
heap_allocator :: proc() -> mem.Allocator {
|
||||||
|
|||||||
@@ -252,7 +252,6 @@ Source_Code_Location :: struct {
|
|||||||
|
|
||||||
Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location);
|
Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_Location);
|
||||||
|
|
||||||
|
|
||||||
// Allocation Stuff
|
// Allocation Stuff
|
||||||
Allocator_Mode :: enum byte {
|
Allocator_Mode :: enum byte {
|
||||||
Alloc,
|
Alloc,
|
||||||
@@ -271,9 +270,16 @@ Allocator_Query_Info :: struct {
|
|||||||
alignment: Maybe(int),
|
alignment: Maybe(int),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Allocator_Error :: enum byte {
|
||||||
|
None = 0,
|
||||||
|
Out_Of_Memory = 1,
|
||||||
|
Invalid_Pointer = 2,
|
||||||
|
}
|
||||||
|
|
||||||
Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
|
Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, location: Source_Code_Location = #caller_location) -> rawptr;
|
old_memory: rawptr, old_size: int,
|
||||||
|
location: Source_Code_Location = #caller_location) -> ([]byte, Allocator_Error);
|
||||||
Allocator :: struct {
|
Allocator :: struct {
|
||||||
procedure: Allocator_Proc,
|
procedure: Allocator_Proc,
|
||||||
data: rawptr,
|
data: rawptr,
|
||||||
|
|||||||
@@ -180,12 +180,14 @@ DEFAULT_RESERVE_CAPACITY :: 16;
|
|||||||
|
|
||||||
make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
|
make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
make_slice_error_loc(loc, len);
|
make_slice_error_loc(loc, len);
|
||||||
data := mem_alloc(size_of(E)*len, alignment, allocator, loc);
|
data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc);
|
||||||
if data == nil && size_of(E) != 0 {
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return nil;
|
||||||
|
case data == nil && size_of(E) != 0:
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
// mem_zero(data, size_of(E)*len);
|
s := Raw_Slice{raw_data(data), len};
|
||||||
s := Raw_Slice{data, len};
|
|
||||||
return transmute(T)s;
|
return transmute(T)s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -449,15 +451,15 @@ reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #cal
|
|||||||
new_size := capacity * size_of(E);
|
new_size := capacity * size_of(E);
|
||||||
allocator := a.allocator;
|
allocator := a.allocator;
|
||||||
|
|
||||||
new_data := allocator.procedure(
|
new_data, err := allocator.procedure(
|
||||||
allocator.data, .Resize, new_size, align_of(E),
|
allocator.data, .Resize, new_size, align_of(E),
|
||||||
a.data, old_size, 0, loc,
|
a.data, old_size, loc,
|
||||||
);
|
);
|
||||||
if new_data == nil {
|
if new_data == nil || err != nil {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
a.data = new_data;
|
a.data = raw_data(new_data);
|
||||||
a.cap = capacity;
|
a.cap = capacity;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -483,15 +485,15 @@ resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller
|
|||||||
new_size := length * size_of(E);
|
new_size := length * size_of(E);
|
||||||
allocator := a.allocator;
|
allocator := a.allocator;
|
||||||
|
|
||||||
new_data := allocator.procedure(
|
new_data, err := allocator.procedure(
|
||||||
allocator.data, .Resize, new_size, align_of(E),
|
allocator.data, .Resize, new_size, align_of(E),
|
||||||
a.data, old_size, 0, loc,
|
a.data, old_size, loc,
|
||||||
);
|
);
|
||||||
if new_data == nil {
|
if new_data == nil || err != nil {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
a.data = new_data;
|
a.data = raw_data(new_data);
|
||||||
a.len = length;
|
a.len = length;
|
||||||
a.cap = length;
|
a.cap = length;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -226,13 +226,14 @@ reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, loc := #caller_lo
|
|||||||
|
|
||||||
old_data := (^rawptr)(array)^;
|
old_data := (^rawptr)(array)^;
|
||||||
|
|
||||||
new_data := array.allocator.procedure(
|
new_bytes, err := array.allocator.procedure(
|
||||||
array.allocator.data, .Alloc, new_size, max_align,
|
array.allocator.data, .Alloc, new_size, max_align,
|
||||||
nil, old_size, 0, loc,
|
nil, old_size, loc,
|
||||||
);
|
);
|
||||||
if new_data == nil {
|
if new_bytes == nil || err != nil {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
new_data := raw_data(new_bytes);
|
||||||
|
|
||||||
|
|
||||||
footer.cap = capacity;
|
footer.cap = capacity;
|
||||||
@@ -256,9 +257,9 @@ reserve_soa :: proc(array: ^$T/#soa[dynamic]$E, capacity: int, loc := #caller_lo
|
|||||||
new_offset += type.size * capacity;
|
new_offset += type.size * capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
array.allocator.procedure(
|
_, err = array.allocator.procedure(
|
||||||
array.allocator.data, .Free, 0, max_align,
|
array.allocator.data, .Free, 0, max_align,
|
||||||
old_data, old_size, 0, loc,
|
old_data, old_size, loc,
|
||||||
);
|
);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR || ODIN_OS == "freestanding" {
|
|||||||
|
|
||||||
default_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
default_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
return nil;
|
return nil, .None;
|
||||||
}
|
}
|
||||||
|
|
||||||
default_allocator :: proc() -> Allocator {
|
default_allocator :: proc() -> Allocator {
|
||||||
@@ -26,6 +26,13 @@ when ODIN_DEFAULT_TO_NIL_ALLOCATOR || ODIN_OS == "freestanding" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@(private)
|
||||||
|
byte_slice :: #force_inline proc "contextless" (data: rawptr, len: int) -> (res: []byte) {
|
||||||
|
r := (^Raw_Slice)(&res);
|
||||||
|
r.data, r.len = data, len;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE: int : #config(DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, 1<<22);
|
DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE: int : #config(DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, 1<<22);
|
||||||
|
|
||||||
@@ -35,7 +42,7 @@ Default_Temp_Allocator :: struct {
|
|||||||
curr_offset: int,
|
curr_offset: int,
|
||||||
prev_allocation: rawptr,
|
prev_allocation: rawptr,
|
||||||
backup_allocator: Allocator,
|
backup_allocator: Allocator,
|
||||||
leaked_allocations: [dynamic]rawptr,
|
leaked_allocations: [dynamic][]byte,
|
||||||
}
|
}
|
||||||
|
|
||||||
default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backup_allocator := context.allocator) {
|
default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backup_allocator := context.allocator) {
|
||||||
@@ -51,7 +58,7 @@ default_temp_allocator_destroy :: proc(s: ^Default_Temp_Allocator) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for ptr in s.leaked_allocations {
|
for ptr in s.leaked_allocations {
|
||||||
free(ptr, s.backup_allocator);
|
free(raw_data(ptr), s.backup_allocator);
|
||||||
}
|
}
|
||||||
delete(s.leaked_allocations);
|
delete(s.leaked_allocations);
|
||||||
delete(s.data, s.backup_allocator);
|
delete(s.data, s.backup_allocator);
|
||||||
@@ -60,7 +67,7 @@ default_temp_allocator_destroy :: proc(s: ^Default_Temp_Allocator) {
|
|||||||
|
|
||||||
default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
|
|
||||||
s := (^Default_Temp_Allocator)(allocator_data);
|
s := (^Default_Temp_Allocator)(allocator_data);
|
||||||
|
|
||||||
@@ -84,7 +91,7 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
|
|||||||
s.prev_allocation = rawptr(ptr);
|
s.prev_allocation = rawptr(ptr);
|
||||||
offset := int(ptr - start);
|
offset := int(ptr - start);
|
||||||
s.curr_offset = offset + size;
|
s.curr_offset = offset + size;
|
||||||
return rawptr(ptr);
|
return byte_slice(rawptr(ptr), size), .None;
|
||||||
|
|
||||||
case size <= len(s.data):
|
case size <= len(s.data):
|
||||||
start := uintptr(raw_data(s.data));
|
start := uintptr(raw_data(s.data));
|
||||||
@@ -94,7 +101,7 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
|
|||||||
s.prev_allocation = rawptr(ptr);
|
s.prev_allocation = rawptr(ptr);
|
||||||
offset := int(ptr - start);
|
offset := int(ptr - start);
|
||||||
s.curr_offset = offset + size;
|
s.curr_offset = offset + size;
|
||||||
return rawptr(ptr);
|
return byte_slice(rawptr(ptr), size), .None;
|
||||||
}
|
}
|
||||||
a := s.backup_allocator;
|
a := s.backup_allocator;
|
||||||
if a.procedure == nil {
|
if a.procedure == nil {
|
||||||
@@ -102,11 +109,14 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
|
|||||||
s.backup_allocator = a;
|
s.backup_allocator = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr := mem_alloc(size, alignment, a, loc);
|
data, err := mem_alloc_bytes(size, alignment, a, loc);
|
||||||
if s.leaked_allocations == nil {
|
if err != nil {
|
||||||
s.leaked_allocations = make([dynamic]rawptr, a);
|
return data, err;
|
||||||
}
|
}
|
||||||
append(&s.leaked_allocations, ptr);
|
if s.leaked_allocations == nil {
|
||||||
|
s.leaked_allocations = make([dynamic][]byte, a);
|
||||||
|
}
|
||||||
|
append(&s.leaked_allocations, data);
|
||||||
|
|
||||||
// TODO(bill): Should leaks be notified about?
|
// TODO(bill): Should leaks be notified about?
|
||||||
if logger := context.logger; logger.lowest_level <= .Warning {
|
if logger := context.logger; logger.lowest_level <= .Warning {
|
||||||
@@ -115,11 +125,11 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ptr;
|
return data, .None;
|
||||||
|
|
||||||
case .Free:
|
case .Free:
|
||||||
if old_memory == nil {
|
if old_memory == nil {
|
||||||
return nil;
|
return nil, .None;
|
||||||
}
|
}
|
||||||
|
|
||||||
start := uintptr(raw_data(s.data));
|
start := uintptr(raw_data(s.data));
|
||||||
@@ -129,30 +139,32 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
|
|||||||
if s.prev_allocation == old_memory {
|
if s.prev_allocation == old_memory {
|
||||||
s.curr_offset = int(uintptr(s.prev_allocation) - start);
|
s.curr_offset = int(uintptr(s.prev_allocation) - start);
|
||||||
s.prev_allocation = nil;
|
s.prev_allocation = nil;
|
||||||
return nil;
|
return nil, .None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if start <= old_ptr && old_ptr < end {
|
if start <= old_ptr && old_ptr < end {
|
||||||
// NOTE(bill): Cannot free this pointer but it is valid
|
// NOTE(bill): Cannot free this pointer but it is valid
|
||||||
return nil;
|
return nil, .None;
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(s.leaked_allocations) != 0 {
|
if len(s.leaked_allocations) != 0 {
|
||||||
for ptr, i in s.leaked_allocations {
|
for data, i in s.leaked_allocations {
|
||||||
|
ptr := raw_data(data);
|
||||||
if ptr == old_memory {
|
if ptr == old_memory {
|
||||||
free(ptr, s.backup_allocator);
|
free(ptr, s.backup_allocator);
|
||||||
ordered_remove(&s.leaked_allocations, i);
|
ordered_remove(&s.leaked_allocations, i);
|
||||||
return nil;
|
return nil, .None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic("invalid pointer passed to default_temp_allocator");
|
return nil, .Invalid_Pointer;
|
||||||
|
// panic("invalid pointer passed to default_temp_allocator");
|
||||||
|
|
||||||
case .Free_All:
|
case .Free_All:
|
||||||
s.curr_offset = 0;
|
s.curr_offset = 0;
|
||||||
s.prev_allocation = nil;
|
s.prev_allocation = nil;
|
||||||
for ptr in s.leaked_allocations {
|
for data in s.leaked_allocations {
|
||||||
free(ptr, s.backup_allocator);
|
free(raw_data(data), s.backup_allocator);
|
||||||
}
|
}
|
||||||
clear(&s.leaked_allocations);
|
clear(&s.leaked_allocations);
|
||||||
|
|
||||||
@@ -163,26 +175,28 @@ default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode
|
|||||||
if old_memory == s.prev_allocation && old_ptr & uintptr(alignment)-1 == 0 {
|
if old_memory == s.prev_allocation && old_ptr & uintptr(alignment)-1 == 0 {
|
||||||
if old_ptr+uintptr(size) < end {
|
if old_ptr+uintptr(size) < end {
|
||||||
s.curr_offset = int(old_ptr-begin)+size;
|
s.curr_offset = int(old_ptr-begin)+size;
|
||||||
return old_memory;
|
return byte_slice(old_memory, size), .None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ptr := default_temp_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, flags, loc);
|
ptr, err := default_temp_allocator_proc(allocator_data, .Alloc, size, alignment, old_memory, old_size, loc);
|
||||||
mem_copy(ptr, old_memory, old_size);
|
if err == .None {
|
||||||
default_temp_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, flags, loc);
|
copy(ptr, byte_slice(old_memory, old_size));
|
||||||
return ptr;
|
_, err = default_temp_allocator_proc(allocator_data, .Free, 0, alignment, old_memory, old_size, loc);
|
||||||
|
}
|
||||||
|
return ptr, err;
|
||||||
|
|
||||||
case .Query_Features:
|
case .Query_Features:
|
||||||
set := (^Allocator_Mode_Set)(old_memory);
|
set := (^Allocator_Mode_Set)(old_memory);
|
||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
|
set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, .None;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, .None;
|
||||||
}
|
}
|
||||||
|
|
||||||
default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator {
|
default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator {
|
||||||
|
|||||||
@@ -29,10 +29,13 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap:
|
|||||||
new_size := cap * elem_size;
|
new_size := cap * elem_size;
|
||||||
allocator := array.allocator;
|
allocator := array.allocator;
|
||||||
|
|
||||||
new_data := allocator.procedure(allocator.data, .Resize, new_size, elem_align, array.data, old_size, 0, loc);
|
new_data, err := allocator.procedure(allocator.data, .Resize, new_size, elem_align, array.data, old_size, loc);
|
||||||
|
if err != nil {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if new_data != nil || elem_size == 0 {
|
if new_data != nil || elem_size == 0 {
|
||||||
array.data = new_data;
|
array.data = raw_data(new_data);
|
||||||
array.cap = cap;
|
array.cap = min(cap, len(new_data)/elem_size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -173,8 +173,8 @@ __slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: Allocator, l
|
|||||||
old_size := array.len*size_of(T);
|
old_size := array.len*size_of(T);
|
||||||
new_size := new_count*size_of(T);
|
new_size := new_count*size_of(T);
|
||||||
|
|
||||||
new_data := mem_resize(array.data, old_size, new_size, align_of(T), allocator, loc);
|
new_data, err := mem_resize(array.data, old_size, new_size, align_of(T), allocator, loc);
|
||||||
if new_data == nil {
|
if new_data == nil || err != nil {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
array.data = new_data;
|
array.data = new_data;
|
||||||
|
|||||||
+30
-13
@@ -159,6 +159,16 @@ mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> r
|
|||||||
|
|
||||||
DEFAULT_ALIGNMENT :: 2*align_of(rawptr);
|
DEFAULT_ALIGNMENT :: 2*align_of(rawptr);
|
||||||
|
|
||||||
|
mem_alloc_bytes :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
|
if size == 0 {
|
||||||
|
return nil, nil;
|
||||||
|
}
|
||||||
|
if allocator.procedure == nil {
|
||||||
|
return nil, nil;
|
||||||
|
}
|
||||||
|
return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc);
|
||||||
|
}
|
||||||
|
|
||||||
mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
return nil;
|
return nil;
|
||||||
@@ -166,36 +176,43 @@ mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, a
|
|||||||
if allocator.procedure == nil {
|
if allocator.procedure == nil {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, 0, loc);
|
data, err := allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc);
|
||||||
|
_ = err;
|
||||||
|
return raw_data(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) {
|
mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
|
||||||
if ptr == nil {
|
if ptr == nil {
|
||||||
return;
|
return .None;
|
||||||
}
|
}
|
||||||
if allocator.procedure == nil {
|
if allocator.procedure == nil {
|
||||||
return;
|
return .None;
|
||||||
}
|
}
|
||||||
allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, 0, loc);
|
_, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc);
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
mem_free_all :: #force_inline proc(allocator := context.allocator, loc := #caller_location) {
|
mem_free_all :: #force_inline proc(allocator := context.allocator, loc := #caller_location) -> (err: Allocator_Error) {
|
||||||
if allocator.procedure != nil {
|
if allocator.procedure != nil {
|
||||||
allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, 0, loc);
|
_, err = allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, loc);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mem_resize :: #force_inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
|
mem_resize :: #force_inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (new_ptr: rawptr, err: Allocator_Error) {
|
||||||
|
new_data: []byte;
|
||||||
switch {
|
switch {
|
||||||
case allocator.procedure == nil:
|
case allocator.procedure == nil:
|
||||||
return nil;
|
return;
|
||||||
case new_size == 0:
|
case new_size == 0:
|
||||||
allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, 0, loc);
|
new_data, err = allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc);
|
||||||
return nil;
|
|
||||||
case ptr == nil:
|
case ptr == nil:
|
||||||
return allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, 0, loc);
|
new_data, err = allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc);
|
||||||
|
case:
|
||||||
|
new_data, err = allocator.procedure(allocator.data, .Resize, new_size, alignment, ptr, old_size, loc);
|
||||||
}
|
}
|
||||||
return allocator.procedure(allocator.data, .Resize, new_size, alignment, ptr, old_size, 0, loc);
|
new_ptr = raw_data(new_data);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
memory_equal :: proc "contextless" (a, b: rawptr, n: int) -> bool {
|
memory_equal :: proc "contextless" (a, b: rawptr, n: int) -> bool {
|
||||||
return memory_compare(a, b, n) == 0;
|
return memory_compare(a, b, n) == 0;
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ heap_free :: proc "contextless" (ptr: rawptr) {
|
|||||||
|
|
||||||
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||||
size, alignment: int,
|
size, alignment: int,
|
||||||
old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
|
||||||
|
|
||||||
//
|
//
|
||||||
// NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
|
// NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
|
||||||
@@ -97,7 +97,7 @@ default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
// the pointer we return to the user.
|
// the pointer we return to the user.
|
||||||
//
|
//
|
||||||
|
|
||||||
aligned_alloc :: proc "contextless" (size, alignment: int, old_ptr: rawptr = nil) -> rawptr {
|
aligned_alloc :: proc "contextless" (size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, Allocator_Error) {
|
||||||
a := max(alignment, align_of(rawptr));
|
a := max(alignment, align_of(rawptr));
|
||||||
space := size + a - 1;
|
space := size + a - 1;
|
||||||
|
|
||||||
@@ -114,13 +114,13 @@ default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a);
|
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a);
|
||||||
diff := int(aligned_ptr - ptr);
|
diff := int(aligned_ptr - ptr);
|
||||||
if (size + diff) > space {
|
if (size + diff) > space {
|
||||||
return nil;
|
return nil, .Out_Of_Memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
aligned_mem = rawptr(aligned_ptr);
|
aligned_mem = rawptr(aligned_ptr);
|
||||||
ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem;
|
ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem;
|
||||||
|
|
||||||
return aligned_mem;
|
return byte_slice(aligned_mem, size), nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
aligned_free :: proc "contextless" (p: rawptr) {
|
aligned_free :: proc "contextless" (p: rawptr) {
|
||||||
@@ -129,9 +129,9 @@ default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
aligned_resize :: proc "contextless" (p: rawptr, old_size: int, new_size: int, new_alignment: int) -> rawptr {
|
aligned_resize :: proc "contextless" (p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, Allocator_Error) {
|
||||||
if p == nil {
|
if p == nil {
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
return aligned_alloc(new_size, new_alignment, p);
|
return aligned_alloc(new_size, new_alignment, p);
|
||||||
}
|
}
|
||||||
@@ -157,13 +157,13 @@ default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|||||||
if set != nil {
|
if set != nil {
|
||||||
set^ = {.Alloc, .Free, .Resize, .Query_Features};
|
set^ = {.Alloc, .Free, .Resize, .Query_Features};
|
||||||
}
|
}
|
||||||
return set;
|
return nil, nil;
|
||||||
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil, nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
default_allocator :: proc() -> Allocator {
|
default_allocator :: proc() -> Allocator {
|
||||||
|
|||||||
@@ -838,7 +838,7 @@ void init_build_context(TargetMetrics *cross_target) {
|
|||||||
bc->link_flags = str_lit("-arch arm64 ");
|
bc->link_flags = str_lit("-arch arm64 ");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!bc->use_llvm_api) {
|
if ((bc->command_kind & Command__does_build) != 0 && !bc->use_llvm_api) {
|
||||||
gb_printf_err("The arm64 architecture is only supported with -llvm-api\n");;
|
gb_printf_err("The arm64 architecture is only supported with -llvm-api\n");;
|
||||||
gb_exit(1);
|
gb_exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user