string_name.cpp 12 KB

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