string_name.cpp 8.7 KB

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