string_name.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. StaticCString StaticCString::create(const char *p_ptr) {
  34. StaticCString scs;
  35. scs.ptr = p_ptr;
  36. return scs;
  37. }
  38. bool StringName::_Data::operator==(const String &p_name) const {
  39. if (cname) {
  40. return p_name == cname;
  41. } else {
  42. return name == p_name;
  43. }
  44. }
  45. bool StringName::_Data::operator!=(const String &p_name) const {
  46. return !operator==(p_name);
  47. }
  48. bool StringName::_Data::operator==(const char *p_name) const {
  49. if (cname) {
  50. return strcmp(cname, p_name) == 0;
  51. } else {
  52. return name == p_name;
  53. }
  54. }
  55. bool StringName::_Data::operator!=(const char *p_name) const {
  56. return !operator==(p_name);
  57. }
  58. StringName _scs_create(const char *p_chr, bool p_static) {
  59. return (p_chr[0] ? StringName(StaticCString::create(p_chr), p_static) : StringName());
  60. }
  61. void StringName::setup() {
  62. ERR_FAIL_COND(configured);
  63. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  64. _table[i] = nullptr;
  65. }
  66. configured = true;
  67. }
  68. void StringName::cleanup() {
  69. MutexLock lock(mutex);
  70. #ifdef DEBUG_ENABLED
  71. if (unlikely(debug_stringname)) {
  72. Vector<_Data *> data;
  73. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  74. _Data *d = _table[i];
  75. while (d) {
  76. data.push_back(d);
  77. d = d->next;
  78. }
  79. }
  80. print_line("\nStringName reference ranking (from most to least referenced):\n");
  81. data.sort_custom<DebugSortReferences>();
  82. int unreferenced_stringnames = 0;
  83. int rarely_referenced_stringnames = 0;
  84. for (int i = 0; i < data.size(); i++) {
  85. print_line(itos(i + 1) + ": " + data[i]->get_name() + " - " + itos(data[i]->debug_references));
  86. if (data[i]->debug_references == 0) {
  87. unreferenced_stringnames += 1;
  88. } else if (data[i]->debug_references < 5) {
  89. rarely_referenced_stringnames += 1;
  90. }
  91. }
  92. 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));
  93. 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));
  94. }
  95. #endif
  96. int lost_strings = 0;
  97. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  98. while (_table[i]) {
  99. _Data *d = _table[i];
  100. if (d->static_count.get() != d->refcount.get()) {
  101. lost_strings++;
  102. if (OS::get_singleton()->is_stdout_verbose()) {
  103. String dname = String(d->cname ? d->cname : d->name);
  104. print_line(vformat("Orphan StringName: %s (static: %d, total: %d)", dname, d->static_count.get(), d->refcount.get()));
  105. }
  106. }
  107. _table[i] = _table[i]->next;
  108. memdelete(d);
  109. }
  110. }
  111. if (lost_strings) {
  112. print_verbose(vformat("StringName: %d unclaimed string names at exit.", lost_strings));
  113. }
  114. configured = false;
  115. }
  116. void StringName::unref() {
  117. ERR_FAIL_COND(!configured);
  118. if (_data && _data->refcount.unref()) {
  119. MutexLock lock(mutex);
  120. if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) {
  121. if (_data->cname) {
  122. ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->cname));
  123. } else {
  124. ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->name));
  125. }
  126. }
  127. if (_data->prev) {
  128. _data->prev->next = _data->next;
  129. } else {
  130. if (_table[_data->idx] != _data) {
  131. ERR_PRINT("BUG!");
  132. }
  133. _table[_data->idx] = _data->next;
  134. }
  135. if (_data->next) {
  136. _data->next->prev = _data->prev;
  137. }
  138. memdelete(_data);
  139. }
  140. _data = nullptr;
  141. }
  142. bool StringName::operator==(const String &p_name) const {
  143. if (_data) {
  144. return _data->operator==(p_name);
  145. }
  146. return p_name.is_empty();
  147. }
  148. bool StringName::operator==(const char *p_name) const {
  149. if (_data) {
  150. return _data->operator==(p_name);
  151. }
  152. return p_name[0] == 0;
  153. }
  154. bool StringName::operator!=(const String &p_name) const {
  155. return !(operator==(p_name));
  156. }
  157. bool StringName::operator!=(const char *p_name) const {
  158. return !(operator==(p_name));
  159. }
  160. bool StringName::operator!=(const StringName &p_name) const {
  161. // the real magic of all this mess happens here.
  162. // this is why path comparisons are very fast
  163. return _data != p_name._data;
  164. }
  165. char32_t StringName::operator[](int p_index) const {
  166. if (_data) {
  167. if (_data->cname) {
  168. CRASH_BAD_INDEX(p_index, static_cast<long>(strlen(_data->cname)));
  169. return _data->cname[p_index];
  170. } else {
  171. return _data->name[p_index];
  172. }
  173. }
  174. CRASH_BAD_INDEX(p_index, 0);
  175. return 0;
  176. }
  177. int StringName::length() const {
  178. if (_data) {
  179. if (_data->cname) {
  180. return strlen(_data->cname);
  181. } else {
  182. return _data->name.length();
  183. }
  184. }
  185. return 0;
  186. }
  187. bool StringName::is_empty() const {
  188. if (_data) {
  189. if (_data->cname) {
  190. return _data->cname[0] == 0;
  191. } else {
  192. return _data->name.is_empty();
  193. }
  194. }
  195. return true;
  196. }
  197. StringName &StringName::operator=(const StringName &p_name) {
  198. if (this == &p_name) {
  199. return *this;
  200. }
  201. unref();
  202. if (p_name._data && p_name._data->refcount.ref()) {
  203. _data = p_name._data;
  204. }
  205. return *this;
  206. }
  207. StringName::StringName(const StringName &p_name) {
  208. _data = nullptr;
  209. ERR_FAIL_COND(!configured);
  210. if (p_name._data && p_name._data->refcount.ref()) {
  211. _data = p_name._data;
  212. }
  213. }
  214. void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
  215. mutex.lock();
  216. if (*ptr == StringName()) {
  217. *ptr = StringName(p_name, true);
  218. }
  219. mutex.unlock();
  220. }
  221. StringName::StringName(const char *p_name, bool p_static) {
  222. _data = nullptr;
  223. ERR_FAIL_COND(!configured);
  224. if (!p_name || p_name[0] == 0) {
  225. return; //empty, ignore
  226. }
  227. MutexLock lock(mutex);
  228. uint32_t hash = String::hash(p_name);
  229. uint32_t idx = hash & STRING_TABLE_MASK;
  230. _data = _table[idx];
  231. while (_data) {
  232. // compare hash first
  233. if (_data->hash == hash && _data->operator==(p_name)) {
  234. break;
  235. }
  236. _data = _data->next;
  237. }
  238. if (_data && _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. _data = memnew(_Data);
  251. _data->name = p_name;
  252. _data->refcount.init();
  253. _data->static_count.set(p_static ? 1 : 0);
  254. _data->hash = hash;
  255. _data->idx = idx;
  256. _data->cname = nullptr;
  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 StaticCString &p_static_string, bool p_static) {
  272. _data = nullptr;
  273. ERR_FAIL_COND(!configured);
  274. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  275. MutexLock lock(mutex);
  276. uint32_t hash = String::hash(p_static_string.ptr);
  277. uint32_t idx = hash & STRING_TABLE_MASK;
  278. _data = _table[idx];
  279. while (_data) {
  280. // compare hash first
  281. if (_data->hash == hash && _data->operator==(p_static_string.ptr)) {
  282. break;
  283. }
  284. _data = _data->next;
  285. }
  286. if (_data && _data->refcount.ref()) {
  287. // exists
  288. if (p_static) {
  289. _data->static_count.increment();
  290. }
  291. #ifdef DEBUG_ENABLED
  292. if (unlikely(debug_stringname)) {
  293. _data->debug_references++;
  294. }
  295. #endif
  296. return;
  297. }
  298. _data = memnew(_Data);
  299. _data->refcount.init();
  300. _data->static_count.set(p_static ? 1 : 0);
  301. _data->hash = hash;
  302. _data->idx = idx;
  303. _data->cname = p_static_string.ptr;
  304. _data->next = _table[idx];
  305. _data->prev = nullptr;
  306. #ifdef DEBUG_ENABLED
  307. if (unlikely(debug_stringname)) {
  308. // Keep in memory, force static.
  309. _data->refcount.ref();
  310. _data->static_count.increment();
  311. }
  312. #endif
  313. if (_table[idx]) {
  314. _table[idx]->prev = _data;
  315. }
  316. _table[idx] = _data;
  317. }
  318. StringName::StringName(const String &p_name, bool p_static) {
  319. _data = nullptr;
  320. ERR_FAIL_COND(!configured);
  321. if (p_name.is_empty()) {
  322. return;
  323. }
  324. MutexLock lock(mutex);
  325. uint32_t hash = p_name.hash();
  326. uint32_t idx = hash & STRING_TABLE_MASK;
  327. _data = _table[idx];
  328. while (_data) {
  329. if (_data->hash == hash && _data->operator==(p_name)) {
  330. break;
  331. }
  332. _data = _data->next;
  333. }
  334. if (_data && _data->refcount.ref()) {
  335. // exists
  336. if (p_static) {
  337. _data->static_count.increment();
  338. }
  339. #ifdef DEBUG_ENABLED
  340. if (unlikely(debug_stringname)) {
  341. _data->debug_references++;
  342. }
  343. #endif
  344. return;
  345. }
  346. _data = memnew(_Data);
  347. _data->name = p_name;
  348. _data->refcount.init();
  349. _data->static_count.set(p_static ? 1 : 0);
  350. _data->hash = hash;
  351. _data->idx = idx;
  352. _data->cname = nullptr;
  353. _data->next = _table[idx];
  354. _data->prev = nullptr;
  355. #ifdef DEBUG_ENABLED
  356. if (unlikely(debug_stringname)) {
  357. // Keep in memory, force static.
  358. _data->refcount.ref();
  359. _data->static_count.increment();
  360. }
  361. #endif
  362. if (_table[idx]) {
  363. _table[idx]->prev = _data;
  364. }
  365. _table[idx] = _data;
  366. }
  367. StringName StringName::search(const char *p_name) {
  368. ERR_FAIL_COND_V(!configured, StringName());
  369. ERR_FAIL_NULL_V(p_name, StringName());
  370. if (!p_name[0]) {
  371. return StringName();
  372. }
  373. MutexLock lock(mutex);
  374. uint32_t hash = String::hash(p_name);
  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 && _data->operator==(p_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. StringName StringName::search(const char32_t *p_name) {
  395. ERR_FAIL_COND_V(!configured, StringName());
  396. ERR_FAIL_NULL_V(p_name, StringName());
  397. if (!p_name[0]) {
  398. return StringName();
  399. }
  400. MutexLock lock(mutex);
  401. uint32_t hash = String::hash(p_name);
  402. uint32_t idx = hash & STRING_TABLE_MASK;
  403. _Data *_data = _table[idx];
  404. while (_data) {
  405. // compare hash first
  406. if (_data->hash == hash && _data->operator==(p_name)) {
  407. break;
  408. }
  409. _data = _data->next;
  410. }
  411. if (_data && _data->refcount.ref()) {
  412. return StringName(_data);
  413. }
  414. return StringName(); //does not exist
  415. }
  416. StringName StringName::search(const String &p_name) {
  417. ERR_FAIL_COND_V(p_name.is_empty(), StringName());
  418. MutexLock lock(mutex);
  419. uint32_t hash = p_name.hash();
  420. uint32_t idx = hash & STRING_TABLE_MASK;
  421. _Data *_data = _table[idx];
  422. while (_data) {
  423. // compare hash first
  424. if (_data->hash == hash && _data->operator==(p_name)) {
  425. break;
  426. }
  427. _data = _data->next;
  428. }
  429. if (_data && _data->refcount.ref()) {
  430. #ifdef DEBUG_ENABLED
  431. if (unlikely(debug_stringname)) {
  432. _data->debug_references++;
  433. }
  434. #endif
  435. return StringName(_data);
  436. }
  437. return StringName(); //does not exist
  438. }
  439. bool operator==(const String &p_name, const StringName &p_string_name) {
  440. return p_string_name.operator==(p_name);
  441. }
  442. bool operator!=(const String &p_name, const StringName &p_string_name) {
  443. return p_string_name.operator!=(p_name);
  444. }
  445. bool operator==(const char *p_name, const StringName &p_string_name) {
  446. return p_string_name.operator==(p_name);
  447. }
  448. bool operator!=(const char *p_name, const StringName &p_string_name) {
  449. return p_string_name.operator!=(p_name);
  450. }