string_name.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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::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. unref();
  116. if (p_name._data && p_name._data->refcount.ref()) {
  117. _data = p_name._data;
  118. }
  119. }
  120. StringName::StringName(const StringName &p_name) {
  121. _data = nullptr;
  122. ERR_FAIL_COND(!configured);
  123. if (p_name._data && p_name._data->refcount.ref()) {
  124. _data = p_name._data;
  125. }
  126. }
  127. StringName::StringName(const char *p_name) {
  128. _data = nullptr;
  129. ERR_FAIL_COND(!configured);
  130. if (!p_name || p_name[0] == 0)
  131. return; //empty, ignore
  132. MutexLock lock(mutex);
  133. uint32_t hash = String::hash(p_name);
  134. uint32_t idx = hash & STRING_TABLE_MASK;
  135. _data = _table[idx];
  136. while (_data) {
  137. // compare hash first
  138. if (_data->hash == hash && _data->get_name() == p_name)
  139. break;
  140. _data = _data->next;
  141. }
  142. if (_data) {
  143. if (_data->refcount.ref()) {
  144. // exists
  145. return;
  146. }
  147. }
  148. _data = memnew(_Data);
  149. _data->name = p_name;
  150. _data->refcount.init();
  151. _data->hash = hash;
  152. _data->idx = idx;
  153. _data->cname = nullptr;
  154. _data->next = _table[idx];
  155. _data->prev = nullptr;
  156. if (_table[idx])
  157. _table[idx]->prev = _data;
  158. _table[idx] = _data;
  159. }
  160. StringName::StringName(const StaticCString &p_static_string) {
  161. _data = nullptr;
  162. ERR_FAIL_COND(!configured);
  163. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  164. MutexLock lock(mutex);
  165. uint32_t hash = String::hash(p_static_string.ptr);
  166. uint32_t idx = hash & STRING_TABLE_MASK;
  167. _data = _table[idx];
  168. while (_data) {
  169. // compare hash first
  170. if (_data->hash == hash && _data->get_name() == p_static_string.ptr)
  171. break;
  172. _data = _data->next;
  173. }
  174. if (_data) {
  175. if (_data->refcount.ref()) {
  176. // exists
  177. return;
  178. }
  179. }
  180. _data = memnew(_Data);
  181. _data->refcount.init();
  182. _data->hash = hash;
  183. _data->idx = idx;
  184. _data->cname = p_static_string.ptr;
  185. _data->next = _table[idx];
  186. _data->prev = nullptr;
  187. if (_table[idx])
  188. _table[idx]->prev = _data;
  189. _table[idx] = _data;
  190. }
  191. StringName::StringName(const String &p_name) {
  192. _data = nullptr;
  193. ERR_FAIL_COND(!configured);
  194. if (p_name == String())
  195. return;
  196. MutexLock lock(mutex);
  197. uint32_t hash = p_name.hash();
  198. uint32_t idx = hash & STRING_TABLE_MASK;
  199. _data = _table[idx];
  200. while (_data) {
  201. if (_data->hash == hash && _data->get_name() == p_name)
  202. break;
  203. _data = _data->next;
  204. }
  205. if (_data) {
  206. if (_data->refcount.ref()) {
  207. // exists
  208. return;
  209. }
  210. }
  211. _data = memnew(_Data);
  212. _data->name = p_name;
  213. _data->refcount.init();
  214. _data->hash = hash;
  215. _data->idx = idx;
  216. _data->cname = nullptr;
  217. _data->next = _table[idx];
  218. _data->prev = nullptr;
  219. if (_table[idx])
  220. _table[idx]->prev = _data;
  221. _table[idx] = _data;
  222. }
  223. StringName StringName::search(const char *p_name) {
  224. ERR_FAIL_COND_V(!configured, StringName());
  225. ERR_FAIL_COND_V(!p_name, StringName());
  226. if (!p_name[0])
  227. return StringName();
  228. MutexLock lock(mutex);
  229. uint32_t hash = String::hash(p_name);
  230. uint32_t idx = hash & STRING_TABLE_MASK;
  231. _Data *_data = _table[idx];
  232. while (_data) {
  233. // compare hash first
  234. if (_data->hash == hash && _data->get_name() == p_name)
  235. break;
  236. _data = _data->next;
  237. }
  238. if (_data && _data->refcount.ref()) {
  239. return StringName(_data);
  240. }
  241. return StringName(); //does not exist
  242. }
  243. StringName StringName::search(const CharType *p_name) {
  244. ERR_FAIL_COND_V(!configured, StringName());
  245. ERR_FAIL_COND_V(!p_name, StringName());
  246. if (!p_name[0])
  247. return StringName();
  248. MutexLock lock(mutex);
  249. uint32_t hash = String::hash(p_name);
  250. uint32_t idx = hash & STRING_TABLE_MASK;
  251. _Data *_data = _table[idx];
  252. while (_data) {
  253. // compare hash first
  254. if (_data->hash == hash && _data->get_name() == p_name)
  255. break;
  256. _data = _data->next;
  257. }
  258. if (_data && _data->refcount.ref()) {
  259. return StringName(_data);
  260. }
  261. return StringName(); //does not exist
  262. }
  263. StringName StringName::search(const String &p_name) {
  264. ERR_FAIL_COND_V(p_name == "", StringName());
  265. MutexLock lock(mutex);
  266. uint32_t hash = p_name.hash();
  267. uint32_t idx = hash & STRING_TABLE_MASK;
  268. _Data *_data = _table[idx];
  269. while (_data) {
  270. // compare hash first
  271. if (_data->hash == hash && p_name == _data->get_name())
  272. break;
  273. _data = _data->next;
  274. }
  275. if (_data && _data->refcount.ref()) {
  276. return StringName(_data);
  277. }
  278. return StringName(); //does not exist
  279. }
  280. StringName::~StringName() {
  281. unref();
  282. }