handle_table.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <utility>
  5. #include "common/assert.h"
  6. #include "common/logging/log.h"
  7. #include "core/core.h"
  8. #include "core/hle/kernel/errors.h"
  9. #include "core/hle/kernel/handle_table.h"
  10. #include "core/hle/kernel/scheduler.h"
  11. #include "core/hle/kernel/process.h"
  12. #include "core/hle/kernel/thread.h"
  13. namespace Kernel {
  14. namespace {
  15. constexpr u16 GetSlot(Handle handle) {
  16. return static_cast<u16>(handle >> 15);
  17. }
  18. constexpr u16 GetGeneration(Handle handle) {
  19. return static_cast<u16>(handle & 0x7FFF);
  20. }
  21. } // Anonymous namespace
  22. HandleTable::HandleTable() {
  23. Clear();
  24. }
  25. HandleTable::~HandleTable() = default;
  26. ResultCode HandleTable::SetSize(s32 handle_table_size) {
  27. if (static_cast<u32>(handle_table_size) > MAX_COUNT) {
  28. LOG_ERROR(Kernel, "Handle table size {} is greater than {}", handle_table_size, MAX_COUNT);
  29. return ERR_OUT_OF_MEMORY;
  30. }
  31. // Values less than or equal to zero indicate to use the maximum allowable
  32. // size for the handle table in the actual kernel, so we ignore the given
  33. // value in that case, since we assume this by default unless this function
  34. // is called.
  35. if (handle_table_size > 0) {
  36. table_size = static_cast<u16>(handle_table_size);
  37. }
  38. return RESULT_SUCCESS;
  39. }
  40. ResultVal<Handle> HandleTable::Create(std::shared_ptr<Object> obj) {
  41. DEBUG_ASSERT(obj != nullptr);
  42. const u16 slot = next_free_slot;
  43. if (slot >= table_size) {
  44. LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
  45. return ERR_HANDLE_TABLE_FULL;
  46. }
  47. next_free_slot = generations[slot];
  48. const u16 generation = next_generation++;
  49. // Overflow count so it fits in the 15 bits dedicated to the generation in the handle.
  50. // Horizon OS uses zero to represent an invalid handle, so skip to 1.
  51. if (next_generation >= (1 << 15)) {
  52. next_generation = 1;
  53. }
  54. generations[slot] = generation;
  55. objects[slot] = std::move(obj);
  56. Handle handle = generation | (slot << 15);
  57. return MakeResult<Handle>(handle);
  58. }
  59. ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
  60. std::shared_ptr<Object> object = GetGeneric(handle);
  61. if (object == nullptr) {
  62. LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle);
  63. return ERR_INVALID_HANDLE;
  64. }
  65. return Create(std::move(object));
  66. }
  67. ResultCode HandleTable::Close(Handle handle) {
  68. if (!IsValid(handle)) {
  69. LOG_ERROR(Kernel, "Handle is not valid! handle={:08X}", handle);
  70. return ERR_INVALID_HANDLE;
  71. }
  72. const u16 slot = GetSlot(handle);
  73. objects[slot] = nullptr;
  74. generations[slot] = next_free_slot;
  75. next_free_slot = slot;
  76. return RESULT_SUCCESS;
  77. }
  78. bool HandleTable::IsValid(Handle handle) const {
  79. const std::size_t slot = GetSlot(handle);
  80. const u16 generation = GetGeneration(handle);
  81. return slot < table_size && objects[slot] != nullptr && generations[slot] == generation;
  82. }
  83. std::shared_ptr<Object> HandleTable::GetGeneric(Handle handle) const {
  84. if (handle == CurrentThread) {
  85. return SharedFrom(Core::System::GetInstance().CurrentScheduler().GetCurrentThread());
  86. } else if (handle == CurrentProcess) {
  87. return SharedFrom(Core::System::GetInstance().CurrentProcess());
  88. }
  89. if (!IsValid(handle)) {
  90. return nullptr;
  91. }
  92. return objects[GetSlot(handle)];
  93. }
  94. void HandleTable::Clear() {
  95. for (u16 i = 0; i < table_size; ++i) {
  96. generations[i] = i + 1;
  97. objects[i] = nullptr;
  98. }
  99. next_free_slot = 0;
  100. }
  101. } // namespace Kernel