mem.odin 7.5 KB

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