allocators.odin 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //+private
  2. package os2
  3. import "base:runtime"
  4. @(require_results)
  5. file_allocator :: proc() -> runtime.Allocator {
  6. return heap_allocator()
  7. }
  8. temp_allocator_proc :: runtime.arena_allocator_proc
  9. @(private="file")
  10. MAX_TEMP_ARENA_COUNT :: 2
  11. @(private="file", thread_local)
  12. global_default_temp_allocator_arenas: [MAX_TEMP_ARENA_COUNT]runtime.Arena
  13. @(private="file", thread_local)
  14. global_default_temp_allocator_index: uint
  15. @(require_results)
  16. temp_allocator :: proc() -> runtime.Allocator {
  17. return runtime.Allocator{
  18. procedure = temp_allocator_proc,
  19. data = &global_default_temp_allocator_arenas[global_default_temp_allocator_index],
  20. }
  21. }
  22. @(require_results)
  23. temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: runtime.Arena_Temp) {
  24. temp = runtime.arena_temp_begin(&global_default_temp_allocator_arenas[global_default_temp_allocator_index], loc)
  25. return
  26. }
  27. temp_allocator_temp_end :: proc(temp: runtime.Arena_Temp, loc := #caller_location) {
  28. runtime.arena_temp_end(temp, loc)
  29. }
  30. @(fini, private)
  31. temp_allocator_fini :: proc() {
  32. for &arena in global_default_temp_allocator_arenas {
  33. runtime.arena_destroy(&arena)
  34. }
  35. global_default_temp_allocator_arenas = {}
  36. }
  37. TEMP_ALLOCATOR_GUARD_END :: proc(temp: runtime.Arena_Temp, loc := #caller_location) {
  38. runtime.arena_temp_end(temp, loc)
  39. if temp.arena != nil {
  40. global_default_temp_allocator_index = (global_default_temp_allocator_index-1)%MAX_TEMP_ARENA_COUNT
  41. }
  42. }
  43. @(deferred_out=TEMP_ALLOCATOR_GUARD_END)
  44. TEMP_ALLOCATOR_GUARD :: #force_inline proc(loc := #caller_location) -> (runtime.Arena_Temp, runtime.Source_Code_Location) {
  45. tmp := temp_allocator_temp_begin(loc)
  46. global_default_temp_allocator_index = (global_default_temp_allocator_index+1)%MAX_TEMP_ARENA_COUNT
  47. return tmp, loc
  48. }
  49. @(init, private)
  50. init_thread_local_cleaner :: proc() {
  51. runtime.add_thread_local_cleaner(temp_allocator_fini)
  52. }