string_name.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*************************************************************************/
  2. /* string_name.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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:\n");
  67. data.sort_custom<DebugSortReferences>();
  68. for (int i = 0; i < MIN(100, data.size()); i++) {
  69. print_line(itos(i + 1) + ": " + data[i]->get_name() + " - " + itos(data[i]->debug_references));
  70. }
  71. }
  72. #endif
  73. int lost_strings = 0;
  74. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  75. while (_table[i]) {
  76. _Data *d = _table[i];
  77. if (d->static_count.get() != d->refcount.get()) {
  78. lost_strings++;
  79. if (OS::get_singleton()->is_stdout_verbose()) {
  80. if (d->cname) {
  81. print_line("Orphan StringName: " + String(d->cname));
  82. } else {
  83. print_line("Orphan StringName: " + String(d->name));
  84. }
  85. }
  86. }
  87. _table[i] = _table[i]->next;
  88. memdelete(d);
  89. }
  90. }
  91. if (lost_strings) {
  92. print_verbose("StringName: " + itos(lost_strings) + " unclaimed string names at exit.");
  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 (_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 StringName &p_name) const {
  138. // the real magic of all this mess happens here.
  139. // this is why path comparisons are very fast
  140. return _data != p_name._data;
  141. }
  142. void StringName::operator=(const StringName &p_name) {
  143. if (this == &p_name) {
  144. return;
  145. }
  146. unref();
  147. if (p_name._data && p_name._data->refcount.ref()) {
  148. _data = p_name._data;
  149. }
  150. }
  151. StringName::StringName(const StringName &p_name) {
  152. _data = nullptr;
  153. ERR_FAIL_COND(!configured);
  154. if (p_name._data && p_name._data->refcount.ref()) {
  155. _data = p_name._data;
  156. }
  157. }
  158. StringName::StringName(const char *p_name, bool p_static) {
  159. _data = nullptr;
  160. ERR_FAIL_COND(!configured);
  161. if (!p_name || p_name[0] == 0) {
  162. return; //empty, ignore
  163. }
  164. MutexLock lock(mutex);
  165. uint32_t hash = String::hash(p_name);
  166. uint32_t idx = hash & STRING_TABLE_MASK;
  167. _data = _table[idx];
  168. while (_data) {
  169. // compare hash first
  170. if (_data->hash == hash && _data->get_name() == p_name) {
  171. break;
  172. }
  173. _data = _data->next;
  174. }
  175. if (_data) {
  176. if (_data->refcount.ref()) {
  177. // exists
  178. if (p_static) {
  179. _data->static_count.increment();
  180. }
  181. #ifdef DEBUG_ENABLED
  182. if (unlikely(debug_stringname)) {
  183. _data->debug_references++;
  184. }
  185. #endif
  186. }
  187. return;
  188. }
  189. _data = memnew(_Data);
  190. _data->name = p_name;
  191. _data->refcount.init();
  192. _data->static_count.set(p_static ? 1 : 0);
  193. _data->hash = hash;
  194. _data->idx = idx;
  195. _data->cname = nullptr;
  196. _data->next = _table[idx];
  197. _data->prev = nullptr;
  198. #ifdef DEBUG_ENABLED
  199. if (unlikely(debug_stringname)) {
  200. // Keep in memory, force static.
  201. _data->refcount.ref();
  202. _data->static_count.increment();
  203. }
  204. #endif
  205. if (_table[idx]) {
  206. _table[idx]->prev = _data;
  207. }
  208. _table[idx] = _data;
  209. }
  210. StringName::StringName(const StaticCString &p_static_string, bool p_static) {
  211. _data = nullptr;
  212. ERR_FAIL_COND(!configured);
  213. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  214. MutexLock lock(mutex);
  215. uint32_t hash = String::hash(p_static_string.ptr);
  216. uint32_t idx = hash & STRING_TABLE_MASK;
  217. _data = _table[idx];
  218. while (_data) {
  219. // compare hash first
  220. if (_data->hash == hash && _data->get_name() == p_static_string.ptr) {
  221. break;
  222. }
  223. _data = _data->next;
  224. }
  225. if (_data) {
  226. if (_data->refcount.ref()) {
  227. // exists
  228. if (p_static) {
  229. _data->static_count.increment();
  230. }
  231. #ifdef DEBUG_ENABLED
  232. if (unlikely(debug_stringname)) {
  233. _data->debug_references++;
  234. }
  235. #endif
  236. return;
  237. }
  238. }
  239. _data = memnew(_Data);
  240. _data->refcount.init();
  241. _data->static_count.set(p_static ? 1 : 0);
  242. _data->hash = hash;
  243. _data->idx = idx;
  244. _data->cname = p_static_string.ptr;
  245. _data->next = _table[idx];
  246. _data->prev = nullptr;
  247. #ifdef DEBUG_ENABLED
  248. if (unlikely(debug_stringname)) {
  249. // Keep in memory, force static.
  250. _data->refcount.ref();
  251. _data->static_count.increment();
  252. }
  253. #endif
  254. if (_table[idx]) {
  255. _table[idx]->prev = _data;
  256. }
  257. _table[idx] = _data;
  258. }
  259. StringName::StringName(const String &p_name, bool p_static) {
  260. _data = nullptr;
  261. ERR_FAIL_COND(!configured);
  262. if (p_name.is_empty()) {
  263. return;
  264. }
  265. MutexLock lock(mutex);
  266. uint32_t hash = p_name.hash();
  267. uint32_t idx = hash & STRING_TABLE_MASK;
  268. _data = _table[idx];
  269. while (_data) {
  270. if (_data->hash == hash && _data->get_name() == p_name) {
  271. break;
  272. }
  273. _data = _data->next;
  274. }
  275. if (_data) {
  276. if (_data->refcount.ref()) {
  277. // exists
  278. if (p_static) {
  279. _data->static_count.increment();
  280. }
  281. #ifdef DEBUG_ENABLED
  282. if (unlikely(debug_stringname)) {
  283. _data->debug_references++;
  284. }
  285. #endif
  286. return;
  287. }
  288. }
  289. _data = memnew(_Data);
  290. _data->name = p_name;
  291. _data->refcount.init();
  292. _data->static_count.set(p_static ? 1 : 0);
  293. _data->hash = hash;
  294. _data->idx = idx;
  295. _data->cname = nullptr;
  296. _data->next = _table[idx];
  297. _data->prev = nullptr;
  298. #ifdef DEBUG_ENABLED
  299. if (unlikely(debug_stringname)) {
  300. // Keep in memory, force static.
  301. _data->refcount.ref();
  302. _data->static_count.increment();
  303. }
  304. #endif
  305. if (_table[idx]) {
  306. _table[idx]->prev = _data;
  307. }
  308. _table[idx] = _data;
  309. }
  310. StringName StringName::search(const char *p_name) {
  311. ERR_FAIL_COND_V(!configured, StringName());
  312. ERR_FAIL_COND_V(!p_name, StringName());
  313. if (!p_name[0]) {
  314. return StringName();
  315. }
  316. MutexLock lock(mutex);
  317. uint32_t hash = String::hash(p_name);
  318. uint32_t idx = hash & STRING_TABLE_MASK;
  319. _Data *_data = _table[idx];
  320. while (_data) {
  321. // compare hash first
  322. if (_data->hash == hash && _data->get_name() == p_name) {
  323. break;
  324. }
  325. _data = _data->next;
  326. }
  327. if (_data && _data->refcount.ref()) {
  328. #ifdef DEBUG_ENABLED
  329. if (unlikely(debug_stringname)) {
  330. _data->debug_references++;
  331. }
  332. #endif
  333. return StringName(_data);
  334. }
  335. return StringName(); //does not exist
  336. }
  337. StringName StringName::search(const char32_t *p_name) {
  338. ERR_FAIL_COND_V(!configured, StringName());
  339. ERR_FAIL_COND_V(!p_name, StringName());
  340. if (!p_name[0]) {
  341. return StringName();
  342. }
  343. MutexLock lock(mutex);
  344. uint32_t hash = String::hash(p_name);
  345. uint32_t idx = hash & STRING_TABLE_MASK;
  346. _Data *_data = _table[idx];
  347. while (_data) {
  348. // compare hash first
  349. if (_data->hash == hash && _data->get_name() == p_name) {
  350. break;
  351. }
  352. _data = _data->next;
  353. }
  354. if (_data && _data->refcount.ref()) {
  355. return StringName(_data);
  356. }
  357. return StringName(); //does not exist
  358. }
  359. StringName StringName::search(const String &p_name) {
  360. ERR_FAIL_COND_V(p_name.is_empty(), StringName());
  361. MutexLock lock(mutex);
  362. uint32_t hash = p_name.hash();
  363. uint32_t idx = hash & STRING_TABLE_MASK;
  364. _Data *_data = _table[idx];
  365. while (_data) {
  366. // compare hash first
  367. if (_data->hash == hash && p_name == _data->get_name()) {
  368. break;
  369. }
  370. _data = _data->next;
  371. }
  372. if (_data && _data->refcount.ref()) {
  373. #ifdef DEBUG_ENABLED
  374. if (unlikely(debug_stringname)) {
  375. _data->debug_references++;
  376. }
  377. #endif
  378. return StringName(_data);
  379. }
  380. return StringName(); //does not exist
  381. }
  382. bool operator==(const String &p_name, const StringName &p_string_name) {
  383. return p_name == p_string_name.operator String();
  384. }
  385. bool operator!=(const String &p_name, const StringName &p_string_name) {
  386. return p_name != p_string_name.operator String();
  387. }
  388. bool operator==(const char *p_name, const StringName &p_string_name) {
  389. return p_name == p_string_name.operator String();
  390. }
  391. bool operator!=(const char *p_name, const StringName &p_string_name) {
  392. return p_name != p_string_name.operator String();
  393. }