|
@@ -1,11 +1,44 @@
|
|
|
package runtime
|
|
|
|
|
|
-import "core:intrinsics"
|
|
|
+import "base:intrinsics"
|
|
|
|
|
|
@builtin
|
|
|
Maybe :: union($T: typeid) {T}
|
|
|
|
|
|
|
|
|
+/*
|
|
|
+Recovers the containing/parent struct from a pointer to one of its fields.
|
|
|
+Works by "walking back" to the struct's starting address using the offset between the field and the struct.
|
|
|
+
|
|
|
+Inputs:
|
|
|
+- ptr: Pointer to the field of a container struct
|
|
|
+- T: The type of the container struct
|
|
|
+- field_name: The name of the field in the `T` struct
|
|
|
+
|
|
|
+Returns:
|
|
|
+- A pointer to the container struct based on a pointer to a field in it
|
|
|
+
|
|
|
+Example:
|
|
|
+ package container_of
|
|
|
+ import "base:runtime"
|
|
|
+
|
|
|
+ Node :: struct {
|
|
|
+ value: int,
|
|
|
+ prev: ^Node,
|
|
|
+ next: ^Node,
|
|
|
+ }
|
|
|
+
|
|
|
+ main :: proc() {
|
|
|
+ node: Node
|
|
|
+ field_ptr := &node.next
|
|
|
+ container_struct_ptr: ^Node = runtime.container_of(field_ptr, Node, "next")
|
|
|
+ assert(container_struct_ptr == &node)
|
|
|
+ assert(uintptr(field_ptr) - uintptr(container_struct_ptr) == size_of(node.value) + size_of(node.prev))
|
|
|
+ }
|
|
|
+
|
|
|
+Output:
|
|
|
+ ^Node
|
|
|
+*/
|
|
|
@(builtin, require_results)
|
|
|
container_of :: #force_inline proc "contextless" (ptr: $P/^$Field_Type, $T: typeid, $field_name: string) -> ^T
|
|
|
where intrinsics.type_has_field(T, field_name),
|
|
@@ -40,7 +73,7 @@ copy_slice :: proc "contextless" (dst, src: $T/[]$E) -> int {
|
|
|
}
|
|
|
return n
|
|
|
}
|
|
|
-// `copy_from_string` is a built-in procedure that copies elements from a source slice `src` to a destination string `dst`.
|
|
|
+// `copy_from_string` is a built-in procedure that copies elements from a source string `src` to a destination slice `dst`.
|
|
|
// The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum
|
|
|
// of len(src) and len(dst).
|
|
|
//
|
|
@@ -53,7 +86,7 @@ copy_from_string :: proc "contextless" (dst: $T/[]$E/u8, src: $S/string) -> int
|
|
|
}
|
|
|
return n
|
|
|
}
|
|
|
-// `copy` is a built-in procedure that copies elements from a source slice `src` to a destination slice/string `dst`.
|
|
|
+// `copy` is a built-in procedure that copies elements from a source slice/string `src` to a destination slice `dst`.
|
|
|
// The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum
|
|
|
// of len(src) and len(dst).
|
|
|
@builtin
|
|
@@ -65,10 +98,10 @@ copy :: proc{copy_slice, copy_from_string}
|
|
|
// with the old value, and reducing the length of the dynamic array by 1.
|
|
|
//
|
|
|
// Note: This is an O(1) operation.
|
|
|
-// Note: If you the elements to remain in their order, use `ordered_remove`.
|
|
|
+// Note: If you want the elements to remain in their order, use `ordered_remove`.
|
|
|
// Note: If the index is out of bounds, this procedure will panic.
|
|
|
@builtin
|
|
|
-unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
|
|
|
+unordered_remove :: proc(array: ^$D/[dynamic]$T, #any_int index: int, loc := #caller_location) #no_bounds_check {
|
|
|
bounds_check_error_loc(loc, index, len(array))
|
|
|
n := len(array)-1
|
|
|
if index != n {
|
|
@@ -79,10 +112,10 @@ unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_loca
|
|
|
// `ordered_remove` removed the element at the specified `index` whilst keeping the order of the other elements.
|
|
|
//
|
|
|
// Note: This is an O(N) operation.
|
|
|
-// Note: If you the elements do not have to remain in their order, prefer `unordered_remove`.
|
|
|
+// Note: If the elements do not have to remain in their order, prefer `unordered_remove`.
|
|
|
// Note: If the index is out of bounds, this procedure will panic.
|
|
|
@builtin
|
|
|
-ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
|
|
|
+ordered_remove :: proc(array: ^$D/[dynamic]$T, #any_int index: int, loc := #caller_location) #no_bounds_check {
|
|
|
bounds_check_error_loc(loc, index, len(array))
|
|
|
if index+1 < len(array) {
|
|
|
copy(array[index:], array[index+1:])
|
|
@@ -95,7 +128,7 @@ ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_locati
|
|
|
// Note: This is an O(N) operation.
|
|
|
// Note: If the range is out of bounds, this procedure will panic.
|
|
|
@builtin
|
|
|
-remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) #no_bounds_check {
|
|
|
+remove_range :: proc(array: ^$D/[dynamic]$T, #any_int lo, hi: int, loc := #caller_location) #no_bounds_check {
|
|
|
slice_expr_error_lo_hi_loc(loc, lo, hi, len(array))
|
|
|
n := max(hi-lo, 0)
|
|
|
if n > 0 {
|
|
@@ -109,7 +142,7 @@ remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_locatio
|
|
|
|
|
|
// `pop` will remove and return the end value of dynamic array `array` and reduces the length of `array` by 1.
|
|
|
//
|
|
|
-// Note: If the dynamic array as no elements (`len(array) == 0`), this procedure will panic.
|
|
|
+// Note: If the dynamic array has no elements (`len(array) == 0`), this procedure will panic.
|
|
|
@builtin
|
|
|
pop :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
|
|
|
assert(len(array) > 0, loc=loc)
|
|
@@ -122,7 +155,7 @@ pop :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bou
|
|
|
// `pop_safe` trys to remove and return the end value of dynamic array `array` and reduces the length of `array` by 1.
|
|
|
// If the operation is not possible, it will return false.
|
|
|
@builtin
|
|
|
-pop_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
|
|
|
+pop_safe :: proc "contextless" (array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
|
|
|
if len(array) == 0 {
|
|
|
return
|
|
|
}
|
|
@@ -148,7 +181,7 @@ pop_front :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #
|
|
|
// `pop_front_safe` trys to return and remove the first value of dynamic array `array` and reduces the length of `array` by 1.
|
|
|
// If the operation is not possible, it will return false.
|
|
|
@builtin
|
|
|
-pop_front_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
|
|
|
+pop_front_safe :: proc "contextless" (array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
|
|
|
if len(array) == 0 {
|
|
|
return
|
|
|
}
|
|
@@ -163,15 +196,43 @@ pop_front_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_
|
|
|
|
|
|
// `clear` will set the length of a passed dynamic array or map to `0`
|
|
|
@builtin
|
|
|
-clear :: proc{clear_dynamic_array, clear_map}
|
|
|
+clear :: proc{
|
|
|
+ clear_dynamic_array,
|
|
|
+ clear_map,
|
|
|
+
|
|
|
+ clear_soa_dynamic_array,
|
|
|
+}
|
|
|
|
|
|
// `reserve` will try to reserve memory of a passed dynamic array or map to the requested element count (setting the `cap`).
|
|
|
@builtin
|
|
|
-reserve :: proc{reserve_dynamic_array, reserve_map}
|
|
|
+reserve :: proc{
|
|
|
+ reserve_dynamic_array,
|
|
|
+ reserve_map,
|
|
|
+
|
|
|
+ reserve_soa,
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+non_zero_reserve :: proc{
|
|
|
+ non_zero_reserve_dynamic_array,
|
|
|
+
|
|
|
+ non_zero_reserve_soa,
|
|
|
+}
|
|
|
+
|
|
|
+// `resize` will try to resize memory of a passed dynamic array to the requested element count (setting the `len`, and possibly `cap`).
|
|
|
+@builtin
|
|
|
+resize :: proc{
|
|
|
+ resize_dynamic_array,
|
|
|
+
|
|
|
+ resize_soa,
|
|
|
+}
|
|
|
|
|
|
-// `resize` will try to resize memory of a passed dynamic array or map to the requested element count (setting the `len`, and possibly `cap`).
|
|
|
@builtin
|
|
|
-resize :: proc{resize_dynamic_array}
|
|
|
+non_zero_resize :: proc{
|
|
|
+ non_zero_resize_dynamic_array,
|
|
|
+
|
|
|
+ non_zero_resize_soa,
|
|
|
+}
|
|
|
|
|
|
// Shrinks the capacity of a dynamic array or map down to the current length, or the given capacity.
|
|
|
@builtin
|
|
@@ -234,6 +295,8 @@ delete :: proc{
|
|
|
delete_dynamic_array,
|
|
|
delete_slice,
|
|
|
delete_map,
|
|
|
+ delete_soa_slice,
|
|
|
+ delete_soa_dynamic_array,
|
|
|
}
|
|
|
|
|
|
|
|
@@ -260,7 +323,7 @@ new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_locat
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-DEFAULT_RESERVE_CAPACITY :: 16
|
|
|
+DEFAULT_DYNAMIC_ARRAY_CAPACITY :: 8
|
|
|
|
|
|
@(require_results)
|
|
|
make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
|
|
@@ -287,7 +350,7 @@ make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allo
|
|
|
// Note: Prefer using the procedure group `make`.
|
|
|
@(builtin, require_results)
|
|
|
make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
|
|
|
- return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
|
|
|
+ return make_dynamic_array_len_cap(T, 0, 0, allocator, loc)
|
|
|
}
|
|
|
// `make_dynamic_array_len` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
|
|
|
// Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
|
|
@@ -303,28 +366,47 @@ make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, alloca
|
|
|
// Note: Prefer using the procedure group `make`.
|
|
|
@(builtin, require_results)
|
|
|
make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error {
|
|
|
+ err = _make_dynamic_array_len_cap((^Raw_Dynamic_Array)(&array), size_of(E), align_of(E), len, cap, allocator, loc)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+@(require_results)
|
|
|
+_make_dynamic_array_len_cap :: proc(array: ^Raw_Dynamic_Array, size_of_elem, align_of_elem: int, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> (err: Allocator_Error) {
|
|
|
make_dynamic_array_error_loc(loc, len, cap)
|
|
|
- data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
|
|
|
- s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
|
|
|
- if data == nil && size_of(E) != 0 {
|
|
|
- s.len, s.cap = 0, 0
|
|
|
- }
|
|
|
- array = transmute(T)s
|
|
|
+ array.allocator = allocator // initialize allocator before just in case it fails to allocate any memory
|
|
|
+ data := mem_alloc_bytes(size_of_elem*cap, align_of_elem, allocator, loc) or_return
|
|
|
+ use_zero := data == nil && size_of_elem != 0
|
|
|
+ array.data = raw_data(data)
|
|
|
+ array.len = 0 if use_zero else len
|
|
|
+ array.cap = 0 if use_zero else cap
|
|
|
+ array.allocator = allocator
|
|
|
return
|
|
|
}
|
|
|
-// `make_map` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
|
|
|
+
|
|
|
+// `make_map` initializes a map with an allocator. Like `new`, the first argument is a type, not a value.
|
|
|
+// Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
|
|
|
+//
|
|
|
+// Note: Prefer using the procedure group `make`.
|
|
|
+@(builtin, require_results)
|
|
|
+make_map :: proc($T: typeid/map[$K]$E, allocator := context.allocator, loc := #caller_location) -> (m: T) {
|
|
|
+ m.allocator = allocator
|
|
|
+ return m
|
|
|
+}
|
|
|
+
|
|
|
+// `make_map_cap` initializes a map with an allocator and allocates space using `capacity`.
|
|
|
+// Like `new`, the first argument is a type, not a value.
|
|
|
// Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
|
|
|
//
|
|
|
// Note: Prefer using the procedure group `make`.
|
|
|
@(builtin, require_results)
|
|
|
-make_map :: proc($T: typeid/map[$K]$E, #any_int capacity: int = 1<<MAP_MIN_LOG2_CAPACITY, allocator := context.allocator, loc := #caller_location) -> (m: T, err: Allocator_Error) #optional_allocator_error {
|
|
|
+make_map_cap :: proc($T: typeid/map[$K]$E, #any_int capacity: int, allocator := context.allocator, loc := #caller_location) -> (m: T, err: Allocator_Error) #optional_allocator_error {
|
|
|
make_map_expr_error_loc(loc, capacity)
|
|
|
context.allocator = allocator
|
|
|
|
|
|
err = reserve_map(&m, capacity, loc)
|
|
|
return
|
|
|
}
|
|
|
-// `make_multi_pointer` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
|
|
|
+// `make_multi_pointer` allocates and initializes a multi-pointer. Like `new`, the first argument is a type, not a value.
|
|
|
// Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
|
|
|
//
|
|
|
// This is "similar" to doing `raw_data(make([]E, len, allocator))`.
|
|
@@ -346,7 +428,7 @@ make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := con
|
|
|
//
|
|
|
// Similar to `new`, the first argument is a type, not a value. Unlike new, make's return type is the same as the
|
|
|
// type of its argument, not a pointer to it.
|
|
|
-// Make uses the specified allocator, default is context.allocator, default is context.allocator
|
|
|
+// Make uses the specified allocator, default is context.allocator.
|
|
|
@builtin
|
|
|
make :: proc{
|
|
|
make_slice,
|
|
@@ -354,7 +436,13 @@ make :: proc{
|
|
|
make_dynamic_array_len,
|
|
|
make_dynamic_array_len_cap,
|
|
|
make_map,
|
|
|
+ make_map_cap,
|
|
|
make_multi_pointer,
|
|
|
+
|
|
|
+ make_soa_slice,
|
|
|
+ make_soa_dynamic_array,
|
|
|
+ make_soa_dynamic_array_len,
|
|
|
+ make_soa_dynamic_array_len_cap,
|
|
|
}
|
|
|
|
|
|
|
|
@@ -374,7 +462,7 @@ clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
|
|
|
//
|
|
|
// Note: Prefer the procedure group `reserve`
|
|
|
@builtin
|
|
|
-reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) -> Allocator_Error {
|
|
|
+reserve_map :: proc(m: ^$T/map[$K]$V, #any_int capacity: int, loc := #caller_location) -> Allocator_Error {
|
|
|
return __dynamic_map_reserve((^Raw_Map)(m), map_info(T), uint(capacity), loc) if m != nil else nil
|
|
|
}
|
|
|
|
|
@@ -404,75 +492,112 @@ delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value:
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+_append_elem :: #force_inline proc(array: ^Raw_Dynamic_Array, size_of_elem, align_of_elem: int, arg_ptr: rawptr, should_zero: bool, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
+ if array == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
+ if array.cap < array.len+1 {
|
|
|
+ // Same behavior as _append_elems but there's only one arg, so we always just add DEFAULT_DYNAMIC_ARRAY_CAPACITY.
|
|
|
+ cap := 2 * array.cap + DEFAULT_DYNAMIC_ARRAY_CAPACITY
|
|
|
|
|
|
-@builtin
|
|
|
-append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
- if array == nil {
|
|
|
- return 0, nil
|
|
|
+ // do not 'or_return' here as it could be a partial success
|
|
|
+ err = _reserve_dynamic_array(array, size_of_elem, align_of_elem, cap, should_zero, loc)
|
|
|
}
|
|
|
- when size_of(E) == 0 {
|
|
|
- array := (^Raw_Dynamic_Array)(array)
|
|
|
+ if array.cap-array.len > 0 {
|
|
|
+ data := ([^]byte)(array.data)
|
|
|
+ assert(data != nil, loc=loc)
|
|
|
+ data = data[array.len*size_of_elem:]
|
|
|
+ intrinsics.mem_copy_non_overlapping(data, arg_ptr, size_of_elem)
|
|
|
array.len += 1
|
|
|
+ n = 1
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+append_elem :: proc(array: ^$T/[dynamic]$E, #no_broadcast arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
+ when size_of(E) == 0 {
|
|
|
+ (^Raw_Dynamic_Array)(array).len += 1
|
|
|
return 1, nil
|
|
|
} else {
|
|
|
- if cap(array) < len(array)+1 {
|
|
|
- cap := 2 * cap(array) + max(8, 1)
|
|
|
- err = reserve(array, cap, loc) // do not 'or_return' here as it could be a partial success
|
|
|
- }
|
|
|
- if cap(array)-len(array) > 0 {
|
|
|
- a := (^Raw_Dynamic_Array)(array)
|
|
|
- when size_of(E) != 0 {
|
|
|
- data := ([^]E)(a.data)
|
|
|
- assert(data != nil, loc=loc)
|
|
|
- data[a.len] = arg
|
|
|
- }
|
|
|
- a.len += 1
|
|
|
- return 1, err
|
|
|
- }
|
|
|
- return 0, err
|
|
|
+ arg := arg
|
|
|
+ return _append_elem((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), &arg, true, loc=loc)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@builtin
|
|
|
-append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
+non_zero_append_elem :: proc(array: ^$T/[dynamic]$E, #no_broadcast arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
+ when size_of(E) == 0 {
|
|
|
+ (^Raw_Dynamic_Array)(array).len += 1
|
|
|
+ return 1, nil
|
|
|
+ } else {
|
|
|
+ arg := arg
|
|
|
+ return _append_elem((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), &arg, false, loc=loc)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+_append_elems :: #force_inline proc(array: ^Raw_Dynamic_Array, size_of_elem, align_of_elem: int, should_zero: bool, loc := #caller_location, args: rawptr, arg_len: int) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
if array == nil {
|
|
|
return 0, nil
|
|
|
}
|
|
|
|
|
|
- arg_len := len(args)
|
|
|
if arg_len <= 0 {
|
|
|
return 0, nil
|
|
|
}
|
|
|
|
|
|
- when size_of(E) == 0 {
|
|
|
- array := (^Raw_Dynamic_Array)(array)
|
|
|
+ if array.cap < array.len+arg_len {
|
|
|
+ cap := 2 * array.cap + max(DEFAULT_DYNAMIC_ARRAY_CAPACITY, arg_len)
|
|
|
+
|
|
|
+ // do not 'or_return' here as it could be a partial success
|
|
|
+ err = _reserve_dynamic_array(array, size_of_elem, align_of_elem, cap, should_zero, loc)
|
|
|
+ }
|
|
|
+ arg_len := arg_len
|
|
|
+ arg_len = min(array.cap-array.len, arg_len)
|
|
|
+ if arg_len > 0 {
|
|
|
+ data := ([^]byte)(array.data)
|
|
|
+ assert(data != nil, loc=loc)
|
|
|
+ data = data[array.len*size_of_elem:]
|
|
|
+ intrinsics.mem_copy(data, args, size_of_elem * arg_len) // must be mem_copy (overlapping)
|
|
|
array.len += arg_len
|
|
|
- return arg_len, nil
|
|
|
+ }
|
|
|
+ return arg_len, err
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+append_elems :: proc(array: ^$T/[dynamic]$E, #no_broadcast args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
+ when size_of(E) == 0 {
|
|
|
+ a := (^Raw_Dynamic_Array)(array)
|
|
|
+ a.len += len(args)
|
|
|
+ return len(args), nil
|
|
|
} else {
|
|
|
- if cap(array) < len(array)+arg_len {
|
|
|
- cap := 2 * cap(array) + max(8, arg_len)
|
|
|
- err = reserve(array, cap, loc) // do not 'or_return' here as it could be a partial success
|
|
|
- }
|
|
|
- arg_len = min(cap(array)-len(array), arg_len)
|
|
|
- if arg_len > 0 {
|
|
|
- a := (^Raw_Dynamic_Array)(array)
|
|
|
- when size_of(E) != 0 {
|
|
|
- data := ([^]E)(a.data)
|
|
|
- assert(data != nil, loc=loc)
|
|
|
- intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len)
|
|
|
- }
|
|
|
- a.len += arg_len
|
|
|
- }
|
|
|
- return arg_len, err
|
|
|
+ return _append_elems((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), true, loc, raw_data(args), len(args))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+non_zero_append_elems :: proc(array: ^$T/[dynamic]$E, #no_broadcast args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
+ when size_of(E) == 0 {
|
|
|
+ a := (^Raw_Dynamic_Array)(array)
|
|
|
+ a.len += len(args)
|
|
|
+ return len(args), nil
|
|
|
+ } else {
|
|
|
+ return _append_elems((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), false, loc, raw_data(args), len(args))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
|
|
|
+_append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, should_zero: bool, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
+ return _append_elems((^Raw_Dynamic_Array)(array), 1, 1, should_zero, loc, raw_data(arg), len(arg))
|
|
|
+}
|
|
|
+
|
|
|
@builtin
|
|
|
append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
- args := transmute([]E)arg
|
|
|
- return append_elems(array, ..args, loc=loc)
|
|
|
+ return _append_elem_string(array, arg, true, loc)
|
|
|
+}
|
|
|
+@builtin
|
|
|
+non_zero_append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
|
|
|
+ return _append_elem_string(array, arg, false, loc)
|
|
|
}
|
|
|
|
|
|
|
|
@@ -491,7 +616,23 @@ append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_
|
|
|
}
|
|
|
|
|
|
// The append built-in procedure appends elements to the end of a dynamic array
|
|
|
-@builtin append :: proc{append_elem, append_elems, append_elem_string}
|
|
|
+@builtin append :: proc{
|
|
|
+ append_elem,
|
|
|
+ append_elems,
|
|
|
+ append_elem_string,
|
|
|
+
|
|
|
+ append_soa_elem,
|
|
|
+ append_soa_elems,
|
|
|
+}
|
|
|
+
|
|
|
+@builtin non_zero_append :: proc{
|
|
|
+ non_zero_append_elem,
|
|
|
+ non_zero_append_elems,
|
|
|
+ non_zero_append_elem_string,
|
|
|
+
|
|
|
+ non_zero_append_soa_elem,
|
|
|
+ non_zero_append_soa_elems,
|
|
|
+}
|
|
|
|
|
|
|
|
|
@builtin
|
|
@@ -506,7 +647,7 @@ append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (n: i
|
|
|
|
|
|
|
|
|
@builtin
|
|
|
-inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
+inject_at_elem :: proc(array: ^$T/[dynamic]$E, #any_int index: int, #no_broadcast arg: E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
if array == nil {
|
|
|
return
|
|
|
}
|
|
@@ -524,7 +665,7 @@ inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #calle
|
|
|
}
|
|
|
|
|
|
@builtin
|
|
|
-inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
+inject_at_elems :: proc(array: ^$T/[dynamic]$E, #any_int index: int, #no_broadcast args: ..E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
if array == nil {
|
|
|
return
|
|
|
}
|
|
@@ -547,7 +688,7 @@ inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #c
|
|
|
}
|
|
|
|
|
|
@builtin
|
|
|
-inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
+inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, #any_int index: int, arg: string, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
if array == nil {
|
|
|
return
|
|
|
}
|
|
@@ -572,7 +713,7 @@ inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string
|
|
|
|
|
|
|
|
|
@builtin
|
|
|
-assign_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
+assign_at_elem :: proc(array: ^$T/[dynamic]$E, #any_int index: int, arg: E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
if index < len(array) {
|
|
|
array[index] = arg
|
|
|
ok = true
|
|
@@ -586,12 +727,15 @@ assign_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #calle
|
|
|
|
|
|
|
|
|
@builtin
|
|
|
-assign_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
- if index+len(args) < len(array) {
|
|
|
+assign_at_elems :: proc(array: ^$T/[dynamic]$E, #any_int index: int, #no_broadcast args: ..E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
+ new_size := index + len(args)
|
|
|
+ if len(args) == 0 {
|
|
|
+ ok = true
|
|
|
+ } else if new_size < len(array) {
|
|
|
copy(array[index:], args)
|
|
|
ok = true
|
|
|
} else {
|
|
|
- resize(array, index+1+len(args), loc) or_return
|
|
|
+ resize(array, new_size, loc) or_return
|
|
|
copy(array[index:], args)
|
|
|
ok = true
|
|
|
}
|
|
@@ -600,7 +744,7 @@ assign_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #c
|
|
|
|
|
|
|
|
|
@builtin
|
|
|
-assign_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
+assign_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, #any_int index: int, arg: string, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
|
|
|
new_size := index + len(arg)
|
|
|
if len(arg) == 0 {
|
|
|
ok = true
|
|
@@ -633,12 +777,10 @@ clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
|
|
|
// `reserve_dynamic_array` will try to reserve memory of a passed dynamic array or map to the requested element count (setting the `cap`).
|
|
|
//
|
|
|
// Note: Prefer the procedure group `reserve`.
|
|
|
-@builtin
|
|
|
-reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> Allocator_Error {
|
|
|
- if array == nil {
|
|
|
+_reserve_dynamic_array :: #force_inline proc(a: ^Raw_Dynamic_Array, size_of_elem, align_of_elem: int, capacity: int, should_zero: bool, loc := #caller_location) -> Allocator_Error {
|
|
|
+ if a == nil {
|
|
|
return nil
|
|
|
}
|
|
|
- a := (^Raw_Dynamic_Array)(array)
|
|
|
|
|
|
if capacity <= a.cap {
|
|
|
return nil
|
|
@@ -649,11 +791,16 @@ reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #cal
|
|
|
}
|
|
|
assert(a.allocator.procedure != nil)
|
|
|
|
|
|
- old_size := a.cap * size_of(E)
|
|
|
- new_size := capacity * size_of(E)
|
|
|
+ old_size := a.cap * size_of_elem
|
|
|
+ new_size := capacity * size_of_elem
|
|
|
allocator := a.allocator
|
|
|
|
|
|
- new_data := mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return
|
|
|
+ new_data: []byte
|
|
|
+ if should_zero {
|
|
|
+ new_data = mem_resize(a.data, old_size, new_size, align_of_elem, allocator, loc) or_return
|
|
|
+ } else {
|
|
|
+ new_data = non_zero_mem_resize(a.data, old_size, new_size, align_of_elem, allocator, loc) or_return
|
|
|
+ }
|
|
|
if new_data == nil && new_size > 0 {
|
|
|
return .Out_Of_Memory
|
|
|
}
|
|
@@ -663,15 +810,26 @@ reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #cal
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// `resize_dynamic_array` will try to resize memory of a passed dynamic array or map to the requested element count (setting the `len`, and possibly `cap`).
|
|
|
-//
|
|
|
-// Note: Prefer the procedure group `resize`
|
|
|
@builtin
|
|
|
-resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> Allocator_Error {
|
|
|
- if array == nil {
|
|
|
+reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, #any_int capacity: int, loc := #caller_location) -> Allocator_Error {
|
|
|
+ return _reserve_dynamic_array((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), capacity, true, loc)
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+non_zero_reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, #any_int capacity: int, loc := #caller_location) -> Allocator_Error {
|
|
|
+ return _reserve_dynamic_array((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), capacity, false, loc)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+_resize_dynamic_array :: #force_inline proc(a: ^Raw_Dynamic_Array, size_of_elem, align_of_elem: int, length: int, should_zero: bool, loc := #caller_location) -> Allocator_Error {
|
|
|
+ if a == nil {
|
|
|
return nil
|
|
|
}
|
|
|
- a := (^Raw_Dynamic_Array)(array)
|
|
|
+
|
|
|
+ if should_zero && a.len < length {
|
|
|
+ num_reused := min(a.cap, length) - a.len
|
|
|
+ intrinsics.mem_zero(([^]byte)(a.data)[a.len*size_of_elem:], num_reused*size_of_elem)
|
|
|
+ }
|
|
|
|
|
|
if length <= a.cap {
|
|
|
a.len = max(length, 0)
|
|
@@ -683,11 +841,16 @@ resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller
|
|
|
}
|
|
|
assert(a.allocator.procedure != nil)
|
|
|
|
|
|
- old_size := a.cap * size_of(E)
|
|
|
- new_size := length * size_of(E)
|
|
|
+ old_size := a.cap * size_of_elem
|
|
|
+ new_size := length * size_of_elem
|
|
|
allocator := a.allocator
|
|
|
|
|
|
- new_data := mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return
|
|
|
+ new_data : []byte
|
|
|
+ if should_zero {
|
|
|
+ new_data = mem_resize(a.data, old_size, new_size, align_of_elem, allocator, loc) or_return
|
|
|
+ } else {
|
|
|
+ new_data = non_zero_mem_resize(a.data, old_size, new_size, align_of_elem, allocator, loc) or_return
|
|
|
+ }
|
|
|
if new_data == nil && new_size > 0 {
|
|
|
return .Out_Of_Memory
|
|
|
}
|
|
@@ -698,6 +861,19 @@ resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+// `resize_dynamic_array` will try to resize memory of a passed dynamic array or map to the requested element count (setting the `len`, and possibly `cap`).
|
|
|
+//
|
|
|
+// Note: Prefer the procedure group `resize`
|
|
|
+@builtin
|
|
|
+resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, #any_int length: int, loc := #caller_location) -> Allocator_Error {
|
|
|
+ return _resize_dynamic_array((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), length, true, loc=loc)
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+non_zero_resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, #any_int length: int, loc := #caller_location) -> Allocator_Error {
|
|
|
+ return _resize_dynamic_array((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), length, false, loc=loc)
|
|
|
+}
|
|
|
+
|
|
|
/*
|
|
|
Shrinks the capacity of a dynamic array down to the current length, or the given capacity.
|
|
|
|
|
@@ -709,11 +885,14 @@ resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller
|
|
|
|
|
|
Note: Prefer the procedure group `shrink`
|
|
|
*/
|
|
|
-shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #caller_location) -> (did_shrink: bool, err: Allocator_Error) {
|
|
|
- if array == nil {
|
|
|
+shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, #any_int new_cap := -1, loc := #caller_location) -> (did_shrink: bool, err: Allocator_Error) {
|
|
|
+ return _shrink_dynamic_array((^Raw_Dynamic_Array)(array), size_of(E), align_of(E), new_cap, loc)
|
|
|
+}
|
|
|
+
|
|
|
+_shrink_dynamic_array :: proc(a: ^Raw_Dynamic_Array, size_of_elem, align_of_elem: int, new_cap := -1, loc := #caller_location) -> (did_shrink: bool, err: Allocator_Error) {
|
|
|
+ if a == nil {
|
|
|
return
|
|
|
}
|
|
|
- a := (^Raw_Dynamic_Array)(array)
|
|
|
|
|
|
new_cap := new_cap if new_cap >= 0 else a.len
|
|
|
|
|
@@ -726,10 +905,10 @@ shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #call
|
|
|
}
|
|
|
assert(a.allocator.procedure != nil)
|
|
|
|
|
|
- old_size := a.cap * size_of(E)
|
|
|
- new_size := new_cap * size_of(E)
|
|
|
+ old_size := a.cap * size_of_elem
|
|
|
+ new_size := new_cap * size_of_elem
|
|
|
|
|
|
- new_data := mem_resize(a.data, old_size, new_size, align_of(E), a.allocator, loc) or_return
|
|
|
+ new_data := mem_resize(a.data, old_size, new_size, align_of_elem, a.allocator, loc) or_return
|
|
|
|
|
|
a.data = raw_data(new_data)
|
|
|
a.len = min(new_cap, a.len)
|
|
@@ -743,62 +922,59 @@ map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location)
|
|
|
return (^V)(__dynamic_map_set_without_hash((^Raw_Map)(m), map_info(T), rawptr(&key), rawptr(&value), loc))
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-@builtin
|
|
|
-incl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
|
|
|
- s^ |= {elem}
|
|
|
-}
|
|
|
-@builtin
|
|
|
-incl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
|
|
|
- for elem in elems {
|
|
|
- s^ |= {elem}
|
|
|
- }
|
|
|
-}
|
|
|
-@builtin
|
|
|
-incl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
|
|
|
- s^ |= other
|
|
|
-}
|
|
|
-@builtin
|
|
|
-excl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
|
|
|
- s^ &~= {elem}
|
|
|
-}
|
|
|
-@builtin
|
|
|
-excl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
|
|
|
- for elem in elems {
|
|
|
- s^ &~= {elem}
|
|
|
+// Explicitly inserts a key and value into a map `m`, the same as `map_insert`, but the return values differ.
|
|
|
+// - `prev_key` will return the previous pointer of a key if it exists, check `found_previous` if was previously found
|
|
|
+// - `value_ptr` will return the pointer of the memory where the insertion happens, and `nil` if the map failed to resize
|
|
|
+// - `found_previous` will be true a previous key was found
|
|
|
+@(builtin, require_results)
|
|
|
+map_upsert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (prev_key: K, value_ptr: ^V, found_previous: bool) {
|
|
|
+ key, value := key, value
|
|
|
+ kp, vp := __dynamic_map_set_extra_without_hash((^Raw_Map)(m), map_info(T), rawptr(&key), rawptr(&value), loc)
|
|
|
+ if kp != nil {
|
|
|
+ prev_key = (^K)(kp)^
|
|
|
+ found_previous = true
|
|
|
}
|
|
|
+ value_ptr = (^V)(vp)
|
|
|
+ return
|
|
|
}
|
|
|
-@builtin
|
|
|
-excl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
|
|
|
- s^ &~= other
|
|
|
-}
|
|
|
|
|
|
-@builtin incl :: proc{incl_elem, incl_elems, incl_bit_set}
|
|
|
-@builtin excl :: proc{excl_elem, excl_elems, excl_bit_set}
|
|
|
+/*
|
|
|
+Retrieves a pointer to the key and value for a possibly just inserted entry into the map.
|
|
|
+
|
|
|
+If the `key` was not in the map `m`, an entry is inserted with the zero value and `just_inserted` will be `true`.
|
|
|
+Otherwise the existing entry is left untouched and pointers to its key and value are returned.
|
|
|
+
|
|
|
+If the map has to grow in order to insert the entry and the allocation fails, `err` is set and returned.
|
|
|
+
|
|
|
+If `err` is `nil`, `key_ptr` and `value_ptr` are valid pointers and will not be `nil`.
|
|
|
+
|
|
|
+WARN: User modification of the key pointed at by `key_ptr` should only be done if the new key is equal to (in hash) the old key.
|
|
|
+If that is not the case you will corrupt the map.
|
|
|
+*/
|
|
|
+@(builtin, require_results)
|
|
|
+map_entry :: proc(m: ^$T/map[$K]$V, key: K, loc := #caller_location) -> (key_ptr: ^K, value_ptr: ^V, just_inserted: bool, err: Allocator_Error) {
|
|
|
+ key := key
|
|
|
+ zero: V
|
|
|
+
|
|
|
+ _key_ptr, _value_ptr: rawptr
|
|
|
+ _key_ptr, _value_ptr, just_inserted, err = __dynamic_map_entry((^Raw_Map)(m), map_info(T), &key, &zero, loc)
|
|
|
+
|
|
|
+ key_ptr = (^K)(_key_ptr)
|
|
|
+ value_ptr = (^V)(_value_ptr)
|
|
|
+ return
|
|
|
+}
|
|
|
|
|
|
|
|
|
@builtin
|
|
|
-card :: proc(s: $S/bit_set[$E; $U]) -> int {
|
|
|
- when size_of(S) == 1 {
|
|
|
- return int(intrinsics.count_ones(transmute(u8)s))
|
|
|
- } else when size_of(S) == 2 {
|
|
|
- return int(intrinsics.count_ones(transmute(u16)s))
|
|
|
- } else when size_of(S) == 4 {
|
|
|
- return int(intrinsics.count_ones(transmute(u32)s))
|
|
|
- } else when size_of(S) == 8 {
|
|
|
- return int(intrinsics.count_ones(transmute(u64)s))
|
|
|
- } else when size_of(S) == 16 {
|
|
|
- return int(intrinsics.count_ones(transmute(u128)s))
|
|
|
- } else {
|
|
|
- #panic("Unhandled card bit_set size")
|
|
|
- }
|
|
|
+card :: proc "contextless" (s: $S/bit_set[$E; $U]) -> int {
|
|
|
+ return int(intrinsics.count_ones(transmute(intrinsics.type_bit_set_underlying_type(S))s))
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@builtin
|
|
|
@(disabled=ODIN_DISABLE_ASSERT)
|
|
|
-assert :: proc(condition: bool, message := "", loc := #caller_location) {
|
|
|
+assert :: proc(condition: bool, message := #caller_expression(condition), loc := #caller_location) {
|
|
|
if !condition {
|
|
|
// NOTE(bill): This is wrapped in a procedure call
|
|
|
// to improve performance to make the CPU not
|
|
@@ -816,6 +992,24 @@ assert :: proc(condition: bool, message := "", loc := #caller_location) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// Evaluates the condition and aborts the program iff the condition is
|
|
|
+// false. This routine ignores `ODIN_DISABLE_ASSERT`, and will always
|
|
|
+// execute.
|
|
|
+@builtin
|
|
|
+ensure :: proc(condition: bool, message := #caller_expression(condition), loc := #caller_location) {
|
|
|
+ if !condition {
|
|
|
+ @(cold)
|
|
|
+ internal :: proc(message: string, loc: Source_Code_Location) {
|
|
|
+ p := context.assertion_failure_proc
|
|
|
+ if p == nil {
|
|
|
+ p = default_assertion_failure_proc
|
|
|
+ }
|
|
|
+ p("unsatisfied ensure", message, loc)
|
|
|
+ }
|
|
|
+ internal(message, loc)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
@builtin
|
|
|
panic :: proc(message: string, loc := #caller_location) -> ! {
|
|
|
p := context.assertion_failure_proc
|
|
@@ -833,3 +1027,41 @@ unimplemented :: proc(message := "", loc := #caller_location) -> ! {
|
|
|
}
|
|
|
p("not yet implemented", message, loc)
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+@builtin
|
|
|
+@(disabled=ODIN_DISABLE_ASSERT)
|
|
|
+assert_contextless :: proc "contextless" (condition: bool, message := #caller_expression(condition), loc := #caller_location) {
|
|
|
+ if !condition {
|
|
|
+ // NOTE(bill): This is wrapped in a procedure call
|
|
|
+ // to improve performance to make the CPU not
|
|
|
+ // execute speculatively, making it about an order of
|
|
|
+ // magnitude faster
|
|
|
+ @(cold)
|
|
|
+ internal :: proc "contextless" (message: string, loc: Source_Code_Location) {
|
|
|
+ default_assertion_contextless_failure_proc("runtime assertion", message, loc)
|
|
|
+ }
|
|
|
+ internal(message, loc)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+ensure_contextless :: proc "contextless" (condition: bool, message := #caller_expression(condition), loc := #caller_location) {
|
|
|
+ if !condition {
|
|
|
+ @(cold)
|
|
|
+ internal :: proc "contextless" (message: string, loc: Source_Code_Location) {
|
|
|
+ default_assertion_contextless_failure_proc("unsatisfied ensure", message, loc)
|
|
|
+ }
|
|
|
+ internal(message, loc)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+panic_contextless :: proc "contextless" (message: string, loc := #caller_location) -> ! {
|
|
|
+ default_assertion_contextless_failure_proc("panic", message, loc)
|
|
|
+}
|
|
|
+
|
|
|
+@builtin
|
|
|
+unimplemented_contextless :: proc "contextless" (message := "", loc := #caller_location) -> ! {
|
|
|
+ default_assertion_contextless_failure_proc("not yet implemented", message, loc)
|
|
|
+}
|