mem.odin 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package mem
  2. import "core:runtime"
  3. import "core:intrinsics"
  4. Byte :: runtime.Byte
  5. Kilobyte :: runtime.Kilobyte
  6. Megabyte :: runtime.Megabyte
  7. Gigabyte :: runtime.Gigabyte
  8. Terabyte :: runtime.Terabyte
  9. Petabyte :: runtime.Petabyte
  10. Exabyte :: runtime.Exabyte
  11. set :: proc "contextless" (data: rawptr, value: byte, len: int) -> rawptr {
  12. return runtime.memset(data, i32(value), len)
  13. }
  14. zero :: proc "contextless" (data: rawptr, len: int) -> rawptr {
  15. intrinsics.mem_zero(data, len)
  16. return data
  17. }
  18. zero_explicit :: proc "contextless" (data: rawptr, len: int) -> rawptr {
  19. // This routine tries to avoid the compiler optimizing away the call,
  20. // so that it is always executed. It is intended to provided
  21. // equivalent semantics to those provided by the C11 Annex K 3.7.4.1
  22. // memset_s call.
  23. intrinsics.mem_zero_volatile(data, len) // Use the volatile mem_zero
  24. intrinsics.atomic_thread_fence(.Seq_Cst) // Prevent reordering
  25. return data
  26. }
  27. zero_item :: proc "contextless" (item: $P/^$T) -> P {
  28. intrinsics.mem_zero(item, size_of(T))
  29. return item
  30. }
  31. zero_slice :: proc "contextless" (data: $T/[]$E) -> T {
  32. zero(raw_data(data), size_of(E)*len(data))
  33. return data
  34. }
  35. copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
  36. intrinsics.mem_copy(dst, src, len)
  37. return dst
  38. }
  39. copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr {
  40. intrinsics.mem_copy_non_overlapping(dst, src, len)
  41. return dst
  42. }
  43. compare :: proc "contextless" (a, b: []byte) -> int {
  44. res := compare_byte_ptrs(raw_data(a), raw_data(b), min(len(a), len(b)))
  45. if res == 0 && len(a) != len(b) {
  46. return len(a) <= len(b) ? -1 : +1
  47. } else if len(a) == 0 && len(b) == 0 {
  48. return 0
  49. }
  50. return res
  51. }
  52. @(require_results)
  53. compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int #no_bounds_check {
  54. return runtime.memory_compare(a, b, n)
  55. }
  56. @(require_results)
  57. check_zero :: proc(data: []byte) -> bool {
  58. return check_zero_ptr(raw_data(data), len(data))
  59. }
  60. @(require_results)
  61. check_zero_ptr :: proc(ptr: rawptr, len: int) -> bool {
  62. switch {
  63. case len <= 0:
  64. return true
  65. case ptr == nil:
  66. return true
  67. }
  68. switch len {
  69. case 1: return (^u8)(ptr)^ == 0
  70. case 2: return intrinsics.unaligned_load((^u16)(ptr)) == 0
  71. case 4: return intrinsics.unaligned_load((^u32)(ptr)) == 0
  72. case 8: return intrinsics.unaligned_load((^u64)(ptr)) == 0
  73. }
  74. start := uintptr(ptr)
  75. start_aligned := align_forward_uintptr(start, align_of(uintptr))
  76. end := start + uintptr(len)
  77. end_aligned := align_backward_uintptr(end, align_of(uintptr))
  78. for b in start..<start_aligned {
  79. if (^byte)(b)^ != 0 {
  80. return false
  81. }
  82. }
  83. for b := start_aligned; b < end_aligned; b += size_of(uintptr) {
  84. if (^uintptr)(b)^ != 0 {
  85. return false
  86. }
  87. }
  88. for b in end_aligned..<end {
  89. if (^byte)(b)^ != 0 {
  90. return false
  91. }
  92. }
  93. return true
  94. }
  95. @(require_results)
  96. simple_equal :: proc "contextless" (a, b: $T) -> bool where intrinsics.type_is_simple_compare(T) {
  97. a, b := a, b
  98. return compare_byte_ptrs((^byte)(&a), (^byte)(&b), size_of(T)) == 0
  99. }
  100. @(require_results)
  101. compare_ptrs :: proc "contextless" (a, b: rawptr, n: int) -> int {
  102. return compare_byte_ptrs((^byte)(a), (^byte)(b), n)
  103. }
  104. ptr_offset :: intrinsics.ptr_offset
  105. ptr_sub :: intrinsics.ptr_sub
  106. @(require_results)
  107. slice_ptr :: proc "contextless" (ptr: ^$T, len: int) -> []T {
  108. return ([^]T)(ptr)[:len]
  109. }
  110. @(require_results)
  111. byte_slice :: #force_inline proc "contextless" (data: rawptr, #any_int len: int) -> []byte {
  112. return ([^]u8)(data)[:max(len, 0)]
  113. }
  114. @(require_results)
  115. slice_to_bytes :: proc "contextless" (slice: $E/[]$T) -> []byte {
  116. s := transmute(Raw_Slice)slice
  117. s.len *= size_of(T)
  118. return transmute([]byte)s
  119. }
  120. @(require_results)
  121. slice_data_cast :: proc "contextless" ($T: typeid/[]$A, slice: $S/[]$B) -> T {
  122. when size_of(A) == 0 || size_of(B) == 0 {
  123. return nil
  124. } else {
  125. s := transmute(Raw_Slice)slice
  126. s.len = (len(slice) * size_of(B)) / size_of(A)
  127. return transmute(T)s
  128. }
  129. }
  130. @(require_results)
  131. slice_to_components :: proc "contextless" (slice: $E/[]$T) -> (data: ^T, len: int) {
  132. s := transmute(Raw_Slice)slice
  133. return (^T)(s.data), s.len
  134. }
  135. @(require_results)
  136. buffer_from_slice :: proc "contextless" (backing: $T/[]$E) -> [dynamic]E {
  137. return transmute([dynamic]E)Raw_Dynamic_Array{
  138. data = raw_data(backing),
  139. len = 0,
  140. cap = len(backing),
  141. allocator = Allocator{
  142. procedure = nil_allocator_proc,
  143. data = nil,
  144. },
  145. }
  146. }
  147. @(require_results)
  148. ptr_to_bytes :: proc "contextless" (ptr: ^$T, len := 1) -> []byte {
  149. return transmute([]byte)Raw_Slice{ptr, len*size_of(T)}
  150. }
  151. @(require_results)
  152. any_to_bytes :: proc "contextless" (val: any) -> []byte {
  153. ti := type_info_of(val.id)
  154. size := ti != nil ? ti.size : 0
  155. return transmute([]byte)Raw_Slice{val.data, size}
  156. }
  157. @(require_results)
  158. is_power_of_two :: proc "contextless" (x: uintptr) -> bool {
  159. if x <= 0 {
  160. return false
  161. }
  162. return (x & (x-1)) == 0
  163. }
  164. @(require_results)
  165. align_forward :: proc(ptr: rawptr, align: uintptr) -> rawptr {
  166. return rawptr(align_forward_uintptr(uintptr(ptr), align))
  167. }
  168. @(require_results)
  169. align_forward_uintptr :: proc(ptr, align: uintptr) -> uintptr {
  170. assert(is_power_of_two(align))
  171. p := ptr
  172. modulo := p & (align-1)
  173. if modulo != 0 {
  174. p += align - modulo
  175. }
  176. return p
  177. }
  178. @(require_results)
  179. align_forward_int :: proc(ptr, align: int) -> int {
  180. return int(align_forward_uintptr(uintptr(ptr), uintptr(align)))
  181. }
  182. @(require_results)
  183. align_forward_uint :: proc(ptr, align: uint) -> uint {
  184. return uint(align_forward_uintptr(uintptr(ptr), uintptr(align)))
  185. }
  186. @(require_results)
  187. align_backward :: proc(ptr: rawptr, align: uintptr) -> rawptr {
  188. return rawptr(align_backward_uintptr(uintptr(ptr), align))
  189. }
  190. @(require_results)
  191. align_backward_uintptr :: proc(ptr, align: uintptr) -> uintptr {
  192. return align_forward_uintptr(ptr - align + 1, align)
  193. }
  194. @(require_results)
  195. align_backward_int :: proc(ptr, align: int) -> int {
  196. return int(align_backward_uintptr(uintptr(ptr), uintptr(align)))
  197. }
  198. @(require_results)
  199. align_backward_uint :: proc(ptr, align: uint) -> uint {
  200. return uint(align_backward_uintptr(uintptr(ptr), uintptr(align)))
  201. }
  202. @(require_results)
  203. context_from_allocator :: proc(a: Allocator) -> type_of(context) {
  204. context.allocator = a
  205. return context
  206. }
  207. @(require_results)
  208. reinterpret_copy :: proc "contextless" ($T: typeid, ptr: rawptr) -> (value: T) {
  209. copy(&value, ptr, size_of(T))
  210. return
  211. }
  212. Fixed_Byte_Buffer :: distinct [dynamic]byte
  213. @(require_results)
  214. make_fixed_byte_buffer :: proc "contextless" (backing: []byte) -> Fixed_Byte_Buffer {
  215. s := transmute(Raw_Slice)backing
  216. d: Raw_Dynamic_Array
  217. d.data = s.data
  218. d.len = 0
  219. d.cap = s.len
  220. d.allocator = Allocator{
  221. procedure = nil_allocator_proc,
  222. data = nil,
  223. }
  224. return transmute(Fixed_Byte_Buffer)d
  225. }
  226. @(require_results)
  227. align_formula :: proc "contextless" (size, align: int) -> int {
  228. result := size + align-1
  229. return result - result%align
  230. }
  231. @(require_results)
  232. calc_padding_with_header :: proc "contextless" (ptr: uintptr, align: uintptr, header_size: int) -> int {
  233. p, a := ptr, align
  234. modulo := p & (a-1)
  235. padding := uintptr(0)
  236. if modulo != 0 {
  237. padding = a - modulo
  238. }
  239. needed_space := uintptr(header_size)
  240. if padding < needed_space {
  241. needed_space -= padding
  242. if needed_space & (a-1) > 0 {
  243. padding += align * (1+(needed_space/align))
  244. } else {
  245. padding += align * (needed_space/align)
  246. }
  247. }
  248. return int(padding)
  249. }
  250. @(require_results, deprecated="prefer 'slice.clone'")
  251. clone_slice :: proc(slice: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> (new_slice: T) {
  252. new_slice, _ = make(T, len(slice), allocator, loc)
  253. runtime.copy(new_slice, slice)
  254. return new_slice
  255. }