mem.odin 6.9 KB

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