rid_handle.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**************************************************************************/
  2. /* rid_handle.cpp */
  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. #include "rid_handle.h"
  31. #ifdef RID_HANDLES_ENABLED
  32. #include "core/os/memory.h"
  33. #include "core/print_string.h"
  34. #include "core/ustring.h"
  35. // This define will flag up an error when get() or getptr() is called with a NULL object.
  36. // These calls should more correctly be made as get_or_null().
  37. // #define RID_HANDLE_FLAG_NULL_GETS
  38. RID_Database g_rid_database;
  39. RID_Data::~RID_Data() {
  40. }
  41. void RID_OwnerBase::init_rid() {
  42. // NOOP
  43. }
  44. RID_OwnerBase::~RID_OwnerBase() {
  45. _shutdown = true;
  46. }
  47. void RID_OwnerBase::_rid_print(const char *pszType, String sz, const RID &p_rid) {
  48. String tn = pszType;
  49. print_line(tn + " : " + sz + " " + itos(p_rid._id) + " [ " + itos(p_rid._revision) + " ]");
  50. }
  51. RID_Database::RID_Database() {
  52. // request first element so the handles start from 1
  53. uint32_t dummy;
  54. _pool.request(dummy);
  55. }
  56. RID_Database::~RID_Database() {
  57. }
  58. String RID_Database::_rid_to_string(const RID &p_rid, const PoolElement &p_pe) const {
  59. String s = "RID id=" + itos(p_rid._id);
  60. s += " [ rev " + itos(p_rid._revision) + " ], ";
  61. s += "PE [ rev " + itos(p_pe.revision) + " ] ";
  62. #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  63. if (p_pe.filename) {
  64. s += String(p_pe.filename).get_file() + " ";
  65. }
  66. s += "line " + itos(p_pe.line_number);
  67. if (p_pe.previous_filename) {
  68. s += " ( prev ";
  69. s += String(p_pe.previous_filename).get_file() + " ";
  70. s += "line " + itos(p_pe.previous_line_number) + " )";
  71. }
  72. #endif
  73. return s;
  74. }
  75. void RID_Database::preshutdown() {
  76. #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  77. for (uint32_t n = 0; n < _pool.active_size(); n++) {
  78. uint32_t id = _pool.get_active_id(n);
  79. // ignore zero as it is a dummy
  80. if (!id) {
  81. continue;
  82. }
  83. PoolElement &pe = _pool[id];
  84. if (pe.data) {
  85. if (pe.data->_owner) {
  86. const char *tn = pe.data->_owner->get_typename();
  87. // does it already exist?
  88. bool found = false;
  89. for (unsigned int i = 0; i < _owner_names.size(); i++) {
  90. if (_owner_names[i] == tn) {
  91. found = true;
  92. pe.owner_name_id = i;
  93. }
  94. }
  95. if (!found) {
  96. pe.owner_name_id = _owner_names.size();
  97. _owner_names.push_back(tn);
  98. }
  99. }
  100. }
  101. }
  102. #endif
  103. }
  104. void RID_Database::register_leak(uint32_t p_line_number, uint32_t p_owner_name_id, const char *p_filename) {
  105. // does the leak exist already?
  106. for (unsigned int n = 0; n < _leaks.size(); n++) {
  107. Leak &leak = _leaks[n];
  108. if ((leak.line_number == p_line_number) && (leak.filename == p_filename) && (leak.owner_name_id == p_owner_name_id)) {
  109. leak.num_objects_leaked += 1;
  110. return;
  111. }
  112. }
  113. Leak leak;
  114. leak.filename = p_filename;
  115. leak.line_number = p_line_number;
  116. leak.owner_name_id = p_owner_name_id;
  117. leak.num_objects_leaked = 1;
  118. _leaks.push_back(leak);
  119. }
  120. void RID_Database::shutdown() {
  121. // free the first dummy element, so we don't report a false leak
  122. _pool.free(0);
  123. // print leaks
  124. if (_pool.active_size()) {
  125. ERR_PRINT("RID_Database leaked " + itos(_pool.active_size()) + " objects at exit.");
  126. #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  127. for (uint32_t n = 0; n < _pool.active_size(); n++) {
  128. const PoolElement &pe = _pool.get_active(n);
  129. register_leak(pe.line_number, pe.owner_name_id, pe.filename);
  130. }
  131. #endif
  132. }
  133. #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  134. for (uint32_t n = 0; n < _leaks.size(); n++) {
  135. const Leak &leak = _leaks[n];
  136. const char *tn = "RID_Owner unknown";
  137. if (_owner_names.size()) {
  138. tn = _owner_names[leak.owner_name_id];
  139. }
  140. const char *fn = "Filename unknown";
  141. if (leak.filename) {
  142. fn = leak.filename;
  143. }
  144. _err_print_error(tn, fn, leak.line_number, itos(leak.num_objects_leaked) + " RID objects leaked");
  145. }
  146. #endif
  147. _shutdown = true;
  148. }
  149. void RID_Database::handle_make_rid(RID &r_rid, RID_Data *p_data, RID_OwnerBase *p_owner) {
  150. ERR_FAIL_COND_MSG(_shutdown, "RID_Database make_rid use after shutdown.");
  151. ERR_FAIL_COND_MSG(!p_data, "RID_Database make_rid, data is empty.");
  152. bool data_was_empty = true;
  153. _mutex.lock();
  154. PoolElement *pe = _pool.request(r_rid._id);
  155. data_was_empty = !pe->data;
  156. pe->data = p_data;
  157. p_data->_owner = p_owner;
  158. pe->revision = pe->revision + 1;
  159. r_rid._revision = pe->revision;
  160. #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  161. // make a note of the previous allocation - this isn't super necessary
  162. // but can pinpoint source allocations when dangling RIDs occur.
  163. pe->previous_filename = pe->filename;
  164. pe->previous_line_number = pe->line_number;
  165. pe->line_number = 0;
  166. pe->filename = nullptr;
  167. #endif
  168. _mutex.unlock();
  169. ERR_FAIL_COND_MSG(!data_was_empty, "RID_Database make_rid, previous data was not empty.");
  170. }
  171. RID_Data *RID_Database::handle_get(const RID &p_rid) const {
  172. RID_Data *data = handle_get_or_null(p_rid);
  173. #ifdef RID_HANDLE_FLAG_NULL_GETS
  174. ERR_FAIL_COND_V_MSG(!data, nullptr, "RID_Database get is NULL");
  175. #endif
  176. return data;
  177. }
  178. RID_Data *RID_Database::handle_getptr(const RID &p_rid) const {
  179. RID_Data *data = handle_get_or_null(p_rid);
  180. #ifdef RID_HANDLE_FLAG_NULL_GETS
  181. ERR_FAIL_COND_V_MSG(!data, nullptr, "RID_Database getptr is NULL");
  182. #endif
  183. return data;
  184. }
  185. RID_Data *RID_Database::handle_get_or_null(const RID &p_rid) const {
  186. if (p_rid.is_valid()) {
  187. ERR_FAIL_COND_V_MSG(_shutdown, nullptr, "RID_Database get_or_null after shutdown.");
  188. MutexLock lock(_mutex);
  189. // The if statement is to allow breakpointing without a recompile.
  190. if (p_rid._id >= _pool.pool_reserved_size()) {
  191. ERR_FAIL_COND_V_MSG(p_rid._id >= _pool.pool_reserved_size(), nullptr, "RID_Database get_or_null, RID id was outside pool size.");
  192. }
  193. const PoolElement &pe = _pool[p_rid._id];
  194. if (pe.revision != p_rid._revision) {
  195. ERR_FAIL_COND_V_MSG(pe.revision != p_rid._revision, nullptr, "RID get_or_null, revision is incorrect, possible dangling RID. " + _rid_to_string(p_rid, pe));
  196. }
  197. return pe.data;
  198. }
  199. return nullptr;
  200. }
  201. bool RID_Database::handle_is_owner(const RID &p_rid, const RID_OwnerBase *p_owner) const {
  202. if (p_rid.is_valid()) {
  203. ERR_FAIL_COND_V_MSG(_shutdown, false, "RID_Database handle_is_owner after shutdown.");
  204. MutexLock lock(_mutex);
  205. // The if statement is to allow breakpointing without a recompile.
  206. if (p_rid._id >= _pool.pool_reserved_size()) {
  207. ERR_FAIL_COND_V_MSG(p_rid._id >= _pool.pool_reserved_size(), false, "RID_Database handle_is_owner, RID id was outside pool size.");
  208. }
  209. const PoolElement &pe = _pool[p_rid._id];
  210. if (pe.revision != p_rid._revision) {
  211. ERR_FAIL_COND_V_MSG(pe.revision != p_rid._revision, false, "RID handle_is_owner, revision is incorrect, possible dangling RID. " + _rid_to_string(p_rid, pe));
  212. }
  213. return pe.data->_owner == p_owner;
  214. }
  215. return false;
  216. }
  217. void RID_Database::handle_free(const RID &p_rid) {
  218. ERR_FAIL_COND_MSG(_shutdown, "RID_Database free after shutdown.");
  219. bool revision_correct = true;
  220. {
  221. MutexLock lock(_mutex);
  222. ERR_FAIL_COND_MSG(p_rid._id >= _pool.pool_reserved_size(), "RID_Database free, RID id was outside pool size.");
  223. PoolElement &pe = _pool[p_rid._id];
  224. revision_correct = pe.revision == p_rid._revision;
  225. // mark the data as zero, which indicates unused element
  226. if (revision_correct) {
  227. pe.data->_owner = nullptr;
  228. pe.data = nullptr;
  229. _pool.free(p_rid._id);
  230. }
  231. }
  232. ERR_FAIL_COND_MSG(!revision_correct, "RID_Database free, revision is incorrect, object possibly freed more than once.");
  233. }
  234. RID RID_Database::prime(const RID &p_rid, int p_line_number, const char *p_filename) {
  235. #ifdef RID_HANDLE_ALLOCATION_TRACKING_ENABLED
  236. if (p_rid.is_valid()) {
  237. ERR_FAIL_COND_V_MSG(_shutdown, p_rid, "RID_Database prime after shutdown.");
  238. ERR_FAIL_COND_V_MSG(p_rid._id >= _pool.pool_reserved_size(), p_rid, "RID_Database prime, RID id was outside pool size.");
  239. // We could also probably get away without using a lock here if purely for debugging, and not for release editor / templates.
  240. MutexLock lock(_mutex);
  241. PoolElement &pe = _pool[p_rid._id];
  242. ERR_FAIL_COND_V_MSG(pe.revision != p_rid._revision, p_rid, "RID_Database prime, revision is incorrect, object possibly freed before use.");
  243. pe.line_number = p_line_number;
  244. pe.filename = p_filename;
  245. }
  246. #endif
  247. return p_rid;
  248. }
  249. #endif // RID_HANDLES_ENABLED