default_allocators.odin 5.6 KB

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