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) 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. 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. String dname = String(d->cname ? d->cname : d->name);
  90. print_line(vformat("Orphan StringName: %s (static: %d, total: %d)", dname, d->static_count.get(), d->refcount.get()));
  91. }
  92. }
  93. _table[i] = _table[i]->next;
  94. memdelete(d);
  95. }
  96. }
  97. if (lost_strings) {
  98. print_verbose(vformat("StringName: %d unclaimed string names at exit.", lost_strings));
  99. }
  100. configured = false;
  101. }
  102. void StringName::unref() {
  103. ERR_FAIL_COND(!configured);
  104. if (_data && _data->refcount.unref()) {
  105. MutexLock lock(mutex);
  106. if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) {
  107. if (_data->cname) {
  108. ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->cname));
  109. } else {
  110. ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->name));
  111. }
  112. }
  113. if (_data->prev) {
  114. _data->prev->next = _data->next;
  115. } else {
  116. if (_table[_data->idx] != _data) {
  117. ERR_PRINT("BUG!");
  118. }
  119. _table[_data->idx] = _data->next;
  120. }
  121. if (_data->next) {
  122. _data->next->prev = _data->prev;
  123. }
  124. memdelete(_data);
  125. }
  126. _data = nullptr;
  127. }
  128. bool StringName::operator==(const String &p_name) const {
  129. if (!_data) {
  130. return (p_name.length() == 0);
  131. }
  132. return (_data->get_name() == p_name);
  133. }
  134. bool StringName::operator==(const char *p_name) const {
  135. if (!_data) {
  136. return (p_name[0] == 0);
  137. }
  138. return (_data->get_name() == p_name);
  139. }
  140. bool StringName::operator!=(const String &p_name) const {
  141. return !(operator==(p_name));
  142. }
  143. bool StringName::operator!=(const char *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. void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
  168. mutex.lock();
  169. if (*ptr == StringName()) {
  170. *ptr = StringName(p_name, true);
  171. }
  172. mutex.unlock();
  173. }
  174. StringName::StringName(const char *p_name, bool p_static) {
  175. _data = nullptr;
  176. ERR_FAIL_COND(!configured);
  177. if (!p_name || p_name[0] == 0) {
  178. return; //empty, ignore
  179. }
  180. MutexLock lock(mutex);
  181. uint32_t hash = String::hash(p_name);
  182. uint32_t idx = hash & STRING_TABLE_MASK;
  183. _data = _table[idx];
  184. while (_data) {
  185. // compare hash first
  186. if (_data->hash == hash && _data->get_name() == p_name) {
  187. break;
  188. }
  189. _data = _data->next;
  190. }
  191. if (_data && _data->refcount.ref()) {
  192. // exists
  193. if (p_static) {
  194. _data->static_count.increment();
  195. }
  196. #ifdef DEBUG_ENABLED
  197. if (unlikely(debug_stringname)) {
  198. _data->debug_references++;
  199. }
  200. #endif
  201. return;
  202. }
  203. _data = memnew(_Data);
  204. _data->name = p_name;
  205. _data->refcount.init();
  206. _data->static_count.set(p_static ? 1 : 0);
  207. _data->hash = hash;
  208. _data->idx = idx;
  209. _data->cname = nullptr;
  210. _data->next = _table[idx];
  211. _data->prev = nullptr;
  212. #ifdef DEBUG_ENABLED
  213. if (unlikely(debug_stringname)) {
  214. // Keep in memory, force static.
  215. _data->refcount.ref();
  216. _data->static_count.increment();
  217. }
  218. #endif
  219. if (_table[idx]) {
  220. _table[idx]->prev = _data;
  221. }
  222. _table[idx] = _data;
  223. }
  224. StringName::StringName(const StaticCString &p_static_string, bool p_static) {
  225. _data = nullptr;
  226. ERR_FAIL_COND(!configured);
  227. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  228. MutexLock lock(mutex);
  229. uint32_t hash = String::hash(p_static_string.ptr);
  230. uint32_t idx = hash & STRING_TABLE_MASK;
  231. _data = _table[idx];
  232. while (_data) {
  233. // compare hash first
  234. if (_data->hash == hash && _data->get_name() == p_static_string.ptr) {
  235. break;
  236. }
  237. _data = _data->next;
  238. }
  239. if (_data && _data->refcount.ref()) {
  240. // exists
  241. if (p_static) {
  242. _data->static_count.increment();
  243. }
  244. #ifdef DEBUG_ENABLED
  245. if (unlikely(debug_stringname)) {
  246. _data->debug_references++;
  247. }
  248. #endif
  249. return;
  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 && _data->refcount.ref()) {
  288. // exists
  289. if (p_static) {
  290. _data->static_count.increment();
  291. }
  292. #ifdef DEBUG_ENABLED
  293. if (unlikely(debug_stringname)) {
  294. _data->debug_references++;
  295. }
  296. #endif
  297. return;
  298. }
  299. _data = memnew(_Data);
  300. _data->name = p_name;
  301. _data->refcount.init();
  302. _data->static_count.set(p_static ? 1 : 0);
  303. _data->hash = hash;
  304. _data->idx = idx;
  305. _data->cname = nullptr;
  306. _data->next = _table[idx];
  307. _data->prev = nullptr;
  308. #ifdef DEBUG_ENABLED
  309. if (unlikely(debug_stringname)) {
  310. // Keep in memory, force static.
  311. _data->refcount.ref();
  312. _data->static_count.increment();
  313. }
  314. #endif
  315. if (_table[idx]) {
  316. _table[idx]->prev = _data;
  317. }
  318. _table[idx] = _data;
  319. }
  320. StringName StringName::search(const char *p_name) {
  321. ERR_FAIL_COND_V(!configured, StringName());
  322. ERR_FAIL_NULL_V(p_name, StringName());
  323. if (!p_name[0]) {
  324. return StringName();
  325. }
  326. MutexLock lock(mutex);
  327. uint32_t hash = String::hash(p_name);
  328. uint32_t idx = hash & STRING_TABLE_MASK;
  329. _Data *_data = _table[idx];
  330. while (_data) {
  331. // compare hash first
  332. if (_data->hash == hash && _data->get_name() == p_name) {
  333. break;
  334. }
  335. _data = _data->next;
  336. }
  337. if (_data && _data->refcount.ref()) {
  338. #ifdef DEBUG_ENABLED
  339. if (unlikely(debug_stringname)) {
  340. _data->debug_references++;
  341. }
  342. #endif
  343. return StringName(_data);
  344. }
  345. return StringName(); //does not exist
  346. }
  347. StringName StringName::search(const char32_t *p_name) {
  348. ERR_FAIL_COND_V(!configured, StringName());
  349. ERR_FAIL_NULL_V(p_name, StringName());
  350. if (!p_name[0]) {
  351. return StringName();
  352. }
  353. MutexLock lock(mutex);
  354. uint32_t hash = String::hash(p_name);
  355. uint32_t idx = hash & STRING_TABLE_MASK;
  356. _Data *_data = _table[idx];
  357. while (_data) {
  358. // compare hash first
  359. if (_data->hash == hash && _data->get_name() == p_name) {
  360. break;
  361. }
  362. _data = _data->next;
  363. }
  364. if (_data && _data->refcount.ref()) {
  365. return StringName(_data);
  366. }
  367. return StringName(); //does not exist
  368. }
  369. StringName StringName::search(const String &p_name) {
  370. ERR_FAIL_COND_V(p_name.is_empty(), StringName());
  371. MutexLock lock(mutex);
  372. uint32_t hash = p_name.hash();
  373. uint32_t idx = hash & STRING_TABLE_MASK;
  374. _Data *_data = _table[idx];
  375. while (_data) {
  376. // compare hash first
  377. if (_data->hash == hash && p_name == _data->get_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. bool operator==(const String &p_name, const StringName &p_string_name) {
  393. return p_name == p_string_name.operator String();
  394. }
  395. bool operator!=(const String &p_name, const StringName &p_string_name) {
  396. return p_name != p_string_name.operator String();
  397. }
  398. bool operator==(const char *p_name, const StringName &p_string_name) {
  399. return p_name == p_string_name.operator String();
  400. }
  401. bool operator!=(const char *p_name, const StringName &p_string_name) {
  402. return p_name != p_string_name.operator String();
  403. }