alloc.odin 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package mem
  2. import "core:runtime"
  3. DEFAULT_ALIGNMENT :: 2*align_of(rawptr);
  4. Allocator_Mode :: enum byte {
  5. Alloc,
  6. Free,
  7. Free_All,
  8. Resize,
  9. }
  10. Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode,
  11. size, alignment: int,
  12. old_memory: rawptr, old_size: int, flags: u64 = 0, location := #caller_location) -> rawptr;
  13. Allocator :: struct {
  14. procedure: Allocator_Proc,
  15. data: rawptr,
  16. }
  17. alloc_with_allocator :: inline proc(a: Allocator, size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
  18. if size == 0 do return nil;
  19. return a.procedure(a.data, Allocator_Mode.Alloc, size, alignment, nil, 0, 0, loc);
  20. }
  21. alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
  22. return alloc_with_allocator(context.allocator, size, alignment, loc);
  23. }
  24. free_ptr_with_allocator :: inline proc(a: Allocator, ptr: rawptr, loc := #caller_location) {
  25. if ptr == nil do return;
  26. if a.procedure == nil do return;
  27. a.procedure(a.data, Allocator_Mode.Free, 0, 0, ptr, 0, 0, loc);
  28. }
  29. free :: inline proc(ptr: rawptr, loc := #caller_location) do free_ptr_with_allocator(context.allocator, ptr, loc);
  30. free_all_with_allocator :: inline proc(a: Allocator, loc := #caller_location) {
  31. a.procedure(a.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc);
  32. }
  33. free_all :: inline proc(loc := #caller_location) {
  34. free_all_with_allocator(context.allocator, loc);
  35. }
  36. resize_with_allocator :: inline proc(a: Allocator, ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
  37. if new_size == 0 {
  38. free_ptr_with_allocator(a, ptr, loc);
  39. return nil;
  40. }
  41. return a.procedure(a.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, 0, loc);
  42. }
  43. resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, loc := #caller_location) -> rawptr {
  44. return resize_with_allocator(context.allocator, ptr, old_size, new_size, alignment, loc);
  45. }
  46. delete_string :: proc(str: string, loc := #caller_location) {
  47. free(raw_data(str), loc);
  48. }
  49. delete_cstring :: proc(str: cstring, loc := #caller_location) {
  50. free((^byte)(str), loc);
  51. }
  52. delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) {
  53. free(raw_data(array), loc);
  54. }
  55. delete_slice :: proc(array: $T/[]$E, loc := #caller_location) {
  56. free(raw_data(array), loc);
  57. }
  58. delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) {
  59. raw := transmute(Raw_Map)m;
  60. delete_dynamic_array(raw.hashes, loc);
  61. free(raw.entries.data, loc);
  62. }
  63. delete :: proc[
  64. delete_string,
  65. delete_cstring,
  66. delete_dynamic_array,
  67. delete_slice,
  68. delete_map,
  69. ];
  70. new :: inline proc(T: type, loc := #caller_location) -> ^T {
  71. ptr := (^T)(alloc(size_of(T), align_of(T), loc));
  72. if ptr != nil do ptr^ = T{};
  73. return ptr;
  74. }
  75. new_clone :: inline proc(data: $T, loc := #caller_location) -> ^T {
  76. ptr := (^T)(alloc(size_of(T), align_of(T), loc));
  77. if ptr != nil do ptr^ = data;
  78. return ptr;
  79. }
  80. new_with_allocator :: inline proc(a: Allocator, T: type, loc := #caller_location) -> ^T {
  81. ptr := (^T)(alloc_with_allocator(a, size_of(T), align_of(T), loc));
  82. if ptr != nil do ptr^ = T{};
  83. return ptr;
  84. }
  85. new_clone_with_allocator :: inline proc(a: Allocator, data: $T, loc := #caller_location) -> ^T {
  86. ptr := (^T)(alloc_with_allocator(a, size_of(T), align_of(T), loc));
  87. if ptr != nil do ptr^ = data;
  88. return ptr;
  89. }
  90. make_slice :: proc(T: type/[]$E, auto_cast len: int, loc := #caller_location) -> T {
  91. runtime.make_slice_error_loc(loc, len);
  92. data := alloc(size_of(E)*len, align_of(E));
  93. s := Raw_Slice{data, len};
  94. return transmute(T)s;
  95. }
  96. make_dynamic_array :: proc(T: type/[dynamic]$E, loc := #caller_location) -> T {
  97. return make_dynamic_array_len_cap(T, 0, 16, loc);
  98. }
  99. make_dynamic_array_len :: proc(T: type/[dynamic]$E, auto_cast len: int, loc := #caller_location) -> T {
  100. return make_dynamic_array_len_cap(T, len, len, loc);
  101. }
  102. make_dynamic_array_len_cap :: proc(T: type/[dynamic]$E, auto_cast len: int, auto_cast cap: int, loc := #caller_location) -> T {
  103. runtime.make_dynamic_array_error_loc(loc, len, cap);
  104. data := alloc(size_of(E)*cap, align_of(E));
  105. s := Raw_Dynamic_Array{data, len, cap, context.allocator};
  106. return transmute(T)s;
  107. }
  108. make_map :: proc(T: type/map[$K]$E, auto_cast cap: int = 16, loc := #caller_location) -> T {
  109. runtime.make_map_expr_error_loc(loc, cap);
  110. m: T;
  111. reserve_map(&m, cap);
  112. return m;
  113. }
  114. make :: proc[
  115. make_slice,
  116. make_dynamic_array,
  117. make_dynamic_array_len,
  118. make_dynamic_array_len_cap,
  119. make_map,
  120. ];
  121. default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment: int, loc := #caller_location) -> rawptr {
  122. if old_memory == nil do return alloc(new_size, alignment, loc);
  123. if new_size == 0 {
  124. free(old_memory, loc);
  125. return nil;
  126. }
  127. if new_size == old_size do return old_memory;
  128. new_memory := alloc(new_size, alignment, loc);
  129. if new_memory == nil do return nil;
  130. copy(new_memory, old_memory, min(old_size, new_size));;
  131. free(old_memory, loc);
  132. return new_memory;
  133. }
  134. nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  135. size, alignment: int,
  136. old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
  137. return nil;
  138. }
  139. nil_allocator :: proc() -> Allocator {
  140. return Allocator{
  141. procedure = nil_allocator_proc,
  142. data = nil,
  143. };
  144. }
  145. Pool :: struct {
  146. block_size: int,
  147. out_band_size: int,
  148. alignment: int,
  149. unused_blocks: [dynamic]rawptr,
  150. used_blocks: [dynamic]rawptr,
  151. out_band_allocations: [dynamic]rawptr,
  152. current_block: rawptr,
  153. current_pos: rawptr,
  154. bytes_left: int,
  155. block_allocator: Allocator,
  156. }
  157. POOL_BLOCK_SIZE_DEFAULT :: 65536;
  158. POOL_OUT_OF_BAND_SIZE_DEFAULT :: 6554;
  159. pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  160. size, alignment: int,
  161. old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
  162. pool := (^Pool)(allocator_data);
  163. switch mode {
  164. case Allocator_Mode.Alloc:
  165. return pool_alloc(pool, size);
  166. case Allocator_Mode.Free:
  167. panic("Allocator_Mode.Free is not supported for a pool");
  168. case Allocator_Mode.Free_All:
  169. pool_free_all(pool);
  170. case Allocator_Mode.Resize:
  171. panic("Allocator_Mode.Resize is not supported for a pool");
  172. }
  173. return nil;
  174. }
  175. pool_allocator :: proc(pool: ^Pool) -> Allocator {
  176. return Allocator{
  177. procedure = pool_allocator_proc,
  178. data = pool,
  179. };
  180. }
  181. pool_init :: proc(pool: ^Pool,
  182. block_allocator := Allocator{} , array_allocator := Allocator{},
  183. block_size := POOL_BLOCK_SIZE_DEFAULT, out_band_size := POOL_OUT_OF_BAND_SIZE_DEFAULT,
  184. alignment := 8) {
  185. pool.block_size = block_size;
  186. pool.out_band_size = out_band_size;
  187. pool.alignment = alignment;
  188. if block_allocator.procedure == nil {
  189. block_allocator = context.allocator;
  190. }
  191. if array_allocator.procedure == nil {
  192. array_allocator = context.allocator;
  193. }
  194. pool.block_allocator = block_allocator;
  195. pool.out_band_allocations.allocator = array_allocator;
  196. pool. unused_blocks.allocator = array_allocator;
  197. pool. used_blocks.allocator = array_allocator;
  198. }
  199. pool_destroy :: proc(using pool: ^Pool) {
  200. pool_free_all(pool);
  201. delete(unused_blocks);
  202. delete(used_blocks);
  203. zero(pool, size_of(pool^));
  204. }
  205. pool_alloc :: proc(using pool: ^Pool, bytes: int) -> rawptr {
  206. cycle_new_block :: proc(using pool: ^Pool) {
  207. if block_allocator.procedure == nil {
  208. panic("You must call pool_init on a Pool before using it");
  209. }
  210. if current_block != nil {
  211. append(&used_blocks, current_block);
  212. }
  213. new_block: rawptr;
  214. if len(unused_blocks) > 0 {
  215. new_block = pop(&unused_blocks);
  216. } else {
  217. new_block = block_allocator.procedure(block_allocator.data, Allocator_Mode.Alloc,
  218. block_size, alignment,
  219. nil, 0);
  220. }
  221. bytes_left = block_size;
  222. current_pos = new_block;
  223. current_block = new_block;
  224. }
  225. extra := alignment - (bytes % alignment);
  226. bytes += extra;
  227. if bytes >= out_band_size {
  228. assert(block_allocator.procedure != nil);
  229. memory := block_allocator.procedure(block_allocator.data, Allocator_Mode.Alloc,
  230. block_size, alignment,
  231. nil, 0);
  232. if memory != nil {
  233. append(&out_band_allocations, (^byte)(memory));
  234. }
  235. return memory;
  236. }
  237. if bytes_left < bytes {
  238. cycle_new_block(pool);
  239. if current_block == nil {
  240. return nil;
  241. }
  242. }
  243. memory := current_pos;
  244. current_pos = ptr_offset((^byte)(current_pos), bytes);
  245. bytes_left -= bytes;
  246. return memory;
  247. }
  248. pool_reset :: proc(using pool: ^Pool) {
  249. if current_block != nil {
  250. append(&unused_blocks, current_block);
  251. current_block = nil;
  252. }
  253. for block in used_blocks {
  254. append(&unused_blocks, block);
  255. }
  256. clear(&used_blocks);
  257. for a in out_band_allocations {
  258. free_ptr_with_allocator(block_allocator, a);
  259. }
  260. clear(&out_band_allocations);
  261. }
  262. pool_free_all :: proc(using pool: ^Pool) {
  263. pool_reset(pool);
  264. for block in unused_blocks {
  265. free_ptr_with_allocator(block_allocator, block);
  266. }
  267. clear(&unused_blocks);
  268. }