string_name.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. void StringName::setup() {
  45. ERR_FAIL_COND(configured);
  46. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  47. _table[i] = nullptr;
  48. }
  49. configured = true;
  50. }
  51. void StringName::cleanup() {
  52. MutexLock lock(mutex);
  53. int lost_strings = 0;
  54. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  55. while (_table[i]) {
  56. _Data *d = _table[i];
  57. lost_strings++;
  58. if (d->static_count.get() != d->refcount.get() && OS::get_singleton()->is_stdout_verbose()) {
  59. if (d->cname) {
  60. print_line("Orphan StringName: " + String(d->cname));
  61. } else {
  62. print_line("Orphan StringName: " + String(d->name));
  63. }
  64. }
  65. _table[i] = _table[i]->next;
  66. memdelete(d);
  67. }
  68. }
  69. if (lost_strings) {
  70. print_verbose("StringName: " + itos(lost_strings) + " unclaimed string names at exit.");
  71. }
  72. configured = false;
  73. }
  74. void StringName::unref() {
  75. ERR_FAIL_COND(!configured);
  76. if (_data && _data->refcount.unref()) {
  77. MutexLock lock(mutex);
  78. if (_data->static_count.get() > 0) {
  79. if (_data->cname) {
  80. ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->cname));
  81. } else {
  82. ERR_PRINT("BUG: Unreferenced static string to 0: " + String(_data->name));
  83. }
  84. }
  85. if (_data->prev) {
  86. _data->prev->next = _data->next;
  87. } else {
  88. if (_table[_data->idx] != _data) {
  89. ERR_PRINT("BUG!");
  90. }
  91. _table[_data->idx] = _data->next;
  92. }
  93. if (_data->next) {
  94. _data->next->prev = _data->prev;
  95. }
  96. memdelete(_data);
  97. }
  98. _data = nullptr;
  99. }
  100. bool StringName::operator==(const String &p_name) const {
  101. if (!_data) {
  102. return (p_name.length() == 0);
  103. }
  104. return (_data->get_name() == p_name);
  105. }
  106. bool StringName::operator==(const char *p_name) const {
  107. if (!_data) {
  108. return (p_name[0] == 0);
  109. }
  110. return (_data->get_name() == p_name);
  111. }
  112. bool StringName::operator!=(const String &p_name) const {
  113. return !(operator==(p_name));
  114. }
  115. bool StringName::operator!=(const StringName &p_name) const {
  116. // the real magic of all this mess happens here.
  117. // this is why path comparisons are very fast
  118. return _data != p_name._data;
  119. }
  120. void StringName::operator=(const StringName &p_name) {
  121. if (this == &p_name) {
  122. return;
  123. }
  124. unref();
  125. if (p_name._data && p_name._data->refcount.ref()) {
  126. _data = p_name._data;
  127. }
  128. }
  129. StringName::StringName(const StringName &p_name) {
  130. _data = nullptr;
  131. ERR_FAIL_COND(!configured);
  132. if (p_name._data && p_name._data->refcount.ref()) {
  133. _data = p_name._data;
  134. }
  135. }
  136. StringName::StringName(const char *p_name, bool p_static) {
  137. _data = nullptr;
  138. ERR_FAIL_COND(!configured);
  139. if (!p_name || p_name[0] == 0) {
  140. return; //empty, ignore
  141. }
  142. MutexLock lock(mutex);
  143. uint32_t hash = String::hash(p_name);
  144. uint32_t idx = hash & STRING_TABLE_MASK;
  145. _data = _table[idx];
  146. while (_data) {
  147. // compare hash first
  148. if (_data->hash == hash && _data->get_name() == p_name) {
  149. break;
  150. }
  151. _data = _data->next;
  152. }
  153. if (_data) {
  154. if (_data->refcount.ref()) {
  155. // exists
  156. if (p_static) {
  157. _data->static_count.increment();
  158. }
  159. return;
  160. }
  161. }
  162. _data = memnew(_Data);
  163. _data->name = p_name;
  164. _data->refcount.init();
  165. _data->static_count.set(p_static ? 1 : 0);
  166. _data->hash = hash;
  167. _data->idx = idx;
  168. _data->cname = nullptr;
  169. _data->next = _table[idx];
  170. _data->prev = nullptr;
  171. if (_table[idx]) {
  172. _table[idx]->prev = _data;
  173. }
  174. _table[idx] = _data;
  175. }
  176. StringName::StringName(const StaticCString &p_static_string, bool p_static) {
  177. _data = nullptr;
  178. ERR_FAIL_COND(!configured);
  179. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  180. MutexLock lock(mutex);
  181. uint32_t hash = String::hash(p_static_string.ptr);
  182. uint32_t idx = hash & STRING_TABLE_MASK;
  183. _data = _table[idx];
  184. while (_data) {
  185. // compare hash first
  186. if (_data->hash == hash && _data->get_name() == p_static_string.ptr) {
  187. break;
  188. }
  189. _data = _data->next;
  190. }
  191. if (_data) {
  192. if (_data->refcount.ref()) {
  193. // exists
  194. if (p_static) {
  195. _data->static_count.increment();
  196. }
  197. return;
  198. }
  199. }
  200. _data = memnew(_Data);
  201. _data->refcount.init();
  202. _data->static_count.set(p_static ? 1 : 0);
  203. _data->hash = hash;
  204. _data->idx = idx;
  205. _data->cname = p_static_string.ptr;
  206. _data->next = _table[idx];
  207. _data->prev = nullptr;
  208. if (_table[idx]) {
  209. _table[idx]->prev = _data;
  210. }
  211. _table[idx] = _data;
  212. }
  213. StringName::StringName(const String &p_name, bool p_static) {
  214. _data = nullptr;
  215. ERR_FAIL_COND(!configured);
  216. if (p_name == String()) {
  217. return;
  218. }
  219. MutexLock lock(mutex);
  220. uint32_t hash = p_name.hash();
  221. uint32_t idx = hash & STRING_TABLE_MASK;
  222. _data = _table[idx];
  223. while (_data) {
  224. if (_data->hash == hash && _data->get_name() == p_name) {
  225. break;
  226. }
  227. _data = _data->next;
  228. }
  229. if (_data) {
  230. if (_data->refcount.ref()) {
  231. // exists
  232. if (p_static) {
  233. _data->static_count.increment();
  234. }
  235. return;
  236. }
  237. }
  238. _data = memnew(_Data);
  239. _data->name = p_name;
  240. _data->refcount.init();
  241. _data->static_count.set(p_static ? 1 : 0);
  242. _data->hash = hash;
  243. _data->idx = idx;
  244. _data->cname = nullptr;
  245. _data->next = _table[idx];
  246. _data->prev = nullptr;
  247. if (_table[idx]) {
  248. _table[idx]->prev = _data;
  249. }
  250. _table[idx] = _data;
  251. }
  252. StringName StringName::search(const char *p_name) {
  253. ERR_FAIL_COND_V(!configured, StringName());
  254. ERR_FAIL_COND_V(!p_name, StringName());
  255. if (!p_name[0]) {
  256. return StringName();
  257. }
  258. MutexLock lock(mutex);
  259. uint32_t hash = String::hash(p_name);
  260. uint32_t idx = hash & STRING_TABLE_MASK;
  261. _Data *_data = _table[idx];
  262. while (_data) {
  263. // compare hash first
  264. if (_data->hash == hash && _data->get_name() == p_name) {
  265. break;
  266. }
  267. _data = _data->next;
  268. }
  269. if (_data && _data->refcount.ref()) {
  270. return StringName(_data);
  271. }
  272. return StringName(); //does not exist
  273. }
  274. StringName StringName::search(const char32_t *p_name) {
  275. ERR_FAIL_COND_V(!configured, StringName());
  276. ERR_FAIL_COND_V(!p_name, StringName());
  277. if (!p_name[0]) {
  278. return StringName();
  279. }
  280. MutexLock lock(mutex);
  281. uint32_t hash = String::hash(p_name);
  282. uint32_t idx = hash & STRING_TABLE_MASK;
  283. _Data *_data = _table[idx];
  284. while (_data) {
  285. // compare hash first
  286. if (_data->hash == hash && _data->get_name() == p_name) {
  287. break;
  288. }
  289. _data = _data->next;
  290. }
  291. if (_data && _data->refcount.ref()) {
  292. return StringName(_data);
  293. }
  294. return StringName(); //does not exist
  295. }
  296. StringName StringName::search(const String &p_name) {
  297. ERR_FAIL_COND_V(p_name == "", StringName());
  298. MutexLock lock(mutex);
  299. uint32_t hash = p_name.hash();
  300. uint32_t idx = hash & STRING_TABLE_MASK;
  301. _Data *_data = _table[idx];
  302. while (_data) {
  303. // compare hash first
  304. if (_data->hash == hash && p_name == _data->get_name()) {
  305. break;
  306. }
  307. _data = _data->next;
  308. }
  309. if (_data && _data->refcount.ref()) {
  310. return StringName(_data);
  311. }
  312. return StringName(); //does not exist
  313. }
  314. bool operator==(const String &p_name, const StringName &p_string_name) {
  315. return p_name == p_string_name.operator String();
  316. }
  317. bool operator!=(const String &p_name, const StringName &p_string_name) {
  318. return p_name != p_string_name.operator String();
  319. }
  320. bool operator==(const char *p_name, const StringName &p_string_name) {
  321. return p_name == p_string_name.operator String();
  322. }
  323. bool operator!=(const char *p_name, const StringName &p_string_name) {
  324. return p_name != p_string_name.operator String();
  325. }