default_temporary_allocator.odin 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package runtime
  2. DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE: int : #config(DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, 1<<22)
  3. when ODIN_OS == .Freestanding || ODIN_OS == .JS || ODIN_DEFAULT_TO_NIL_ALLOCATOR {
  4. Default_Temp_Allocator :: struct {}
  5. default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backup_allocator := context.allocator) {}
  6. default_temp_allocator_destroy :: proc(s: ^Default_Temp_Allocator) {}
  7. default_temp_allocator_proc :: nil_allocator_proc
  8. } else {
  9. Default_Temp_Allocator :: struct {
  10. data: []byte,
  11. curr_offset: int,
  12. prev_allocation: rawptr,
  13. backup_allocator: Allocator,
  14. leaked_allocations: [dynamic][]byte,
  15. }
  16. default_temp_allocator_init :: proc(s: ^Default_Temp_Allocator, size: int, backup_allocator := context.allocator) {
  17. s.data = make_aligned([]byte, size, 2*align_of(rawptr), backup_allocator)
  18. s.curr_offset = 0
  19. s.prev_allocation = nil
  20. s.backup_allocator = backup_allocator
  21. s.leaked_allocations.allocator = backup_allocator
  22. }
  23. default_temp_allocator_destroy :: proc(s: ^Default_Temp_Allocator) {
  24. if s == nil {
  25. return
  26. }
  27. for ptr in s.leaked_allocations {
  28. free(raw_data(ptr), s.backup_allocator)
  29. }
  30. delete(s.leaked_allocations)
  31. delete(s.data, s.backup_allocator)
  32. s^ = {}
  33. }
  34. @(private)
  35. default_temp_allocator_alloc :: proc(s: ^Default_Temp_Allocator, size, alignment: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
  36. size := size
  37. size = align_forward_int(size, alignment)
  38. switch {
  39. case s.curr_offset+size <= len(s.data):
  40. start := uintptr(raw_data(s.data))
  41. ptr := start + uintptr(s.curr_offset)
  42. ptr = align_forward_uintptr(ptr, uintptr(alignment))
  43. mem_zero(rawptr(ptr), size)
  44. s.prev_allocation = rawptr(ptr)
  45. offset := int(ptr - start)
  46. s.curr_offset = offset + size
  47. return byte_slice(rawptr(ptr), size), .None
  48. case size <= len(s.data):
  49. start := uintptr(raw_data(s.data))
  50. ptr := align_forward_uintptr(start, uintptr(alignment))
  51. mem_zero(rawptr(ptr), size)
  52. s.prev_allocation = rawptr(ptr)
  53. offset := int(ptr - start)
  54. s.curr_offset = offset + size
  55. return byte_slice(rawptr(ptr), size), .None
  56. }
  57. a := s.backup_allocator
  58. if a.procedure == nil {
  59. a = context.allocator
  60. s.backup_allocator = a
  61. }
  62. data, err := mem_alloc_bytes(size, alignment, a, loc)
  63. if err != nil {
  64. return data, err
  65. }
  66. if s.leaked_allocations == nil {
  67. s.leaked_allocations = make([dynamic][]byte, a)
  68. }
  69. append(&s.leaked_allocations, data)
  70. // TODO(bill): Should leaks be notified about?
  71. if logger := context.logger; logger.lowest_level <= .Warning {
  72. if logger.procedure != nil {
  73. logger.procedure(logger.data, .Warning, "default temp allocator resorted to backup_allocator" , logger.options, loc)
  74. }
  75. }
  76. return data, .None
  77. }
  78. @(private)
  79. default_temp_allocator_free :: proc(s: ^Default_Temp_Allocator, old_memory: rawptr, loc := #caller_location) -> Allocator_Error {
  80. if old_memory == nil {
  81. return .None
  82. }
  83. start := uintptr(raw_data(s.data))
  84. end := start + uintptr(len(s.data))
  85. old_ptr := uintptr(old_memory)
  86. if s.prev_allocation == old_memory {
  87. s.curr_offset = int(uintptr(s.prev_allocation) - start)
  88. s.prev_allocation = nil
  89. return .None
  90. }
  91. if start <= old_ptr && old_ptr < end {
  92. // NOTE(bill): Cannot free this pointer but it is valid
  93. return .None
  94. }
  95. if len(s.leaked_allocations) != 0 {
  96. for data, i in s.leaked_allocations {
  97. ptr := raw_data(data)
  98. if ptr == old_memory {
  99. free(ptr, s.backup_allocator)
  100. ordered_remove(&s.leaked_allocations, i)
  101. return .None
  102. }
  103. }
  104. }
  105. return .Invalid_Pointer
  106. // panic("invalid pointer passed to default_temp_allocator");
  107. }
  108. @(private)
  109. default_temp_allocator_free_all :: proc(s: ^Default_Temp_Allocator, loc := #caller_location) {
  110. s.curr_offset = 0
  111. s.prev_allocation = nil
  112. for data in s.leaked_allocations {
  113. free(raw_data(data), s.backup_allocator)
  114. }
  115. clear(&s.leaked_allocations)
  116. }
  117. @(private)
  118. default_temp_allocator_resize :: proc(s: ^Default_Temp_Allocator, old_memory: rawptr, old_size, size, alignment: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
  119. begin := uintptr(raw_data(s.data))
  120. end := begin + uintptr(len(s.data))
  121. old_ptr := uintptr(old_memory)
  122. if old_memory == s.prev_allocation && old_ptr & uintptr(alignment)-1 == 0 {
  123. if old_ptr+uintptr(size) < end {
  124. s.curr_offset = int(old_ptr-begin)+size
  125. return byte_slice(old_memory, size), .None
  126. }
  127. }
  128. data, err := default_temp_allocator_alloc(s, size, alignment, loc)
  129. if err == .None {
  130. copy(data, byte_slice(old_memory, old_size))
  131. err = default_temp_allocator_free(s, old_memory, loc)
  132. }
  133. return data, err
  134. }
  135. default_temp_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  136. size, alignment: int,
  137. old_memory: rawptr, old_size: int, loc := #caller_location) -> (data: []byte, err: Allocator_Error) {
  138. s := (^Default_Temp_Allocator)(allocator_data)
  139. if s.data == nil {
  140. default_temp_allocator_init(s, DEFAULT_TEMP_ALLOCATOR_BACKING_SIZE, default_allocator())
  141. }
  142. switch mode {
  143. case .Alloc:
  144. data, err = default_temp_allocator_alloc(s, size, alignment, loc)
  145. case .Free:
  146. err = default_temp_allocator_free(s, old_memory, loc)
  147. case .Free_All:
  148. default_temp_allocator_free_all(s, loc)
  149. case .Resize:
  150. data, err = default_temp_allocator_resize(s, old_memory, old_size, size, alignment, loc)
  151. case .Query_Features:
  152. set := (^Allocator_Mode_Set)(old_memory)
  153. if set != nil {
  154. set^ = {.Alloc, .Free, .Free_All, .Resize, .Query_Features}
  155. }
  156. case .Query_Info:
  157. return nil, .Mode_Not_Implemented
  158. }
  159. return
  160. }
  161. }
  162. default_temp_allocator :: proc(allocator: ^Default_Temp_Allocator) -> Allocator {
  163. return Allocator{
  164. procedure = default_temp_allocator_proc,
  165. data = allocator,
  166. }
  167. }