mem.odin 7.4 KB

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