handle_table.cpp 3.3 KB

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