string_name.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*************************************************************************/
  2. /* string_name.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/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 = NULL;
  44. void StringName::setup() {
  45. lock = Mutex::create();
  46. ERR_FAIL_COND(configured);
  47. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  48. _table[i] = NULL;
  49. }
  50. configured = true;
  51. }
  52. void StringName::cleanup() {
  53. lock->lock();
  54. int lost_strings = 0;
  55. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  56. while (_table[i]) {
  57. _Data *d = _table[i];
  58. lost_strings++;
  59. if (OS::get_singleton()->is_stdout_verbose()) {
  60. if (d->cname) {
  61. print_line("Orphan StringName: " + String(d->cname));
  62. } else {
  63. print_line("Orphan StringName: " + String(d->name));
  64. }
  65. }
  66. _table[i] = _table[i]->next;
  67. memdelete(d);
  68. }
  69. }
  70. if (lost_strings) {
  71. print_verbose("StringName: " + itos(lost_strings) + " unclaimed string names at exit.");
  72. }
  73. lock->unlock();
  74. memdelete(lock);
  75. }
  76. void StringName::unref() {
  77. ERR_FAIL_COND(!configured);
  78. if (_data && _data->refcount.unref()) {
  79. lock->lock();
  80. if (_data->prev) {
  81. _data->prev->next = _data->next;
  82. } else {
  83. if (_table[_data->idx] != _data) {
  84. ERR_PRINT("BUG!");
  85. }
  86. _table[_data->idx] = _data->next;
  87. }
  88. if (_data->next) {
  89. _data->next->prev = _data->prev;
  90. }
  91. memdelete(_data);
  92. lock->unlock();
  93. }
  94. _data = NULL;
  95. }
  96. bool StringName::operator==(const String &p_name) const {
  97. if (!_data) {
  98. return (p_name.length() == 0);
  99. }
  100. return (_data->get_name() == p_name);
  101. }
  102. bool StringName::operator==(const char *p_name) const {
  103. if (!_data) {
  104. return (p_name[0] == 0);
  105. }
  106. return (_data->get_name() == p_name);
  107. }
  108. bool StringName::operator!=(const String &p_name) const {
  109. return !(operator==(p_name));
  110. }
  111. bool StringName::operator!=(const StringName &p_name) const {
  112. // the real magic of all this mess happens here.
  113. // this is why path comparisons are very fast
  114. return _data != p_name._data;
  115. }
  116. void StringName::operator=(const StringName &p_name) {
  117. if (this == &p_name)
  118. return;
  119. unref();
  120. if (p_name._data && p_name._data->refcount.ref()) {
  121. _data = p_name._data;
  122. }
  123. }
  124. StringName::StringName(const StringName &p_name) {
  125. _data = NULL;
  126. ERR_FAIL_COND(!configured);
  127. if (p_name._data && p_name._data->refcount.ref()) {
  128. _data = p_name._data;
  129. }
  130. }
  131. StringName::StringName(const char *p_name) {
  132. _data = NULL;
  133. ERR_FAIL_COND(!configured);
  134. if (!p_name || p_name[0] == 0)
  135. return; //empty, ignore
  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. _data = _data->next;
  145. }
  146. if (_data) {
  147. if (_data->refcount.ref()) {
  148. // exists
  149. lock->unlock();
  150. return;
  151. }
  152. }
  153. _data = memnew(_Data);
  154. _data->name = p_name;
  155. _data->refcount.init();
  156. _data->hash = hash;
  157. _data->idx = idx;
  158. _data->cname = NULL;
  159. _data->next = _table[idx];
  160. _data->prev = NULL;
  161. if (_table[idx])
  162. _table[idx]->prev = _data;
  163. _table[idx] = _data;
  164. lock->unlock();
  165. }
  166. StringName::StringName(const StaticCString &p_static_string) {
  167. _data = NULL;
  168. ERR_FAIL_COND(!configured);
  169. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  170. lock->lock();
  171. uint32_t hash = String::hash(p_static_string.ptr);
  172. uint32_t idx = hash & STRING_TABLE_MASK;
  173. _data = _table[idx];
  174. while (_data) {
  175. // compare hash first
  176. if (_data->hash == hash && _data->get_name() == p_static_string.ptr)
  177. break;
  178. _data = _data->next;
  179. }
  180. if (_data) {
  181. if (_data->refcount.ref()) {
  182. // exists
  183. lock->unlock();
  184. return;
  185. }
  186. }
  187. _data = memnew(_Data);
  188. _data->refcount.init();
  189. _data->hash = hash;
  190. _data->idx = idx;
  191. _data->cname = p_static_string.ptr;
  192. _data->next = _table[idx];
  193. _data->prev = NULL;
  194. if (_table[idx])
  195. _table[idx]->prev = _data;
  196. _table[idx] = _data;
  197. lock->unlock();
  198. }
  199. StringName::StringName(const String &p_name) {
  200. _data = NULL;
  201. ERR_FAIL_COND(!configured);
  202. if (p_name == String())
  203. return;
  204. lock->lock();
  205. uint32_t hash = p_name.hash();
  206. uint32_t idx = hash & STRING_TABLE_MASK;
  207. _data = _table[idx];
  208. while (_data) {
  209. if (_data->hash == hash && _data->get_name() == p_name)
  210. break;
  211. _data = _data->next;
  212. }
  213. if (_data) {
  214. if (_data->refcount.ref()) {
  215. // exists
  216. lock->unlock();
  217. return;
  218. }
  219. }
  220. _data = memnew(_Data);
  221. _data->name = p_name;
  222. _data->refcount.init();
  223. _data->hash = hash;
  224. _data->idx = idx;
  225. _data->cname = NULL;
  226. _data->next = _table[idx];
  227. _data->prev = NULL;
  228. if (_table[idx])
  229. _table[idx]->prev = _data;
  230. _table[idx] = _data;
  231. lock->unlock();
  232. }
  233. StringName StringName::search(const char *p_name) {
  234. ERR_FAIL_COND_V(!configured, StringName());
  235. ERR_FAIL_COND_V(!p_name, StringName());
  236. if (!p_name[0])
  237. return StringName();
  238. lock->lock();
  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. _data = _data->next;
  247. }
  248. if (_data && _data->refcount.ref()) {
  249. lock->unlock();
  250. return StringName(_data);
  251. }
  252. lock->unlock();
  253. return StringName(); //does not exist
  254. }
  255. StringName StringName::search(const CharType *p_name) {
  256. ERR_FAIL_COND_V(!configured, StringName());
  257. ERR_FAIL_COND_V(!p_name, StringName());
  258. if (!p_name[0])
  259. return StringName();
  260. lock->lock();
  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. _data = _data->next;
  269. }
  270. if (_data && _data->refcount.ref()) {
  271. lock->unlock();
  272. return StringName(_data);
  273. }
  274. lock->unlock();
  275. return StringName(); //does not exist
  276. }
  277. StringName StringName::search(const String &p_name) {
  278. ERR_FAIL_COND_V(p_name == "", StringName());
  279. lock->lock();
  280. uint32_t hash = p_name.hash();
  281. uint32_t idx = hash & STRING_TABLE_MASK;
  282. _Data *_data = _table[idx];
  283. while (_data) {
  284. // compare hash first
  285. if (_data->hash == hash && p_name == _data->get_name())
  286. break;
  287. _data = _data->next;
  288. }
  289. if (_data && _data->refcount.ref()) {
  290. lock->unlock();
  291. return StringName(_data);
  292. }
  293. lock->unlock();
  294. return StringName(); //does not exist
  295. }
  296. StringName::StringName() {
  297. _data = NULL;
  298. }
  299. StringName::~StringName() {
  300. unref();
  301. }