alloc.odin 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. alloc :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
  51. if size == 0 {
  52. return nil
  53. }
  54. if allocator.procedure == nil {
  55. return nil
  56. }
  57. data, err := allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, loc)
  58. _ = err
  59. return raw_data(data)
  60. }
  61. alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  62. if size == 0 {
  63. return nil, nil
  64. }
  65. if allocator.procedure == nil {
  66. return nil, nil
  67. }
  68. return allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, loc)
  69. }
  70. free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  71. if ptr == nil {
  72. return nil
  73. }
  74. if allocator.procedure == nil {
  75. return nil
  76. }
  77. _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, 0, loc)
  78. return err
  79. }
  80. free_bytes :: proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  81. if bytes == nil {
  82. return nil
  83. }
  84. if allocator.procedure == nil {
  85. return nil
  86. }
  87. _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, raw_data(bytes), len(bytes), loc)
  88. return err
  89. }
  90. free_all :: proc(allocator := context.allocator, loc := #caller_location) -> Allocator_Error {
  91. if allocator.procedure != nil {
  92. _, err := allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, loc)
  93. return err
  94. }
  95. return nil
  96. }
  97. resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
  98. if allocator.procedure == nil {
  99. return nil
  100. }
  101. if new_size == 0 {
  102. if ptr != nil {
  103. allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc)
  104. }
  105. return nil
  106. } else if ptr == nil {
  107. _, err := allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc)
  108. _ = err
  109. return nil
  110. }
  111. data, err := allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, loc)
  112. if err == .Mode_Not_Implemented {
  113. data, err = allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc)
  114. if err != nil {
  115. return nil
  116. }
  117. runtime.copy(data, byte_slice(ptr, old_size))
  118. _, err = allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc)
  119. return raw_data(data)
  120. }
  121. return raw_data(data)
  122. }
  123. resize_bytes :: proc(old_data: []byte, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  124. if allocator.procedure == nil {
  125. return nil, nil
  126. }
  127. ptr := raw_data(old_data)
  128. old_size := len(old_data)
  129. if new_size == 0 {
  130. if ptr != nil {
  131. _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc)
  132. return nil, err
  133. }
  134. return nil, nil
  135. } else if ptr == nil {
  136. return allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc)
  137. }
  138. data, err := allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, loc)
  139. if err == .Mode_Not_Implemented {
  140. data, err = allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc)
  141. if err != nil {
  142. return data, err
  143. }
  144. runtime.copy(data, old_data)
  145. _, err = allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc)
  146. }
  147. return data, err
  148. }
  149. query_features :: proc(allocator: Allocator, loc := #caller_location) -> (set: Allocator_Mode_Set) {
  150. if allocator.procedure != nil {
  151. allocator.procedure(allocator.data, Allocator_Mode.Query_Features, 0, 0, &set, 0, loc)
  152. return set
  153. }
  154. return nil
  155. }
  156. query_info :: proc(pointer: rawptr, allocator: Allocator, loc := #caller_location) -> (props: Allocator_Query_Info) {
  157. props.pointer = pointer
  158. if allocator.procedure != nil {
  159. allocator.procedure(allocator.data, Allocator_Mode.Query_Info, 0, 0, &props, 0, loc)
  160. }
  161. return
  162. }
  163. delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) {
  164. free(raw_data(str), allocator, loc)
  165. }
  166. delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) {
  167. free((^byte)(str), allocator, loc)
  168. }
  169. delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) {
  170. free(raw_data(array), array.allocator, loc)
  171. }
  172. delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) {
  173. free(raw_data(array), allocator, loc)
  174. }
  175. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) {
  176. raw := transmute(Raw_Map)m
  177. delete_slice(raw.hashes, raw.entries.allocator, loc)
  178. free(raw.entries.data, raw.entries.allocator, loc)
  179. }
  180. delete :: proc{
  181. delete_string,
  182. delete_cstring,
  183. delete_dynamic_array,
  184. delete_slice,
  185. delete_map,
  186. }
  187. new :: proc($T: typeid, allocator := context.allocator, loc := #caller_location) -> (^T, Allocator_Error) {
  188. return new_aligned(T, align_of(T), allocator, loc)
  189. }
  190. new_aligned :: proc($T: typeid, alignment: int, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  191. data := alloc_bytes(size_of(T), alignment, allocator, loc) or_return
  192. t = (^T)(raw_data(data))
  193. return
  194. }
  195. new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_location) -> (t: ^T, err: Allocator_Error) {
  196. backing := alloc_bytes(size_of(T), align_of(T), allocator, loc) or_return
  197. t = (^T)(raw_data(backing))
  198. if t != nil {
  199. t^ = data
  200. return t, nil
  201. }
  202. return nil, .Out_Of_Memory
  203. }
  204. DEFAULT_RESERVE_CAPACITY :: 16
  205. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (slice: T, err: Allocator_Error) {
  206. runtime.make_slice_error_loc(loc, len)
  207. data := alloc_bytes(size_of(E)*len, alignment, allocator, loc) or_return
  208. if data == nil && size_of(E) != 0 {
  209. return
  210. }
  211. slice = transmute(T)Raw_Slice{raw_data(data), len}
  212. return
  213. }
  214. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  215. return make_aligned(T, len, align_of(E), allocator, loc)
  216. }
  217. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  218. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc)
  219. }
  220. make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  221. return make_dynamic_array_len_cap(T, len, len, allocator, loc)
  222. }
  223. 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) {
  224. runtime.make_dynamic_array_error_loc(loc, len, cap)
  225. data := alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return
  226. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator}
  227. if data == nil && size_of(E) != 0 {
  228. s.len, s.cap = 0, 0
  229. }
  230. array = transmute(T)s
  231. return
  232. }
  233. make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
  234. runtime.make_map_expr_error_loc(loc, cap)
  235. context.allocator = allocator
  236. m: T
  237. reserve_map(&m, cap)
  238. return m
  239. }
  240. make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) {
  241. runtime.make_slice_error_loc(loc, len)
  242. data := alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return
  243. if data == nil && size_of(E) != 0 {
  244. return
  245. }
  246. mp = cast(T)raw_data(data)
  247. return
  248. }
  249. make :: proc{
  250. make_slice,
  251. make_dynamic_array,
  252. make_dynamic_array_len,
  253. make_dynamic_array_len_cap,
  254. make_map,
  255. make_multi_pointer,
  256. }
  257. default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> rawptr {
  258. if old_memory == nil {
  259. return alloc(new_size, alignment, allocator, loc)
  260. }
  261. if new_size == 0 {
  262. free(old_memory, allocator, loc)
  263. return nil
  264. }
  265. if new_size == old_size {
  266. return old_memory
  267. }
  268. new_memory := alloc(new_size, alignment, allocator, loc)
  269. if new_memory == nil {
  270. return nil
  271. }
  272. copy(new_memory, old_memory, min(old_size, new_size))
  273. free(old_memory, allocator, loc)
  274. return new_memory
  275. }
  276. default_resize_bytes_align :: proc(old_data: []byte, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  277. old_memory := raw_data(old_data)
  278. old_size := len(old_data)
  279. if old_memory == nil {
  280. return alloc_bytes(new_size, alignment, allocator, loc)
  281. }
  282. if new_size == 0 {
  283. err := free_bytes(old_data, allocator, loc)
  284. return nil, err
  285. }
  286. if new_size == old_size {
  287. return old_data, .None
  288. }
  289. new_memory, err := alloc_bytes(new_size, alignment, allocator, loc)
  290. if new_memory == nil || err != nil {
  291. return nil, err
  292. }
  293. runtime.copy(new_memory, old_data)
  294. free_bytes(old_data, allocator, loc)
  295. return new_memory, err
  296. }