handle_table.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. ResultVal<Handle> HandleTable::Create(SharedPtr<Object> obj) {
  26. DEBUG_ASSERT(obj != nullptr);
  27. u16 slot = next_free_slot;
  28. if (slot >= generations.size()) {
  29. LOG_ERROR(Kernel, "Unable to allocate Handle, too many slots in use.");
  30. return ERR_HANDLE_TABLE_FULL;
  31. }
  32. next_free_slot = generations[slot];
  33. u16 generation = next_generation++;
  34. // Overflow count so it fits in the 15 bits dedicated to the generation in the handle.
  35. // Horizon OS uses zero to represent an invalid handle, so skip to 1.
  36. if (next_generation >= (1 << 15)) {
  37. next_generation = 1;
  38. }
  39. generations[slot] = generation;
  40. objects[slot] = std::move(obj);
  41. Handle handle = generation | (slot << 15);
  42. return MakeResult<Handle>(handle);
  43. }
  44. ResultVal<Handle> HandleTable::Duplicate(Handle handle) {
  45. SharedPtr<Object> object = GetGeneric(handle);
  46. if (object == nullptr) {
  47. LOG_ERROR(Kernel, "Tried to duplicate invalid handle: {:08X}", handle);
  48. return ERR_INVALID_HANDLE;
  49. }
  50. return Create(std::move(object));
  51. }
  52. ResultCode HandleTable::Close(Handle handle) {
  53. if (!IsValid(handle))
  54. return ERR_INVALID_HANDLE;
  55. u16 slot = GetSlot(handle);
  56. objects[slot] = nullptr;
  57. generations[slot] = next_free_slot;
  58. next_free_slot = slot;
  59. return RESULT_SUCCESS;
  60. }
  61. bool HandleTable::IsValid(Handle handle) const {
  62. std::size_t slot = GetSlot(handle);
  63. u16 generation = GetGeneration(handle);
  64. return slot < MAX_COUNT && objects[slot] != nullptr && generations[slot] == generation;
  65. }
  66. SharedPtr<Object> HandleTable::GetGeneric(Handle handle) const {
  67. if (handle == CurrentThread) {
  68. return GetCurrentThread();
  69. } else if (handle == CurrentProcess) {
  70. return Core::CurrentProcess();
  71. }
  72. if (!IsValid(handle)) {
  73. return nullptr;
  74. }
  75. return objects[GetSlot(handle)];
  76. }
  77. void HandleTable::Clear() {
  78. for (u16 i = 0; i < MAX_COUNT; ++i) {
  79. generations[i] = i + 1;
  80. objects[i] = nullptr;
  81. }
  82. next_free_slot = 0;
  83. }
  84. } // namespace Kernel