k_slab_heap.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. namespace impl {
  10. class KSlabHeapImpl final : NonCopyable {
  11. public:
  12. struct Node {
  13. Node* next{};
  14. };
  15. constexpr KSlabHeapImpl() = default;
  16. void Initialize(std::size_t size) {
  17. ASSERT(head == nullptr);
  18. obj_size = size;
  19. }
  20. constexpr std::size_t GetObjectSize() const {
  21. return obj_size;
  22. }
  23. Node* GetHead() const {
  24. return head;
  25. }
  26. void* Allocate() {
  27. Node* ret = head.load();
  28. do {
  29. if (ret == nullptr) {
  30. break;
  31. }
  32. } while (!head.compare_exchange_weak(ret, ret->next));
  33. return ret;
  34. }
  35. void Free(void* obj) {
  36. Node* node = static_cast<Node*>(obj);
  37. Node* cur_head = head.load();
  38. do {
  39. node->next = cur_head;
  40. } while (!head.compare_exchange_weak(cur_head, node));
  41. }
  42. private:
  43. std::atomic<Node*> head{};
  44. std::size_t obj_size{};
  45. };
  46. } // namespace impl
  47. class KSlabHeapBase : NonCopyable {
  48. public:
  49. constexpr KSlabHeapBase() = default;
  50. constexpr bool Contains(uintptr_t addr) const {
  51. return start <= addr && addr < end;
  52. }
  53. constexpr std::size_t GetSlabHeapSize() const {
  54. return (end - start) / GetObjectSize();
  55. }
  56. constexpr std::size_t GetObjectSize() const {
  57. return impl.GetObjectSize();
  58. }
  59. constexpr uintptr_t GetSlabHeapAddress() const {
  60. return start;
  61. }
  62. std::size_t GetObjectIndexImpl(const void* obj) const {
  63. return (reinterpret_cast<uintptr_t>(obj) - start) / GetObjectSize();
  64. }
  65. std::size_t GetPeakIndex() const {
  66. return GetObjectIndexImpl(reinterpret_cast<const void*>(peak));
  67. }
  68. void* AllocateImpl() {
  69. return impl.Allocate();
  70. }
  71. void FreeImpl(void* obj) {
  72. // Don't allow freeing an object that wasn't allocated from this heap
  73. ASSERT(Contains(reinterpret_cast<uintptr_t>(obj)));
  74. impl.Free(obj);
  75. }
  76. void InitializeImpl(std::size_t obj_size, void* memory, std::size_t memory_size) {
  77. // Ensure we don't initialize a slab using null memory
  78. ASSERT(memory != nullptr);
  79. // Initialize the base allocator
  80. impl.Initialize(obj_size);
  81. // Set our tracking variables
  82. const std::size_t num_obj = (memory_size / obj_size);
  83. start = reinterpret_cast<uintptr_t>(memory);
  84. end = start + num_obj * obj_size;
  85. peak = start;
  86. // Free the objects
  87. u8* cur = reinterpret_cast<u8*>(end);
  88. for (std::size_t i{}; i < num_obj; i++) {
  89. cur -= obj_size;
  90. impl.Free(cur);
  91. }
  92. }
  93. private:
  94. using Impl = impl::KSlabHeapImpl;
  95. Impl impl;
  96. uintptr_t peak{};
  97. uintptr_t start{};
  98. uintptr_t end{};
  99. };
  100. template <typename T>
  101. class KSlabHeap final : public KSlabHeapBase {
  102. public:
  103. constexpr KSlabHeap() : KSlabHeapBase() {}
  104. void Initialize(void* memory, std::size_t memory_size) {
  105. InitializeImpl(sizeof(T), memory, memory_size);
  106. }
  107. T* Allocate() {
  108. T* obj = static_cast<T*>(AllocateImpl());
  109. if (obj != nullptr) {
  110. new (obj) T();
  111. }
  112. return obj;
  113. }
  114. T* AllocateWithKernel(KernelCore& kernel) {
  115. T* obj = static_cast<T*>(AllocateImpl());
  116. if (obj != nullptr) {
  117. new (obj) T(kernel);
  118. }
  119. return obj;
  120. }
  121. void Free(T* obj) {
  122. FreeImpl(obj);
  123. }
  124. constexpr std::size_t GetObjectIndex(const T* obj) const {
  125. return GetObjectIndexImpl(obj);
  126. }
  127. };
  128. } // namespace Kernel