core_builtin.odin 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. package runtime
  2. import "core:intrinsics"
  3. @builtin
  4. Maybe :: union($T: typeid) {T}
  5. @builtin
  6. container_of :: #force_inline proc "contextless" (ptr: $P/^$Field_Type, $T: typeid, $field_name: string) -> ^T
  7. where intrinsics.type_has_field(T, field_name),
  8. intrinsics.type_field_type(T, field_name) == Field_Type {
  9. offset :: offset_of_by_string(T, field_name)
  10. return (^T)(uintptr(ptr) - offset) if ptr != nil else nil
  11. }
  12. @thread_local global_default_temp_allocator_data: Default_Temp_Allocator
  13. @builtin
  14. init_global_temporary_allocator :: proc(size: int, backup_allocator := context.allocator) {
  15. default_temp_allocator_init(&global_default_temp_allocator_data, size, backup_allocator)
  16. }
  17. @builtin
  18. copy_slice :: proc "contextless" (dst, src: $T/[]$E) -> int {
  19. n := max(0, min(len(dst), len(src)))
  20. if n > 0 {
  21. intrinsics.mem_copy(raw_data(dst), raw_data(src), n*size_of(E))
  22. }
  23. return n
  24. }
  25. @builtin
  26. copy_from_string :: proc "contextless" (dst: $T/[]$E/u8, src: $S/string) -> int {
  27. n := max(0, min(len(dst), len(src)))
  28. if n > 0 {
  29. intrinsics.mem_copy(raw_data(dst), raw_data(src), n)
  30. }
  31. return n
  32. }
  33. @builtin
  34. copy :: proc{copy_slice, copy_from_string}
  35. @builtin
  36. unordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
  37. bounds_check_error_loc(loc, index, len(array))
  38. n := len(array)-1
  39. if index != n {
  40. array[index] = array[n]
  41. }
  42. (^Raw_Dynamic_Array)(array).len -= 1
  43. }
  44. @builtin
  45. ordered_remove :: proc(array: ^$D/[dynamic]$T, index: int, loc := #caller_location) #no_bounds_check {
  46. bounds_check_error_loc(loc, index, len(array))
  47. if index+1 < len(array) {
  48. copy(array[index:], array[index+1:])
  49. }
  50. (^Raw_Dynamic_Array)(array).len -= 1
  51. }
  52. @builtin
  53. remove_range :: proc(array: ^$D/[dynamic]$T, lo, hi: int, loc := #caller_location) #no_bounds_check {
  54. slice_expr_error_lo_hi_loc(loc, lo, hi, len(array))
  55. n := max(hi-lo, 0)
  56. if n > 0 {
  57. if hi != len(array) {
  58. copy(array[lo:], array[hi:])
  59. }
  60. (^Raw_Dynamic_Array)(array).len -= n
  61. }
  62. }
  63. @builtin
  64. pop :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
  65. assert(len(array) > 0, "", loc)
  66. res = array[len(array)-1]
  67. (^Raw_Dynamic_Array)(array).len -= 1
  68. return res
  69. }
  70. @builtin
  71. pop_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
  72. if len(array) == 0 {
  73. return
  74. }
  75. res, ok = array[len(array)-1], true
  76. (^Raw_Dynamic_Array)(array).len -= 1
  77. return
  78. }
  79. @builtin
  80. pop_front :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (res: E) #no_bounds_check {
  81. assert(len(array) > 0, "", loc)
  82. res = array[0]
  83. if len(array) > 1 {
  84. copy(array[0:], array[1:])
  85. }
  86. (^Raw_Dynamic_Array)(array).len -= 1
  87. return res
  88. }
  89. @builtin
  90. pop_front_safe :: proc(array: ^$T/[dynamic]$E) -> (res: E, ok: bool) #no_bounds_check {
  91. if len(array) == 0 {
  92. return
  93. }
  94. res, ok = array[0], true
  95. if len(array) > 1 {
  96. copy(array[0:], array[1:])
  97. }
  98. (^Raw_Dynamic_Array)(array).len -= 1
  99. return
  100. }
  101. @builtin
  102. clear :: proc{clear_dynamic_array, clear_map}
  103. @builtin
  104. reserve :: proc{reserve_dynamic_array, reserve_map}
  105. @builtin
  106. resize :: proc{resize_dynamic_array}
  107. // Shrinks the capacity of a dynamic array or map down to the current length, or the given capacity.
  108. @builtin
  109. shrink :: proc{shrink_dynamic_array, shrink_map}
  110. @builtin
  111. free :: proc{mem_free}
  112. @builtin
  113. free_all :: proc{mem_free_all}
  114. @builtin
  115. delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  116. return mem_free_with_size(raw_data(str), len(str), allocator, loc)
  117. }
  118. @builtin
  119. delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  120. return mem_free((^byte)(str), allocator, loc)
  121. }
  122. @builtin
  123. delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error {
  124. return mem_free_with_size(raw_data(array), cap(array)*size_of(E), array.allocator, loc)
  125. }
  126. @builtin
  127. delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  128. return mem_free_with_size(raw_data(array), len(array)*size_of(E), allocator, loc)
  129. }
  130. @builtin
  131. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error {
  132. Entry :: struct {
  133. hash: uintptr,
  134. next: int,
  135. key: K,
  136. value: V,
  137. }
  138. raw := transmute(Raw_Map)m
  139. err := delete_slice(raw.hashes, raw.entries.allocator, loc)
  140. err1 := mem_free_with_size(raw.entries.data, raw.entries.cap*size_of(Entry), raw.entries.allocator, loc)
  141. if err == nil {
  142. err = err1
  143. }
  144. return err
  145. }
  146. @builtin
  147. delete :: proc{
  148. delete_string,
  149. delete_cstring,
  150. delete_dynamic_array,
  151. delete_slice,
  152. delete_map,
  153. }
  154. // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
  155. // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
  156. @builtin
  157. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_allocator_error {
  158. return new_aligned(T, align_of(T), allocator, loc)
  159. }
  160. new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  161. data := mem_alloc_bytes(size_of(T), alignment, allocator, loc) or_return
  162. t = (^T)(raw_data(data))
  163. return
  164. }
  165. @builtin
  166. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_allocator_error {
  167. t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
  168. t = (^T)(raw_data(t_data))
  169. if t != nil {
  170. t^ = data
  171. }
  172. return
  173. }
  174. DEFAULT_RESERVE_CAPACITY :: 16
  175. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  176. make_slice_error_loc(loc, len)
  177. data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
  178. if data == nil && size_of(E) != 0 {
  179. return nil, err
  180. }
  181. s := Raw_Slice{raw_data(data), len}
  182. return transmute(T)s, err
  183. }
  184. @(builtin)
  185. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  186. return make_aligned(T, len, align_of(E), allocator, loc)
  187. }
  188. @(builtin)
  189. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  190. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
  191. }
  192. @(builtin)
  193. make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  194. return make_dynamic_array_len_cap(T, len, len, allocator, loc)
  195. }
  196. @(builtin)
  197. 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 {
  198. make_dynamic_array_error_loc(loc, len, cap)
  199. data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
  200. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
  201. if data == nil && size_of(E) != 0 {
  202. s.len, s.cap = 0, 0
  203. }
  204. array = transmute(T)s
  205. return
  206. }
  207. @(builtin)
  208. make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
  209. make_map_expr_error_loc(loc, cap)
  210. context.allocator = allocator
  211. m: T
  212. reserve_map(&m, cap)
  213. return m
  214. }
  215. @(builtin)
  216. make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) #optional_allocator_error {
  217. make_slice_error_loc(loc, len)
  218. data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
  219. if data == nil && size_of(E) != 0 {
  220. return
  221. }
  222. mp = cast(T)raw_data(data)
  223. return
  224. }
  225. // The make built-in procedure allocates and initializes a value of type slice, dynamic array, or map (only)
  226. // Similar to new, the first argument is a type, not a value. Unlike new, make's return type is the same as the
  227. // type of its argument, not a pointer to it.
  228. // Make uses the specified allocator, default is context.allocator, default is context.allocator
  229. @builtin
  230. make :: proc{
  231. make_slice,
  232. make_dynamic_array,
  233. make_dynamic_array_len,
  234. make_dynamic_array_len_cap,
  235. make_map,
  236. make_multi_pointer,
  237. }
  238. @builtin
  239. clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
  240. if m == nil {
  241. return
  242. }
  243. raw_map := (^Raw_Map)(m)
  244. entries := (^Raw_Dynamic_Array)(&raw_map.entries)
  245. entries.len = 0
  246. for _, i in raw_map.hashes {
  247. raw_map.hashes[i] = MAP_SENTINEL
  248. }
  249. }
  250. @builtin
  251. reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) {
  252. if m != nil {
  253. h := __get_map_header_table(T)
  254. __dynamic_map_reserve(m, h, uint(capacity), loc)
  255. }
  256. }
  257. /*
  258. Shrinks the capacity of a map down to the current length, or the given capacity.
  259. If `new_cap` is negative, then `len(m)` is used.
  260. Returns false if `cap(m) < new_cap`, or the allocator report failure.
  261. If `len(m) < new_cap`, then `len(m)` will be left unchanged.
  262. */
  263. @builtin
  264. shrink_map :: proc(m: ^$T/map[$K]$V, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
  265. if m != nil {
  266. new_cap := new_cap if new_cap >= 0 else len(m)
  267. return __dynamic_map_shrink(__get_map_header(m), new_cap, loc)
  268. }
  269. return
  270. }
  271. // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
  272. // If m is nil, or there is no such element, this procedure is a no-op
  273. @builtin
  274. delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) {
  275. if m != nil {
  276. key := key
  277. h := __get_map_header(m)
  278. fr := __map_find(h, &key)
  279. if fr.entry_index != MAP_SENTINEL {
  280. entry := __dynamic_map_get_entry(h, fr.entry_index)
  281. deleted_key = (^K)(uintptr(entry)+h.key_offset)^
  282. deleted_value = (^V)(uintptr(entry)+h.value_offset)^
  283. __dynamic_map_erase(h, fr)
  284. }
  285. }
  286. return
  287. }
  288. @builtin
  289. append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> int {
  290. if array == nil {
  291. return 0
  292. }
  293. when size_of(E) == 0 {
  294. array.len += 1
  295. return 1
  296. } else {
  297. if cap(array) < len(array)+1 {
  298. cap := 2 * cap(array) + max(8, 1)
  299. _ = reserve(array, cap, loc)
  300. }
  301. if cap(array)-len(array) > 0 {
  302. a := (^Raw_Dynamic_Array)(array)
  303. when size_of(E) != 0 {
  304. data := ([^]E)(a.data)
  305. assert(condition=data != nil, loc=loc)
  306. data[a.len] = arg
  307. }
  308. a.len += 1
  309. return 1
  310. }
  311. return 0
  312. }
  313. }
  314. @builtin
  315. append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> int {
  316. if array == nil {
  317. return 0
  318. }
  319. arg_len := len(args)
  320. if arg_len <= 0 {
  321. return 0
  322. }
  323. when size_of(E) == 0 {
  324. array.len += arg_len
  325. return arg_len
  326. } else {
  327. if cap(array) < len(array)+arg_len {
  328. cap := 2 * cap(array) + max(8, arg_len)
  329. _ = reserve(array, cap, loc)
  330. }
  331. arg_len = min(cap(array)-len(array), arg_len)
  332. if arg_len > 0 {
  333. a := (^Raw_Dynamic_Array)(array)
  334. when size_of(E) != 0 {
  335. data := ([^]E)(a.data)
  336. assert(condition=data != nil, loc=loc)
  337. intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len)
  338. }
  339. a.len += arg_len
  340. }
  341. return arg_len
  342. }
  343. }
  344. // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
  345. @builtin
  346. append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) -> int {
  347. args := transmute([]E)arg
  348. return append_elems(array=array, args=args, loc=loc)
  349. }
  350. // The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
  351. @builtin
  352. append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) -> (n: int) {
  353. for arg in args {
  354. n += append(array = array, args = transmute([]E)(arg), loc = loc)
  355. }
  356. return
  357. }
  358. // The append built-in procedure appends elements to the end of a dynamic array
  359. @builtin append :: proc{append_elem, append_elems, append_elem_string}
  360. @builtin
  361. append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> int {
  362. if array == nil {
  363. return 0
  364. }
  365. prev_len := len(array)
  366. resize(array, len(array)+1)
  367. return len(array)-prev_len
  368. }
  369. @builtin
  370. inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  371. if array == nil {
  372. return
  373. }
  374. n := max(len(array), index)
  375. m :: 1
  376. new_size := n + m
  377. if resize(array, new_size, loc) {
  378. when size_of(E) != 0 {
  379. copy(array[index + m:], array[index:])
  380. array[index] = arg
  381. }
  382. ok = true
  383. }
  384. return
  385. }
  386. @builtin
  387. inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  388. if array == nil {
  389. return
  390. }
  391. if len(args) == 0 {
  392. ok = true
  393. return
  394. }
  395. n := max(len(array), index)
  396. m := len(args)
  397. new_size := n + m
  398. if resize(array, new_size, loc) {
  399. when size_of(E) != 0 {
  400. copy(array[index + m:], array[index:])
  401. copy(array[index:], args)
  402. }
  403. ok = true
  404. }
  405. return
  406. }
  407. @builtin
  408. inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  409. if array == nil {
  410. return
  411. }
  412. if len(arg) == 0 {
  413. ok = true
  414. return
  415. }
  416. n := max(len(array), index)
  417. m := len(arg)
  418. new_size := n + m
  419. if resize(array, new_size, loc) {
  420. copy(array[index+m:], array[index:])
  421. copy(array[index:], arg)
  422. ok = true
  423. }
  424. return
  425. }
  426. @builtin inject_at :: proc{inject_at_elem, inject_at_elems, inject_at_elem_string}
  427. @builtin
  428. assign_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  429. if index < len(array) {
  430. array[index] = arg
  431. ok = true
  432. } else if resize(array, index+1, loc) {
  433. array[index] = arg
  434. ok = true
  435. }
  436. return
  437. }
  438. @builtin
  439. assign_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  440. if index+len(args) < len(array) {
  441. copy(array[index:], args)
  442. ok = true
  443. } else if resize(array, index+1+len(args), loc) {
  444. copy(array[index:], args)
  445. ok = true
  446. }
  447. return
  448. }
  449. @builtin
  450. assign_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check {
  451. if len(args) == 0 {
  452. ok = true
  453. } else if index+len(args) < len(array) {
  454. copy(array[index:], args)
  455. ok = true
  456. } else if resize(array, index+1+len(args), loc) {
  457. copy(array[index:], args)
  458. ok = true
  459. }
  460. return
  461. }
  462. @builtin assign_at :: proc{assign_at_elem, assign_at_elems, assign_at_elem_string}
  463. @builtin
  464. clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
  465. if array != nil {
  466. (^Raw_Dynamic_Array)(array).len = 0
  467. }
  468. }
  469. @builtin
  470. reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> bool {
  471. if array == nil {
  472. return false
  473. }
  474. a := (^Raw_Dynamic_Array)(array)
  475. if capacity <= a.cap {
  476. return true
  477. }
  478. if a.allocator.procedure == nil {
  479. a.allocator = context.allocator
  480. }
  481. assert(a.allocator.procedure != nil)
  482. old_size := a.cap * size_of(E)
  483. new_size := capacity * size_of(E)
  484. allocator := a.allocator
  485. new_data, err := allocator.procedure(
  486. allocator.data, .Resize, new_size, align_of(E),
  487. a.data, old_size, loc,
  488. )
  489. if new_data == nil || err != nil {
  490. return false
  491. }
  492. a.data = raw_data(new_data)
  493. a.cap = capacity
  494. return true
  495. }
  496. @builtin
  497. resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> bool {
  498. if array == nil {
  499. return false
  500. }
  501. a := (^Raw_Dynamic_Array)(array)
  502. if length <= a.cap {
  503. a.len = max(length, 0)
  504. return true
  505. }
  506. if a.allocator.procedure == nil {
  507. a.allocator = context.allocator
  508. }
  509. assert(a.allocator.procedure != nil)
  510. old_size := a.cap * size_of(E)
  511. new_size := length * size_of(E)
  512. allocator := a.allocator
  513. new_data, err := allocator.procedure(
  514. allocator.data, .Resize, new_size, align_of(E),
  515. a.data, old_size, loc,
  516. )
  517. if new_data == nil || err != nil {
  518. return false
  519. }
  520. a.data = raw_data(new_data)
  521. a.len = length
  522. a.cap = length
  523. return true
  524. }
  525. /*
  526. Shrinks the capacity of a dynamic array down to the current length, or the given capacity.
  527. If `new_cap` is negative, then `len(array)` is used.
  528. Returns false if `cap(array) < new_cap`, or the allocator report failure.
  529. If `len(array) < new_cap`, then `len(array)` will be left unchanged.
  530. */
  531. shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #caller_location) -> (did_shrink: bool) {
  532. if array == nil {
  533. return
  534. }
  535. a := (^Raw_Dynamic_Array)(array)
  536. new_cap := new_cap if new_cap >= 0 else a.len
  537. if new_cap > a.cap {
  538. return
  539. }
  540. if a.allocator.procedure == nil {
  541. a.allocator = context.allocator
  542. }
  543. assert(a.allocator.procedure != nil)
  544. old_size := a.cap * size_of(E)
  545. new_size := new_cap * size_of(E)
  546. new_data, err := a.allocator.procedure(
  547. a.allocator.data,
  548. .Resize,
  549. new_size,
  550. align_of(E),
  551. a.data,
  552. old_size,
  553. loc,
  554. )
  555. if err != nil {
  556. return
  557. }
  558. a.data = raw_data(new_data)
  559. a.len = min(new_cap, a.len)
  560. a.cap = new_cap
  561. return true
  562. }
  563. @builtin
  564. map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (ptr: ^V) {
  565. key, value := key, value
  566. h := __get_map_header_table(T)
  567. e := __dynamic_map_set(m, h, __get_map_key_hash(&key), &key, &value, loc)
  568. return (^V)(uintptr(e) + h.value_offset)
  569. }
  570. @builtin
  571. incl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  572. s^ |= {elem}
  573. }
  574. @builtin
  575. incl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  576. for elem in elems {
  577. s^ |= {elem}
  578. }
  579. }
  580. @builtin
  581. incl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  582. s^ |= other
  583. }
  584. @builtin
  585. excl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  586. s^ &~= {elem}
  587. }
  588. @builtin
  589. excl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  590. for elem in elems {
  591. s^ &~= {elem}
  592. }
  593. }
  594. @builtin
  595. excl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  596. s^ &~= other
  597. }
  598. @builtin incl :: proc{incl_elem, incl_elems, incl_bit_set}
  599. @builtin excl :: proc{excl_elem, excl_elems, excl_bit_set}
  600. @builtin
  601. card :: proc(s: $S/bit_set[$E; $U]) -> int {
  602. when size_of(S) == 1 {
  603. return int(intrinsics.count_ones(transmute(u8)s))
  604. } else when size_of(S) == 2 {
  605. return int(intrinsics.count_ones(transmute(u16)s))
  606. } else when size_of(S) == 4 {
  607. return int(intrinsics.count_ones(transmute(u32)s))
  608. } else when size_of(S) == 8 {
  609. return int(intrinsics.count_ones(transmute(u64)s))
  610. } else when size_of(S) == 16 {
  611. return int(intrinsics.count_ones(transmute(u128)s))
  612. } else {
  613. #panic("Unhandled card bit_set size")
  614. }
  615. }
  616. @builtin
  617. raw_array_data :: proc "contextless" (a: $P/^($T/[$N]$E)) -> [^]E {
  618. return ([^]E)(a)
  619. }
  620. @builtin
  621. raw_simd_data :: proc "contextless" (a: $P/^($T/#simd[$N]$E)) -> [^]E {
  622. return ([^]E)(a)
  623. }
  624. @builtin
  625. raw_slice_data :: proc "contextless" (s: $S/[]$E) -> [^]E {
  626. ptr := (transmute(Raw_Slice)s).data
  627. return ([^]E)(ptr)
  628. }
  629. @builtin
  630. raw_dynamic_array_data :: proc "contextless" (s: $S/[dynamic]$E) -> [^]E {
  631. ptr := (transmute(Raw_Dynamic_Array)s).data
  632. return ([^]E)(ptr)
  633. }
  634. @builtin
  635. raw_string_data :: proc "contextless" (s: $S/string) -> [^]u8 {
  636. return (transmute(Raw_String)s).data
  637. }
  638. @builtin
  639. raw_data :: proc{raw_array_data, raw_slice_data, raw_dynamic_array_data, raw_string_data, raw_simd_data}
  640. @builtin
  641. @(disabled=ODIN_DISABLE_ASSERT)
  642. assert :: proc(condition: bool, message := "", loc := #caller_location) {
  643. if !condition {
  644. // NOTE(bill): This is wrapped in a procedure call
  645. // to improve performance to make the CPU not
  646. // execute speculatively, making it about an order of
  647. // magnitude faster
  648. @(cold)
  649. internal :: proc(message: string, loc: Source_Code_Location) {
  650. p := context.assertion_failure_proc
  651. if p == nil {
  652. p = default_assertion_failure_proc
  653. }
  654. p("runtime assertion", message, loc)
  655. }
  656. internal(message, loc)
  657. }
  658. }
  659. @builtin
  660. @(disabled=ODIN_DISABLE_ASSERT)
  661. panic :: proc(message: string, loc := #caller_location) -> ! {
  662. p := context.assertion_failure_proc
  663. if p == nil {
  664. p = default_assertion_failure_proc
  665. }
  666. p("panic", message, loc)
  667. }
  668. @builtin
  669. @(disabled=ODIN_DISABLE_ASSERT)
  670. unimplemented :: proc(message := "", loc := #caller_location) -> ! {
  671. p := context.assertion_failure_proc
  672. if p == nil {
  673. p = default_assertion_failure_proc
  674. }
  675. p("not yet implemented", message, loc)
  676. }