default_temporary_allocator.odin 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package runtime
  2. DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE: int : #config(DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, 4 * Megabyte)
  3. NO_DEFAULT_TEMP_ALLOCATOR: bool : ODIN_OS == .Freestanding || ODIN_DEFAULT_TO_NIL_ALLOCATOR
  4. when NO_DEFAULT_TEMP_ALLOCATOR {
  5. // `Default_Temp_Allocator` is a `nil_allocator` when `NO_DEFAULT_TEMP_ALLOCATOR` is `true`.
  6. Default_Temp_Allocator :: struct {}
  7. default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backing_allocator := context.allocator) {}
  8. default_temp_allocator_destroy :: proc "contextless" (s: ^Default_Temp_Allocator) {}
  9. default_temp_allocator_proc :: nil_allocator_proc
  10. @(require_results)
  11. default_temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: Arena_Temp) {
  12. return
  13. }
  14. default_temp_allocator_temp_end :: proc(temp: Arena_Temp, loc := #caller_location) {
  15. }
  16. } else {
  17. // `Default_Temp_Allocator` is an `Arena` based type of allocator. See `runtime.Arena` for its implementation.
  18. // The default `context.temp_allocator` is typically called with `free_all(context.temp_allocator)` once per "frame-loop"
  19. // to prevent it from "leaking" memory.
  20. //
  21. // Note: `Default_Temp_Allocator` is a `nil_allocator` when `NO_DEFAULT_TEMP_ALLOCATOR` is `true`.
  22. Default_Temp_Allocator :: struct {
  23. arena: Arena,
  24. }
  25. default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backing_allocator := context.allocator) {
  26. _ = arena_init(&s.arena, uint(size), backing_allocator)
  27. }
  28. default_temp_allocator_destroy :: proc "contextless" (s: ^Default_Temp_Allocator) {
  29. if s != nil {
  30. arena_destroy(&s.arena)
  31. s^ = {}
  32. }
  33. }
  34. default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  35. size, alignment: int,
  36. old_memory: rawptr, old_size: int, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
  37. s := (^Default_Temp_Allocator)(allocator_data)
  38. return arena_allocator_proc(&s.arena, mode, size, alignment, old_memory, old_size, loc)
  39. }
  40. @(require_results)
  41. default_temp_allocator_temp_begin :: proc(loc := #caller_location) -> (temp: Arena_Temp) {
  42. if context.temp_allocator.data == &global_default_temp_allocator_data {
  43. temp = arena_temp_begin(&global_default_temp_allocator_data.arena, loc)
  44. }
  45. return
  46. }
  47. default_temp_allocator_temp_end :: proc(temp: Arena_Temp, loc := #caller_location) {
  48. arena_temp_end(temp, loc)
  49. }
  50. @(fini, private)
  51. _destroy_temp_allocator_fini :: proc "contextless" () {
  52. default_temp_allocator_destroy(&global_default_temp_allocator_data)
  53. }
  54. }
  55. @(deferred_out=default_temp_allocator_temp_end)
  56. DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD :: #force_inline proc(ignore := false, loc := #caller_location) -> (Arena_Temp, Source_Code_Location) {
  57. if ignore {
  58. return {}, loc
  59. } else {
  60. return default_temp_allocator_temp_begin(loc), loc
  61. }
  62. }
  63. @(require_results)
  64. default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator {
  65. return Allocator{
  66. procedure = default_temp_allocator_proc,
  67. data = allocator,
  68. }
  69. }