handle_table.cpp 3.5 KB

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