mem.odin 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. slice_ptr_to_bytes :: proc(data: rawptr, len: int) -> []byte {
  122. return transmute([]u8)Raw_Slice{data=data, len=max(len, 0)};
  123. }
  124. slice_to_bytes :: proc(slice: $E/[]$T) -> []byte {
  125. s := transmute(Raw_Slice)slice;
  126. s.len *= size_of(T);
  127. return transmute([]byte)s;
  128. }
  129. slice_data_cast :: proc($T: typeid/[]$A, slice: $S/[]$B) -> T {
  130. when size_of(A) == 0 || size_of(B) == 0 {
  131. return nil;
  132. } else {
  133. s := transmute(Raw_Slice)slice;
  134. s.len = (len(slice) * size_of(B)) / size_of(A);
  135. return transmute(T)s;
  136. }
  137. }
  138. slice_to_components :: proc(slice: $E/[]$T) -> (data: ^T, len: int) {
  139. s := transmute(Raw_Slice)slice;
  140. return s.data, s.len;
  141. }
  142. buffer_from_slice :: proc(backing: $T/[]$E) -> [dynamic]E {
  143. return transmute([dynamic]E)Raw_Dynamic_Array{
  144. data = raw_data(backing),
  145. len = 0,
  146. cap = len(backing),
  147. allocator = nil_allocator(),
  148. };
  149. }
  150. ptr_to_bytes :: proc(ptr: ^$T, len := 1) -> []byte {
  151. assert(len >= 0);
  152. return transmute([]byte)Raw_Slice{ptr, len*size_of(T)};
  153. }
  154. any_to_bytes :: proc(val: any) -> []byte {
  155. ti := type_info_of(val.id);
  156. size := ti != nil ? ti.size : 0;
  157. return transmute([]byte)Raw_Slice{val.data, size};
  158. }
  159. kilobytes :: proc(x: int) -> int { return (x) * 1024; }
  160. megabytes :: proc(x: int) -> int { return kilobytes(x) * 1024; }
  161. gigabytes :: proc(x: int) -> int { return megabytes(x) * 1024; }
  162. terabytes :: proc(x: int) -> int { return gigabytes(x) * 1024; }
  163. is_power_of_two :: proc(x: uintptr) -> bool {
  164. if x <= 0 {
  165. return false;
  166. }
  167. return (x & (x-1)) == 0;
  168. }
  169. align_forward :: proc(ptr: rawptr, align: uintptr) -> rawptr {
  170. return rawptr(align_forward_uintptr(uintptr(ptr), align));
  171. }
  172. align_forward_uintptr :: proc(ptr, align: uintptr) -> uintptr {
  173. assert(is_power_of_two(align));
  174. p := ptr;
  175. modulo := p & (align-1);
  176. if modulo != 0 {
  177. p += align - modulo;
  178. }
  179. return p;
  180. }
  181. align_forward_int :: proc(ptr, align: int) -> int {
  182. return int(align_forward_uintptr(uintptr(ptr), uintptr(align)));
  183. }
  184. align_forward_uint :: proc(ptr, align: uint) -> uint {
  185. return uint(align_forward_uintptr(uintptr(ptr), uintptr(align)));
  186. }
  187. align_backward :: proc(ptr: rawptr, align: uintptr) -> rawptr {
  188. return rawptr(align_backward_uintptr(uintptr(ptr), align));
  189. }
  190. align_backward_uintptr :: proc(ptr, align: uintptr) -> uintptr {
  191. assert(is_power_of_two(align));
  192. return align_forward_uintptr(ptr - align + 1, align);
  193. }
  194. align_backward_int :: proc(ptr, align: int) -> int {
  195. return int(align_backward_uintptr(uintptr(ptr), uintptr(align)));
  196. }
  197. align_backward_uint :: proc(ptr, align: uint) -> uint {
  198. return uint(align_backward_uintptr(uintptr(ptr), uintptr(align)));
  199. }
  200. context_from_allocator :: proc(a: Allocator) -> type_of(context) {
  201. context.allocator = a;
  202. return context;
  203. }
  204. Fixed_Byte_Buffer :: distinct [dynamic]byte;
  205. make_fixed_byte_buffer :: proc(backing: []byte) -> Fixed_Byte_Buffer {
  206. s := transmute(Raw_Slice)backing;
  207. d: Raw_Dynamic_Array;
  208. d.data = s.data;
  209. d.len = 0;
  210. d.cap = s.len;
  211. d.allocator = nil_allocator();
  212. return transmute(Fixed_Byte_Buffer)d;
  213. }
  214. align_formula :: proc(size, align: int) -> int {
  215. result := size + align-1;
  216. return result - result%align;
  217. }
  218. calc_padding_with_header :: proc(ptr: uintptr, align: uintptr, header_size: int) -> int {
  219. p, a := ptr, align;
  220. modulo := p & (a-1);
  221. padding := uintptr(0);
  222. if modulo != 0 {
  223. padding = a - modulo;
  224. }
  225. needed_space := uintptr(header_size);
  226. if padding < needed_space {
  227. needed_space -= padding;
  228. if needed_space & (a-1) > 0 {
  229. padding += align * (1+(needed_space/align));
  230. } else {
  231. padding += align * (needed_space/align);
  232. }
  233. }
  234. return int(padding);
  235. }
  236. clone_slice :: proc(slice: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> T {
  237. new_slice := make(T, len(slice), allocator, loc);
  238. runtime.copy(new_slice, slice);
  239. return new_slice;
  240. }