heap_allocator.odin 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package runtime
  2. import "base:intrinsics"
  3. @(require_results)
  4. heap_allocator :: proc() -> Allocator {
  5. return Allocator{
  6. procedure = heap_allocator_proc,
  7. data = nil,
  8. }
  9. }
  10. heap_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
  11. size, alignment: int,
  12. old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, Allocator_Error) {
  13. //
  14. // NOTE(tetra, 2020-01-14): The heap doesn't respect alignment.
  15. // Instead, we overallocate by `alignment + size_of(rawptr) - 1`, and insert
  16. // padding. We also store the original pointer returned by heap_alloc right before
  17. // the pointer we return to the user.
  18. //
  19. aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr, old_size: int, zero_memory := true) -> ([]byte, Allocator_Error) {
  20. // Not(flysand): We need to reserve enough space for alignment, which
  21. // includes the user data itself, the space to store the pointer to
  22. // allocation start, as well as the padding required to align both
  23. // the user data and the pointer.
  24. a := max(alignment, align_of(rawptr))
  25. space := a-1 + size_of(rawptr) + size
  26. allocated_mem: rawptr
  27. force_copy := old_ptr != nil && alignment > align_of(rawptr)
  28. if old_ptr != nil && !force_copy {
  29. original_old_ptr := ([^]rawptr)(old_ptr)[-1]
  30. allocated_mem = heap_resize(original_old_ptr, space)
  31. } else {
  32. allocated_mem = heap_alloc(space, zero_memory)
  33. }
  34. aligned_mem := rawptr(([^]u8)(allocated_mem)[size_of(rawptr):])
  35. ptr := uintptr(aligned_mem)
  36. aligned_ptr := (ptr + uintptr(a)-1) & ~(uintptr(a)-1)
  37. if allocated_mem == nil {
  38. aligned_free(old_ptr)
  39. aligned_free(allocated_mem)
  40. return nil, .Out_Of_Memory
  41. }
  42. aligned_mem = rawptr(aligned_ptr)
  43. ([^]rawptr)(aligned_mem)[-1] = allocated_mem
  44. if force_copy {
  45. mem_copy_non_overlapping(aligned_mem, old_ptr, min(old_size, size))
  46. aligned_free(old_ptr)
  47. }
  48. return byte_slice(aligned_mem, size), nil
  49. }
  50. aligned_free :: proc(p: rawptr) {
  51. if p != nil {
  52. heap_free(([^]rawptr)(p)[-1])
  53. }
  54. }
  55. aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int, zero_memory := true) -> (new_memory: []byte, err: Allocator_Error) {
  56. if p == nil {
  57. return aligned_alloc(new_size, new_alignment, nil, old_size, zero_memory)
  58. }
  59. new_memory = aligned_alloc(new_size, new_alignment, p, old_size, zero_memory) or_return
  60. when ODIN_OS != .Windows {
  61. // NOTE: heap_resize does not zero the new memory, so we do it
  62. if zero_memory && new_size > old_size {
  63. new_region := raw_data(new_memory[old_size:])
  64. conditional_mem_zero(new_region, new_size - old_size)
  65. }
  66. }
  67. return
  68. }
  69. switch mode {
  70. case .Alloc, .Alloc_Non_Zeroed:
  71. return aligned_alloc(size, alignment, nil, 0, mode == .Alloc)
  72. case .Free:
  73. aligned_free(old_memory)
  74. case .Free_All:
  75. return nil, .Mode_Not_Implemented
  76. case .Resize, .Resize_Non_Zeroed:
  77. return aligned_resize(old_memory, old_size, size, alignment, mode == .Resize)
  78. case .Query_Features:
  79. set := (^Allocator_Mode_Set)(old_memory)
  80. if set != nil {
  81. set^ = {.Alloc, .Alloc_Non_Zeroed, .Free, .Resize, .Resize_Non_Zeroed, .Query_Features}
  82. }
  83. return nil, nil
  84. case .Query_Info:
  85. return nil, .Mode_Not_Implemented
  86. }
  87. return nil, nil
  88. }
  89. heap_alloc :: proc "contextless" (size: int, zero_memory := true) -> rawptr {
  90. return _heap_alloc(size, zero_memory)
  91. }
  92. heap_resize :: proc "contextless" (ptr: rawptr, new_size: int) -> rawptr {
  93. return _heap_resize(ptr, new_size)
  94. }
  95. heap_free :: proc "contextless" (ptr: rawptr) {
  96. _heap_free(ptr)
  97. }