core_builtin.odin 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 as 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. }
  207. // The new built-in procedure allocates memory. The first argument is a type, not a value, and the value
  208. // return is a pointer to a newly allocated value of that type using the specified allocator, default is context.allocator
  209. @(builtin, require_results)
  210. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) #optional_allocator_error {
  211. return new_aligned(T, align_of(T), allocator, loc)
  212. }
  213. @(require_results)
  214. new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  215. data := mem_alloc_bytes(size_of(T), alignment, allocator, loc) or_return
  216. t = (^T)(raw_data(data))
  217. return
  218. }
  219. @(builtin, require_results)
  220. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) #optional_allocator_error {
  221. t_data := mem_alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
  222. t = (^T)(raw_data(t_data))
  223. if t != nil {
  224. t^ = data
  225. }
  226. return
  227. }
  228. DEFAULT_RESERVE_CAPACITY :: 16
  229. @(require_results)
  230. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  231. make_slice_error_loc(loc, len)
  232. data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc)
  233. if data == nil && size_of(E) != 0 {
  234. return nil, err
  235. }
  236. s := Raw_Slice{raw_data(data), len}
  237. return transmute(T)s, err
  238. }
  239. // `make_slice` allocates and initializes a slice. Like `new`, the first argument is a type, not a value.
  240. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  241. //
  242. // Note: Prefer using the procedure group `make`.
  243. @(builtin, require_results)
  244. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  245. return make_aligned(T, len, align_of(E), allocator, loc)
  246. }
  247. // `make_dynamic_array` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  248. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  249. //
  250. // Note: Prefer using the procedure group `make`.
  251. @(builtin, require_results)
  252. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_allocator_error {
  253. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
  254. }
  255. // `make_dynamic_array_len` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  256. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  257. //
  258. // Note: Prefer using the procedure group `make`.
  259. @(builtin, require_results)
  260. 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 {
  261. return make_dynamic_array_len_cap(T, len, len, allocator, loc)
  262. }
  263. // `make_dynamic_array_len_cap` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  264. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  265. //
  266. // Note: Prefer using the procedure group `make`.
  267. @(builtin, require_results)
  268. 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 {
  269. make_dynamic_array_error_loc(loc, len, cap)
  270. data := mem_alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
  271. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
  272. if data == nil && size_of(E) != 0 {
  273. s.len, s.cap = 0, 0
  274. }
  275. array = transmute(T)s
  276. return
  277. }
  278. // `make_map` 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_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 {
  284. make_map_expr_error_loc(loc, capacity)
  285. context.allocator = allocator
  286. err = reserve_map(&m, capacity, loc)
  287. return
  288. }
  289. // `make_multi_pointer` allocates and initializes a dynamic array. Like `new`, the first argument is a type, not a value.
  290. // Unlike `new`, `make`'s return value is the same as the type of its argument, not a pointer to it.
  291. //
  292. // This is "similar" to doing `raw_data(make([]E, len, allocator))`.
  293. //
  294. // Note: Prefer using the procedure group `make`.
  295. @(builtin, require_results)
  296. 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 {
  297. make_slice_error_loc(loc, len)
  298. data := mem_alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
  299. if data == nil && size_of(E) != 0 {
  300. return
  301. }
  302. mp = cast(T)raw_data(data)
  303. return
  304. }
  305. // `make` built-in procedure allocates and initializes a value of type slice, dynamic array, map, or multi-pointer (only).
  306. //
  307. // Similar to `new`, the first argument is a type, not a value. Unlike new, make's return type is the same as the
  308. // type of its argument, not a pointer to it.
  309. // Make uses the specified allocator, default is context.allocator, default is context.allocator
  310. @builtin
  311. make :: proc{
  312. make_slice,
  313. make_dynamic_array,
  314. make_dynamic_array_len,
  315. make_dynamic_array_len_cap,
  316. make_map,
  317. make_multi_pointer,
  318. }
  319. // `clear_map` will set the length of a passed map to `0`
  320. //
  321. // Note: Prefer the procedure group `clear`
  322. @builtin
  323. clear_map :: proc "contextless" (m: ^$T/map[$K]$V) {
  324. if m == nil {
  325. return
  326. }
  327. map_clear_dynamic((^Raw_Map)(m), map_info(T))
  328. }
  329. // `reserve_map` will try to reserve memory of a passed map to the requested element count (setting the `cap`).
  330. //
  331. // Note: Prefer the procedure group `reserve`
  332. @builtin
  333. reserve_map :: proc(m: ^$T/map[$K]$V, capacity: int, loc := #caller_location) -> Allocator_Error {
  334. return __dynamic_map_reserve((^Raw_Map)(m), map_info(T), uint(capacity), loc) if m != nil else nil
  335. }
  336. // Shrinks the capacity of a map down to the current length.
  337. //
  338. // Note: Prefer the procedure group `shrink`
  339. @builtin
  340. shrink_map :: proc(m: ^$T/map[$K]$V, loc := #caller_location) -> (did_shrink: bool, err: Allocator_Error) {
  341. if m != nil {
  342. return map_shrink_dynamic((^Raw_Map)(m), map_info(T), loc)
  343. }
  344. return
  345. }
  346. // The delete_key built-in procedure deletes the element with the specified key (m[key]) from the map.
  347. // If m is nil, or there is no such element, this procedure is a no-op
  348. @builtin
  349. delete_key :: proc(m: ^$T/map[$K]$V, key: K) -> (deleted_key: K, deleted_value: V) {
  350. if m != nil {
  351. key := key
  352. old_k, old_v, ok := map_erase_dynamic((^Raw_Map)(m), map_info(T), uintptr(&key))
  353. if ok {
  354. deleted_key = (^K)(old_k)^
  355. deleted_value = (^V)(old_v)^
  356. }
  357. }
  358. return
  359. }
  360. @builtin
  361. append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  362. if array == nil {
  363. return 0, nil
  364. }
  365. when size_of(E) == 0 {
  366. array := (^Raw_Dynamic_Array)(array)
  367. array.len += 1
  368. return 1, nil
  369. } else {
  370. if cap(array) < len(array)+1 {
  371. cap := 2 * cap(array) + max(8, 1)
  372. err = reserve(array, cap, loc) // do not 'or_return' here as it could be a partial success
  373. }
  374. if cap(array)-len(array) > 0 {
  375. a := (^Raw_Dynamic_Array)(array)
  376. when size_of(E) != 0 {
  377. data := ([^]E)(a.data)
  378. assert(data != nil, loc=loc)
  379. data[a.len] = arg
  380. }
  381. a.len += 1
  382. return 1, err
  383. }
  384. return 0, err
  385. }
  386. }
  387. @builtin
  388. append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  389. if array == nil {
  390. return 0, nil
  391. }
  392. arg_len := len(args)
  393. if arg_len <= 0 {
  394. return 0, nil
  395. }
  396. when size_of(E) == 0 {
  397. array := (^Raw_Dynamic_Array)(array)
  398. array.len += arg_len
  399. return arg_len, nil
  400. } else {
  401. if cap(array) < len(array)+arg_len {
  402. cap := 2 * cap(array) + max(8, arg_len)
  403. err = reserve(array, cap, loc) // do not 'or_return' here as it could be a partial success
  404. }
  405. arg_len = min(cap(array)-len(array), arg_len)
  406. if arg_len > 0 {
  407. a := (^Raw_Dynamic_Array)(array)
  408. when size_of(E) != 0 {
  409. data := ([^]E)(a.data)
  410. assert(data != nil, loc=loc)
  411. intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len)
  412. }
  413. a.len += arg_len
  414. }
  415. return arg_len, err
  416. }
  417. }
  418. // The append_string built-in procedure appends a string to the end of a [dynamic]u8 like type
  419. @builtin
  420. append_elem_string :: proc(array: ^$T/[dynamic]$E/u8, arg: $A/string, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  421. args := transmute([]E)arg
  422. return append_elems(array, ..args, loc=loc)
  423. }
  424. // The append_string built-in procedure appends multiple strings to the end of a [dynamic]u8 like type
  425. @builtin
  426. append_string :: proc(array: ^$T/[dynamic]$E/u8, args: ..string, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  427. n_arg: int
  428. for arg in args {
  429. n_arg, err = append(array, ..transmute([]E)(arg), loc=loc)
  430. n += n_arg
  431. if err != nil {
  432. return
  433. }
  434. }
  435. return
  436. }
  437. // The append built-in procedure appends elements to the end of a dynamic array
  438. @builtin append :: proc{append_elem, append_elems, append_elem_string}
  439. @builtin
  440. append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) -> (n: int, err: Allocator_Error) #optional_allocator_error {
  441. if array == nil {
  442. return 0, nil
  443. }
  444. prev_len := len(array)
  445. resize(array, len(array)+1, loc) or_return
  446. return len(array)-prev_len, nil
  447. }
  448. @builtin
  449. 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 {
  450. if array == nil {
  451. return
  452. }
  453. n := max(len(array), index)
  454. m :: 1
  455. new_size := n + m
  456. resize(array, new_size, loc) or_return
  457. when size_of(E) != 0 {
  458. copy(array[index + m:], array[index:])
  459. array[index] = arg
  460. }
  461. ok = true
  462. return
  463. }
  464. @builtin
  465. 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 {
  466. if array == nil {
  467. return
  468. }
  469. if len(args) == 0 {
  470. ok = true
  471. return
  472. }
  473. n := max(len(array), index)
  474. m := len(args)
  475. new_size := n + m
  476. resize(array, new_size, loc) or_return
  477. when size_of(E) != 0 {
  478. copy(array[index + m:], array[index:])
  479. copy(array[index:], args)
  480. }
  481. ok = true
  482. return
  483. }
  484. @builtin
  485. 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 {
  486. if array == nil {
  487. return
  488. }
  489. if len(arg) == 0 {
  490. ok = true
  491. return
  492. }
  493. n := max(len(array), index)
  494. m := len(arg)
  495. new_size := n + m
  496. resize(array, new_size, loc) or_return
  497. copy(array[index+m:], array[index:])
  498. copy(array[index:], arg)
  499. ok = true
  500. return
  501. }
  502. @builtin inject_at :: proc{inject_at_elem, inject_at_elems, inject_at_elem_string}
  503. @builtin
  504. 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 {
  505. if index < len(array) {
  506. array[index] = arg
  507. ok = true
  508. } else {
  509. resize(array, index+1, loc) or_return
  510. array[index] = arg
  511. ok = true
  512. }
  513. return
  514. }
  515. @builtin
  516. 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 {
  517. if index+len(args) < len(array) {
  518. copy(array[index:], args)
  519. ok = true
  520. } else {
  521. resize(array, index+1+len(args), loc) or_return
  522. copy(array[index:], args)
  523. ok = true
  524. }
  525. return
  526. }
  527. @builtin
  528. 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 {
  529. if len(args) == 0 {
  530. ok = true
  531. } else if index+len(args) < len(array) {
  532. copy(array[index:], args)
  533. ok = true
  534. } else {
  535. resize(array, index+1+len(args), loc) or_return
  536. copy(array[index:], args)
  537. ok = true
  538. }
  539. return
  540. }
  541. @builtin assign_at :: proc{assign_at_elem, assign_at_elems, assign_at_elem_string}
  542. // `clear_dynamic_array` will set the length of a passed dynamic array to `0`
  543. //
  544. // Note: Prefer the procedure group `clear`.
  545. @builtin
  546. clear_dynamic_array :: proc "contextless" (array: ^$T/[dynamic]$E) {
  547. if array != nil {
  548. (^Raw_Dynamic_Array)(array).len = 0
  549. }
  550. }
  551. // `reserve_dynamic_array` will try to reserve memory of a passed dynamic array or map to the requested element count (setting the `cap`).
  552. //
  553. // Note: Prefer the procedure group `reserve`.
  554. @builtin
  555. reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #caller_location) -> Allocator_Error {
  556. if array == nil {
  557. return nil
  558. }
  559. a := (^Raw_Dynamic_Array)(array)
  560. if capacity <= a.cap {
  561. return nil
  562. }
  563. if a.allocator.procedure == nil {
  564. a.allocator = context.allocator
  565. }
  566. assert(a.allocator.procedure != nil)
  567. old_size := a.cap * size_of(E)
  568. new_size := capacity * size_of(E)
  569. allocator := a.allocator
  570. new_data := mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return
  571. if new_data == nil && new_size > 0 {
  572. return .Out_Of_Memory
  573. }
  574. a.data = raw_data(new_data)
  575. a.cap = capacity
  576. return nil
  577. }
  578. // `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`).
  579. //
  580. // Note: Prefer the procedure group `resize`
  581. @builtin
  582. resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> Allocator_Error {
  583. if array == nil {
  584. return nil
  585. }
  586. a := (^Raw_Dynamic_Array)(array)
  587. if length <= a.cap {
  588. a.len = max(length, 0)
  589. return nil
  590. }
  591. if a.allocator.procedure == nil {
  592. a.allocator = context.allocator
  593. }
  594. assert(a.allocator.procedure != nil)
  595. old_size := a.cap * size_of(E)
  596. new_size := length * size_of(E)
  597. allocator := a.allocator
  598. new_data := mem_resize(a.data, old_size, new_size, align_of(E), allocator, loc) or_return
  599. if new_data == nil && new_size > 0 {
  600. return .Out_Of_Memory
  601. }
  602. a.data = raw_data(new_data)
  603. a.len = length
  604. a.cap = length
  605. return nil
  606. }
  607. /*
  608. Shrinks the capacity of a dynamic array down to the current length, or the given capacity.
  609. If `new_cap` is negative, then `len(array)` is used.
  610. Returns false if `cap(array) < new_cap`, or the allocator report failure.
  611. If `len(array) < new_cap`, then `len(array)` will be left unchanged.
  612. Note: Prefer the procedure group `shrink`
  613. */
  614. shrink_dynamic_array :: proc(array: ^$T/[dynamic]$E, new_cap := -1, loc := #caller_location) -> (did_shrink: bool, err: Allocator_Error) {
  615. if array == nil {
  616. return
  617. }
  618. a := (^Raw_Dynamic_Array)(array)
  619. new_cap := new_cap if new_cap >= 0 else a.len
  620. if new_cap > a.cap {
  621. return
  622. }
  623. if a.allocator.procedure == nil {
  624. a.allocator = context.allocator
  625. }
  626. assert(a.allocator.procedure != nil)
  627. old_size := a.cap * size_of(E)
  628. new_size := new_cap * size_of(E)
  629. new_data := mem_resize(a.data, old_size, new_size, align_of(E), a.allocator, loc) or_return
  630. a.data = raw_data(new_data)
  631. a.len = min(new_cap, a.len)
  632. a.cap = new_cap
  633. return true, nil
  634. }
  635. @builtin
  636. map_insert :: proc(m: ^$T/map[$K]$V, key: K, value: V, loc := #caller_location) -> (ptr: ^V) {
  637. key, value := key, value
  638. return (^V)(__dynamic_map_set_without_hash((^Raw_Map)(m), map_info(T), rawptr(&key), rawptr(&value), loc))
  639. }
  640. @builtin
  641. incl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  642. s^ |= {elem}
  643. }
  644. @builtin
  645. incl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  646. for elem in elems {
  647. s^ |= {elem}
  648. }
  649. }
  650. @builtin
  651. incl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  652. s^ |= other
  653. }
  654. @builtin
  655. excl_elem :: proc(s: ^$S/bit_set[$E; $U], elem: E) {
  656. s^ &~= {elem}
  657. }
  658. @builtin
  659. excl_elems :: proc(s: ^$S/bit_set[$E; $U], elems: ..E) {
  660. for elem in elems {
  661. s^ &~= {elem}
  662. }
  663. }
  664. @builtin
  665. excl_bit_set :: proc(s: ^$S/bit_set[$E; $U], other: S) {
  666. s^ &~= other
  667. }
  668. @builtin incl :: proc{incl_elem, incl_elems, incl_bit_set}
  669. @builtin excl :: proc{excl_elem, excl_elems, excl_bit_set}
  670. @builtin
  671. card :: proc(s: $S/bit_set[$E; $U]) -> int {
  672. when size_of(S) == 1 {
  673. return int(intrinsics.count_ones(transmute(u8)s))
  674. } else when size_of(S) == 2 {
  675. return int(intrinsics.count_ones(transmute(u16)s))
  676. } else when size_of(S) == 4 {
  677. return int(intrinsics.count_ones(transmute(u32)s))
  678. } else when size_of(S) == 8 {
  679. return int(intrinsics.count_ones(transmute(u64)s))
  680. } else when size_of(S) == 16 {
  681. return int(intrinsics.count_ones(transmute(u128)s))
  682. } else {
  683. #panic("Unhandled card bit_set size")
  684. }
  685. }
  686. @builtin
  687. @(disabled=ODIN_DISABLE_ASSERT)
  688. assert :: proc(condition: bool, message := "", loc := #caller_location) {
  689. if !condition {
  690. // NOTE(bill): This is wrapped in a procedure call
  691. // to improve performance to make the CPU not
  692. // execute speculatively, making it about an order of
  693. // magnitude faster
  694. @(cold)
  695. internal :: proc(message: string, loc: Source_Code_Location) {
  696. p := context.assertion_failure_proc
  697. if p == nil {
  698. p = default_assertion_failure_proc
  699. }
  700. p("runtime assertion", message, loc)
  701. }
  702. internal(message, loc)
  703. }
  704. }
  705. @builtin
  706. @(disabled=ODIN_DISABLE_ASSERT)
  707. panic :: proc(message: string, loc := #caller_location) -> ! {
  708. p := context.assertion_failure_proc
  709. if p == nil {
  710. p = default_assertion_failure_proc
  711. }
  712. p("panic", message, loc)
  713. }
  714. @builtin
  715. @(disabled=ODIN_DISABLE_ASSERT)
  716. unimplemented :: proc(message := "", loc := #caller_location) -> ! {
  717. p := context.assertion_failure_proc
  718. if p == nil {
  719. p = default_assertion_failure_proc
  720. }
  721. p("not yet implemented", message, loc)
  722. }