string_name.cpp 11 KB

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