string_name.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**************************************************************************/
  2. /* string_name.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 "string_name.h"
  31. #include "core/os/os.h"
  32. #include "core/string/print_string.h"
  33. struct StringName::Table {
  34. constexpr static uint32_t TABLE_BITS = 16;
  35. constexpr static uint32_t TABLE_LEN = 1 << TABLE_BITS;
  36. constexpr static uint32_t TABLE_MASK = TABLE_LEN - 1;
  37. static inline _Data *table[TABLE_LEN];
  38. static inline Mutex mutex;
  39. };
  40. void StringName::setup() {
  41. ERR_FAIL_COND(configured);
  42. for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
  43. Table::table[i] = nullptr;
  44. }
  45. configured = true;
  46. }
  47. void StringName::cleanup() {
  48. MutexLock lock(Table::mutex);
  49. #ifdef DEBUG_ENABLED
  50. if (unlikely(debug_stringname)) {
  51. Vector<_Data *> data;
  52. for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
  53. _Data *d = Table::table[i];
  54. while (d) {
  55. data.push_back(d);
  56. d = d->next;
  57. }
  58. }
  59. print_line("\nStringName reference ranking (from most to least referenced):\n");
  60. data.sort_custom<DebugSortReferences>();
  61. int unreferenced_stringnames = 0;
  62. int rarely_referenced_stringnames = 0;
  63. for (int i = 0; i < data.size(); i++) {
  64. print_line(itos(i + 1) + ": " + data[i]->name + " - " + itos(data[i]->debug_references));
  65. if (data[i]->debug_references == 0) {
  66. unreferenced_stringnames += 1;
  67. } else if (data[i]->debug_references < 5) {
  68. rarely_referenced_stringnames += 1;
  69. }
  70. }
  71. print_line(vformat("\nOut of %d StringNames, %d StringNames were never referenced during this run (0 times) (%.2f%%).", data.size(), unreferenced_stringnames, unreferenced_stringnames / float(data.size()) * 100));
  72. print_line(vformat("Out of %d StringNames, %d StringNames were rarely referenced during this run (1-4 times) (%.2f%%).", data.size(), rarely_referenced_stringnames, rarely_referenced_stringnames / float(data.size()) * 100));
  73. }
  74. #endif
  75. int lost_strings = 0;
  76. for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
  77. while (Table::table[i]) {
  78. _Data *d = Table::table[i];
  79. if (d->static_count.get() != d->refcount.get()) {
  80. lost_strings++;
  81. if (OS::get_singleton()->is_stdout_verbose()) {
  82. print_line(vformat("Orphan StringName: %s (static: %d, total: %d)", d->name, d->static_count.get(), d->refcount.get()));
  83. }
  84. }
  85. Table::table[i] = Table::table[i]->next;
  86. memdelete(d);
  87. }
  88. }
  89. if (lost_strings) {
  90. print_verbose(vformat("StringName: %d unclaimed string names at exit.", lost_strings));
  91. }
  92. configured = false;
  93. }
  94. void StringName::unref() {
  95. ERR_FAIL_COND(!configured);
  96. if (_data && _data->refcount.unref()) {
  97. MutexLock lock(Table::mutex);
  98. if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) {
  99. ERR_PRINT("BUG: Unreferenced static string to 0: " + _data->name);
  100. }
  101. if (_data->prev) {
  102. _data->prev->next = _data->next;
  103. } else {
  104. const uint32_t idx = _data->hash & Table::TABLE_MASK;
  105. Table::table[idx] = _data->next;
  106. }
  107. if (_data->next) {
  108. _data->next->prev = _data->prev;
  109. }
  110. memdelete(_data);
  111. }
  112. _data = nullptr;
  113. }
  114. uint32_t StringName::get_empty_hash() {
  115. static uint32_t empty_hash = String::hash("");
  116. return empty_hash;
  117. }
  118. bool StringName::operator==(const String &p_name) const {
  119. if (_data) {
  120. return _data->name == p_name;
  121. }
  122. return p_name.is_empty();
  123. }
  124. bool StringName::operator==(const char *p_name) const {
  125. if (_data) {
  126. return _data->name == p_name;
  127. }
  128. return p_name[0] == 0;
  129. }
  130. bool StringName::operator!=(const String &p_name) const {
  131. return !(operator==(p_name));
  132. }
  133. bool StringName::operator!=(const char *p_name) const {
  134. return !(operator==(p_name));
  135. }
  136. char32_t StringName::operator[](int p_index) const {
  137. if (_data) {
  138. return _data->name[p_index];
  139. }
  140. CRASH_BAD_INDEX(p_index, 0);
  141. return 0;
  142. }
  143. int StringName::length() const {
  144. if (_data) {
  145. return _data->name.length();
  146. }
  147. return 0;
  148. }
  149. bool StringName::is_empty() const {
  150. if (_data) {
  151. return _data->name.is_empty();
  152. }
  153. return true;
  154. }
  155. StringName &StringName::operator=(const StringName &p_name) {
  156. if (this == &p_name) {
  157. return *this;
  158. }
  159. unref();
  160. if (p_name._data && p_name._data->refcount.ref()) {
  161. _data = p_name._data;
  162. }
  163. return *this;
  164. }
  165. StringName::StringName(const StringName &p_name) {
  166. _data = nullptr;
  167. ERR_FAIL_COND(!configured);
  168. if (p_name._data && p_name._data->refcount.ref()) {
  169. _data = p_name._data;
  170. }
  171. }
  172. void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
  173. MutexLock lock(Table::mutex);
  174. if (*ptr == StringName()) {
  175. *ptr = StringName(p_name, true);
  176. }
  177. }
  178. StringName::StringName(const char *p_name, bool p_static) {
  179. _data = nullptr;
  180. ERR_FAIL_COND(!configured);
  181. if (!p_name || p_name[0] == 0) {
  182. return; //empty, ignore
  183. }
  184. const uint32_t hash = String::hash(p_name);
  185. const uint32_t idx = hash & Table::TABLE_MASK;
  186. MutexLock lock(Table::mutex);
  187. _data = Table::table[idx];
  188. while (_data) {
  189. // compare hash first
  190. if (_data->hash == hash && _data->name == p_name) {
  191. break;
  192. }
  193. _data = _data->next;
  194. }
  195. if (_data && _data->refcount.ref()) {
  196. // exists
  197. if (p_static) {
  198. _data->static_count.increment();
  199. }
  200. #ifdef DEBUG_ENABLED
  201. if (unlikely(debug_stringname)) {
  202. _data->debug_references++;
  203. }
  204. #endif
  205. return;
  206. }
  207. _data = memnew(_Data);
  208. _data->name = p_name;
  209. _data->refcount.init();
  210. _data->static_count.set(p_static ? 1 : 0);
  211. _data->hash = hash;
  212. _data->next = Table::table[idx];
  213. _data->prev = nullptr;
  214. #ifdef DEBUG_ENABLED
  215. if (unlikely(debug_stringname)) {
  216. // Keep in memory, force static.
  217. _data->refcount.ref();
  218. _data->static_count.increment();
  219. }
  220. #endif
  221. if (Table::table[idx]) {
  222. Table::table[idx]->prev = _data;
  223. }
  224. Table::table[idx] = _data;
  225. }
  226. StringName::StringName(const String &p_name, bool p_static) {
  227. _data = nullptr;
  228. ERR_FAIL_COND(!configured);
  229. if (p_name.is_empty()) {
  230. return;
  231. }
  232. const uint32_t hash = p_name.hash();
  233. const uint32_t idx = hash & Table::TABLE_MASK;
  234. MutexLock lock(Table::mutex);
  235. _data = Table::table[idx];
  236. while (_data) {
  237. if (_data->hash == hash && _data->name == p_name) {
  238. break;
  239. }
  240. _data = _data->next;
  241. }
  242. if (_data && _data->refcount.ref()) {
  243. // exists
  244. if (p_static) {
  245. _data->static_count.increment();
  246. }
  247. #ifdef DEBUG_ENABLED
  248. if (unlikely(debug_stringname)) {
  249. _data->debug_references++;
  250. }
  251. #endif
  252. return;
  253. }
  254. _data = memnew(_Data);
  255. _data->name = p_name;
  256. _data->refcount.init();
  257. _data->static_count.set(p_static ? 1 : 0);
  258. _data->hash = hash;
  259. _data->next = Table::table[idx];
  260. _data->prev = nullptr;
  261. #ifdef DEBUG_ENABLED
  262. if (unlikely(debug_stringname)) {
  263. // Keep in memory, force static.
  264. _data->refcount.ref();
  265. _data->static_count.increment();
  266. }
  267. #endif
  268. if (Table::table[idx]) {
  269. Table::table[idx]->prev = _data;
  270. }
  271. Table::table[idx] = _data;
  272. }
  273. StringName StringName::search(const char *p_name) {
  274. ERR_FAIL_COND_V(!configured, StringName());
  275. ERR_FAIL_NULL_V(p_name, StringName());
  276. if (!p_name[0]) {
  277. return StringName();
  278. }
  279. const uint32_t hash = String::hash(p_name);
  280. const uint32_t idx = hash & Table::TABLE_MASK;
  281. MutexLock lock(Table::mutex);
  282. _Data *_data = Table::table[idx];
  283. while (_data) {
  284. // compare hash first
  285. if (_data->hash == hash && _data->name == p_name) {
  286. break;
  287. }
  288. _data = _data->next;
  289. }
  290. if (_data && _data->refcount.ref()) {
  291. #ifdef DEBUG_ENABLED
  292. if (unlikely(debug_stringname)) {
  293. _data->debug_references++;
  294. }
  295. #endif
  296. return StringName(_data);
  297. }
  298. return StringName(); //does not exist
  299. }
  300. StringName StringName::search(const char32_t *p_name) {
  301. ERR_FAIL_COND_V(!configured, StringName());
  302. ERR_FAIL_NULL_V(p_name, StringName());
  303. if (!p_name[0]) {
  304. return StringName();
  305. }
  306. const uint32_t hash = String::hash(p_name);
  307. const uint32_t idx = hash & Table::TABLE_MASK;
  308. MutexLock lock(Table::mutex);
  309. _Data *_data = Table::table[idx];
  310. while (_data) {
  311. // compare hash first
  312. if (_data->hash == hash && _data->name == p_name) {
  313. break;
  314. }
  315. _data = _data->next;
  316. }
  317. if (_data && _data->refcount.ref()) {
  318. return StringName(_data);
  319. }
  320. return StringName(); //does not exist
  321. }
  322. StringName StringName::search(const String &p_name) {
  323. ERR_FAIL_COND_V(p_name.is_empty(), StringName());
  324. const uint32_t hash = p_name.hash();
  325. const uint32_t idx = hash & Table::TABLE_MASK;
  326. MutexLock lock(Table::mutex);
  327. _Data *_data = Table::table[idx];
  328. while (_data) {
  329. // compare hash first
  330. if (_data->hash == hash && _data->name == p_name) {
  331. break;
  332. }
  333. _data = _data->next;
  334. }
  335. if (_data && _data->refcount.ref()) {
  336. #ifdef DEBUG_ENABLED
  337. if (unlikely(debug_stringname)) {
  338. _data->debug_references++;
  339. }
  340. #endif
  341. return StringName(_data);
  342. }
  343. return StringName(); //does not exist
  344. }
  345. bool operator==(const String &p_name, const StringName &p_string_name) {
  346. return p_string_name.operator==(p_name);
  347. }
  348. bool operator!=(const String &p_name, const StringName &p_string_name) {
  349. return p_string_name.operator!=(p_name);
  350. }
  351. bool operator==(const char *p_name, const StringName &p_string_name) {
  352. return p_string_name.operator==(p_name);
  353. }
  354. bool operator!=(const char *p_name, const StringName &p_string_name) {
  355. return p_string_name.operator!=(p_name);
  356. }