handle_table.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <cstddef>
  7. #include "common/common_types.h"
  8. #include "core/hle/kernel/object.h"
  9. #include "core/hle/result.h"
  10. namespace Kernel {
  11. enum KernelHandle : Handle {
  12. CurrentThread = 0xFFFF8000,
  13. CurrentProcess = 0xFFFF8001,
  14. };
  15. /**
  16. * This class allows the creation of Handles, which are references to objects that can be tested
  17. * for validity and looked up. Here they are used to pass references to kernel objects to/from the
  18. * emulated process. it has been designed so that it follows the same handle format and has
  19. * approximately the same restrictions as the handle manager in the CTR-OS.
  20. *
  21. * Handles contain two sub-fields: a slot index (bits 31:15) and a generation value (bits 14:0).
  22. * The slot index is used to index into the arrays in this class to access the data corresponding
  23. * to the Handle.
  24. *
  25. * To prevent accidental use of a freed Handle whose slot has already been reused, a global counter
  26. * is kept and incremented every time a Handle is created. This is the Handle's "generation". The
  27. * value of the counter is stored into the Handle as well as in the handle table (in the
  28. * "generations" array). When looking up a handle, the Handle's generation must match with the
  29. * value stored on the class, otherwise the Handle is considered invalid.
  30. *
  31. * To find free slots when allocating a Handle without needing to scan the entire object array, the
  32. * generations field of unallocated slots is re-purposed as a linked list of indices to free slots.
  33. * When a Handle is created, an index is popped off the list and used for the new Handle. When it
  34. * is destroyed, it is again pushed onto the list to be re-used by the next allocation. It is
  35. * likely that this allocation strategy differs from the one used in CTR-OS, but this hasn't been
  36. * verified and isn't likely to cause any problems.
  37. */
  38. class HandleTable final : NonCopyable {
  39. public:
  40. HandleTable();
  41. /**
  42. * Allocates a handle for the given object.
  43. * @return The created Handle or one of the following errors:
  44. * - `ERR_HANDLE_TABLE_FULL`: the maximum number of handles has been exceeded.
  45. */
  46. ResultVal<Handle> Create(SharedPtr<Object> obj);
  47. /**
  48. * Returns a new handle that points to the same object as the passed in handle.
  49. * @return The duplicated Handle or one of the following errors:
  50. * - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
  51. * - Any errors returned by `Create()`.
  52. */
  53. ResultVal<Handle> Duplicate(Handle handle);
  54. /**
  55. * Closes a handle, removing it from the table and decreasing the object's ref-count.
  56. * @return `RESULT_SUCCESS` or one of the following errors:
  57. * - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
  58. */
  59. ResultCode Close(Handle handle);
  60. /// Checks if a handle is valid and points to an existing object.
  61. bool IsValid(Handle handle) const;
  62. /**
  63. * Looks up a handle.
  64. * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid.
  65. */
  66. SharedPtr<Object> GetGeneric(Handle handle) const;
  67. /**
  68. * Looks up a handle while verifying its type.
  69. * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid or its
  70. * type differs from the requested one.
  71. */
  72. template <class T>
  73. SharedPtr<T> Get(Handle handle) const {
  74. return DynamicObjectCast<T>(GetGeneric(handle));
  75. }
  76. /// Closes all handles held in this table.
  77. void Clear();
  78. private:
  79. /**
  80. * This is the maximum limit of handles allowed per process in CTR-OS. It can be further
  81. * reduced by ExHeader values, but this is not emulated here.
  82. */
  83. static const size_t MAX_COUNT = 4096;
  84. static u16 GetSlot(Handle handle) {
  85. return handle >> 15;
  86. }
  87. static u16 GetGeneration(Handle handle) {
  88. return handle & 0x7FFF;
  89. }
  90. /// Stores the Object referenced by the handle or null if the slot is empty.
  91. std::array<SharedPtr<Object>, MAX_COUNT> objects;
  92. /**
  93. * The value of `next_generation` when the handle was created, used to check for validity. For
  94. * empty slots, contains the index of the next free slot in the list.
  95. */
  96. std::array<u16, MAX_COUNT> generations;
  97. /**
  98. * Global counter of the number of created handles. Stored in `generations` when a handle is
  99. * created, and wraps around to 1 when it hits 0x8000.
  100. */
  101. u16 next_generation;
  102. /// Head of the free slots linked list.
  103. u16 next_free_slot;
  104. };
  105. } // namespace Kernel