mem.odin 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #import "fmt.odin";
  2. #import "os.odin";
  3. set :: proc(data: rawptr, value: i32, len: int) -> rawptr #link_name "__mem_set" {
  4. llvm_memset_64bit :: proc(dst: rawptr, val: byte, len: int, align: i32, is_volatile: bool) #foreign "llvm.memset.p0i8.i64"
  5. llvm_memset_64bit(data, value as byte, len, 1, false);
  6. return data;
  7. }
  8. zero :: proc(data: rawptr, len: int) -> rawptr #link_name "__mem_zero" {
  9. return set(data, 0, len);
  10. }
  11. copy :: proc(dst, src: rawptr, len: int) -> rawptr #link_name "__mem_copy" {
  12. // NOTE(bill): This _must_ implemented like C's memmove
  13. llvm_memmove_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memmove.p0i8.p0i8.i64"
  14. llvm_memmove_64bit(dst, src, len, 1, false);
  15. return dst;
  16. }
  17. copy_non_overlapping :: proc(dst, src: rawptr, len: int) -> rawptr #link_name "__mem_copy_non_overlapping" {
  18. // NOTE(bill): This _must_ implemented like C's memcpy
  19. llvm_memcpy_64bit :: proc(dst, src: rawptr, len: int, align: i32, is_volatile: bool) #foreign "llvm.memcpy.p0i8.p0i8.i64"
  20. llvm_memcpy_64bit(dst, src, len, 1, false);
  21. return dst;
  22. }
  23. compare :: proc(dst, src: rawptr, n: int) -> int #link_name "__mem_compare" {
  24. // Translation of http://mgronhol.github.io/fast-strcmp/
  25. a := slice_ptr(dst as ^byte, n);
  26. b := slice_ptr(src as ^byte, n);
  27. fast := n/size_of(int) + 1;
  28. offset := (fast-1)*size_of(int);
  29. curr_block := 0;
  30. if n <= size_of(int) {
  31. fast = 0;
  32. }
  33. la := slice_ptr(^a[0] as ^int, fast);
  34. lb := slice_ptr(^b[0] as ^int, fast);
  35. for _ : curr_block ..< fast {
  36. if (la[curr_block] ~ lb[curr_block]) != 0 {
  37. for pos : curr_block*size_of(int) ..< n {
  38. if (a[pos] ~ b[pos]) != 0 {
  39. return a[pos] as int - b[pos] as int;
  40. }
  41. }
  42. }
  43. }
  44. for _ : offset ..< n {
  45. if (a[offset] ~ b[offset]) != 0 {
  46. return a[offset] as int - b[offset] as int;
  47. }
  48. }
  49. return 0;
  50. }
  51. kilobytes :: proc(x: int) -> int #inline { return (x) * 1024; }
  52. megabytes :: proc(x: int) -> int #inline { return kilobytes(x) * 1024; }
  53. gigabytes :: proc(x: int) -> int #inline { return gigabytes(x) * 1024; }
  54. terabytes :: proc(x: int) -> int #inline { return terabytes(x) * 1024; }
  55. is_power_of_two :: proc(x: int) -> bool {
  56. if x <= 0 {
  57. return false;
  58. }
  59. return (x & (x-1)) == 0;
  60. }
  61. align_forward :: proc(ptr: rawptr, align: int) -> rawptr {
  62. assert(is_power_of_two(align));
  63. a := align as uint;
  64. p := ptr as uint;
  65. modulo := p & (a-1);
  66. if modulo != 0 {
  67. p += a - modulo;
  68. }
  69. return p as rawptr;
  70. }
  71. Allocation_Header :: struct {
  72. size: int;
  73. }
  74. allocation_header_fill :: proc(header: ^Allocation_Header, data: rawptr, size: int) {
  75. header.size = size;
  76. ptr := (header+1) as ^int;
  77. while i := 0; ptr as rawptr < data {
  78. (ptr+i)^ = -1;
  79. i += 1;
  80. }
  81. }
  82. allocation_header :: proc(data: rawptr) -> ^Allocation_Header {
  83. p := data as ^int;
  84. while (p-1)^ == -1 {
  85. p = (p-1);
  86. }
  87. return (p as ^Allocation_Header)-1;
  88. }
  89. // Custom allocators
  90. Arena :: struct {
  91. backing: Allocator;
  92. offset: int;
  93. memory: []byte;
  94. temp_count: int;
  95. }
  96. Arena_Temp_Memory :: struct {
  97. arena: ^Arena;
  98. original_count: int;
  99. }
  100. init_arena_from_memory :: proc(using a: ^Arena, data: []byte) {
  101. backing = Allocator{};
  102. memory = data[:0];
  103. temp_count = 0;
  104. }
  105. init_arena_from_context :: proc(using a: ^Arena, size: int) {
  106. backing = context.allocator;
  107. memory = new_slice(byte, size);
  108. temp_count = 0;
  109. }
  110. free_arena :: proc(using a: ^Arena) {
  111. if backing.procedure != nil {
  112. push_allocator backing {
  113. free(memory.data);
  114. memory = memory[0:0];
  115. offset = 0;
  116. }
  117. }
  118. }
  119. arena_allocator :: proc(arena: ^Arena) -> Allocator {
  120. return Allocator{
  121. procedure = arena_allocator_proc,
  122. data = arena,
  123. };
  124. }
  125. arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  126. size, alignment: int,
  127. old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
  128. using Allocator_Mode;
  129. arena := allocator_data as ^Arena;
  130. match mode {
  131. case ALLOC:
  132. total_size := size + alignment;
  133. if arena.offset + total_size > arena.memory.count {
  134. fmt.fprintln(os.stderr, "Arena out of memory");
  135. return nil;
  136. }
  137. #no_bounds_check end := ^arena.memory[arena.offset];
  138. ptr := align_forward(end, alignment);
  139. arena.offset += total_size;
  140. return zero(ptr, size);
  141. case FREE:
  142. // NOTE(bill): Free all at once
  143. // Use Arena_Temp_Memory if you want to free a block
  144. case FREE_ALL:
  145. arena.offset = 0;
  146. case RESIZE:
  147. return default_resize_align(old_memory, old_size, size, alignment);
  148. }
  149. return nil;
  150. }
  151. begin_arena_temp_memory :: proc(a: ^Arena) -> Arena_Temp_Memory {
  152. tmp: Arena_Temp_Memory;
  153. tmp.arena = a;
  154. tmp.original_count = a.memory.count;
  155. a.temp_count += 1;
  156. return tmp;
  157. }
  158. end_arena_temp_memory :: proc(using tmp: Arena_Temp_Memory) {
  159. assert(arena.memory.count >= original_count);
  160. assert(arena.temp_count > 0);
  161. arena.memory.count = original_count;
  162. arena.temp_count -= 1;
  163. }
  164. align_of_type_info :: proc(type_info: ^Type_Info) -> int {
  165. prev_pow2 :: proc(n: i64) -> i64 {
  166. if n <= 0 {
  167. return 0;
  168. }
  169. n |= n >> 1;
  170. n |= n >> 2;
  171. n |= n >> 4;
  172. n |= n >> 8;
  173. n |= n >> 16;
  174. n |= n >> 32;
  175. return n - (n >> 1);
  176. }
  177. WORD_SIZE :: size_of(int);
  178. MAX_ALIGN :: size_of([vector 64]f64); // TODO(bill): Should these constants be builtin constants?
  179. using Type_Info;
  180. match type info : type_info {
  181. case Named:
  182. return align_of_type_info(info.base);
  183. case Integer:
  184. return info.size;
  185. case Float:
  186. return info.size;
  187. case String:
  188. return WORD_SIZE;
  189. case Boolean:
  190. return 1;
  191. case Pointer:
  192. return WORD_SIZE;
  193. case Maybe:
  194. return max(align_of_type_info(info.elem), 1);
  195. case Procedure:
  196. return WORD_SIZE;
  197. case Array:
  198. return align_of_type_info(info.elem);
  199. case Slice:
  200. return WORD_SIZE;
  201. case Vector:
  202. size := size_of_type_info(info.elem);
  203. count := max(prev_pow2(info.count as i64), 1) as int;
  204. total := size * count;
  205. return clamp(total, 1, MAX_ALIGN);
  206. case Struct:
  207. return info.align;
  208. case Union:
  209. return info.align;
  210. case Raw_Union:
  211. return info.align;
  212. }
  213. return 0;
  214. }
  215. align_formula :: proc(size, align: int) -> int {
  216. result := size + align-1;
  217. return result - result%align;
  218. }
  219. size_of_type_info :: proc(type_info: ^Type_Info) -> int {
  220. WORD_SIZE :: size_of(int);
  221. using Type_Info;
  222. match type info : type_info {
  223. case Named:
  224. return size_of_type_info(info.base);
  225. case Integer:
  226. return info.size;
  227. case Float:
  228. return info.size;
  229. case Any:
  230. return 2*WORD_SIZE;
  231. case String:
  232. return 2*WORD_SIZE;
  233. case Boolean:
  234. return 1;
  235. case Pointer:
  236. return WORD_SIZE;
  237. case Maybe:
  238. return size_of_type_info(info.elem) + 1;
  239. case Procedure:
  240. return WORD_SIZE;
  241. case Array:
  242. count := info.count;
  243. if count == 0 {
  244. return 0;
  245. }
  246. size := size_of_type_info(info.elem);
  247. align := align_of_type_info(info.elem);
  248. alignment := align_formula(size, align);
  249. return alignment*(count-1) + size;
  250. case Slice:
  251. return 3*WORD_SIZE;
  252. case Vector:
  253. is_bool :: proc(type_info: ^Type_Info) -> bool {
  254. match type info : type_info {
  255. case Named:
  256. return is_bool(info.base);
  257. case Boolean:
  258. return true;
  259. }
  260. return false;
  261. }
  262. count := info.count;
  263. if count == 0 {
  264. return 0;
  265. }
  266. bit_size := 8*size_of_type_info(info.elem);
  267. if is_bool(info.elem) {
  268. // NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1`
  269. // Silly LLVM spec
  270. bit_size = 1;
  271. }
  272. total_size_in_bits := bit_size * count;
  273. total_size := (total_size_in_bits+7)/8;
  274. return total_size;
  275. case Struct:
  276. return info.size;
  277. case Union:
  278. return info.size;
  279. case Raw_Union:
  280. return info.size;
  281. }
  282. return 0;
  283. }