123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801 |
- package runtime
- import "core:intrinsics"
- @builtin
- Maybe :: union($T: typeid) {T}
- @builtin
- container_of :: #force_inline proc "contextless" (ptr: $P/^$Field_Type, $T: typeid, $field_name: string) -> ^T
- where intrinsics.type_has_field(T, field_name),
- intrinsics.type_field_type(T, field_name) == Field_Type {
- offset :: offset_of_by_string(T, field_name)
- return (^T)(uintptr(ptr) - offset) if ptr != nil else nil
- }
- @thread_local global_default_temp_allocator_data: Default_Temp_Allocator
- @builtin
- init_global_temporary_allocator :: proc(size: int, backup_allocator := context.allocator) {
- default_temp_allocator_init(&global_default_temp_allocator_data, size, backup_allocator)
- }
- @builtin
- copy_slice :: proc "contextless" (dst, src: $T/[]$E) -> int {
- n := max(0, min(len(dst), len(src)))
- if n > 0 {
- intrinsics.mem_copy(raw_data(dst), raw_data(src), n*size_of(E))
- }
- return n
- }
- @builtin
- copy_from_string :: proc "contextless" (dst: $T/[]$E/u8, src: $S/string) -> int {
- n := max(0, min(len(dst), len(src)))
- if n > 0 {
- intrinsics.mem_copy(raw_data(dst), raw_data(src), n)
- }
- return n
- }
- @builtin
- copy :: proc{copy_slice, copy_from_string}
- @builtin
- unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
- bounds_check_error_loc(loc, index, len(array))
- n := len(array)-1
- if index != n {
- array[index] = array[n]
- }
- (^Raw_Dynamic_Array)(array).len -= 1
- }
- @builtin
- ordered_remove :: proc(array: ^$D/[dynamic]$T, 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:])
- }
- (^Raw_Dynamic_Array)(array).len -= 1
- }
- @builtin
- remove_range :: proc(array: ^$D/[dynamic]$T, 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 {
- if hi != len(array) {
- copy(array[lo:], array[hi:])
- }
- (^Raw_Dynamic_Array)(array).len -= n
- }
- }
- @builtin
- pop :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
- assert(len(array) > 0, "", loc)
- res = array[len(array)-1]
- (^Raw_Dynamic_Array)(array).len -= 1
- return res
- }
- @builtin
- pop_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
- if len(array) == 0 {
- return
- }
- res, ok = array[len(array)-1], true
- (^Raw_Dynamic_Array)(array).len -= 1
- return
- }
- @builtin
- pop_front :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
- assert(len(array) > 0, "", loc)
- res = array[0]
- if len(array) > 1 {
- copy(array[0:], array[1:])
- }
- (^Raw_Dynamic_Array)(array).len -= 1
- return res
- }
- @builtin
- pop_front_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
- if len(array) == 0 {
- return
- }
- res, ok = array[0], true
- if len(array) > 1 {
- copy(array[0:], array[1:])
- }
- (^Raw_Dynamic_Array)(array).len -= 1
- return
- }
- @builtin
- clear :: proc{clear_dynamic_array, clear_map}
- @builtin
- reserve :: proc{reserve_dynamic_array, reserve_map}
- @builtin
- resize :: proc{resize_dynamic_array}
- // Shrinks the capacity of a dynamic array or map down to the current length, or the given capacity.
- @builtin
- shrink :: proc{shrink_dynamic_array, shrink_map}
- @builtin
- free :: proc{mem_free}
- @builtin
- free_all :: proc{mem_free_all}
- @builtin
- delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
- return mem_free(raw_data(str), allocator, loc)
- }
- @builtin
- delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
- return mem_free((^byte)(str), allocator, loc)
- }
- @builtin
- delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error {
- return mem_free(raw_data(array), array.allocator, loc)
- }
- @builtin
- delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
- return mem_free(raw_data(array), allocator, loc)
- }
- @builtin
- delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error {
- raw := transmute(Raw_Map)m
- err := delete_slice(raw.hashes, raw.entries.allocator, loc)
- err1 := mem_free(raw.entries.data, raw.entries.allocator, loc)
- if err == nil {
- err = err1
- }
- return err
- }
- @builtin
- delete :: proc{
- delete_string,
- delete_cstring,
- delete_dynamic_array,
- delete_slice,
- delete_map,
- }
- // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
- // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
- @builtin
- new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_second {
- return new_aligned(T, align_of(T), allocator, loc)
- }
- new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
- data := mem_alloc_bytes(size_of(T), alignment, allocator, loc) or_return
- t = (^T)(raw_data(data))
- return
- }
- @builtin
- new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_second {
- t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
- t = (^T)(raw_data(t_data))
- if t != nil {
- t^ = data
- }
- return
- }
- DEFAULT_RESERVE_CAPACITY :: 16
- make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
- make_slice_error_loc(loc, len)
- data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
- if data == nil && size_of(E) != 0 {
- return nil, err
- }
- s := Raw_Slice{raw_data(data), len}
- return transmute(T)s, err
- }
- @(builtin)
- make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
- return make_aligned(T, len, align_of(E), allocator, loc)
- }
- @(builtin)
- make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
- return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
- }
- @(builtin)
- make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
- return make_dynamic_array_len_cap(T, len, len, allocator, loc)
- }
- @(builtin)
- 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_second {
- 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
- return
- }
- @(builtin)
- make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
- make_map_expr_error_loc(loc, cap)
- context.allocator = allocator
- m: T
- reserve_map(&m, cap)
- return m
- }
- @(builtin)
- make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) #optional_second {
- make_slice_error_loc(loc, len)
- data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
- if data == nil && size_of(E) != 0 {
- return
- }
- mp = cast(T)raw_data(data)
- return
- }
- // The make built-in procedure allocates and initializes a value of type slice, dynamic array, or map (only)
- // 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
- @builtin
- make :: proc{
- make_slice,
- make_dynamic_array,
- make_dynamic_array_len,
- make_dynamic_array_len_cap,
- make_map,
- make_multi_pointer,
- }
- @builtin
- clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
- if m == nil {
- return
- }
- raw_map := (^Raw_Map)(m)
- entries := (^Raw_Dynamic_Array)(&raw_map.entries)
- entries.len = 0
- for _, i in raw_map.hashes {
- raw_map.hashes[i] = -1
- }
- }
- @builtin
- reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) {
- if m != nil {
- __dynamic_map_reserve(__get_map_header(m), capacity, loc)
- }
- }
- /*
- Shrinks the capacity of a map down to the current length, or the given capacity.
- If `new_cap` is negative, then `len(m)` is used.
- Returns false if `cap(m) < new_cap`, or the allocator report failure.
- If `len(m) < new_cap`, then `len(m)` will be left unchanged.
- */
- @builtin
- shrink_map :: proc(m: ^$T/map[$K]$V, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
- if m != nil {
- new_cap := new_cap if new_cap >= 0 else len(m)
- return __dynamic_map_shrink(__get_map_header(m), new_cap, loc)
- }
- return
- }
- // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
- // If m is nil, or there is no such element, this procedure is a no-op
- @builtin
- delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) {
- if m != nil {
- key := key
- h := __get_map_header(m)
- hash := __get_map_hash(&key)
- fr := __dynamic_map_find(h, hash)
- if fr.entry_index >= 0 {
- entry := __dynamic_map_get_entry(h, fr.entry_index)
- deleted_key = (^K)(uintptr(entry)+h.key_offset)^
- deleted_value = (^V)(uintptr(entry)+h.value_offset)^
- __dynamic_map_erase(h, fr)
- }
- }
- return
- }
- @builtin
- append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) {
- if array == nil {
- return
- }
- when size_of(E) == 0 {
- a.len += 1
- } else {
- if cap(array) < len(array)+1 {
- cap := 2 * cap(array) + max(8, 1)
- _ = reserve(array, cap, loc)
- }
- if cap(array)-len(array) > 0 {
- a := (^Raw_Dynamic_Array)(array)
- when size_of(E) != 0 {
- data := ([^]E)(a.data)
- assert(condition=data != nil, loc=loc)
- data[a.len] = arg
- }
- a.len += 1
- }
- }
- }
- @builtin
- append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) {
- if array == nil {
- return
- }
- arg_len := len(args)
- if arg_len <= 0 {
- return
- }
- when size_of(E) == 0 {
- a.len += arg_len
- } else {
- if cap(array) < len(array)+arg_len {
- cap := 2 * cap(array) + max(8, arg_len)
- _ = reserve(array, cap, loc)
- }
- 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(condition=data != nil, loc=loc)
- intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len)
- }
- a.len += arg_len
- }
- }
- }
- // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
- @builtin
- append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) {
- args := transmute([]E)arg
- append_elems(array=array, args=args, loc=loc)
- }
- // The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
- @builtin
- append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) {
- for arg in args {
- append(array = array, args = transmute([]E)(arg), loc = loc)
- }
- }
- // 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_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) {
- if array == nil {
- return
- }
- resize(array, len(array)+1)
- }
- @builtin
- inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
- if array == nil {
- return
- }
- n := max(len(array), index)
- m :: 1
- new_size := n + m
- if resize(array, new_size, loc) {
- when size_of(E) != 0 {
- copy(array[index + m:], array[index:])
- array[index] = arg
- }
- ok = true
- }
- return
- }
- @builtin
- inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
- if array == nil {
- return
- }
- if len(args) == 0 {
- ok = true
- return
- }
- n := max(len(array), index)
- m := len(args)
- new_size := n + m
- if resize(array, new_size, loc) {
- when size_of(E) != 0 {
- copy(array[index + m:], array[index:])
- copy(array[index:], args)
- }
- ok = true
- }
- return
- }
- @builtin
- inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
- if array == nil {
- return
- }
- if len(arg) == 0 {
- ok = true
- return
- }
- n := max(len(array), index)
- m := len(arg)
- new_size := n + m
- if resize(array, new_size, loc) {
- copy(array[index+m:], array[index:])
- copy(array[index:], arg)
- ok = true
- }
- return
- }
- @builtin inject_at :: proc{inject_at_elem, inject_at_elems, inject_at_elem_string}
- @builtin
- assign_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
- if index < len(array) {
- array[index] = arg
- ok = true
- } else if resize(array, index+1, loc) {
- array[index] = arg
- ok = true
- }
- return
- }
- @builtin
- assign_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
- if index+len(args) < len(array) {
- copy(array[index:], args)
- ok = true
- } else if resize(array, index+1+len(args), loc) {
- copy(array[index:], args)
- ok = true
- }
- return
- }
- @builtin
- assign_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
- if len(args) == 0 {
- ok = true
- } else if index+len(args) < len(array) {
- copy(array[index:], args)
- ok = true
- } else if resize(array, index+1+len(args), loc) {
- copy(array[index:], args)
- ok = true
- }
- return
- }
- @builtin assign_at :: proc{assign_at_elem, assign_at_elems, assign_at_elem_string}
- @builtin
- clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
- if array != nil {
- (^Raw_Dynamic_Array)(array).len = 0
- }
- }
- @builtin
- reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
- if array == nil {
- return false
- }
- a := (^Raw_Dynamic_Array)(array)
- if capacity <= a.cap {
- return true
- }
- if a.allocator.procedure == nil {
- a.allocator = context.allocator
- }
- assert(a.allocator.procedure != nil)
- old_size := a.cap * size_of(E)
- new_size := capacity * size_of(E)
- allocator := a.allocator
- new_data, err := allocator.procedure(
- allocator.data, .Resize, new_size, align_of(E),
- a.data, old_size, loc,
- )
- if new_data == nil || err != nil {
- return false
- }
- a.data = raw_data(new_data)
- a.cap = capacity
- return true
- }
- @builtin
- resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> bool {
- if array == nil {
- return false
- }
- a := (^Raw_Dynamic_Array)(array)
- if length <= a.cap {
- a.len = max(length, 0)
- return true
- }
- if a.allocator.procedure == nil {
- a.allocator = context.allocator
- }
- assert(a.allocator.procedure != nil)
- old_size := a.cap * size_of(E)
- new_size := length * size_of(E)
- allocator := a.allocator
- new_data, err := allocator.procedure(
- allocator.data, .Resize, new_size, align_of(E),
- a.data, old_size, loc,
- )
- if new_data == nil || err != nil {
- return false
- }
- a.data = raw_data(new_data)
- a.len = length
- a.cap = length
- return true
- }
- /*
- Shrinks the capacity of a dynamic array down to the current length, or the given capacity.
- If `new_cap` is negative, then `len(array)` is used.
- Returns false if `cap(array) < new_cap`, or the allocator report failure.
- If `len(array) < new_cap`, then `len(array)` will be left unchanged.
- */
- shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
- if array == nil {
- return
- }
- a := (^Raw_Dynamic_Array)(array)
- new_cap := new_cap if new_cap >= 0 else a.len
- if new_cap > a.cap {
- return
- }
- if a.allocator.procedure == nil {
- a.allocator = context.allocator
- }
- assert(a.allocator.procedure != nil)
- old_size := a.cap * size_of(E)
- new_size := new_cap * size_of(E)
- new_data, err := a.allocator.procedure(
- a.allocator.data,
- .Resize,
- new_size,
- align_of(E),
- a.data,
- old_size,
- loc,
- )
- if err != nil {
- return
- }
- a.data = raw_data(new_data)
- a.len = min(new_cap, a.len)
- a.cap = new_cap
- return true
- }
- @builtin
- map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (ptr: ^V) {
- key, value := key, value
- h := __get_map_header(m)
- hash := __get_map_hash(&key)
-
- data := uintptr(__dynamic_map_set(h, hash, &value, loc))
- return (^V)(data + h.value_offset)
- }
- @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}
- }
- }
- @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}
- @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")
- }
- }
- @builtin
- raw_array_data :: proc "contextless" (a: $P/^($T/[$N]$E)) -> [^]E {
- return ([^]E)(a)
- }
- @builtin
- raw_simd_data :: proc "contextless" (a: $P/^($T/#simd[$N]$E)) -> [^]E {
- return ([^]E)(a)
- }
- @builtin
- raw_slice_data :: proc "contextless" (s: $S/[]$E) -> [^]E {
- ptr := (transmute(Raw_Slice)s).data
- return ([^]E)(ptr)
- }
- @builtin
- raw_dynamic_array_data :: proc "contextless" (s: $S/[dynamic]$E) -> [^]E {
- ptr := (transmute(Raw_Dynamic_Array)s).data
- return ([^]E)(ptr)
- }
- @builtin
- raw_string_data :: proc "contextless" (s: $S/string) -> [^]u8 {
- return (transmute(Raw_String)s).data
- }
- @builtin
- raw_data :: proc{raw_array_data, raw_slice_data, raw_dynamic_array_data, raw_string_data, raw_simd_data}
- @builtin
- @(disabled=ODIN_DISABLE_ASSERT)
- assert :: proc(condition: bool, message := "", 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(message: string, loc: Source_Code_Location) {
- p := context.assertion_failure_proc
- if p == nil {
- p = default_assertion_failure_proc
- }
- p("runtime assertion", message, loc)
- }
- internal(message, loc)
- }
- }
- @builtin
- @(disabled=ODIN_DISABLE_ASSERT)
- panic :: proc(message: string, loc := #caller_location) -> ! {
- p := context.assertion_failure_proc
- if p == nil {
- p = default_assertion_failure_proc
- }
- p("panic", message, loc)
- }
- @builtin
- @(disabled=ODIN_DISABLE_ASSERT)
- unimplemented :: proc(message := "", loc := #caller_location) -> ! {
- p := context.assertion_failure_proc
- if p == nil {
- p = default_assertion_failure_proc
- }
- p("not yet implemented", message, loc)
- }
- @builtin
- @(disabled=ODIN_DISABLE_ASSERT)
- unreachable :: proc(message := "", loc := #caller_location) -> ! {
- p := context.assertion_failure_proc
- if p == nil {
- p = default_assertion_failure_proc
- }
- if message != "" {
- p("internal error", message, loc)
- } else {
- p("internal error", "entered unreachable code", loc)
- }
- }
|