mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-28 10:20:05 +00:00
Remove special shared scope for runtime stuff
This commit is contained in:
+22
-37
@@ -1,7 +1,24 @@
|
||||
package mem
|
||||
|
||||
import "core:raw"
|
||||
import "core:os"
|
||||
DEFAULT_ALIGNMENT :: 2*align_of(rawptr);
|
||||
|
||||
Allocator_Mode :: enum byte {
|
||||
Alloc,
|
||||
Free,
|
||||
Free_All,
|
||||
Resize,
|
||||
}
|
||||
|
||||
Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64 = 0, location := #caller_location) -> rawptr;
|
||||
|
||||
|
||||
Allocator :: struct {
|
||||
procedure: Allocator_Proc,
|
||||
data: rawptr,
|
||||
}
|
||||
|
||||
|
||||
|
||||
alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
|
||||
@@ -30,16 +47,16 @@ resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEF
|
||||
|
||||
|
||||
free_string :: proc(str: string, loc := #caller_location) {
|
||||
free_ptr(raw.data(str), loc);
|
||||
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_ptr(raw_data(array), loc);
|
||||
}
|
||||
free_slice :: proc(array: $T/[]$E, loc := #caller_location) {
|
||||
free_ptr(raw.data(array), loc);
|
||||
free_ptr(raw_data(array), loc);
|
||||
}
|
||||
free_map :: proc(m: $T/map[$K]$V, loc := #caller_location) {
|
||||
raw := transmute(raw.Map)m;
|
||||
@@ -78,38 +95,6 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment:
|
||||
}
|
||||
|
||||
|
||||
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 {
|
||||
|
||||
+13
-13
@@ -1,6 +1,6 @@
|
||||
package mem
|
||||
|
||||
import "core:raw"
|
||||
import "core:runtime"
|
||||
|
||||
foreign _ {
|
||||
@(link_name = "llvm.bswap.i16") swap16 :: proc(b: u16) -> u16 ---;
|
||||
@@ -80,20 +80,20 @@ ptr_sub :: proc "contextless" (a, b: $P/^$T) -> int {
|
||||
|
||||
slice_ptr :: proc "contextless" (ptr: ^$T, len: int) -> []T {
|
||||
assert(len >= 0);
|
||||
slice := raw.Slice{data = ptr, len = len};
|
||||
slice := Raw_Slice{data = ptr, len = len};
|
||||
return transmute([]T)slice;
|
||||
}
|
||||
|
||||
slice_to_bytes :: proc "contextless" (slice: $E/[]$T) -> []byte {
|
||||
s := transmute(raw.Slice)slice;
|
||||
s := transmute(Raw_Slice)slice;
|
||||
s.len *= size_of(T);
|
||||
return transmute([]byte)s;
|
||||
}
|
||||
|
||||
|
||||
buffer_from_slice :: proc(backing: $T/[]$E) -> [dynamic]E {
|
||||
s := transmute(raw.Slice)backing;
|
||||
d := raw.Dynamic_Array{
|
||||
s := transmute(Raw_Slice)backing;
|
||||
d := Raw_Dynamic_Array{
|
||||
data = s.data,
|
||||
len = 0,
|
||||
cap = s.len,
|
||||
@@ -104,13 +104,13 @@ buffer_from_slice :: proc(backing: $T/[]$E) -> [dynamic]E {
|
||||
|
||||
ptr_to_bytes :: proc "contextless" (ptr: ^$T, len := 1) -> []byte {
|
||||
assert(len >= 0);
|
||||
return transmute([]byte)raw.Slice{ptr, len*size_of(T)};
|
||||
return transmute([]byte)Raw_Slice{ptr, len*size_of(T)};
|
||||
}
|
||||
|
||||
any_to_bytes :: proc "contextless" (val: any) -> []byte {
|
||||
ti := type_info_of(val.typeid);
|
||||
size := ti != nil ? ti.size : 0;
|
||||
return transmute([]byte)raw.Slice{val.data, size};
|
||||
return transmute([]byte)Raw_Slice{val.data, size};
|
||||
}
|
||||
|
||||
|
||||
@@ -158,8 +158,8 @@ allocation_header :: proc(data: rawptr) -> ^AllocationHeader {
|
||||
Fixed_Byte_Buffer :: distinct [dynamic]byte;
|
||||
|
||||
make_fixed_byte_buffer :: proc(backing: []byte) -> Fixed_Byte_Buffer {
|
||||
s := transmute(raw.Slice)backing;
|
||||
d: raw.Dynamic_Array;
|
||||
s := transmute(Raw_Slice)backing;
|
||||
d: Raw_Dynamic_Array;
|
||||
d.data = s.data;
|
||||
d.len = 0;
|
||||
d.cap = s.len;
|
||||
@@ -199,7 +199,7 @@ init_arena_from_context :: proc(using a: ^Arena, size: int) {
|
||||
}
|
||||
|
||||
|
||||
context_from_allocator :: proc(a: Allocator) -> Context {
|
||||
context_from_allocator :: proc(a: Allocator) -> runtime.Context {
|
||||
c := context;
|
||||
c.allocator = a;
|
||||
return c;
|
||||
@@ -241,7 +241,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||
#no_bounds_check end := &arena.memory[len(arena.memory)];
|
||||
|
||||
ptr := align_forward(end, uintptr(alignment));
|
||||
(^raw.Slice)(&arena.memory).len += total_size;
|
||||
(^Raw_Slice)(&arena.memory).len += total_size;
|
||||
return zero(ptr, size);
|
||||
|
||||
case Free:
|
||||
@@ -249,7 +249,7 @@ arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
||||
// Use ArenaTempMemory if you want to free a block
|
||||
|
||||
case Free_All:
|
||||
(^raw.Slice)(&arena.memory).len = 0;
|
||||
(^Raw_Slice)(&arena.memory).len = 0;
|
||||
|
||||
case Resize:
|
||||
return default_resize_align(old_memory, old_size, size, alignment);
|
||||
@@ -269,7 +269,7 @@ begin_arena_temp_memory :: proc(a: ^Arena) -> ArenaTempMemory {
|
||||
end_arena_temp_memory :: proc(using tmp: ArenaTempMemory) {
|
||||
assert(len(arena.memory) >= original_count);
|
||||
assert(arena.temp_count > 0);
|
||||
(^raw.Dynamic_Array)(&arena.memory).len = original_count;
|
||||
(^Raw_Dynamic_Array)(&arena.memory).len = original_count;
|
||||
arena.temp_count -= 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package mem
|
||||
|
||||
Raw_Any :: struct {
|
||||
data: rawptr,
|
||||
typeid: typeid,
|
||||
}
|
||||
|
||||
Raw_String :: struct {
|
||||
data: ^byte,
|
||||
len: int,
|
||||
}
|
||||
|
||||
Raw_Cstring :: struct {
|
||||
data: ^byte,
|
||||
}
|
||||
|
||||
Raw_Slice :: struct {
|
||||
data: rawptr,
|
||||
len: int,
|
||||
}
|
||||
|
||||
Raw_Dynamic_Array :: struct {
|
||||
data: rawptr,
|
||||
len: int,
|
||||
cap: int,
|
||||
allocator: Allocator,
|
||||
}
|
||||
|
||||
Raw_Map :: struct {
|
||||
hashes: [dynamic]int,
|
||||
entries: Raw_Dynamic_Array,
|
||||
}
|
||||
|
||||
|
||||
make_any :: inline proc(data: rawptr, id: typeid) -> any {
|
||||
return transmute(any)Raw_Any{data, id};
|
||||
}
|
||||
|
||||
raw_string_data :: inline proc(s: $T/string) -> ^byte {
|
||||
return (^Raw_String)(&s).data;
|
||||
}
|
||||
raw_slice_data :: inline proc(a: $T/[]$E) -> ^E {
|
||||
return cast(^E)(^Raw_Slice)(&a).data;
|
||||
}
|
||||
raw_dynamic_array_data :: inline proc(a: $T/[dynamic]$E) -> ^E {
|
||||
return cast(^E)(^Raw_Dynamic_Array)(&a).data;
|
||||
}
|
||||
|
||||
raw_data :: proc[raw_string_data, raw_slice_data, raw_dynamic_array_data];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user