handle_table.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. HandleTable();
  42. ~HandleTable();
  43. /**
  44. * Allocates a handle for the given object.
  45. * @return The created Handle or one of the following errors:
  46. * - `ERR_HANDLE_TABLE_FULL`: the maximum number of handles has been exceeded.
  47. */
  48. ResultVal<Handle> Create(SharedPtr<Object> obj);
  49. /**
  50. * Returns a new handle that points to the same object as the passed in handle.
  51. * @return The duplicated Handle or one of the following errors:
  52. * - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
  53. * - Any errors returned by `Create()`.
  54. */
  55. ResultVal<Handle> Duplicate(Handle handle);
  56. /**
  57. * Closes a handle, removing it from the table and decreasing the object's ref-count.
  58. * @return `RESULT_SUCCESS` or one of the following errors:
  59. * - `ERR_INVALID_HANDLE`: an invalid handle was passed in.
  60. */
  61. ResultCode Close(Handle handle);
  62. /// Checks if a handle is valid and points to an existing object.
  63. bool IsValid(Handle handle) const;
  64. /**
  65. * Looks up a handle.
  66. * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid.
  67. */
  68. SharedPtr<Object> GetGeneric(Handle handle) const;
  69. /**
  70. * Looks up a handle while verifying its type.
  71. * @return Pointer to the looked-up object, or `nullptr` if the handle is not valid or its
  72. * type differs from the requested one.
  73. */
  74. template <class T>
  75. SharedPtr<T> Get(Handle handle) const {
  76. return DynamicObjectCast<T>(GetGeneric(handle));
  77. }
  78. /// Closes all handles held in this table.
  79. void Clear();
  80. private:
  81. /// This is the maximum limit of handles allowed per process in Horizon
  82. static constexpr std::size_t MAX_COUNT = 1024;
  83. /// Stores the Object referenced by the handle or null if the slot is empty.
  84. std::array<SharedPtr<Object>, MAX_COUNT> objects;
  85. /**
  86. * The value of `next_generation` when the handle was created, used to check for validity. For
  87. * empty slots, contains the index of the next free slot in the list.
  88. */
  89. std::array<u16, MAX_COUNT> generations;
  90. /**
  91. * Global counter of the number of created handles. Stored in `generations` when a handle is
  92. * created, and wraps around to 1 when it hits 0x8000.
  93. */
  94. u16 next_generation;
  95. /// Head of the free slots linked list.
  96. u16 next_free_slot;
  97. };
  98. } // namespace Kernel