handle_table.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. InvalidHandle = 0,
  13. CurrentThread = 0xFFFF8000,
  14. CurrentProcess = 0xFFFF8001,
  15. };
  16. /**
  17. * This class allows the creation of Handles, which are references to objects that can be tested
  18. * for validity and looked up. Here they are used to pass references to kernel objects to/from the
  19. * emulated process. it has been designed so that it follows the same handle format and has
  20. * approximately the same restrictions as the handle manager in the CTR-OS.
  21. *
  22. * Handles contain two sub-fields: a slot index (bits 31:15) and a generation value (bits 14:0).
  23. * The slot index is used to index into the arrays in this class to access the data corresponding
  24. * to the Handle.
  25. *
  26. * To prevent accidental use of a freed Handle whose slot has already been reused, a global counter
  27. * is kept and incremented every time a Handle is created. This is the Handle's "generation". The
  28. * value of the counter is stored into the Handle as well as in the handle table (in the
  29. * "generations" array). When looking up a handle, the Handle's generation must match with the
  30. * value stored on the class, otherwise the Handle is considered invalid.
  31. *
  32. * To find free slots when allocating a Handle without needing to scan the entire object array, the
  33. * generations field of unallocated slots is re-purposed as a linked list of indices to free slots.
  34. * When a Handle is created, an index is popped off the list and used for the new Handle. When it
  35. * is destroyed, it is again pushed onto the list to be re-used by the next allocation. It is
  36. * likely that this allocation strategy differs from the one used in CTR-OS, but this hasn't been
  37. * verified and isn't likely to cause any problems.
  38. */
  39. class HandleTable final : NonCopyable {
  40. public:
  41. /// This is the maximum limit of handles allowed per process in Horizon
  42. static constexpr std::size_t MAX_COUNT = 1024;
  43. HandleTable();
  44. ~HandleTable();
  45. /**
  46. * Sets the number of handles that may be in use at one time
  47. * for this handle table.
  48. *
  49. * @param handle_table_size The desired size to limit the handle table to.
  50. *
  51. * @returns an error code indicating if initialization was successful.
  52. * If initialization was not successful, then ERR_OUT_OF_MEMORY
  53. * will be returned.
  54. *
  55. * @pre handle_table_size must be within the range [0, 1024]
  56. */
  57. ResultCode SetSize(s32 handle_table_size);
  58. /**
  59. * Allocates a handle for the given object.
  60. * @return The created Handle or one of the following errors:
  61. * - `ERR_HANDLE_TABLE_FULL`: the maximum number of handles has been exceeded.
  62. */
  63. ResultVal<Handle> Create(SharedPtr<Object> obj);
  64. /**
  65. * Returns a new handle that points to the same object as the passed in handle.
  66. * @return The duplicated Handle or one of the following errors:
  67. * - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
  68. * - Any errors returned by `Create()`.
  69. */
  70. ResultVal<Handle> Duplicate(Handle handle);
  71. /**
  72. * Closes a handle, removing it from the table and decreasing the object's ref-count.
  73. * @return `RESULT_SUCCESS` or one of the following errors:
  74. * - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
  75. */
  76. ResultCode Close(Handle handle);
  77. /// Checks if a handle is valid and points to an existing object.
  78. bool IsValid(Handle handle) const;
  79. /**
  80. * Looks up a handle.
  81. * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid.
  82. */
  83. SharedPtr<Object> GetGeneric(Handle handle) const;
  84. /**
  85. * Looks up a handle while verifying its type.
  86. * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid or its
  87. * type differs from the requested one.
  88. */
  89. template <class T>
  90. SharedPtr<T> Get(Handle handle) const {
  91. return DynamicObjectCast<T>(GetGeneric(handle));
  92. }
  93. /// Closes all handles held in this table.
  94. void Clear();
  95. private:
  96. /// Stores the Object referenced by the handle or null if the slot is empty.
  97. std::array<SharedPtr<Object>, MAX_COUNT> objects;
  98. /**
  99. * The value of `next_generation` when the handle was created, used to check for validity. For
  100. * empty slots, contains the index of the next free slot in the list.
  101. */
  102. std::array<u16, MAX_COUNT> generations;
  103. /**
  104. * The limited size of the handle table. This can be specified by process
  105. * capabilities in order to restrict the overall number of handles that
  106. * can be created in a process instance
  107. */
  108. u16 table_size = static_cast<u16>(MAX_COUNT);
  109. /**
  110. * Global counter of the number of created handles. Stored in `generations` when a handle is
  111. * created, and wraps around to 1 when it hits 0x8000.
  112. */
  113. u16 next_generation = 1;
  114. /// Head of the free slots linked list.
  115. u16 next_free_slot = 0;
  116. };
  117. } // namespace Kernel