handle_table.cpp 3.6 KB

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