string_name.cpp 12 KB

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