allocators.odin 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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", thread_local)
  10. global_default_temp_allocator_arena: runtime.Arena
  11. @(require_results)
  12. temp_allocator :: proc() -> runtime.Allocator {
  13. return runtime.Allocator{
  14. procedure = temp_allocator_proc,
  15. data = &global_default_temp_allocator_arena,
  16. }
  17. }
  18. @(require_results)
  19. temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: runtime.Arena_Temp) {
  20. temp = runtime.arena_temp_begin(&global_default_temp_allocator_arena, loc)
  21. return
  22. }
  23. temp_allocator_temp_end :: proc(temp: runtime.Arena_Temp, loc := #caller_location) {
  24. runtime.arena_temp_end(temp, loc)
  25. }
  26. @(fini, private)
  27. temp_allocator_fini :: proc() {
  28. runtime.arena_destroy(&global_default_temp_allocator_arena)
  29. global_default_temp_allocator_arena = {}
  30. }
  31. @(deferred_out=temp_allocator_temp_end)
  32. TEMP_ALLOCATOR_GUARD :: #force_inline proc(ignore := false, loc := #caller_location) -> (runtime.Arena_Temp, runtime.Source_Code_Location) {
  33. if ignore {
  34. return {}, loc
  35. } else {
  36. return temp_allocator_temp_begin(loc), loc
  37. }
  38. }