allocators.odin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package mem
  2. nil_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  3. size, alignment: int,
  4. old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
  5. return nil;
  6. }
  7. nil_allocator :: proc() -> Allocator {
  8. return Allocator{
  9. procedure = nil_allocator_proc,
  10. data = nil,
  11. };
  12. }
  13. // Custom allocators
  14. Arena :: struct {
  15. data: []byte,
  16. offset: int,
  17. peak_used: int,
  18. temp_count: int,
  19. }
  20. Arena_Temp_Memory :: struct {
  21. arena: ^Arena,
  22. prev_offset: int,
  23. }
  24. init_arena :: proc(a: ^Arena, data: []byte) {
  25. a.data = data;
  26. a.offset = 0;
  27. a.peak_used = 0;
  28. a.temp_count = 0;
  29. }
  30. arena_allocator :: proc(arena: ^Arena) -> Allocator {
  31. return Allocator{
  32. procedure = arena_allocator_proc,
  33. data = arena,
  34. };
  35. }
  36. arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  37. size, alignment: int,
  38. old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
  39. arena := cast(^Arena)allocator_data;
  40. switch mode {
  41. case .Alloc:
  42. total_size := size + alignment;
  43. if arena.offset + total_size > len(arena.data) {
  44. return nil;
  45. }
  46. #no_bounds_check end := &arena.data[len(arena.data)];
  47. ptr := align_forward(end, uintptr(alignment));
  48. arena.offset += total_size;
  49. arena.peak_used = max(arena.peak_used, arena.offset);
  50. return zero(ptr, size);
  51. case .Free:
  52. // NOTE(bill): Free all at once
  53. // Use Arena_Temp_Memory if you want to free a block
  54. case .Free_All:
  55. arena.offset = 0;
  56. case .Resize:
  57. return default_resize_align(old_memory, old_size, size, alignment, arena_allocator(arena));
  58. }
  59. return nil;
  60. }
  61. begin_arena_temp_memory :: proc(a: ^Arena) -> Arena_Temp_Memory {
  62. tmp: Arena_Temp_Memory;
  63. tmp.arena = a;
  64. tmp.prev_offset = a.offset;
  65. a.temp_count += 1;
  66. return tmp;
  67. }
  68. end_arena_temp_memory :: proc(using tmp: Arena_Temp_Memory) {
  69. assert(arena.offset >= prev_offset);
  70. assert(arena.temp_count > 0);
  71. arena.offset = prev_offset;
  72. arena.temp_count -= 1;
  73. }
  74. Scratch_Allocator :: struct {
  75. data: []byte,
  76. curr_offset: int,
  77. prev_offset: int,
  78. backup_allocator: Allocator,
  79. leaked_allocations: [dynamic]rawptr,
  80. }
  81. scratch_allocator_init :: proc(scratch: ^Scratch_Allocator, data: []byte, backup_allocator := context.allocator) {
  82. scratch.data = data;
  83. scratch.curr_offset = 0;
  84. scratch.prev_offset = 0;
  85. scratch.backup_allocator = backup_allocator;
  86. }
  87. scratch_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  88. size, alignment: int,
  89. old_memory: rawptr, old_size: int, flags: u64 = 0, loc := #caller_location) -> rawptr {
  90. scratch := (^Scratch_Allocator)(allocator_data);
  91. if scratch.data == nil {
  92. DEFAULT_SCRATCH_BACKING_SIZE :: 1<<22;
  93. scratch_allocator_init(scratch, make([]byte, 1<<22));
  94. }
  95. switch mode {
  96. case Allocator_Mode.Alloc:
  97. switch {
  98. case scratch.curr_offset+size <= len(scratch.data):
  99. offset := align_forward_uintptr(uintptr(scratch.curr_offset), uintptr(alignment));
  100. ptr := &scratch.data[offset];
  101. zero(ptr, size);
  102. scratch.prev_offset = int(offset);
  103. scratch.curr_offset = int(offset) + size;
  104. return ptr;
  105. case size <= len(scratch.data):
  106. offset := align_forward_uintptr(uintptr(0), uintptr(alignment));
  107. ptr := &scratch.data[offset];
  108. zero(ptr, size);
  109. scratch.prev_offset = int(offset);
  110. scratch.curr_offset = int(offset) + size;
  111. return ptr;
  112. }
  113. // TODO(bill): Should leaks be notified about? Should probably use a logging system that is built into the context system
  114. a := scratch.backup_allocator;
  115. if a.procedure == nil {
  116. a = context.allocator;
  117. scratch.backup_allocator = a;
  118. }
  119. ptr := alloc(size, alignment, a, loc);
  120. if scratch.leaked_allocations == nil {
  121. scratch.leaked_allocations = make([dynamic]rawptr, a);
  122. }
  123. append(&scratch.leaked_allocations, ptr);
  124. return ptr;
  125. case Allocator_Mode.Free:
  126. last_ptr := rawptr(&scratch.data[scratch.prev_offset]);
  127. if old_memory == last_ptr {
  128. full_size := scratch.curr_offset - scratch.prev_offset;
  129. scratch.curr_offset = scratch.prev_offset;
  130. zero(last_ptr, full_size);
  131. return nil;
  132. }
  133. // NOTE(bill): It's scratch memory, don't worry about freeing
  134. case Allocator_Mode.Free_All:
  135. scratch.curr_offset = 0;
  136. scratch.prev_offset = 0;
  137. for ptr in scratch.leaked_allocations {
  138. free(ptr, scratch.backup_allocator);
  139. }
  140. clear(&scratch.leaked_allocations);
  141. case Allocator_Mode.Resize:
  142. last_ptr := rawptr(&scratch.data[scratch.prev_offset]);
  143. if old_memory == last_ptr && len(scratch.data)-scratch.prev_offset >= size {
  144. scratch.curr_offset = scratch.prev_offset+size;
  145. return old_memory;
  146. }
  147. return scratch_allocator_proc(allocator_data, Allocator_Mode.Alloc, size, alignment, old_memory, old_size, flags, loc);
  148. }
  149. return nil;
  150. }
  151. scratch_allocator :: proc(scratch: ^Scratch_Allocator) -> Allocator {
  152. return Allocator{
  153. procedure = scratch_allocator_proc,
  154. data = scratch,
  155. };
  156. }
  157. Stack_Allocation_Header :: struct {
  158. prev_offset: int,
  159. padding: int,
  160. }
  161. // Stack is a stack-like allocator which has a strict memory freeing order
  162. Stack :: struct {
  163. data: []byte,
  164. prev_offset: int,
  165. curr_offset: int,
  166. peak_used: int,
  167. }
  168. init_stack :: proc(s: ^Stack, data: []byte) {
  169. s.data = data;
  170. s.prev_offset = 0;
  171. s.curr_offset = 0;
  172. s.peak_used = 0;
  173. }
  174. stack_allocator :: proc(stack: ^Stack) -> Allocator {
  175. return Allocator{
  176. procedure = stack_allocator_proc,
  177. data = stack,
  178. };
  179. }
  180. stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  181. size, alignment: int,
  182. old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
  183. s := cast(^Stack)allocator_data;
  184. if s.data == nil {
  185. return nil;
  186. }
  187. raw_alloc :: proc(s: ^Stack, size, alignment: int) -> rawptr {
  188. curr_addr := uintptr(&s.data[0]) + uintptr(s.curr_offset);
  189. padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Stack_Allocation_Header));
  190. if s.curr_offset + padding + size > len(s.data) {
  191. return nil;
  192. }
  193. s.prev_offset = s.curr_offset;
  194. s.curr_offset += padding;
  195. next_addr := curr_addr + uintptr(padding);
  196. header := (^Stack_Allocation_Header)(next_addr - size_of(Stack_Allocation_Header));
  197. header.padding = auto_cast padding;
  198. header.prev_offset = auto_cast s.prev_offset;
  199. s.curr_offset += size;
  200. s.peak_used = max(s.peak_used, s.curr_offset);
  201. return zero(rawptr(next_addr), size);
  202. }
  203. switch mode {
  204. case .Alloc:
  205. return raw_alloc(s, size, alignment);
  206. case .Free:
  207. if old_memory == nil {
  208. return nil;
  209. }
  210. start := uintptr(&s.data[0]);
  211. end := start + uintptr(len(s.data));
  212. curr_addr := uintptr(old_memory);
  213. if !(start <= curr_addr && curr_addr < end) {
  214. panic("Out of bounds memory address passed to stack allocator (free)");
  215. return nil;
  216. }
  217. if curr_addr >= start+uintptr(s.curr_offset) {
  218. // NOTE(bill): Allow double frees
  219. return nil;
  220. }
  221. header := (^Stack_Allocation_Header)(curr_addr - size_of(Stack_Allocation_Header));
  222. old_offset := int(curr_addr - uintptr(header.padding) - uintptr(&s.data[0]));
  223. if old_offset != int(header.prev_offset) {
  224. panic("Out of order stack allocator free");
  225. return nil;
  226. }
  227. s.curr_offset = int(old_offset);
  228. s.prev_offset = int(header.prev_offset);
  229. case .Free_All:
  230. s.prev_offset = 0;
  231. s.curr_offset = 0;
  232. case .Resize:
  233. if old_memory == nil {
  234. return raw_alloc(s, size, alignment);
  235. }
  236. if size == 0 {
  237. return nil;
  238. }
  239. start := uintptr(&s.data[0]);
  240. end := start + uintptr(len(s.data));
  241. curr_addr := uintptr(old_memory);
  242. if !(start <= curr_addr && curr_addr < end) {
  243. panic("Out of bounds memory address passed to stack allocator (resize)");
  244. return nil;
  245. }
  246. if curr_addr >= start+uintptr(s.curr_offset) {
  247. // NOTE(bill): Allow double frees
  248. return nil;
  249. }
  250. if old_size == size {
  251. return old_memory;
  252. }
  253. header := (^Stack_Allocation_Header)(curr_addr - size_of(Stack_Allocation_Header));
  254. old_offset := int(curr_addr - uintptr(header.padding) - uintptr(&s.data[0]));
  255. if old_offset != int(header.prev_offset) {
  256. ptr := raw_alloc(s, size, alignment);
  257. copy(ptr, old_memory, min(old_size, size));
  258. return ptr;
  259. }
  260. old_memory_size := uintptr(s.curr_offset) - (curr_addr - start);
  261. assert(old_memory_size == uintptr(old_size));
  262. diff := size - old_size;
  263. s.curr_offset += diff; // works for smaller sizes too
  264. if diff > 0 {
  265. zero(rawptr(curr_addr + uintptr(diff)), diff);
  266. }
  267. return old_memory;
  268. }
  269. return nil;
  270. }
  271. Small_Stack_Allocation_Header :: struct {
  272. padding: u8,
  273. }
  274. // Small_Stack is a stack-like allocator which uses the smallest possible header but at the cost of non-strict memory freeing order
  275. Small_Stack :: struct {
  276. data: []byte,
  277. offset: int,
  278. peak_used: int,
  279. }
  280. init_small_stack :: proc(s: ^Small_Stack, data: []byte) {
  281. s.data = data;
  282. s.offset = 0;
  283. s.peak_used = 0;
  284. }
  285. small_stack_allocator :: proc(stack: ^Small_Stack) -> Allocator {
  286. return Allocator{
  287. procedure = small_stack_allocator_proc,
  288. data = stack,
  289. };
  290. }
  291. small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  292. size, alignment: int,
  293. old_memory: rawptr, old_size: int, flags: u64, location := #caller_location) -> rawptr {
  294. s := cast(^Small_Stack)allocator_data;
  295. if s.data == nil {
  296. return nil;
  297. }
  298. align := clamp(alignment, 1, 8*size_of(Stack_Allocation_Header{}.padding)/2);
  299. raw_alloc :: proc(s: ^Small_Stack, size, alignment: int) -> rawptr {
  300. curr_addr := uintptr(&s.data[0]) + uintptr(s.offset);
  301. padding := calc_padding_with_header(curr_addr, uintptr(alignment), size_of(Small_Stack_Allocation_Header));
  302. if s.offset + padding + size > len(s.data) {
  303. return nil;
  304. }
  305. s.offset += padding;
  306. next_addr := curr_addr + uintptr(padding);
  307. header := (^Small_Stack_Allocation_Header)(next_addr - size_of(Small_Stack_Allocation_Header));
  308. header.padding = auto_cast padding;
  309. s.offset += size;
  310. s.peak_used = max(s.peak_used, s.offset);
  311. return zero(rawptr(next_addr), size);
  312. }
  313. switch mode {
  314. case .Alloc:
  315. return raw_alloc(s, size, align);
  316. case .Free:
  317. if old_memory == nil {
  318. return nil;
  319. }
  320. start := uintptr(&s.data[0]);
  321. end := start + uintptr(len(s.data));
  322. curr_addr := uintptr(old_memory);
  323. if !(start <= curr_addr && curr_addr < end) {
  324. panic("Out of bounds memory address passed to stack allocator (free)");
  325. return nil;
  326. }
  327. if curr_addr >= start+uintptr(s.offset) {
  328. // NOTE(bill): Allow double frees
  329. return nil;
  330. }
  331. header := (^Small_Stack_Allocation_Header)(curr_addr - size_of(Small_Stack_Allocation_Header));
  332. old_offset := int(curr_addr - uintptr(header.padding) - uintptr(&s.data[0]));
  333. s.offset = int(old_offset);
  334. case .Free_All:
  335. s.offset = 0;
  336. case .Resize:
  337. if old_memory == nil {
  338. return raw_alloc(s, size, align);
  339. }
  340. if size == 0 {
  341. return nil;
  342. }
  343. start := uintptr(&s.data[0]);
  344. end := start + uintptr(len(s.data));
  345. curr_addr := uintptr(old_memory);
  346. if !(start <= curr_addr && curr_addr < end) {
  347. panic("Out of bounds memory address passed to stack allocator (resize)");
  348. return nil;
  349. }
  350. if curr_addr >= start+uintptr(s.offset) {
  351. // NOTE(bill): Treat as a double free
  352. return nil;
  353. }
  354. if old_size == size {
  355. return old_memory;
  356. }
  357. ptr := raw_alloc(s, size, align);
  358. copy(ptr, old_memory, min(old_size, size));
  359. return ptr;
  360. }
  361. return nil;
  362. }
  363. Dynamic_Pool :: struct {
  364. block_size: int,
  365. out_band_size: int,
  366. alignment: int,
  367. unused_blocks: [dynamic]rawptr,
  368. used_blocks: [dynamic]rawptr,
  369. out_band_allocations: [dynamic]rawptr,
  370. current_block: rawptr,
  371. current_pos: rawptr,
  372. bytes_left: int,
  373. block_allocator: Allocator,
  374. }
  375. DYNAMIC_POOL_BLOCK_SIZE_DEFAULT :: 65536;
  376. DYNAMIC_POOL_OUT_OF_BAND_SIZE_DEFAULT :: 6554;
  377. dynamic_pool_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  378. size, alignment: int,
  379. old_memory: rawptr, old_size: int,
  380. flags: u64 = 0, loc := #caller_location) -> rawptr {
  381. pool := (^Dynamic_Pool)(allocator_data);
  382. switch mode {
  383. case Allocator_Mode.Alloc:
  384. return dynamic_pool_alloc(pool, size);
  385. case Allocator_Mode.Free:
  386. panic("Allocator_Mode.Free is not supported for a pool");
  387. case Allocator_Mode.Free_All:
  388. dynamic_pool_free_all(pool);
  389. case Allocator_Mode.Resize:
  390. panic("Allocator_Mode.Resize is not supported for a pool");
  391. if old_size >= size {
  392. return old_memory;
  393. }
  394. ptr := dynamic_pool_alloc(pool, size);
  395. copy(ptr, old_memory, old_size);
  396. return ptr;
  397. }
  398. return nil;
  399. }
  400. dynamic_pool_allocator :: proc(pool: ^Dynamic_Pool) -> Allocator {
  401. return Allocator{
  402. procedure = dynamic_pool_allocator_proc,
  403. data = pool,
  404. };
  405. }
  406. dynamic_pool_init :: proc(pool: ^Dynamic_Pool,
  407. block_allocator := context.allocator,
  408. array_allocator := context.allocator,
  409. block_size := DYNAMIC_POOL_BLOCK_SIZE_DEFAULT,
  410. out_band_size := DYNAMIC_POOL_OUT_OF_BAND_SIZE_DEFAULT,
  411. alignment := 8) {
  412. pool.block_size = block_size;
  413. pool.out_band_size = out_band_size;
  414. pool.alignment = alignment;
  415. pool.block_allocator = block_allocator;
  416. pool.out_band_allocations.allocator = array_allocator;
  417. pool. unused_blocks.allocator = array_allocator;
  418. pool. used_blocks.allocator = array_allocator;
  419. }
  420. dynamic_pool_destroy :: proc(using pool: ^Dynamic_Pool) {
  421. dynamic_pool_free_all(pool);
  422. delete(unused_blocks);
  423. delete(used_blocks);
  424. zero(pool, size_of(pool^));
  425. }
  426. dynamic_pool_alloc :: proc(using pool: ^Dynamic_Pool, bytes: int) -> rawptr {
  427. cycle_new_block :: proc(using pool: ^Dynamic_Pool) {
  428. if block_allocator.procedure == nil {
  429. panic("You must call pool_init on a Pool before using it");
  430. }
  431. if current_block != nil {
  432. append(&used_blocks, current_block);
  433. }
  434. new_block: rawptr;
  435. if len(unused_blocks) > 0 {
  436. new_block = pop(&unused_blocks);
  437. } else {
  438. new_block = block_allocator.procedure(block_allocator.data, Allocator_Mode.Alloc,
  439. block_size, alignment,
  440. nil, 0);
  441. }
  442. bytes_left = block_size;
  443. current_pos = new_block;
  444. current_block = new_block;
  445. }
  446. n := bytes;
  447. extra := alignment - (n % alignment);
  448. n += extra;
  449. if n >= out_band_size {
  450. assert(block_allocator.procedure != nil);
  451. memory := block_allocator.procedure(block_allocator.data, Allocator_Mode.Alloc,
  452. block_size, alignment,
  453. nil, 0);
  454. if memory != nil {
  455. append(&out_band_allocations, (^byte)(memory));
  456. }
  457. return memory;
  458. }
  459. if bytes_left < n {
  460. cycle_new_block(pool);
  461. if current_block == nil {
  462. return nil;
  463. }
  464. }
  465. memory := current_pos;
  466. current_pos = ptr_offset((^byte)(current_pos), n);
  467. bytes_left -= n;
  468. return memory;
  469. }
  470. dynamic_pool_reset :: proc(using pool: ^Dynamic_Pool) {
  471. if current_block != nil {
  472. append(&unused_blocks, current_block);
  473. current_block = nil;
  474. }
  475. for block in used_blocks {
  476. append(&unused_blocks, block);
  477. }
  478. clear(&used_blocks);
  479. for a in out_band_allocations {
  480. free(a, block_allocator);
  481. }
  482. clear(&out_band_allocations);
  483. }
  484. dynamic_pool_free_all :: proc(using pool: ^Dynamic_Pool) {
  485. dynamic_pool_reset(pool);
  486. for block in unused_blocks {
  487. free(block, block_allocator);
  488. }
  489. clear(&unused_blocks);
  490. }