mem.odin 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package mem
  2. import "base:runtime"
  3. import "base: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. @(require_results)
  44. compare :: proc "contextless" (a, b: []byte) -> int {
  45. res := compare_byte_ptrs(raw_data(a), raw_data(b), min(len(a), len(b)))
  46. if res == 0 && len(a) != len(b) {
  47. return len(a) <= len(b) ? -1 : +1
  48. } else if len(a) == 0 && len(b) == 0 {
  49. return 0
  50. }
  51. return res
  52. }
  53. @(require_results)
  54. compare_byte_ptrs :: proc "contextless" (a, b: ^byte, n: int) -> int #no_bounds_check {
  55. return runtime.memory_compare(a, b, n)
  56. }
  57. @(require_results)
  58. check_zero :: proc(data: []byte) -> bool {
  59. return check_zero_ptr(raw_data(data), len(data))
  60. }
  61. @(require_results)
  62. check_zero_ptr :: proc(ptr: rawptr, len: int) -> bool {
  63. switch {
  64. case len <= 0:
  65. return true
  66. case ptr == nil:
  67. return true
  68. }
  69. switch len {
  70. case 1: return (^u8)(ptr)^ == 0
  71. case 2: return intrinsics.unaligned_load((^u16)(ptr)) == 0
  72. case 4: return intrinsics.unaligned_load((^u32)(ptr)) == 0
  73. case 8: return intrinsics.unaligned_load((^u64)(ptr)) == 0
  74. }
  75. start := uintptr(ptr)
  76. start_aligned := align_forward_uintptr(start, align_of(uintptr))
  77. end := start + uintptr(len)
  78. end_aligned := align_backward_uintptr(end, align_of(uintptr))
  79. for b in start..<start_aligned {
  80. if (^byte)(b)^ != 0 {
  81. return false
  82. }
  83. }
  84. for b := start_aligned; b < end_aligned; b += size_of(uintptr) {
  85. if (^uintptr)(b)^ != 0 {
  86. return false
  87. }
  88. }
  89. for b in end_aligned..<end {
  90. if (^byte)(b)^ != 0 {
  91. return false
  92. }
  93. }
  94. return true
  95. }
  96. @(require_results)
  97. simple_equal :: proc "contextless" (a, b: $T) -> bool where intrinsics.type_is_simple_compare(T) {
  98. a, b := a, b
  99. return compare_byte_ptrs((^byte)(&a), (^byte)(&b), size_of(T)) == 0
  100. }
  101. @(require_results)
  102. compare_ptrs :: proc "contextless" (a, b: rawptr, n: int) -> int {
  103. return compare_byte_ptrs((^byte)(a), (^byte)(b), n)
  104. }
  105. ptr_offset :: intrinsics.ptr_offset
  106. ptr_sub :: intrinsics.ptr_sub
  107. @(require_results)
  108. slice_ptr :: proc "contextless" (ptr: ^$T, len: int) -> []T {
  109. return ([^]T)(ptr)[:len]
  110. }
  111. @(require_results)
  112. byte_slice :: #force_inline proc "contextless" (data: rawptr, #any_int len: int) -> []byte {
  113. return ([^]u8)(data)[:max(len, 0)]
  114. }
  115. @(require_results)
  116. slice_to_bytes :: proc "contextless" (slice: $E/[]$T) -> []byte {
  117. s := transmute(Raw_Slice)slice
  118. s.len *= size_of(T)
  119. return transmute([]byte)s
  120. }
  121. @(require_results)
  122. slice_data_cast :: proc "contextless" ($T: typeid/[]$A, slice: $S/[]$B) -> T {
  123. when size_of(A) == 0 || size_of(B) == 0 {
  124. return nil
  125. } else {
  126. s := transmute(Raw_Slice)slice
  127. s.len = (len(slice) * size_of(B)) / size_of(A)
  128. return transmute(T)s
  129. }
  130. }
  131. @(require_results)
  132. slice_to_components :: proc "contextless" (slice: $E/[]$T) -> (data: ^T, len: int) {
  133. s := transmute(Raw_Slice)slice
  134. return (^T)(s.data), s.len
  135. }
  136. @(require_results)
  137. buffer_from_slice :: proc "contextless" (backing: $T/[]$E) -> [dynamic]E {
  138. return transmute([dynamic]E)Raw_Dynamic_Array{
  139. data = raw_data(backing),
  140. len = 0,
  141. cap = len(backing),
  142. allocator = Allocator{
  143. procedure = nil_allocator_proc,
  144. data = nil,
  145. },
  146. }
  147. }
  148. @(require_results)
  149. ptr_to_bytes :: proc "contextless" (ptr: ^$T, len := 1) -> []byte {
  150. return transmute([]byte)Raw_Slice{ptr, len*size_of(T)}
  151. }
  152. @(require_results)
  153. any_to_bytes :: proc "contextless" (val: any) -> []byte {
  154. ti := type_info_of(val.id)
  155. size := ti != nil ? ti.size : 0
  156. return transmute([]byte)Raw_Slice{val.data, size}
  157. }
  158. @(require_results)
  159. is_power_of_two :: proc "contextless" (x: uintptr) -> bool {
  160. if x <= 0 {
  161. return false
  162. }
  163. return (x & (x-1)) == 0
  164. }
  165. @(require_results)
  166. align_forward :: proc(ptr: rawptr, align: uintptr) -> rawptr {
  167. return rawptr(align_forward_uintptr(uintptr(ptr), align))
  168. }
  169. @(require_results)
  170. align_forward_uintptr :: proc(ptr, align: uintptr) -> uintptr {
  171. assert(is_power_of_two(align))
  172. p := ptr
  173. modulo := p & (align-1)
  174. if modulo != 0 {
  175. p += align - modulo
  176. }
  177. return p
  178. }
  179. @(require_results)
  180. align_forward_int :: proc(ptr, align: int) -> int {
  181. return int(align_forward_uintptr(uintptr(ptr), uintptr(align)))
  182. }
  183. @(require_results)
  184. align_forward_uint :: proc(ptr, align: uint) -> uint {
  185. return uint(align_forward_uintptr(uintptr(ptr), uintptr(align)))
  186. }
  187. @(require_results)
  188. align_backward :: proc(ptr: rawptr, align: uintptr) -> rawptr {
  189. return rawptr(align_backward_uintptr(uintptr(ptr), align))
  190. }
  191. @(require_results)
  192. align_backward_uintptr :: proc(ptr, align: uintptr) -> uintptr {
  193. return align_forward_uintptr(ptr - align + 1, align)
  194. }
  195. @(require_results)
  196. align_backward_int :: proc(ptr, align: int) -> int {
  197. return int(align_backward_uintptr(uintptr(ptr), uintptr(align)))
  198. }
  199. @(require_results)
  200. align_backward_uint :: proc(ptr, align: uint) -> uint {
  201. return uint(align_backward_uintptr(uintptr(ptr), uintptr(align)))
  202. }
  203. @(require_results)
  204. context_from_allocator :: proc(a: Allocator) -> type_of(context) {
  205. context.allocator = a
  206. return context
  207. }
  208. @(require_results)
  209. reinterpret_copy :: proc "contextless" ($T: typeid, ptr: rawptr) -> (value: T) {
  210. copy(&value, ptr, size_of(T))
  211. return
  212. }
  213. Fixed_Byte_Buffer :: distinct [dynamic]byte
  214. @(require_results)
  215. make_fixed_byte_buffer :: proc "contextless" (backing: []byte) -> Fixed_Byte_Buffer {
  216. s := transmute(Raw_Slice)backing
  217. d: Raw_Dynamic_Array
  218. d.data = s.data
  219. d.len = 0
  220. d.cap = s.len
  221. d.allocator = Allocator{
  222. procedure = nil_allocator_proc,
  223. data = nil,
  224. }
  225. return transmute(Fixed_Byte_Buffer)d
  226. }
  227. @(require_results)
  228. align_formula :: proc "contextless" (size, align: int) -> int {
  229. result := size + align-1
  230. return result - result%align
  231. }
  232. @(require_results)
  233. calc_padding_with_header :: proc "contextless" (ptr: uintptr, align: uintptr, header_size: int) -> int {
  234. p, a := ptr, align
  235. modulo := p & (a-1)
  236. padding := uintptr(0)
  237. if modulo != 0 {
  238. padding = a - modulo
  239. }
  240. needed_space := uintptr(header_size)
  241. if padding < needed_space {
  242. needed_space -= padding
  243. if needed_space & (a-1) > 0 {
  244. padding += align * (1+(needed_space/align))
  245. } else {
  246. padding += align * (needed_space/align)
  247. }
  248. }
  249. return int(padding)
  250. }
  251. @(require_results, deprecated="prefer 'slice.clone'")
  252. clone_slice :: proc(slice: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> (new_slice: T) {
  253. new_slice, _ = make(T, len(slice), allocator, loc)
  254. runtime.copy(new_slice, slice)
  255. return new_slice
  256. }