string_name.cpp 13 KB

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