string_name.cpp 11 KB

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