core_builtin.odin 27 KB

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