k_slab_heap.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include "common/assert.h"
  7. #include "common/common_types.h"
  8. namespace Kernel {
  9. class KernelCore;
  10. namespace impl {
  11. class KSlabHeapImpl final : NonCopyable {
  12. public:
  13. struct Node {
  14. Node* next{};
  15. };
  16. constexpr KSlabHeapImpl() = default;
  17. void Initialize(std::size_t size) {
  18. ASSERT(head == nullptr);
  19. obj_size = size;
  20. }
  21. constexpr std::size_t GetObjectSize() const {
  22. return obj_size;
  23. }
  24. Node* GetHead() const {
  25. return head;
  26. }
  27. void* Allocate() {
  28. Node* ret = head.load();
  29. do {
  30. if (ret == nullptr) {
  31. break;
  32. }
  33. } while (!head.compare_exchange_weak(ret, ret->next));
  34. return ret;
  35. }
  36. void Free(void* obj) {
  37. Node* node = static_cast<Node*>(obj);
  38. Node* cur_head = head.load();
  39. do {
  40. node->next = cur_head;
  41. } while (!head.compare_exchange_weak(cur_head, node));
  42. }
  43. private:
  44. std::atomic<Node*> head{};
  45. std::size_t obj_size{};
  46. };
  47. } // namespace impl
  48. class KSlabHeapBase : NonCopyable {
  49. public:
  50. constexpr KSlabHeapBase() = default;
  51. constexpr bool Contains(uintptr_t addr) const {
  52. return start <= addr && addr < end;
  53. }
  54. constexpr std::size_t GetSlabHeapSize() const {
  55. return (end - start) / GetObjectSize();
  56. }
  57. constexpr std::size_t GetObjectSize() const {
  58. return impl.GetObjectSize();
  59. }
  60. constexpr uintptr_t GetSlabHeapAddress() const {
  61. return start;
  62. }
  63. std::size_t GetObjectIndexImpl(const void* obj) const {
  64. return (reinterpret_cast<uintptr_t>(obj) - start) / GetObjectSize();
  65. }
  66. std::size_t GetPeakIndex() const {
  67. return GetObjectIndexImpl(reinterpret_cast<const void*>(peak));
  68. }
  69. void* AllocateImpl() {
  70. return impl.Allocate();
  71. }
  72. void FreeImpl(void* obj) {
  73. // Don't allow freeing an object that wasn't allocated from this heap
  74. ASSERT(Contains(reinterpret_cast<uintptr_t>(obj)));
  75. impl.Free(obj);
  76. }
  77. void InitializeImpl(std::size_t obj_size, void* memory, std::size_t memory_size) {
  78. // Ensure we don't initialize a slab using null memory
  79. ASSERT(memory != nullptr);
  80. // Initialize the base allocator
  81. impl.Initialize(obj_size);
  82. // Set our tracking variables
  83. const std::size_t num_obj = (memory_size / obj_size);
  84. start = reinterpret_cast<uintptr_t>(memory);
  85. end = start + num_obj * obj_size;
  86. peak = start;
  87. // Free the objects
  88. u8* cur = reinterpret_cast<u8*>(end);
  89. for (std::size_t i{}; i < num_obj; i++) {
  90. cur -= obj_size;
  91. impl.Free(cur);
  92. }
  93. }
  94. private:
  95. using Impl = impl::KSlabHeapImpl;
  96. Impl impl;
  97. uintptr_t peak{};
  98. uintptr_t start{};
  99. uintptr_t end{};
  100. };
  101. template <typename T>
  102. class KSlabHeap final : public KSlabHeapBase {
  103. public:
  104. enum class AllocationType {
  105. Host,
  106. Guest,
  107. };
  108. explicit constexpr KSlabHeap(AllocationType allocation_type_ = AllocationType::Host)
  109. : KSlabHeapBase(), allocation_type{allocation_type_} {}
  110. void Initialize(void* memory, std::size_t memory_size) {
  111. if (allocation_type == AllocationType::Guest) {
  112. InitializeImpl(sizeof(T), memory, memory_size);
  113. }
  114. }
  115. T* Allocate() {
  116. switch (allocation_type) {
  117. case AllocationType::Host:
  118. // Fallback for cases where we do not yet support allocating guest memory from the slab
  119. // heap, such as for kernel memory regions.
  120. return new T;
  121. case AllocationType::Guest:
  122. T* obj = static_cast<T*>(AllocateImpl());
  123. if (obj != nullptr) {
  124. new (obj) T();
  125. }
  126. return obj;
  127. }
  128. UNREACHABLE_MSG("Invalid AllocationType {}", allocation_type);
  129. return nullptr;
  130. }
  131. T* AllocateWithKernel(KernelCore& kernel) {
  132. switch (allocation_type) {
  133. case AllocationType::Host:
  134. // Fallback for cases where we do not yet support allocating guest memory from the slab
  135. // heap, such as for kernel memory regions.
  136. return new T(kernel);
  137. case AllocationType::Guest:
  138. T* obj = static_cast<T*>(AllocateImpl());
  139. if (obj != nullptr) {
  140. new (obj) T(kernel);
  141. }
  142. return obj;
  143. }
  144. UNREACHABLE_MSG("Invalid AllocationType {}", allocation_type);
  145. return nullptr;
  146. }
  147. void Free(T* obj) {
  148. switch (allocation_type) {
  149. case AllocationType::Host:
  150. // Fallback for cases where we do not yet support allocating guest memory from the slab
  151. // heap, such as for kernel memory regions.
  152. delete obj;
  153. return;
  154. case AllocationType::Guest:
  155. FreeImpl(obj);
  156. return;
  157. }
  158. UNREACHABLE_MSG("Invalid AllocationType {}", allocation_type);
  159. }
  160. constexpr std::size_t GetObjectIndex(const T* obj) const {
  161. return GetObjectIndexImpl(obj);
  162. }
  163. private:
  164. const AllocationType allocation_type;
  165. };
  166. } // namespace Kernel