core_builtin.odin 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. package runtime
  2. import "base: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 "contextless" (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 "contextless" (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. @builtin
  150. non_zero_reserve :: proc{non_zero_reserve_dynamic_array}
  151. // `resize` will try to resize memory of a passed dynamic array to the requested element count (setting the `len`, and possibly `cap`).
  152. @builtin
  153. resize :: proc{resize_dynamic_array}
  154. @builtin
  155. non_zero_resize :: proc{non_zero_resize_dynamic_array}
  156. // Shrinks the capacity of a dynamic array or map down to the current length, or the given capacity.
  157. @builtin
  158. shrink :: proc{shrink_dynamic_array, shrink_map}
  159. // `free` will try to free the passed pointer, with the given `allocator` if the allocator supports this operation.
  160. @builtin
  161. free :: proc{mem_free}
  162. // `free_all` will try to free/reset all of the memory of the given `allocator` if the allocator supports this operation.
  163. @builtin
  164. free_all :: proc{mem_free_all}
  165. // `delete_string` will try to free the underlying data of the passed string, with the given `allocator` if the allocator supports this operation.
  166. //
  167. // Note: Prefer the procedure group `delete`.
  168. @builtin
  169. delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  170. return mem_free_with_size(raw_data(str), len(str), allocator, loc)
  171. }
  172. // `delete_cstring` will try to free the underlying data of the passed string, with the given `allocator` if the allocator supports this operation.
  173. //
  174. // Note: Prefer the procedure group `delete`.
  175. @builtin
  176. delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  177. return mem_free((^byte)(str), allocator, loc)
  178. }
  179. // `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.
  180. //
  181. // Note: Prefer the procedure group `delete`.
  182. @builtin
  183. delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error {
  184. return mem_free_with_size(raw_data(array), cap(array)*size_of(E), array.allocator, loc)
  185. }
  186. // `delete_slice` will try to free the underlying data of the passed sliced, with the given `allocator` if the allocator supports this operation.
  187. //
  188. // Note: Prefer the procedure group `delete`.
  189. @builtin
  190. delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  191. return mem_free_with_size(raw_data(array), len(array)*size_of(E), allocator, loc)
  192. }
  193. // `delete_map` will try to free the underlying data of the passed map, with the given `allocator` if the allocator supports this operation.
  194. //
  195. // Note: Prefer the procedure group `delete`.
  196. @builtin
  197. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error {
  198. return map_free_dynamic(transmute(Raw_Map)m, map_info(T), loc)
  199. }
  200. // `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.
  201. //
  202. // Note: Prefer `delete` over the specific `delete_*` procedures where possible.
  203. @builtin
  204. delete :: proc{
  205. delete_string,
  206. delete_cstring,
  207. delete_dynamic_array,
  208. delete_slice,
  209. delete_map,
  210. delete_soa_slice,
  211. delete_soa_dynamic_array,
  212. }
  213. // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
  214. // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
  215. @(builtin, require_results)
  216. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_allocator_error {
  217. return new_aligned(T, align_of(T), allocator, loc)
  218. }
  219. @(require_results)
  220. new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  221. data := mem_alloc_bytes(size_of(T), alignment, allocator, loc) or_return
  222. t = (^T)(raw_data(data))
  223. return
  224. }
  225. @(builtin, require_results)
  226. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_allocator_error {
  227. t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
  228. t = (^T)(raw_data(t_data))
  229. if t != nil {
  230. t^ = data
  231. }
  232. return
  233. }
  234. DEFAULT_RESERVE_CAPACITY :: 16
  235. @(require_results)
  236. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  237. make_slice_error_loc(loc, len)
  238. data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
  239. if data == nil && size_of(E) != 0 {
  240. return nil, err
  241. }
  242. s := Raw_Slice{raw_data(data), len}
  243. return transmute(T)s, err
  244. }
  245. // `make_slice` allocates and initializes a slice. Like `new`, the first argument is a type, not a value.
  246. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  247. //
  248. // Note: Prefer using the procedure group `make`.
  249. @(builtin, require_results)
  250. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  251. return make_aligned(T, len, align_of(E), allocator, loc)
  252. }
  253. // `make_dynamic_array` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  254. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  255. //
  256. // Note: Prefer using the procedure group `make`.
  257. @(builtin, require_results)
  258. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  259. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
  260. }
  261. // `make_dynamic_array_len` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  262. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  263. //
  264. // Note: Prefer using the procedure group `make`.
  265. @(builtin, require_results)
  266. 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 {
  267. return make_dynamic_array_len_cap(T, len, len, allocator, loc)
  268. }
  269. // `make_dynamic_array_len_cap` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  270. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  271. //
  272. // Note: Prefer using the procedure group `make`.
  273. @(builtin, require_results)
  274. 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 {
  275. make_dynamic_array_error_loc(loc, len, cap)
  276. array.allocator = allocator // initialize allocator before just in case it fails to allocate any memory
  277. data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
  278. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
  279. if data == nil && size_of(E) != 0 {
  280. s.len, s.cap = 0, 0
  281. }
  282. array = transmute(T)s
  283. return
  284. }
  285. // `make_map` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  286. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  287. //
  288. // Note: Prefer using the procedure group `make`.
  289. @(builtin, require_results)
  290. 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 {
  291. make_map_expr_error_loc(loc, capacity)
  292. context.allocator = allocator
  293. err = reserve_map(&m, capacity, loc)
  294. return
  295. }
  296. // `make_multi_pointer` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  297. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  298. //
  299. // This is "similar" to doing `raw_data(make([]E, len, allocator))`.
  300. //
  301. // Note: Prefer using the procedure group `make`.
  302. @(builtin, require_results)
  303. 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 {
  304. make_slice_error_loc(loc, len)
  305. data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
  306. if data == nil && size_of(E) != 0 {
  307. return
  308. }
  309. mp = cast(T)raw_data(data)
  310. return
  311. }
  312. // `make` built-in procedure allocates and initializes a value of type slice, dynamic array, map, or multi-pointer (only).
  313. //
  314. // Similar to `new`, the first argument is a type, not a value. Unlike new, make's return type is the same as the
  315. // type of its argument, not a pointer to it.
  316. // Make uses the specified allocator, default is context.allocator.
  317. @builtin
  318. make :: proc{
  319. make_slice,
  320. make_dynamic_array,
  321. make_dynamic_array_len,
  322. make_dynamic_array_len_cap,
  323. make_map,
  324. make_multi_pointer,
  325. }
  326. // `clear_map` will set the length of a passed map to `0`
  327. //
  328. // Note: Prefer the procedure group `clear`
  329. @builtin
  330. clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
  331. if m == nil {
  332. return
  333. }
  334. map_clear_dynamic((^Raw_Map)(m), map_info(T))
  335. }
  336. // `reserve_map` will try to reserve memory of a passed map to the requested element count (setting the `cap`).
  337. //
  338. // Note: Prefer the procedure group `reserve`
  339. @builtin
  340. reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) -> Allocator_Error {
  341. return __dynamic_map_reserve((^Raw_Map)(m), map_info(T), uint(capacity), loc) if m != nil else nil
  342. }
  343. // Shrinks the capacity of a map down to the current length.
  344. //
  345. // Note: Prefer the procedure group `shrink`
  346. @builtin
  347. shrink_map :: proc(m: ^$T/map[$K]$V, loc := #caller_location) -> (did_shrink: bool, err: Allocator_Error) {
  348. if m != nil {
  349. return map_shrink_dynamic((^Raw_Map)(m), map_info(T), loc)
  350. }
  351. return
  352. }
  353. // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
  354. // If m is nil, or there is no such element, this procedure is a no-op
  355. @builtin
  356. delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) {
  357. if m != nil {
  358. key := key
  359. old_k, old_v, ok := map_erase_dynamic((^Raw_Map)(m), map_info(T), uintptr(&key))
  360. if ok {
  361. deleted_key = (^K)(old_k)^
  362. deleted_value = (^V)(old_v)^
  363. }
  364. }
  365. return
  366. }
  367. _append_elem :: #force_inline proc(array: ^$T/[dynamic]$E, arg: E, should_zero: bool, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  368. if array == nil {
  369. return 0, nil
  370. }
  371. when size_of(E) == 0 {
  372. array := (^Raw_Dynamic_Array)(array)
  373. array.len += 1
  374. return 1, nil
  375. } else {
  376. if cap(array) < len(array)+1 {
  377. cap := 2 * cap(array) + max(8, 1)
  378. // do not 'or_return' here as it could be a partial success
  379. if should_zero {
  380. err = reserve(array, cap, loc)
  381. } else {
  382. err = non_zero_reserve(array, cap, loc)
  383. }
  384. }
  385. if cap(array)-len(array) > 0 {
  386. a := (^Raw_Dynamic_Array)(array)
  387. when size_of(E) != 0 {
  388. data := ([^]E)(a.data)
  389. assert(data != nil, loc=loc)
  390. data[a.len] = arg
  391. }
  392. a.len += 1
  393. return 1, err
  394. }
  395. return 0, err
  396. }
  397. }
  398. @builtin
  399. append_elem :: proc(array: ^$T/[dynamic]$E, #no_broadcast arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  400. return _append_elem(array, arg, true, loc=loc)
  401. }
  402. @builtin
  403. non_zero_append_elem :: proc(array: ^$T/[dynamic]$E, #no_broadcast arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  404. return _append_elem(array, arg, false, loc=loc)
  405. }
  406. _append_elems :: #force_inline proc(array: ^$T/[dynamic]$E, should_zero: bool, loc := #caller_location, args: ..E) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  407. if array == nil {
  408. return 0, nil
  409. }
  410. arg_len := len(args)
  411. if arg_len <= 0 {
  412. return 0, nil
  413. }
  414. when size_of(E) == 0 {
  415. array := (^Raw_Dynamic_Array)(array)
  416. array.len += arg_len
  417. return arg_len, nil
  418. } else {
  419. if cap(array) < len(array)+arg_len {
  420. cap := 2 * cap(array) + max(8, arg_len)
  421. // do not 'or_return' here as it could be a partial success
  422. if should_zero {
  423. err = reserve(array, cap, loc)
  424. } else {
  425. err = non_zero_reserve(array, cap, loc)
  426. }
  427. }
  428. arg_len = min(cap(array)-len(array), arg_len)
  429. if arg_len > 0 {
  430. a := (^Raw_Dynamic_Array)(array)
  431. when size_of(E) != 0 {
  432. data := ([^]E)(a.data)
  433. assert(data != nil, loc=loc)
  434. intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len)
  435. }
  436. a.len += arg_len
  437. }
  438. return arg_len, err
  439. }
  440. }
  441. @builtin
  442. append_elems :: proc(array: ^$T/[dynamic]$E, #no_broadcast args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  443. return _append_elems(array, true, loc, ..args)
  444. }
  445. @builtin
  446. non_zero_append_elems :: proc(array: ^$T/[dynamic]$E, #no_broadcast args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  447. return _append_elems(array, false, loc, ..args)
  448. }
  449. // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
  450. _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 {
  451. args := transmute([]E)arg
  452. if should_zero {
  453. return append_elems(array, ..args, loc=loc)
  454. } else {
  455. return non_zero_append_elems(array, ..args, loc=loc)
  456. }
  457. }
  458. @builtin
  459. append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  460. return _append_elem_string(array, arg, true, loc)
  461. }
  462. @builtin
  463. 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 {
  464. return _append_elem_string(array, arg, false, loc)
  465. }
  466. // The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
  467. @builtin
  468. append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  469. n_arg: int
  470. for arg in args {
  471. n_arg, err = append(array, ..transmute([]E)(arg), loc=loc)
  472. n += n_arg
  473. if err != nil {
  474. return
  475. }
  476. }
  477. return
  478. }
  479. // The append built-in procedure appends elements to the end of a dynamic array
  480. @builtin append :: proc{append_elem, append_elems, append_elem_string}
  481. @builtin non_zero_append :: proc{non_zero_append_elem, non_zero_append_elems, non_zero_append_elem_string}
  482. @builtin
  483. append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  484. if array == nil {
  485. return 0, nil
  486. }
  487. prev_len := len(array)
  488. resize(array, len(array)+1, loc) or_return
  489. return len(array)-prev_len, nil
  490. }
  491. @builtin
  492. inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, #no_broadcast arg: E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
  493. if array == nil {
  494. return
  495. }
  496. n := max(len(array), index)
  497. m :: 1
  498. new_size := n + m
  499. resize(array, new_size, loc) or_return
  500. when size_of(E) != 0 {
  501. copy(array[index + m:], array[index:])
  502. array[index] = arg
  503. }
  504. ok = true
  505. return
  506. }
  507. @builtin
  508. inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, #no_broadcast args: ..E, loc := #caller_location) -> (ok: bool, err: Allocator_Error) #no_bounds_check #optional_allocator_error {
  509. if array == nil {
  510. return
  511. }
  512. if len(args) == 0 {
  513. ok = true
  514. return
  515. }
  516. n := max(len(array), index)
  517. m := len(args)
  518. new_size := n + m
  519. resize(array, new_size, loc) or_return
  520. when size_of(E) != 0 {
  521. copy(array[index + m:], array[index:])
  522. copy(array[index:], args)
  523. }
  524. ok = true
  525. return
  526. }
  527. @builtin
  528. 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 {
  529. if array == nil {
  530. return
  531. }
  532. if len(arg) == 0 {
  533. ok = true
  534. return
  535. }
  536. n := max(len(array), index)
  537. m := len(arg)
  538. new_size := n + m
  539. resize(array, new_size, loc) or_return
  540. copy(array[index+m:], array[index:])
  541. copy(array[index:], arg)
  542. ok = true
  543. return
  544. }
  545. @builtin inject_at :: proc{inject_at_elem, inject_at_elems, inject_at_elem_string}
  546. @builtin
  547. 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 {
  548. if index < len(array) {
  549. array[index] = arg
  550. ok = true
  551. } else {
  552. resize(array, index+1, loc) or_return
  553. array[index] = arg
  554. ok = true
  555. }
  556. return
  557. }
  558. @builtin
  559. 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 {
  560. new_size := index + len(args)
  561. if len(args) == 0 {
  562. ok = true
  563. } else if new_size < len(array) {
  564. copy(array[index:], args)
  565. ok = true
  566. } else {
  567. resize(array, new_size, loc) or_return
  568. copy(array[index:], args)
  569. ok = true
  570. }
  571. return
  572. }
  573. @builtin
  574. 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 {
  575. new_size := index + len(arg)
  576. if len(arg) == 0 {
  577. ok = true
  578. } else if new_size < len(array) {
  579. copy(array[index:], arg)
  580. ok = true
  581. } else {
  582. resize(array, new_size, loc) or_return
  583. copy(array[index:], arg)
  584. ok = true
  585. }
  586. return
  587. }
  588. @builtin assign_at :: proc{assign_at_elem, assign_at_elems, assign_at_elem_string}
  589. // `clear_dynamic_array` will set the length of a passed dynamic array to `0`
  590. //
  591. // Note: Prefer the procedure group `clear`.
  592. @builtin
  593. clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
  594. if array != nil {
  595. (^Raw_Dynamic_Array)(array).len = 0
  596. }
  597. }
  598. // `reserve_dynamic_array` will try to reserve memory of a passed dynamic array or map to the requested element count (setting the `cap`).
  599. //
  600. // Note: Prefer the procedure group `reserve`.
  601. _reserve_dynamic_array :: #force_inline proc(array: ^$T/[dynamic]$E, capacity: int, should_zero: bool, loc := #caller_location) -> Allocator_Error {
  602. if array == nil {
  603. return nil
  604. }
  605. a := (^Raw_Dynamic_Array)(array)
  606. if capacity <= a.cap {
  607. return nil
  608. }
  609. if a.allocator.procedure == nil {
  610. a.allocator = context.allocator
  611. }
  612. assert(a.allocator.procedure != nil)
  613. old_size := a.cap * size_of(E)
  614. new_size := capacity * size_of(E)
  615. allocator := a.allocator
  616. new_data: []byte
  617. if should_zero {
  618. new_data = mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return
  619. } else {
  620. new_data = non_zero_mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return
  621. }
  622. if new_data == nil && new_size > 0 {
  623. return .Out_Of_Memory
  624. }
  625. a.data = raw_data(new_data)
  626. a.cap = capacity
  627. return nil
  628. }
  629. @builtin
  630. reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> Allocator_Error {
  631. return _reserve_dynamic_array(array, capacity, true, loc)
  632. }
  633. @builtin
  634. non_zero_reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> Allocator_Error {
  635. return _reserve_dynamic_array(array, capacity, false, loc)
  636. }
  637. // `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`).
  638. //
  639. // Note: Prefer the procedure group `resize`
  640. _resize_dynamic_array :: #force_inline proc(array: ^$T/[dynamic]$E, length: int, should_zero: bool, loc := #caller_location) -> Allocator_Error {
  641. if array == nil {
  642. return nil
  643. }
  644. a := (^Raw_Dynamic_Array)(array)
  645. if length <= a.cap {
  646. if should_zero && a.len < length {
  647. intrinsics.mem_zero(([^]E)(a.data)[a.len:], (length-a.len)*size_of(E))
  648. }
  649. a.len = max(length, 0)
  650. return nil
  651. }
  652. if a.allocator.procedure == nil {
  653. a.allocator = context.allocator
  654. }
  655. assert(a.allocator.procedure != nil)
  656. old_size := a.cap * size_of(E)
  657. new_size := length * size_of(E)
  658. allocator := a.allocator
  659. new_data : []byte
  660. if should_zero {
  661. new_data = mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return
  662. } else {
  663. new_data = non_zero_mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return
  664. }
  665. if new_data == nil && new_size > 0 {
  666. return .Out_Of_Memory
  667. }
  668. a.data = raw_data(new_data)
  669. a.len = length
  670. a.cap = length
  671. return nil
  672. }
  673. @builtin
  674. resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> Allocator_Error {
  675. return _resize_dynamic_array(array, length, true, loc=loc)
  676. }
  677. @builtin
  678. non_zero_resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> Allocator_Error {
  679. return _resize_dynamic_array(array, length, false, loc=loc)
  680. }
  681. /*
  682. Shrinks the capacity of a dynamic array down to the current length, or the given capacity.
  683. If `new_cap` is negative, then `len(array)` is used.
  684. Returns false if `cap(array) < new_cap`, or the allocator report failure.
  685. If `len(array) < new_cap`, then `len(array)` will be left unchanged.
  686. Note: Prefer the procedure group `shrink`
  687. */
  688. shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #caller_location) -> (did_shrink: bool, err: Allocator_Error) {
  689. if array == nil {
  690. return
  691. }
  692. a := (^Raw_Dynamic_Array)(array)
  693. new_cap := new_cap if new_cap >= 0 else a.len
  694. if new_cap > a.cap {
  695. return
  696. }
  697. if a.allocator.procedure == nil {
  698. a.allocator = context.allocator
  699. }
  700. assert(a.allocator.procedure != nil)
  701. old_size := a.cap * size_of(E)
  702. new_size := new_cap * size_of(E)
  703. new_data := mem_resize(a.data, old_size, new_size, align_of(E), a.allocator, loc) or_return
  704. a.data = raw_data(new_data)
  705. a.len = min(new_cap, a.len)
  706. a.cap = new_cap
  707. return true, nil
  708. }
  709. @builtin
  710. map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (ptr: ^V) {
  711. key, value := key, value
  712. return (^V)(__dynamic_map_set_without_hash((^Raw_Map)(m), map_info(T), rawptr(&key), rawptr(&value), loc))
  713. }
  714. // Explicitly inserts a key and value into a map `m`, the same as `map_insert`, but the return values differ.
  715. // - `prev_key` will return the previous pointer of a key if it exists, check `found_previous` if was previously found
  716. // - `value_ptr` will return the pointer of the memory where the insertion happens, and `nil` if the map failed to resize
  717. // - `found_previous` will be true a previous key was found
  718. @(builtin, require_results)
  719. map_upsert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (prev_key: K, value_ptr: ^V, found_previous: bool) {
  720. key, value := key, value
  721. kp, vp := __dynamic_map_set_extra_without_hash((^Raw_Map)(m), map_info(T), rawptr(&key), rawptr(&value), loc)
  722. if kp != nil {
  723. prev_key = (^K)(kp)^
  724. found_previous = true
  725. }
  726. value_ptr = (^V)(vp)
  727. return
  728. }
  729. @builtin
  730. card :: proc "contextless" (s: $S/bit_set[$E; $U]) -> int {
  731. when size_of(S) == 1 {
  732. return int(intrinsics.count_ones(transmute(u8)s))
  733. } else when size_of(S) == 2 {
  734. return int(intrinsics.count_ones(transmute(u16)s))
  735. } else when size_of(S) == 4 {
  736. return int(intrinsics.count_ones(transmute(u32)s))
  737. } else when size_of(S) == 8 {
  738. return int(intrinsics.count_ones(transmute(u64)s))
  739. } else when size_of(S) == 16 {
  740. return int(intrinsics.count_ones(transmute(u128)s))
  741. } else {
  742. #panic("Unhandled card bit_set size")
  743. }
  744. }
  745. @builtin
  746. @(disabled=ODIN_DISABLE_ASSERT)
  747. assert :: proc(condition: bool, message := "", loc := #caller_location) {
  748. if !condition {
  749. // NOTE(bill): This is wrapped in a procedure call
  750. // to improve performance to make the CPU not
  751. // execute speculatively, making it about an order of
  752. // magnitude faster
  753. @(cold)
  754. internal :: proc(message: string, loc: Source_Code_Location) {
  755. p := context.assertion_failure_proc
  756. if p == nil {
  757. p = default_assertion_failure_proc
  758. }
  759. p("runtime assertion", message, loc)
  760. }
  761. internal(message, loc)
  762. }
  763. }
  764. @builtin
  765. panic :: proc(message: string, loc := #caller_location) -> ! {
  766. p := context.assertion_failure_proc
  767. if p == nil {
  768. p = default_assertion_failure_proc
  769. }
  770. p("panic", message, loc)
  771. }
  772. @builtin
  773. unimplemented :: proc(message := "", loc := #caller_location) -> ! {
  774. p := context.assertion_failure_proc
  775. if p == nil {
  776. p = default_assertion_failure_proc
  777. }
  778. p("not yet implemented", message, loc)
  779. }