handle_table.h 5.2 KB

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