mem.odin 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 {
  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; curr_block++ {
  36. if (la[curr_block] ~ lb[curr_block]) != 0 {
  37. for pos := curr_block*size_of(int); pos < n; pos++ {
  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; offset++ {
  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. AllocationHeader :: struct {
  72. size: int
  73. }
  74. allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: int) {
  75. header.size = size
  76. ptr := (header+1) as ^int
  77. for i := 0; ptr as rawptr < data; i++ {
  78. (ptr+i)^ = -1
  79. }
  80. }
  81. allocation_header :: proc(data: rawptr) -> ^AllocationHeader {
  82. p := data as ^int
  83. for (p-1)^ == -1 {
  84. p = (p-1)
  85. }
  86. return (p as ^AllocationHeader)-1
  87. }
  88. // Custom allocators
  89. Arena :: struct {
  90. backing: Allocator
  91. memory: []byte
  92. temp_count: int
  93. Temp_Memory :: struct {
  94. arena: ^Arena
  95. original_count: int
  96. }
  97. }
  98. init_arena_from_memory :: proc(using a: ^Arena, data: []byte) {
  99. backing = Allocator{}
  100. memory = data[:0]
  101. temp_count = 0
  102. }
  103. init_arena_from_context :: proc(using a: ^Arena, size: int) {
  104. backing = context.allocator
  105. memory = new_slice(byte, 0, size)
  106. temp_count = 0
  107. }
  108. free_arena :: proc(using a: ^Arena) {
  109. if backing.procedure != nil {
  110. push_allocator backing {
  111. free(memory.data)
  112. memory = memory[0:0:0]
  113. }
  114. }
  115. }
  116. arena_allocator :: proc(arena: ^Arena) -> Allocator {
  117. return Allocator{
  118. procedure = arena_allocator_proc,
  119. data = arena,
  120. }
  121. }
  122. arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator.Mode,
  123. size, alignment: int,
  124. old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
  125. arena := allocator_data as ^Arena
  126. using Allocator.Mode
  127. match mode {
  128. case ALLOC:
  129. total_size := size + alignment
  130. if arena.memory.count + total_size > arena.memory.capacity {
  131. fmt.fprintln(os.stderr, "Arena out of memory")
  132. return nil
  133. }
  134. #no_bounds_check end := ^arena.memory[arena.memory.count]
  135. ptr := align_forward(end, alignment)
  136. arena.memory.count += total_size
  137. return zero(ptr, size)
  138. case FREE:
  139. // NOTE(bill): Free all at once
  140. // Use Arena.Temp_Memory if you want to free a block
  141. case FREE_ALL:
  142. arena.memory.count = 0
  143. case RESIZE:
  144. return default_resize_align(old_memory, old_size, size, alignment)
  145. }
  146. return nil
  147. }
  148. begin_arena_temp_memory :: proc(a: ^Arena) -> Arena.Temp_Memory {
  149. tmp: Arena.Temp_Memory
  150. tmp.arena = a
  151. tmp.original_count = a.memory.count
  152. a.temp_count++
  153. return tmp
  154. }
  155. end_arena_temp_memory :: proc(using tmp: Arena.Temp_Memory) {
  156. assert(arena.memory.count >= original_count)
  157. assert(arena.temp_count > 0)
  158. arena.memory.count = original_count
  159. arena.temp_count--
  160. }
  161. align_of_type_info :: proc(type_info: ^Type_Info) -> int {
  162. WORD_SIZE :: size_of(int)
  163. using Type_Info
  164. match type info : type_info {
  165. case Named:
  166. return align_of_type_info(info.base)
  167. case Integer:
  168. return info.size
  169. case Float:
  170. return info.size
  171. case String:
  172. return WORD_SIZE
  173. case Boolean:
  174. return 1
  175. case Pointer:
  176. return WORD_SIZE
  177. case Maybe:
  178. return max(align_of_type_info(info.elem), 1)
  179. case Procedure:
  180. return WORD_SIZE
  181. case Array:
  182. return align_of_type_info(info.elem)
  183. case Slice:
  184. return WORD_SIZE
  185. case Vector:
  186. return align_of_type_info(info.elem)
  187. case Struct:
  188. return info.align
  189. case Union:
  190. return info.align
  191. case Raw_Union:
  192. return info.align
  193. case Enum:
  194. return align_of_type_info(info.base)
  195. }
  196. return 0
  197. }
  198. align_formula :: proc(size, align: int) -> int {
  199. result := size + align-1
  200. return result - result%align
  201. }
  202. size_of_type_info :: proc(type_info: ^Type_Info) -> int {
  203. WORD_SIZE :: size_of(int)
  204. using Type_Info
  205. match type info : type_info {
  206. case Named:
  207. return size_of_type_info(info.base)
  208. case Integer:
  209. return info.size
  210. case Float:
  211. return info.size
  212. case Any:
  213. return 2*WORD_SIZE
  214. case String:
  215. return 2*WORD_SIZE
  216. case Boolean:
  217. return 1
  218. case Pointer:
  219. return WORD_SIZE
  220. case Maybe:
  221. return size_of_type_info(info.elem) + 1
  222. case Procedure:
  223. return WORD_SIZE
  224. case Array:
  225. count := info.count
  226. if count == 0 {
  227. return 0
  228. }
  229. size := size_of_type_info(info.elem)
  230. align := align_of_type_info(info.elem)
  231. alignment := align_formula(size, align)
  232. return alignment*(count-1) + size
  233. case Slice:
  234. return 3*WORD_SIZE
  235. case Vector:
  236. is_bool :: proc(type_info: ^Type_Info) -> bool {
  237. match type info : type_info {
  238. case Named:
  239. return is_bool(info.base)
  240. case Boolean:
  241. return true
  242. }
  243. return false
  244. }
  245. count := info.count
  246. if count == 0 {
  247. return 0
  248. }
  249. bit_size := 8*size_of_type_info(info.elem)
  250. if is_bool(info.elem) {
  251. // NOTE(bill): LLVM can store booleans as 1 bit because a boolean _is_ an `i1`
  252. // Silly LLVM spec
  253. bit_size = 1
  254. }
  255. total_size_in_bits := bit_size * count
  256. total_size := (total_size_in_bits+7)/8
  257. return total_size
  258. case Struct:
  259. return info.size
  260. case Union:
  261. return info.size
  262. case Raw_Union:
  263. return info.size
  264. case Enum:
  265. return size_of_type_info(info.base)
  266. }
  267. return 0
  268. }