alloc.odin 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package mem
  2. import "core:runtime"
  3. // NOTE(bill, 2019-12-31): These are defined in `package runtime` as they are used in the `context`. This is to prevent an import definition cycle.
  4. Allocator_Mode :: runtime.Allocator_Mode
  5. /*
  6. Allocator_Mode :: enum byte {
  7. Alloc,
  8. Free,
  9. Free_All,
  10. Resize,
  11. Query_Features,
  12. }
  13. */
  14. Allocator_Mode_Set :: runtime.Allocator_Mode_Set
  15. /*
  16. Allocator_Mode_Set :: distinct bit_set[Allocator_Mode];
  17. */
  18. Allocator_Query_Info :: runtime.Allocator_Query_Info
  19. /*
  20. Allocator_Query_Info :: struct {
  21. pointer: rawptr,
  22. size: Maybe(int),
  23. alignment: Maybe(int),
  24. }
  25. */
  26. Allocator_Error :: runtime.Allocator_Error
  27. /*
  28. Allocator_Error :: enum byte {
  29. None = 0,
  30. Out_Of_Memory = 1,
  31. Invalid_Pointer = 2,
  32. Invalid_Argument = 3,
  33. Mode_Not_Implemented = 4,
  34. }
  35. */
  36. Allocator_Proc :: runtime.Allocator_Proc
  37. /*
  38. Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
  39. size, alignment: int,
  40. old_memory: rawptr, old_size: int, location: Source_Code_Location = #caller_location) -> ([]byte, Allocator_Error);
  41. */
  42. Allocator :: runtime.Allocator
  43. /*
  44. Allocator :: struct {
  45. procedure: Allocator_Proc,
  46. data: rawptr,
  47. }
  48. */
  49. DEFAULT_ALIGNMENT :: 2*align_of(rawptr)
  50. DEFAULT_PAGE_SIZE ::
  51. 64 * 1024 when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64 else
  52. 16 * 1024 when ODIN_OS == .Darwin && ODIN_ARCH == .arm64 else
  53. 4 * 1024
  54. alloc :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
  55. if size == 0 {
  56. return nil
  57. }
  58. if allocator.procedure == nil {
  59. return nil
  60. }
  61. data, err := allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, loc)
  62. _ = err
  63. return raw_data(data)
  64. }
  65. alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  66. if size == 0 {
  67. return nil, nil
  68. }
  69. if allocator.procedure == nil {
  70. return nil, nil
  71. }
  72. return allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, loc)
  73. }
  74. free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  75. if ptr == nil {
  76. return nil
  77. }
  78. if allocator.procedure == nil {
  79. return nil
  80. }
  81. _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, 0, loc)
  82. return err
  83. }
  84. free_bytes :: proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  85. if bytes == nil {
  86. return nil
  87. }
  88. if allocator.procedure == nil {
  89. return nil
  90. }
  91. _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, raw_data(bytes), len(bytes), loc)
  92. return err
  93. }
  94. free_all :: proc(allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  95. if allocator.procedure != nil {
  96. _, err := allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, loc)
  97. return err
  98. }
  99. return nil
  100. }
  101. resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
  102. if allocator.procedure == nil {
  103. return nil
  104. }
  105. if new_size == 0 {
  106. if ptr != nil {
  107. allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc)
  108. }
  109. return nil
  110. } else if ptr == nil {
  111. _, err := allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc)
  112. _ = err
  113. return nil
  114. }
  115. data, err := allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, loc)
  116. if err == .Mode_Not_Implemented {
  117. data, err = allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc)
  118. if err != nil {
  119. return nil
  120. }
  121. runtime.copy(data, byte_slice(ptr, old_size))
  122. _, err = allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc)
  123. return raw_data(data)
  124. }
  125. return raw_data(data)
  126. }
  127. resize_bytes :: proc(old_data: []byte, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  128. if allocator.procedure == nil {
  129. return nil, nil
  130. }
  131. ptr := raw_data(old_data)
  132. old_size := len(old_data)
  133. if new_size == 0 {
  134. if ptr != nil {
  135. _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc)
  136. return nil, err
  137. }
  138. return nil, nil
  139. } else if ptr == nil {
  140. return allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc)
  141. }
  142. data, err := allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, loc)
  143. if err == .Mode_Not_Implemented {
  144. data, err = allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc)
  145. if err != nil {
  146. return data, err
  147. }
  148. runtime.copy(data, old_data)
  149. _, err = allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc)
  150. }
  151. return data, err
  152. }
  153. query_features :: proc(allocator: Allocator, loc := #caller_location) -> (set: Allocator_Mode_Set) {
  154. if allocator.procedure != nil {
  155. allocator.procedure(allocator.data, Allocator_Mode.Query_Features, 0, 0, &set, 0, loc)
  156. return set
  157. }
  158. return nil
  159. }
  160. query_info :: proc(pointer: rawptr, allocator: Allocator, loc := #caller_location) -> (props: Allocator_Query_Info) {
  161. props.pointer = pointer
  162. if allocator.procedure != nil {
  163. allocator.procedure(allocator.data, Allocator_Mode.Query_Info, 0, 0, &props, 0, loc)
  164. }
  165. return
  166. }
  167. delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) {
  168. free(raw_data(str), allocator, loc)
  169. }
  170. delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) {
  171. free((^byte)(str), allocator, loc)
  172. }
  173. delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) {
  174. free(raw_data(array), array.allocator, loc)
  175. }
  176. delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) {
  177. free(raw_data(array), allocator, loc)
  178. }
  179. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) {
  180. raw := transmute(Raw_Map)m
  181. delete_slice(raw.hashes, raw.entries.allocator, loc)
  182. free(raw.entries.data, raw.entries.allocator, loc)
  183. }
  184. delete :: proc{
  185. delete_string,
  186. delete_cstring,
  187. delete_dynamic_array,
  188. delete_slice,
  189. delete_map,
  190. }
  191. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) {
  192. return new_aligned(T, align_of(T), allocator, loc)
  193. }
  194. new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  195. data := alloc_bytes(size_of(T), alignment, allocator, loc) or_return
  196. t = (^T)(raw_data(data))
  197. return
  198. }
  199. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  200. backing := alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
  201. t = (^T)(raw_data(backing))
  202. if t != nil {
  203. t^ = data
  204. return t, nil
  205. }
  206. return nil, .Out_Of_Memory
  207. }
  208. DEFAULT_RESERVE_CAPACITY :: 16
  209. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (slice: T, err: Allocator_Error) {
  210. runtime.make_slice_error_loc(loc, len)
  211. data := alloc_bytes(size_of(E)*len, alignment, allocator, loc) or_return
  212. if data == nil && size_of(E) != 0 {
  213. return
  214. }
  215. slice = transmute(T)Raw_Slice{raw_data(data), len}
  216. return
  217. }
  218. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  219. return make_aligned(T, len, align_of(E), allocator, loc)
  220. }
  221. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  222. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
  223. }
  224. make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  225. return make_dynamic_array_len_cap(T, len, len, allocator, loc)
  226. }
  227. make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) {
  228. runtime.make_dynamic_array_error_loc(loc, len, cap)
  229. data := alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
  230. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
  231. if data == nil && size_of(E) != 0 {
  232. s.len, s.cap = 0, 0
  233. }
  234. array = transmute(T)s
  235. return
  236. }
  237. make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
  238. runtime.make_map_expr_error_loc(loc, cap)
  239. context.allocator = allocator
  240. m: T
  241. reserve_map(&m, cap)
  242. return m
  243. }
  244. make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) {
  245. runtime.make_slice_error_loc(loc, len)
  246. data := alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
  247. if data == nil && size_of(E) != 0 {
  248. return
  249. }
  250. mp = cast(T)raw_data(data)
  251. return
  252. }
  253. make :: proc{
  254. make_slice,
  255. make_dynamic_array,
  256. make_dynamic_array_len,
  257. make_dynamic_array_len_cap,
  258. make_map,
  259. make_multi_pointer,
  260. }
  261. default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> rawptr {
  262. if old_memory == nil {
  263. return alloc(new_size, alignment, allocator, loc)
  264. }
  265. if new_size == 0 {
  266. free(old_memory, allocator, loc)
  267. return nil
  268. }
  269. if new_size == old_size {
  270. return old_memory
  271. }
  272. new_memory := alloc(new_size, alignment, allocator, loc)
  273. if new_memory == nil {
  274. return nil
  275. }
  276. copy(new_memory, old_memory, min(old_size, new_size))
  277. free(old_memory, allocator, loc)
  278. return new_memory
  279. }
  280. default_resize_bytes_align :: proc(old_data: []byte, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  281. old_memory := raw_data(old_data)
  282. old_size := len(old_data)
  283. if old_memory == nil {
  284. return alloc_bytes(new_size, alignment, allocator, loc)
  285. }
  286. if new_size == 0 {
  287. err := free_bytes(old_data, allocator, loc)
  288. return nil, err
  289. }
  290. if new_size == old_size {
  291. return old_data, .None
  292. }
  293. new_memory, err := alloc_bytes(new_size, alignment, allocator, loc)
  294. if new_memory == nil || err != nil {
  295. return nil, err
  296. }
  297. runtime.copy(new_memory, old_data)
  298. free_bytes(old_data, allocator, loc)
  299. return new_memory, err
  300. }