default_allocators.odin 6.5 KB

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