default_temp_allocator_arena.odin 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package runtime
  2. import "base:intrinsics"
  3. DEFAULT_ARENA_GROWING_MINIMUM_BLOCK_SIZE :: uint(DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE)
  4. Memory_Block :: struct {
  5. prev: ^Memory_Block,
  6. allocator: Allocator,
  7. base: [^]byte,
  8. used: uint,
  9. capacity: uint,
  10. }
  11. // NOTE: This is a growing arena that is only used for the default temp allocator.
  12. // For your own growing arena needs, prefer `Arena` from `core:mem/virtual`.
  13. Arena :: struct {
  14. backing_allocator: Allocator,
  15. curr_block: ^Memory_Block,
  16. total_used: uint,
  17. total_capacity: uint,
  18. minimum_block_size: uint,
  19. temp_count: uint,
  20. }
  21. @(private, require_results)
  22. safe_add :: #force_inline proc "contextless" (x, y: uint) -> (uint, bool) {
  23. z, did_overflow := intrinsics.overflow_add(x, y)
  24. return z, !did_overflow
  25. }
  26. @(require_results)
  27. memory_block_alloc :: proc(allocator: Allocator, capacity: uint, alignment: uint, loc := #caller_location) -> (block: ^Memory_Block, err: Allocator_Error) {
  28. total_size := uint(capacity + max(alignment, size_of(Memory_Block)))
  29. base_offset := uintptr(max(alignment, size_of(Memory_Block)))
  30. min_alignment: int = max(16, align_of(Memory_Block), int(alignment))
  31. data := mem_alloc(int(total_size), min_alignment, allocator, loc) or_return
  32. block = (^Memory_Block)(raw_data(data))
  33. end := uintptr(raw_data(data)[len(data):])
  34. block.allocator = allocator
  35. block.base = ([^]byte)(uintptr(block) + base_offset)
  36. block.capacity = uint(end - uintptr(block.base))
  37. // Should be zeroed
  38. assert(block.used == 0)
  39. assert(block.prev == nil)
  40. return
  41. }
  42. memory_block_dealloc :: proc(block_to_free: ^Memory_Block, loc := #caller_location) {
  43. if block_to_free != nil {
  44. allocator := block_to_free.allocator
  45. mem_free(block_to_free, allocator, loc)
  46. }
  47. }
  48. @(require_results)
  49. alloc_from_memory_block :: proc(block: ^Memory_Block, min_size, alignment: uint) -> (data: []byte, err: Allocator_Error) {
  50. calc_alignment_offset :: proc "contextless" (block: ^Memory_Block, alignment: uintptr) -> uint {
  51. alignment_offset := uint(0)
  52. ptr := uintptr(block.base[block.used:])
  53. mask := alignment-1
  54. if ptr & mask != 0 {
  55. alignment_offset = uint(alignment - (ptr & mask))
  56. }
  57. return alignment_offset
  58. }
  59. if block == nil {
  60. return nil, .Out_Of_Memory
  61. }
  62. alignment_offset := calc_alignment_offset(block, uintptr(alignment))
  63. size, size_ok := safe_add(min_size, alignment_offset)
  64. if !size_ok {
  65. err = .Out_Of_Memory
  66. return
  67. }
  68. if to_be_used, ok := safe_add(block.used, size); !ok || to_be_used > block.capacity {
  69. err = .Out_Of_Memory
  70. return
  71. }
  72. data = block.base[block.used+alignment_offset:][:min_size]
  73. block.used += size
  74. return
  75. }
  76. @(require_results)
  77. arena_alloc :: proc(arena: ^Arena, size, alignment: uint, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
  78. align_forward_uint :: proc "contextless" (ptr, align: uint) -> uint {
  79. p := ptr
  80. modulo := p & (align-1)
  81. if modulo != 0 {
  82. p += align - modulo
  83. }
  84. return p
  85. }
  86. assert(alignment & (alignment-1) == 0, "non-power of two alignment", loc)
  87. size := size
  88. if size == 0 {
  89. return
  90. }
  91. needed := align_forward_uint(size, alignment)
  92. if arena.curr_block == nil || (safe_add(arena.curr_block.used, needed) or_else 0) > arena.curr_block.capacity {
  93. if arena.minimum_block_size == 0 {
  94. arena.minimum_block_size = DEFAULT_ARENA_GROWING_MINIMUM_BLOCK_SIZE
  95. }
  96. block_size := max(needed, arena.minimum_block_size)
  97. if arena.backing_allocator.procedure == nil {
  98. arena.backing_allocator = default_allocator()
  99. }
  100. new_block := memory_block_alloc(arena.backing_allocator, block_size, alignment, loc) or_return
  101. new_block.prev = arena.curr_block
  102. arena.curr_block = new_block
  103. arena.total_capacity += new_block.capacity
  104. }
  105. prev_used := arena.curr_block.used
  106. data, err = alloc_from_memory_block(arena.curr_block, size, alignment)
  107. arena.total_used += arena.curr_block.used - prev_used
  108. return
  109. }
  110. // `arena_init` will initialize the arena with a usable block.
  111. // This procedure is not necessary to use the Arena as the default zero as `arena_alloc` will set things up if necessary
  112. @(require_results)
  113. arena_init :: proc(arena: ^Arena, size: uint, backing_allocator: Allocator, loc := #caller_location) -> Allocator_Error {
  114. arena^ = {}
  115. arena.backing_allocator = backing_allocator
  116. arena.minimum_block_size = max(size, 1<<12) // minimum block size of 4 KiB
  117. new_block := memory_block_alloc(arena.backing_allocator, arena.minimum_block_size, 0, loc) or_return
  118. arena.curr_block = new_block
  119. arena.total_capacity += new_block.capacity
  120. return nil
  121. }
  122. arena_free_last_memory_block :: proc(arena: ^Arena, loc := #caller_location) {
  123. if free_block := arena.curr_block; free_block != nil {
  124. arena.curr_block = free_block.prev
  125. arena.total_capacity -= free_block.capacity
  126. memory_block_dealloc(free_block, loc)
  127. }
  128. }
  129. // `arena_free_all` will free all but the first memory block, and then reset the memory block
  130. arena_free_all :: proc(arena: ^Arena, loc := #caller_location) {
  131. for arena.curr_block != nil && arena.curr_block.prev != nil {
  132. arena_free_last_memory_block(arena, loc)
  133. }
  134. if arena.curr_block != nil {
  135. intrinsics.mem_zero(arena.curr_block.base, arena.curr_block.used)
  136. arena.curr_block.used = 0
  137. }
  138. arena.total_used = 0
  139. }
  140. arena_destroy :: proc(arena: ^Arena, loc := #caller_location) {
  141. for arena.curr_block != nil {
  142. free_block := arena.curr_block
  143. arena.curr_block = free_block.prev
  144. arena.total_capacity -= free_block.capacity
  145. memory_block_dealloc(free_block, loc)
  146. }
  147. arena.total_used = 0
  148. arena.total_capacity = 0
  149. }
  150. arena_allocator :: proc(arena: ^Arena) -> Allocator {
  151. return Allocator{arena_allocator_proc, arena}
  152. }
  153. arena_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  154. size, alignment: int,
  155. old_memory: rawptr, old_size: int,
  156. location := #caller_location) -> (data: []byte, err: Allocator_Error) {
  157. arena := (^Arena)(allocator_data)
  158. size, alignment := uint(size), uint(alignment)
  159. old_size := uint(old_size)
  160. switch mode {
  161. case .Alloc, .Alloc_Non_Zeroed:
  162. return arena_alloc(arena, size, alignment, location)
  163. case .Free:
  164. err = .Mode_Not_Implemented
  165. case .Free_All:
  166. arena_free_all(arena, location)
  167. case .Resize, .Resize_Non_Zeroed:
  168. old_data := ([^]byte)(old_memory)
  169. switch {
  170. case old_data == nil:
  171. return arena_alloc(arena, size, alignment, location)
  172. case size == old_size:
  173. // return old memory
  174. data = old_data[:size]
  175. return
  176. case size == 0:
  177. err = .Mode_Not_Implemented
  178. return
  179. case (uintptr(old_data) & uintptr(alignment-1) == 0) && size < old_size:
  180. // shrink data in-place
  181. data = old_data[:size]
  182. return
  183. }
  184. new_memory := arena_alloc(arena, size, alignment, location) or_return
  185. if new_memory == nil {
  186. return
  187. }
  188. copy(new_memory, old_data[:old_size])
  189. return new_memory, nil
  190. case .Query_Features:
  191. set := (^Allocator_Mode_Set)(old_memory)
  192. if set != nil {
  193. set^ = {.Alloc, .Alloc_Non_Zeroed, .Free_All, .Resize, .Query_Features}
  194. }
  195. case .Query_Info:
  196. err = .Mode_Not_Implemented
  197. }
  198. return
  199. }
  200. Arena_Temp :: struct {
  201. arena: ^Arena,
  202. block: ^Memory_Block,
  203. used: uint,
  204. }
  205. @(require_results)
  206. arena_temp_begin :: proc(arena: ^Arena, loc := #caller_location) -> (temp: Arena_Temp) {
  207. assert(arena != nil, "nil arena", loc)
  208. temp.arena = arena
  209. temp.block = arena.curr_block
  210. if arena.curr_block != nil {
  211. temp.used = arena.curr_block.used
  212. }
  213. arena.temp_count += 1
  214. return
  215. }
  216. arena_temp_end :: proc(temp: Arena_Temp, loc := #caller_location) {
  217. if temp.arena == nil {
  218. assert(temp.block == nil)
  219. assert(temp.used == 0)
  220. return
  221. }
  222. arena := temp.arena
  223. if temp.block != nil {
  224. memory_block_found := false
  225. for block := arena.curr_block; block != nil; block = block.prev {
  226. if block == temp.block {
  227. memory_block_found = true
  228. break
  229. }
  230. }
  231. if !memory_block_found {
  232. assert(arena.curr_block == temp.block, "memory block stored within Arena_Temp not owned by Arena", loc)
  233. }
  234. for arena.curr_block != temp.block {
  235. arena_free_last_memory_block(arena)
  236. }
  237. if block := arena.curr_block; block != nil {
  238. assert(block.used >= temp.used, "out of order use of arena_temp_end", loc)
  239. amount_to_zero := min(block.used-temp.used, block.capacity-block.used)
  240. intrinsics.mem_zero(block.base[temp.used:], amount_to_zero)
  241. block.used = temp.used
  242. }
  243. }
  244. assert(arena.temp_count > 0, "double-use of arena_temp_end", loc)
  245. arena.temp_count -= 1
  246. }
  247. // Ignore the use of a `arena_temp_begin` entirely
  248. arena_temp_ignore :: proc(temp: Arena_Temp, loc := #caller_location) {
  249. assert(temp.arena != nil, "nil arena", loc)
  250. arena := temp.arena
  251. assert(arena.temp_count > 0, "double-use of arena_temp_end", loc)
  252. arena.temp_count -= 1
  253. }
  254. arena_check_temp :: proc(arena: ^Arena, loc := #caller_location) {
  255. assert(arena.temp_count == 0, "Arena_Temp not been ended", loc)
  256. }