mem.odin 6.9 KB

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