string_name.cpp 13 KB

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