string_name.cpp 12 KB

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