string_name.cpp 9.0 KB

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