alloc.odin 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 {
  196. data := alloc_bytes(size_of(T), alignment, allocator, loc) or_return;
  197. t = (^T)(raw_data(data));
  198. if t != nil {
  199. t^ = data;
  200. }
  201. return;
  202. }
  203. DEFAULT_RESERVE_CAPACITY :: 16;
  204. make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (slice: T, err: Allocator_Error) {
  205. runtime.make_slice_error_loc(loc, len);
  206. data := alloc_bytes(size_of(E)*len, alignment, allocator, loc) or_return;
  207. if data == nil && size_of(E) != 0 {
  208. return;
  209. }
  210. slice = transmute(T)Raw_Slice{raw_data(data), len};
  211. return;
  212. }
  213. make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  214. return make_aligned(T, len, align_of(E), allocator, loc);
  215. }
  216. make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  217. return make_dynamic_array_len_cap(T, 0, DEFAULT_RESERVE_CAPACITY, allocator, loc);
  218. }
  219. make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) {
  220. return make_dynamic_array_len_cap(T, len, len, allocator, loc);
  221. }
  222. 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) {
  223. runtime.make_dynamic_array_error_loc(loc, len, cap);
  224. data := alloc_bytes(size_of(E)*cap, align_of(E), allocator, loc) or_return;
  225. s := Raw_Dynamic_Array{raw_data(data), len, cap, allocator};
  226. if data == nil && size_of(E) != 0 {
  227. s.len, s.cap = 0, 0;
  228. }
  229. array = transmute(T)s;
  230. return;
  231. }
  232. make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
  233. runtime.make_map_expr_error_loc(loc, cap);
  234. context.allocator = allocator;
  235. m: T;
  236. reserve_map(&m, cap);
  237. return m;
  238. }
  239. make_multi_pointer :: proc($T: typeid/[^]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (mp: T, err: Allocator_Error) {
  240. runtime.make_slice_error_loc(loc, len);
  241. data := alloc_bytes(size_of(E)*len, align_of(E), allocator, loc) or_return;
  242. if data == nil && size_of(E) != 0 {
  243. return;
  244. }
  245. mp = cast(T)raw_data(data);
  246. return;
  247. }
  248. make :: proc{
  249. make_slice,
  250. make_dynamic_array,
  251. make_dynamic_array_len,
  252. make_dynamic_array_len_cap,
  253. make_map,
  254. make_multi_pointer,
  255. };
  256. default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> rawptr {
  257. if old_memory == nil {
  258. return alloc(new_size, alignment, allocator, loc);
  259. }
  260. if new_size == 0 {
  261. free(old_memory, allocator, loc);
  262. return nil;
  263. }
  264. if new_size == old_size {
  265. return old_memory;
  266. }
  267. new_memory := alloc(new_size, alignment, allocator, loc);
  268. if new_memory == nil {
  269. return nil;
  270. }
  271. copy(new_memory, old_memory, min(old_size, new_size));
  272. free(old_memory, allocator, loc);
  273. return new_memory;
  274. }
  275. default_resize_bytes_align :: proc(old_data: []byte, new_size, alignment: int, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) {
  276. old_memory := raw_data(old_data);
  277. old_size := len(old_data);
  278. if old_memory == nil {
  279. return alloc_bytes(new_size, alignment, allocator, loc);
  280. }
  281. if new_size == 0 {
  282. err := free_bytes(old_data, allocator, loc);
  283. return nil, err;
  284. }
  285. if new_size == old_size {
  286. return old_data, .None;
  287. }
  288. new_memory, err := alloc_bytes(new_size, alignment, allocator, loc);
  289. if new_memory == nil || err != nil {
  290. return nil, err;
  291. }
  292. runtime.copy(new_memory, old_data);
  293. free_bytes(old_data, allocator, loc);
  294. return new_memory, err;
  295. }