rid_handle.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* rid_handle.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef RID_HANDLE_H
  31. #define RID_HANDLE_H
  32. #include "core/list.h"
  33. #include "core/os/mutex.h"
  34. #include "core/pooled_list.h"
  35. #include "core/safe_refcount.h"
  36. #include "core/typedefs.h"
  37. #include <typeinfo>
  38. // SCONS parameters:
  39. // rids=pointers (default)
  40. // rids=handles
  41. // rids=tracked_handles (handles plus allocation tracking)
  42. // Defines RID_HANDLES_ENABLED and RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  43. // should be defined from Scons as required following the above convention.
  44. // RID_PRIME is the macro which stores line numbers and file in the RID_Database.
  45. // It will be a NOOP if tracking is off.
  46. #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  47. #define RID_PRIME(a) g_rid_database.prime(a, __LINE__, __FILE__)
  48. #else
  49. #define RID_PRIME(a) a
  50. #endif
  51. // All the handle code can be compiled out if they are not in use.
  52. #ifdef RID_HANDLES_ENABLED
  53. // This define will print out each make_rid and free_rid. Useful for debugging.
  54. // #define RID_HANDLE_PRINT_LIFETIMES
  55. class RID_OwnerBase;
  56. class RID_Database;
  57. class RID_Data {
  58. friend class RID_OwnerBase;
  59. friend class RID_Database;
  60. RID_OwnerBase *_owner;
  61. uint32_t _id;
  62. public:
  63. uint32_t get_id() const { return _id; }
  64. virtual ~RID_Data();
  65. };
  66. class RID_Handle {
  67. public:
  68. union {
  69. struct {
  70. uint32_t _id;
  71. uint32_t _revision;
  72. };
  73. uint64_t _handle_data;
  74. };
  75. RID_Handle() {
  76. _handle_data = 0;
  77. }
  78. bool operator==(const RID_Handle &p_rid) const {
  79. return _handle_data == p_rid._handle_data;
  80. }
  81. bool operator<(const RID_Handle &p_rid) const {
  82. return _handle_data < p_rid._handle_data;
  83. }
  84. bool operator<=(const RID_Handle &p_rid) const {
  85. return _handle_data <= p_rid._handle_data;
  86. }
  87. bool operator>(const RID_Handle &p_rid) const {
  88. return _handle_data > p_rid._handle_data;
  89. }
  90. bool operator!=(const RID_Handle &p_rid) const {
  91. return _handle_data != p_rid._handle_data;
  92. }
  93. bool is_valid() const { return _id != 0; }
  94. uint32_t get_id() const { return _id ? _handle_data : 0; }
  95. };
  96. class RID : public RID_Handle {
  97. };
  98. class RID_Database {
  99. struct PoolElement {
  100. RID_Data *data;
  101. uint32_t revision;
  102. #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  103. // current allocation
  104. uint16_t line_number;
  105. uint16_t owner_name_id;
  106. const char *filename;
  107. // previous allocation (allows identifying dangling RID source allocations)
  108. const char *previous_filename;
  109. uint32_t previous_line_number;
  110. #endif
  111. };
  112. struct Leak {
  113. uint16_t line_number;
  114. uint16_t owner_name_id;
  115. const char *filename;
  116. uint32_t num_objects_leaked;
  117. };
  118. // The pooled list zeros on first request .. this is important
  119. // so that we initialize the revision to zero. Other than that, it
  120. // is treated as a POD type.
  121. TrackedPooledList<PoolElement, uint32_t, true, true> _pool;
  122. bool _shutdown = false;
  123. mutable Mutex _mutex;
  124. // This is purely for printing the leaks at the end, as RID_Owners may be
  125. // destroyed before the RID_Database is shutdown, so the RID_Data may be invalid
  126. // by this point, and we still want to have a record of the owner names.
  127. // The owner names should part of the binary, thus the pointers should still be valid.
  128. // They were retrieved using typeid(T).name()
  129. LocalVector<const char *> _owner_names;
  130. LocalVector<Leak> _leaks;
  131. void register_leak(uint32_t p_line_number, uint32_t p_owner_name_id, const char *p_filename);
  132. String _rid_to_string(const RID &p_rid, const PoolElement &p_pe) const;
  133. public:
  134. RID_Database();
  135. ~RID_Database();
  136. // Called to record the owner names before RID_Owners are destroyed
  137. void preshutdown();
  138. // Called after destroying RID_Owners to detect leaks
  139. void shutdown();
  140. // Prepare a RID for memory tracking
  141. RID prime(const RID &p_rid, int p_line_number, const char *p_filename);
  142. void handle_make_rid(RID &r_rid, RID_Data *p_data, RID_OwnerBase *p_owner);
  143. RID_Data *handle_get(const RID &p_rid) const;
  144. RID_Data *handle_getptr(const RID &p_rid) const;
  145. RID_Data *handle_get_or_null(const RID &p_rid) const;
  146. bool handle_is_owner(const RID &p_rid, const RID_OwnerBase *p_owner) const;
  147. void handle_free(const RID &p_rid);
  148. };
  149. extern RID_Database g_rid_database;
  150. class RID_OwnerBase {
  151. protected:
  152. bool _is_owner(const RID &p_rid) const {
  153. return g_rid_database.handle_is_owner(p_rid, this);
  154. }
  155. void _rid_print(const char *pszType, String sz, const RID &p_rid);
  156. const char *_typename = nullptr;
  157. bool _shutdown = false;
  158. public:
  159. virtual void get_owned_list(List<RID> *p_owned) = 0;
  160. const char *get_typename() const { return _typename; }
  161. static void init_rid();
  162. virtual ~RID_OwnerBase();
  163. };
  164. template <class T>
  165. class RID_Owner : public RID_OwnerBase {
  166. public:
  167. RID make_rid(T *p_data) {
  168. RID rid;
  169. g_rid_database.handle_make_rid(rid, p_data, this);
  170. #ifdef RID_HANDLE_PRINT_LIFETIMES
  171. _rid_print(_typename, "make_rid", rid);
  172. #endif
  173. return rid;
  174. }
  175. T *get(const RID &p_rid) {
  176. return static_cast<T *>(g_rid_database.handle_get(p_rid));
  177. }
  178. T *getornull(const RID &p_rid) {
  179. return static_cast<T *>(g_rid_database.handle_get_or_null(p_rid));
  180. }
  181. T *getptr(const RID &p_rid) {
  182. return static_cast<T *>(g_rid_database.handle_getptr(p_rid));
  183. }
  184. bool owns(const RID &p_rid) const {
  185. return _is_owner(p_rid);
  186. }
  187. void free(RID p_rid) {
  188. #ifdef RID_HANDLE_PRINT_LIFETIMES
  189. _rid_print(_typename, "free_rid", p_rid);
  190. #endif
  191. g_rid_database.handle_free(p_rid);
  192. }
  193. void get_owned_list(List<RID> *p_owned){
  194. #ifdef DEBUG_ENABLED
  195. #endif
  196. }
  197. RID_Owner() {
  198. _typename = typeid(T).name();
  199. }
  200. };
  201. #endif // RID_HANDLES_ENABLED
  202. #endif // RID_HANDLE_H