mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Clean up _preload.odin types
This commit is contained in:
+26
-29
@@ -17,12 +17,14 @@ import (
|
||||
// Local Variables: snake_case
|
||||
// Constant Variables: SCREAMING_SNAKE_CASE
|
||||
|
||||
|
||||
|
||||
|
||||
// IMPORTANT NOTE(bill): `type_info` cannot be used within a
|
||||
// #shared_global_scope due to the internals of the compiler.
|
||||
// This could change at a later date if the all these data structures are
|
||||
// implemented within the compiler rather than in this "preload" file
|
||||
|
||||
|
||||
// NOTE(bill): This must match the compiler's
|
||||
CallingConvention :: enum {
|
||||
Invalid = 0,
|
||||
@@ -81,13 +83,7 @@ TypeInfo :: struct #ordered {
|
||||
Struct :: Record;
|
||||
RawUnion :: Record;
|
||||
Union :: struct #ordered {
|
||||
common_fields: struct #ordered {
|
||||
types: []^TypeInfo;
|
||||
names: []string;
|
||||
offsets: []int; // offsets may not be used in tuples
|
||||
};
|
||||
variant_names: []string;
|
||||
variant_types: []^TypeInfo;
|
||||
variants: []^TypeInfo;
|
||||
};
|
||||
Enum :: struct #ordered {
|
||||
base: ^TypeInfo;
|
||||
@@ -145,17 +141,19 @@ __argv__: ^^u8;
|
||||
__argc__: i32;
|
||||
|
||||
// IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)
|
||||
AllocatorMode :: enum u8 {
|
||||
Alloc,
|
||||
Free,
|
||||
FreeAll,
|
||||
Resize,
|
||||
}
|
||||
AllocatorProc :: proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64 = 0) -> rawptr;
|
||||
|
||||
Allocator :: struct #ordered {
|
||||
procedure: AllocatorProc;
|
||||
Mode :: enum u8 {
|
||||
Alloc,
|
||||
Free,
|
||||
FreeAll,
|
||||
Resize,
|
||||
}
|
||||
Proc :: proc(allocator_data: rawptr, mode: Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64 = 0) -> rawptr;
|
||||
|
||||
procedure: Proc;
|
||||
data: rawptr;
|
||||
}
|
||||
|
||||
@@ -283,26 +281,26 @@ __check_context :: proc() {
|
||||
|
||||
alloc :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT) -> rawptr #inline {
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, AllocatorMode.Alloc, size, alignment, nil, 0, 0);
|
||||
return a.procedure(a.data, Allocator.Mode.Alloc, size, alignment, nil, 0, 0);
|
||||
}
|
||||
|
||||
free_ptr_with_allocator :: proc(a: Allocator, ptr: rawptr) #inline {
|
||||
if ptr == nil do return;
|
||||
if a.procedure == nil do return;
|
||||
a.procedure(a.data, AllocatorMode.Free, 0, 0, ptr, 0, 0);
|
||||
a.procedure(a.data, Allocator.Mode.Free, 0, 0, ptr, 0, 0);
|
||||
}
|
||||
|
||||
free_ptr :: proc(ptr: rawptr) #inline do free_ptr_with_allocator(context.allocator, ptr);
|
||||
|
||||
free_all :: proc() #inline {
|
||||
a := context.allocator;
|
||||
a.procedure(a.data, AllocatorMode.FreeAll, 0, 0, nil, 0, 0);
|
||||
a.procedure(a.data, Allocator.Mode.FreeAll, 0, 0, nil, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT) -> rawptr #inline {
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, AllocatorMode.Resize, new_size, alignment, ptr, old_size, 0);
|
||||
return a.procedure(a.data, Allocator.Mode.Resize, new_size, alignment, ptr, old_size, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -403,7 +401,7 @@ reserve :: proc(array: ^[dynamic]$T, capacity: int) -> bool {
|
||||
new_size := capacity * size_of(T);
|
||||
allocator := a.allocator;
|
||||
|
||||
new_data := allocator.procedure(allocator.data, AllocatorMode.Resize, new_size, align_of(T), a.data, old_size, 0);
|
||||
new_data := allocator.procedure(allocator.data, Allocator.Mode.Resize, new_size, align_of(T), a.data, old_size, 0);
|
||||
if new_data == nil do return false;
|
||||
|
||||
a.data = new_data;
|
||||
@@ -503,10 +501,10 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment:
|
||||
}
|
||||
|
||||
|
||||
default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
default_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator.Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using AllocatorMode;
|
||||
using Allocator.Mode;
|
||||
|
||||
match mode {
|
||||
case Alloc:
|
||||
@@ -715,7 +713,7 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap:
|
||||
new_size := cap * elem_size;
|
||||
allocator := array.allocator;
|
||||
|
||||
new_data := allocator.procedure(allocator.data, AllocatorMode.Resize, new_size, elem_align, array.data, old_size, 0);
|
||||
new_data := allocator.procedure(allocator.data, Allocator.Mode.Resize, new_size, elem_align, array.data, old_size, 0);
|
||||
if new_data == nil do return false;
|
||||
|
||||
array.data = new_data;
|
||||
@@ -736,9 +734,8 @@ __dynamic_array_append :: proc(array_: rawptr, elem_size, elem_align: int,
|
||||
items: rawptr, item_count: int) -> int {
|
||||
array := ^raw.DynamicArray(array_);
|
||||
|
||||
if item_count <= 0 || items == nil {
|
||||
return array.len;
|
||||
}
|
||||
if items == nil do return 0;
|
||||
if item_count <= 0 do return 0;
|
||||
|
||||
|
||||
ok := true;
|
||||
|
||||
+3
-42
@@ -295,32 +295,9 @@ write_type :: proc(buf: ^StringBuffer, ti: ^TypeInfo) {
|
||||
|
||||
case Union:
|
||||
write_string(buf, "union {");
|
||||
cf := info.common_fields;
|
||||
total_count := 0;
|
||||
for name, i in cf.names {
|
||||
for variant, i in info.variants {
|
||||
if i > 0 do write_string(buf, ", ");
|
||||
write_string(buf, name);
|
||||
write_string(buf, ": ");
|
||||
write_type(buf, cf.types[i]);
|
||||
total_count++;
|
||||
}
|
||||
for name, i in info.variant_names {
|
||||
if total_count > 0 || i > 0 do write_string(buf, ", ");
|
||||
write_string(buf, name);
|
||||
write_byte(buf, '{');
|
||||
defer write_byte(buf, '}');
|
||||
|
||||
variant_type := type_info_base(info.variant_types[i]).variant;
|
||||
variant := (&variant_type).(Struct);
|
||||
|
||||
vc := len(variant.names)-len(cf.names);
|
||||
for j in 0..vc {
|
||||
if j > 0 do write_string(buf, ", ");
|
||||
index := j + len(cf.names);
|
||||
write_string(buf, variant.names[index]);
|
||||
write_string(buf, ": ");
|
||||
write_type(buf, variant.types[index]);
|
||||
}
|
||||
write_type(buf, variant);
|
||||
}
|
||||
write_string(buf, "}");
|
||||
|
||||
@@ -897,23 +874,7 @@ fmt_value :: proc(fi: ^FmtInfo, v: any, verb: rune) {
|
||||
}
|
||||
|
||||
case Union:
|
||||
write_byte(fi.buf, '{');
|
||||
defer write_byte(fi.buf, '}');
|
||||
|
||||
cf := info.common_fields;
|
||||
for _, i in cf.names {
|
||||
if i > 0 do write_string(fi.buf, ", ");
|
||||
|
||||
write_string(fi.buf, cf.names[i]);
|
||||
write_string(fi.buf, " = ");
|
||||
|
||||
if t := cf.types[i]; types.is_any(t) {
|
||||
write_string(fi.buf, "any{}");
|
||||
} else {
|
||||
data := ^u8(v.data) + cf.offsets[i];
|
||||
fmt_arg(fi, any{rawptr(data), t}, 'v');
|
||||
}
|
||||
}
|
||||
write_string(fi.buf, "(union)");
|
||||
|
||||
case RawUnion:
|
||||
write_string(fi.buf, "(raw_union)");
|
||||
|
||||
+4
-4
@@ -137,10 +137,10 @@ arena_allocator :: proc(arena: ^Arena) -> Allocator {
|
||||
};
|
||||
}
|
||||
|
||||
arena_allocator_proc :: proc(allocator_data: rawptr, mode: AllocatorMode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using AllocatorMode;
|
||||
arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator.Mode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
using Allocator.Mode;
|
||||
arena := ^Arena(allocator_data);
|
||||
|
||||
match mode {
|
||||
|
||||
Reference in New Issue
Block a user