mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
Reorganize runtime package
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package mem
|
||||
|
||||
import "core:raw"
|
||||
import "core:os"
|
||||
|
||||
|
||||
alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, Allocator_Mode.Alloc, size, alignment, nil, 0, 0, loc);
|
||||
}
|
||||
|
||||
free_ptr_with_allocator :: inline proc(a: Allocator, ptr: rawptr, loc := #caller_location) {
|
||||
if ptr == nil do return;
|
||||
if a.procedure == nil do return;
|
||||
a.procedure(a.data, Allocator_Mode.Free, 0, 0, ptr, 0, 0, loc);
|
||||
}
|
||||
|
||||
free_ptr :: inline proc(ptr: rawptr, loc := #caller_location) do free_ptr_with_allocator(context.allocator, ptr);
|
||||
|
||||
free_all :: inline proc(loc := #caller_location) {
|
||||
a := context.allocator;
|
||||
a.procedure(a.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc);
|
||||
}
|
||||
|
||||
|
||||
resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, 0, loc);
|
||||
}
|
||||
|
||||
|
||||
free_string :: proc(str: string, loc := #caller_location) {
|
||||
free_ptr(raw.data(str), loc);
|
||||
}
|
||||
free_cstring :: proc(str: cstring, loc := #caller_location) {
|
||||
free_ptr((^byte)(str), loc);
|
||||
}
|
||||
free_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) {
|
||||
free_ptr(raw.data(array), loc);
|
||||
}
|
||||
free_slice :: proc(array: $T/[]$E, loc := #caller_location) {
|
||||
free_ptr(raw.data(array), loc);
|
||||
}
|
||||
free_map :: proc(m: $T/map[$K]$V, loc := #caller_location) {
|
||||
raw := transmute(raw.Map)m;
|
||||
free_dynamic_array(raw.hashes, loc);
|
||||
free_ptr(raw.entries.data, loc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, loc := #caller_location) -> rawptr {
|
||||
if old_memory == nil do return alloc(new_size, alignment, loc);
|
||||
|
||||
if new_size == 0 {
|
||||
free(old_memory, loc);
|
||||
return nil;
|
||||
}
|
||||
|
||||
if new_size == old_size do return old_memory;
|
||||
|
||||
new_memory := alloc(new_size, alignment, loc);
|
||||
if new_memory == nil do return nil;
|
||||
|
||||
__mem_copy(new_memory, old_memory, min(old_size, new_size));;
|
||||
free(old_memory, loc);
|
||||
return new_memory;
|
||||
}
|
||||
|
||||
|
||||
default_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 {
|
||||
using Allocator_Mode;
|
||||
|
||||
switch mode {
|
||||
case Alloc:
|
||||
return os.heap_alloc(size);
|
||||
|
||||
case Free:
|
||||
os.heap_free(old_memory);
|
||||
return nil;
|
||||
|
||||
case Free_All:
|
||||
// NOTE(bill): Does nothing
|
||||
|
||||
case Resize:
|
||||
ptr := os.heap_resize(old_memory, size);
|
||||
assert(ptr != nil);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
default_allocator :: proc() -> Allocator {
|
||||
return Allocator{
|
||||
procedure = default_allocator_proc,
|
||||
data = nil,
|
||||
};
|
||||
}
|
||||
|
||||
nil_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 {
|
||||
return nil;
|
||||
}
|
||||
|
||||
nil_allocator :: proc() -> Allocator {
|
||||
return Allocator{
|
||||
procedure = nil_allocator_proc,
|
||||
data = nil,
|
||||
};
|
||||
}
|
||||
|
||||
+24
-5
@@ -23,12 +23,19 @@ copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawpt
|
||||
return __mem_copy_non_overlapping(dst, src, len);
|
||||
}
|
||||
compare :: proc "contextless" (a, b: []byte) -> int {
|
||||
return __mem_compare(&a[0], &b[0], min(len(a), len(b)));
|
||||
return compare_byte_ptrs(&a[0], &b[0], min(len(a), len(b)));
|
||||
}
|
||||
compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int {
|
||||
pa :: ptr_offset;
|
||||
for i in 0..uintptr(n) do switch {
|
||||
case pa(a, i)^ < pa(b, i)^: return -1;
|
||||
case pa(a, i)^ > pa(b, i)^: return +1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
ptr_offset :: proc "contextless" (ptr: $P/^$T, n: int) -> P {
|
||||
new := uintptr(ptr) + uintptr(size_of(T)*n);
|
||||
ptr_offset :: proc "contextless" (ptr: $P/^$T, n: uintptr) -> P {
|
||||
new := uintptr(ptr) + size_of(T)*n;
|
||||
return P(new);
|
||||
}
|
||||
|
||||
@@ -48,6 +55,18 @@ slice_to_bytes :: proc "contextless" (slice: $E/[]$T) -> []byte {
|
||||
return transmute([]byte)s;
|
||||
}
|
||||
|
||||
|
||||
buffer_from_slice :: proc(backing: $T/[]$E) -> [dynamic]E {
|
||||
s := transmute(raw.Slice)backing;
|
||||
d := raw.Dynamic_Array{
|
||||
data = s.data,
|
||||
len = 0,
|
||||
cap = s.len,
|
||||
allocator = nil_allocator(),
|
||||
};
|
||||
return transmute([dynamic]E)d;
|
||||
}
|
||||
|
||||
ptr_to_bytes :: proc "contextless" (ptr: ^$T, len := 1) -> []byte {
|
||||
assert(len >= 0);
|
||||
return transmute([]byte)raw.Slice{ptr, len*size_of(T)};
|
||||
@@ -89,7 +108,7 @@ allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: in
|
||||
ptr := cast(^uint)(ptr_offset(header, 1));
|
||||
n := ptr_sub(cast(^uint)data, ptr);
|
||||
|
||||
for i in 0..n {
|
||||
for i in 0..uintptr(n) {
|
||||
ptr_offset(ptr, i)^ = ~uint(0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user