string_name.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/mutex.h"
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. struct StringName::Table {
  35. constexpr static uint32_t TABLE_BITS = 16;
  36. constexpr static uint32_t TABLE_LEN = 1 << TABLE_BITS;
  37. constexpr static uint32_t TABLE_MASK = TABLE_LEN - 1;
  38. static inline _Data *table[TABLE_LEN];
  39. static inline BinaryMutex mutex;
  40. static inline PagedAllocator<_Data> allocator;
  41. };
  42. void StringName::setup() {
  43. ERR_FAIL_COND(configured);
  44. for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
  45. Table::table[i] = nullptr;
  46. }
  47. configured = true;
  48. }
  49. void StringName::cleanup() {
  50. MutexLock lock(Table::mutex);
  51. #ifdef DEBUG_ENABLED
  52. if (unlikely(debug_stringname)) {
  53. Vector<_Data *> data;
  54. for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
  55. _Data *d = Table::table[i];
  56. while (d) {
  57. data.push_back(d);
  58. d = d->next;
  59. }
  60. }
  61. print_line("\nStringName reference ranking (from most to least referenced):\n");
  62. data.sort_custom<DebugSortReferences>();
  63. int unreferenced_stringnames = 0;
  64. int rarely_referenced_stringnames = 0;
  65. for (int i = 0; i < data.size(); i++) {
  66. print_line(itos(i + 1) + ": " + data[i]->name + " - " + itos(data[i]->debug_references));
  67. if (data[i]->debug_references == 0) {
  68. unreferenced_stringnames += 1;
  69. } else if (data[i]->debug_references < 5) {
  70. rarely_referenced_stringnames += 1;
  71. }
  72. }
  73. 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));
  74. 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));
  75. }
  76. #endif
  77. int lost_strings = 0;
  78. for (uint32_t i = 0; i < Table::TABLE_LEN; i++) {
  79. while (Table::table[i]) {
  80. _Data *d = Table::table[i];
  81. if (d->static_count.get() != d->refcount.get()) {
  82. lost_strings++;
  83. if (OS::get_singleton()->is_stdout_verbose()) {
  84. print_line(vformat("Orphan StringName: %s (static: %d, total: %d)", d->name, d->static_count.get(), d->refcount.get()));
  85. }
  86. }
  87. Table::table[i] = Table::table[i]->next;
  88. Table::allocator.free(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(Table::mutex);
  100. if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) {
  101. ERR_PRINT("BUG: Unreferenced static string to 0: " + _data->name);
  102. }
  103. if (_data->prev) {
  104. _data->prev->next = _data->next;
  105. } else {
  106. const uint32_t idx = _data->hash & Table::TABLE_MASK;
  107. Table::table[idx] = _data->next;
  108. }
  109. if (_data->next) {
  110. _data->next->prev = _data->prev;
  111. }
  112. Table::allocator.free(_data);
  113. }
  114. _data = nullptr;
  115. }
  116. uint32_t StringName::get_empty_hash() {
  117. static uint32_t empty_hash = String::hash("");
  118. return empty_hash;
  119. }
  120. bool StringName::operator==(const String &p_name) const {
  121. if (_data) {
  122. return _data->name == p_name;
  123. }
  124. return p_name.is_empty();
  125. }
  126. bool StringName::operator==(const char *p_name) const {
  127. if (_data) {
  128. return _data->name == p_name;
  129. }
  130. return p_name[0] == 0;
  131. }
  132. bool StringName::operator!=(const String &p_name) const {
  133. return !(operator==(p_name));
  134. }
  135. bool StringName::operator!=(const char *p_name) const {
  136. return !(operator==(p_name));
  137. }
  138. char32_t StringName::operator[](int p_index) const {
  139. if (_data) {
  140. return _data->name[p_index];
  141. }
  142. CRASH_BAD_INDEX(p_index, 0);
  143. return 0;
  144. }
  145. int StringName::length() const {
  146. if (_data) {
  147. return _data->name.length();
  148. }
  149. return 0;
  150. }
  151. StringName &StringName::operator=(const StringName &p_name) {
  152. if (this == &p_name) {
  153. return *this;
  154. }
  155. unref();
  156. if (p_name._data && p_name._data->refcount.ref()) {
  157. _data = p_name._data;
  158. }
  159. return *this;
  160. }
  161. StringName::StringName(const StringName &p_name) {
  162. _data = nullptr;
  163. ERR_FAIL_COND(!configured);
  164. if (p_name._data && p_name._data->refcount.ref()) {
  165. _data = p_name._data;
  166. }
  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. const uint32_t hash = String::hash(p_name);
  175. const uint32_t idx = hash & Table::TABLE_MASK;
  176. MutexLock lock(Table::mutex);
  177. _data = Table::table[idx];
  178. while (_data) {
  179. // compare hash first
  180. if (_data->hash == hash && _data->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 = Table::allocator.alloc();
  198. _data->name = p_name;
  199. _data->refcount.init();
  200. _data->static_count.set(p_static ? 1 : 0);
  201. _data->hash = hash;
  202. _data->next = Table::table[idx];
  203. _data->prev = nullptr;
  204. #ifdef DEBUG_ENABLED
  205. if (unlikely(debug_stringname)) {
  206. // Keep in memory, force static.
  207. _data->refcount.ref();
  208. _data->static_count.increment();
  209. }
  210. #endif
  211. if (Table::table[idx]) {
  212. Table::table[idx]->prev = _data;
  213. }
  214. Table::table[idx] = _data;
  215. }
  216. StringName::StringName(const String &p_name, bool p_static) {
  217. _data = nullptr;
  218. ERR_FAIL_COND(!configured);
  219. if (p_name.is_empty()) {
  220. return;
  221. }
  222. const uint32_t hash = p_name.hash();
  223. const uint32_t idx = hash & Table::TABLE_MASK;
  224. MutexLock lock(Table::mutex);
  225. _data = Table::table[idx];
  226. while (_data) {
  227. if (_data->hash == hash && _data->name == p_name) {
  228. break;
  229. }
  230. _data = _data->next;
  231. }
  232. if (_data && _data->refcount.ref()) {
  233. // exists
  234. if (p_static) {
  235. _data->static_count.increment();
  236. }
  237. #ifdef DEBUG_ENABLED
  238. if (unlikely(debug_stringname)) {
  239. _data->debug_references++;
  240. }
  241. #endif
  242. return;
  243. }
  244. _data = Table::allocator.alloc();
  245. _data->name = p_name;
  246. _data->refcount.init();
  247. _data->static_count.set(p_static ? 1 : 0);
  248. _data->hash = hash;
  249. _data->next = Table::table[idx];
  250. _data->prev = nullptr;
  251. #ifdef DEBUG_ENABLED
  252. if (unlikely(debug_stringname)) {
  253. // Keep in memory, force static.
  254. _data->refcount.ref();
  255. _data->static_count.increment();
  256. }
  257. #endif
  258. if (Table::table[idx]) {
  259. Table::table[idx]->prev = _data;
  260. }
  261. Table::table[idx] = _data;
  262. }
  263. StringName StringName::search(const char *p_name) {
  264. ERR_FAIL_COND_V(!configured, StringName());
  265. ERR_FAIL_NULL_V(p_name, StringName());
  266. if (!p_name[0]) {
  267. return StringName();
  268. }
  269. const uint32_t hash = String::hash(p_name);
  270. const uint32_t idx = hash & Table::TABLE_MASK;
  271. MutexLock lock(Table::mutex);
  272. _Data *_data = Table::table[idx];
  273. while (_data) {
  274. // compare hash first
  275. if (_data->hash == hash && _data->name == p_name) {
  276. break;
  277. }
  278. _data = _data->next;
  279. }
  280. if (_data && _data->refcount.ref()) {
  281. #ifdef DEBUG_ENABLED
  282. if (unlikely(debug_stringname)) {
  283. _data->debug_references++;
  284. }
  285. #endif
  286. return StringName(_data);
  287. }
  288. return StringName(); //does not exist
  289. }
  290. StringName StringName::search(const char32_t *p_name) {
  291. ERR_FAIL_COND_V(!configured, StringName());
  292. ERR_FAIL_NULL_V(p_name, StringName());
  293. if (!p_name[0]) {
  294. return StringName();
  295. }
  296. const uint32_t hash = String::hash(p_name);
  297. const uint32_t idx = hash & Table::TABLE_MASK;
  298. MutexLock lock(Table::mutex);
  299. _Data *_data = Table::table[idx];
  300. while (_data) {
  301. // compare hash first
  302. if (_data->hash == hash && _data->name == p_name) {
  303. break;
  304. }
  305. _data = _data->next;
  306. }
  307. if (_data && _data->refcount.ref()) {
  308. return StringName(_data);
  309. }
  310. return StringName(); //does not exist
  311. }
  312. StringName StringName::search(const String &p_name) {
  313. ERR_FAIL_COND_V(p_name.is_empty(), StringName());
  314. const uint32_t hash = p_name.hash();
  315. const uint32_t idx = hash & Table::TABLE_MASK;
  316. MutexLock lock(Table::mutex);
  317. _Data *_data = Table::table[idx];
  318. while (_data) {
  319. // compare hash first
  320. if (_data->hash == hash && _data->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. bool operator==(const String &p_name, const StringName &p_string_name) {
  336. return p_string_name.operator==(p_name);
  337. }
  338. bool operator!=(const String &p_name, const StringName &p_string_name) {
  339. return p_string_name.operator!=(p_name);
  340. }
  341. bool operator==(const char *p_name, const StringName &p_string_name) {
  342. return p_string_name.operator==(p_name);
  343. }
  344. bool operator!=(const char *p_name, const StringName &p_string_name) {
  345. return p_string_name.operator!=(p_name);
  346. }