core_builtin.odin 32 KB

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