string_name.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 StringName &p_name) const {
  147. // the real magic of all this mess happens here.
  148. // this is why path comparisons are very fast
  149. return _data != p_name._data;
  150. }
  151. void StringName::operator=(const StringName &p_name) {
  152. if (this == &p_name) {
  153. return;
  154. }
  155. unref();
  156. if (p_name._data && p_name._data->refcount.ref()) {
  157. _data = p_name._data;
  158. }
  159. }
  160. StringName::StringName(const StringName &p_name) {
  161. _data = nullptr;
  162. ERR_FAIL_COND(!configured);
  163. if (p_name._data && p_name._data->refcount.ref()) {
  164. _data = p_name._data;
  165. }
  166. }
  167. StringName::StringName(const char *p_name, bool p_static) {
  168. _data = nullptr;
  169. ERR_FAIL_COND(!configured);
  170. if (!p_name || p_name[0] == 0) {
  171. return; //empty, ignore
  172. }
  173. MutexLock lock(mutex);
  174. uint32_t hash = String::hash(p_name);
  175. uint32_t idx = hash & STRING_TABLE_MASK;
  176. _data = _table[idx];
  177. while (_data) {
  178. // compare hash first
  179. if (_data->hash == hash && _data->get_name() == p_name) {
  180. break;
  181. }
  182. _data = _data->next;
  183. }
  184. if (_data) {
  185. if (_data->refcount.ref()) {
  186. // exists
  187. if (p_static) {
  188. _data->static_count.increment();
  189. }
  190. #ifdef DEBUG_ENABLED
  191. if (unlikely(debug_stringname)) {
  192. _data->debug_references++;
  193. }
  194. #endif
  195. }
  196. return;
  197. }
  198. _data = memnew(_Data);
  199. _data->name = p_name;
  200. _data->refcount.init();
  201. _data->static_count.set(p_static ? 1 : 0);
  202. _data->hash = hash;
  203. _data->idx = idx;
  204. _data->cname = nullptr;
  205. _data->next = _table[idx];
  206. _data->prev = nullptr;
  207. #ifdef DEBUG_ENABLED
  208. if (unlikely(debug_stringname)) {
  209. // Keep in memory, force static.
  210. _data->refcount.ref();
  211. _data->static_count.increment();
  212. }
  213. #endif
  214. if (_table[idx]) {
  215. _table[idx]->prev = _data;
  216. }
  217. _table[idx] = _data;
  218. }
  219. StringName::StringName(const StaticCString &p_static_string, bool p_static) {
  220. _data = nullptr;
  221. ERR_FAIL_COND(!configured);
  222. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  223. MutexLock lock(mutex);
  224. uint32_t hash = String::hash(p_static_string.ptr);
  225. uint32_t idx = hash & STRING_TABLE_MASK;
  226. _data = _table[idx];
  227. while (_data) {
  228. // compare hash first
  229. if (_data->hash == hash && _data->get_name() == p_static_string.ptr) {
  230. break;
  231. }
  232. _data = _data->next;
  233. }
  234. if (_data) {
  235. if (_data->refcount.ref()) {
  236. // exists
  237. if (p_static) {
  238. _data->static_count.increment();
  239. }
  240. #ifdef DEBUG_ENABLED
  241. if (unlikely(debug_stringname)) {
  242. _data->debug_references++;
  243. }
  244. #endif
  245. return;
  246. }
  247. }
  248. _data = memnew(_Data);
  249. _data->refcount.init();
  250. _data->static_count.set(p_static ? 1 : 0);
  251. _data->hash = hash;
  252. _data->idx = idx;
  253. _data->cname = p_static_string.ptr;
  254. _data->next = _table[idx];
  255. _data->prev = nullptr;
  256. #ifdef DEBUG_ENABLED
  257. if (unlikely(debug_stringname)) {
  258. // Keep in memory, force static.
  259. _data->refcount.ref();
  260. _data->static_count.increment();
  261. }
  262. #endif
  263. if (_table[idx]) {
  264. _table[idx]->prev = _data;
  265. }
  266. _table[idx] = _data;
  267. }
  268. StringName::StringName(const String &p_name, bool p_static) {
  269. _data = nullptr;
  270. ERR_FAIL_COND(!configured);
  271. if (p_name.is_empty()) {
  272. return;
  273. }
  274. MutexLock lock(mutex);
  275. uint32_t hash = p_name.hash();
  276. uint32_t idx = hash & STRING_TABLE_MASK;
  277. _data = _table[idx];
  278. while (_data) {
  279. if (_data->hash == hash && _data->get_name() == p_name) {
  280. break;
  281. }
  282. _data = _data->next;
  283. }
  284. if (_data) {
  285. if (_data->refcount.ref()) {
  286. // exists
  287. if (p_static) {
  288. _data->static_count.increment();
  289. }
  290. #ifdef DEBUG_ENABLED
  291. if (unlikely(debug_stringname)) {
  292. _data->debug_references++;
  293. }
  294. #endif
  295. return;
  296. }
  297. }
  298. _data = memnew(_Data);
  299. _data->name = p_name;
  300. _data->refcount.init();
  301. _data->static_count.set(p_static ? 1 : 0);
  302. _data->hash = hash;
  303. _data->idx = idx;
  304. _data->cname = nullptr;
  305. _data->next = _table[idx];
  306. _data->prev = nullptr;
  307. #ifdef DEBUG_ENABLED
  308. if (unlikely(debug_stringname)) {
  309. // Keep in memory, force static.
  310. _data->refcount.ref();
  311. _data->static_count.increment();
  312. }
  313. #endif
  314. if (_table[idx]) {
  315. _table[idx]->prev = _data;
  316. }
  317. _table[idx] = _data;
  318. }
  319. StringName StringName::search(const char *p_name) {
  320. ERR_FAIL_COND_V(!configured, StringName());
  321. ERR_FAIL_COND_V(!p_name, StringName());
  322. if (!p_name[0]) {
  323. return StringName();
  324. }
  325. MutexLock lock(mutex);
  326. uint32_t hash = String::hash(p_name);
  327. uint32_t idx = hash & STRING_TABLE_MASK;
  328. _Data *_data = _table[idx];
  329. while (_data) {
  330. // compare hash first
  331. if (_data->hash == hash && _data->get_name() == p_name) {
  332. break;
  333. }
  334. _data = _data->next;
  335. }
  336. if (_data && _data->refcount.ref()) {
  337. #ifdef DEBUG_ENABLED
  338. if (unlikely(debug_stringname)) {
  339. _data->debug_references++;
  340. }
  341. #endif
  342. return StringName(_data);
  343. }
  344. return StringName(); //does not exist
  345. }
  346. StringName StringName::search(const char32_t *p_name) {
  347. ERR_FAIL_COND_V(!configured, StringName());
  348. ERR_FAIL_COND_V(!p_name, StringName());
  349. if (!p_name[0]) {
  350. return StringName();
  351. }
  352. MutexLock lock(mutex);
  353. uint32_t hash = String::hash(p_name);
  354. uint32_t idx = hash & STRING_TABLE_MASK;
  355. _Data *_data = _table[idx];
  356. while (_data) {
  357. // compare hash first
  358. if (_data->hash == hash && _data->get_name() == p_name) {
  359. break;
  360. }
  361. _data = _data->next;
  362. }
  363. if (_data && _data->refcount.ref()) {
  364. return StringName(_data);
  365. }
  366. return StringName(); //does not exist
  367. }
  368. StringName StringName::search(const String &p_name) {
  369. ERR_FAIL_COND_V(p_name.is_empty(), StringName());
  370. MutexLock lock(mutex);
  371. uint32_t hash = p_name.hash();
  372. uint32_t idx = hash & STRING_TABLE_MASK;
  373. _Data *_data = _table[idx];
  374. while (_data) {
  375. // compare hash first
  376. if (_data->hash == hash && p_name == _data->get_name()) {
  377. break;
  378. }
  379. _data = _data->next;
  380. }
  381. if (_data && _data->refcount.ref()) {
  382. #ifdef DEBUG_ENABLED
  383. if (unlikely(debug_stringname)) {
  384. _data->debug_references++;
  385. }
  386. #endif
  387. return StringName(_data);
  388. }
  389. return StringName(); //does not exist
  390. }
  391. bool operator==(const String &p_name, const StringName &p_string_name) {
  392. return p_name == p_string_name.operator String();
  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 char *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. }