string_db.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*************************************************************************/
  2. /* string_db.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_db.h"
  31. #include "os/os.h"
  32. #include "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. void StringName::setup() {
  44. ERR_FAIL_COND(configured);
  45. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  46. _table[i] = NULL;
  47. }
  48. configured = true;
  49. }
  50. void StringName::cleanup() {
  51. _global_lock();
  52. int lost_strings = 0;
  53. for (int i = 0; i < STRING_TABLE_LEN; i++) {
  54. while (_table[i]) {
  55. _Data *d = _table[i];
  56. lost_strings++;
  57. if (OS::get_singleton()->is_stdout_verbose()) {
  58. if (d->cname) {
  59. print_line("Orphan StringName: " + String(d->cname));
  60. } else {
  61. print_line("Orphan StringName: " + String(d->name));
  62. }
  63. }
  64. _table[i] = _table[i]->next;
  65. memdelete(d);
  66. }
  67. }
  68. if (OS::get_singleton()->is_stdout_verbose() && lost_strings) {
  69. print_line("StringName: " + itos(lost_strings) + " unclaimed string names at exit.");
  70. }
  71. _global_unlock();
  72. }
  73. void StringName::unref() {
  74. ERR_FAIL_COND(!configured);
  75. if (_data && _data->refcount.unref()) {
  76. _global_lock();
  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. _global_unlock();
  90. }
  91. _data = NULL;
  92. }
  93. bool StringName::operator==(const String &p_name) const {
  94. if (!_data) {
  95. return (p_name.length() == 0);
  96. }
  97. return (_data->get_name() == p_name);
  98. }
  99. bool StringName::operator==(const char *p_name) const {
  100. if (!_data) {
  101. return (p_name[0] == 0);
  102. }
  103. return (_data->get_name() == p_name);
  104. }
  105. bool StringName::operator!=(const String &p_name) const {
  106. return !(operator==(p_name));
  107. }
  108. bool StringName::operator!=(const StringName &p_name) const {
  109. // the real magic of all this mess happens here.
  110. // this is why path comparisons are very fast
  111. return _data != p_name._data;
  112. }
  113. void StringName::operator=(const StringName &p_name) {
  114. if (this == &p_name)
  115. return;
  116. unref();
  117. if (p_name._data && p_name._data->refcount.ref()) {
  118. _data = p_name._data;
  119. }
  120. }
  121. /* was inlined
  122. StringName::operator String() const {
  123. if (_data)
  124. return _data->get_name();
  125. return "";
  126. }
  127. */
  128. StringName::StringName(const StringName &p_name) {
  129. ERR_FAIL_COND(!configured);
  130. _data = NULL;
  131. if (p_name._data && p_name._data->refcount.ref()) {
  132. _data = p_name._data;
  133. }
  134. }
  135. StringName::StringName(const char *p_name) {
  136. _data = NULL;
  137. ERR_FAIL_COND(!configured);
  138. ERR_FAIL_COND(!p_name || !p_name[0]);
  139. _global_lock();
  140. uint32_t hash = String::hash(p_name);
  141. uint32_t idx = hash & STRING_TABLE_MASK;
  142. _data = _table[idx];
  143. while (_data) {
  144. // compare hash first
  145. if (_data->hash == hash && _data->get_name() == p_name)
  146. break;
  147. _data = _data->next;
  148. }
  149. if (_data) {
  150. if (_data->refcount.ref()) {
  151. // exists
  152. _global_unlock();
  153. return;
  154. } else {
  155. }
  156. }
  157. _data = memnew(_Data);
  158. _data->name = p_name;
  159. _data->refcount.init();
  160. _data->hash = hash;
  161. _data->idx = idx;
  162. _data->cname = NULL;
  163. _data->next = _table[idx];
  164. _data->prev = NULL;
  165. if (_table[idx])
  166. _table[idx]->prev = _data;
  167. _table[idx] = _data;
  168. _global_unlock();
  169. }
  170. StringName::StringName(const StaticCString &p_static_string) {
  171. _data = NULL;
  172. ERR_FAIL_COND(!configured);
  173. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  174. _global_lock();
  175. uint32_t hash = String::hash(p_static_string.ptr);
  176. uint32_t idx = hash & STRING_TABLE_MASK;
  177. _data = _table[idx];
  178. while (_data) {
  179. // compare hash first
  180. if (_data->hash == hash && _data->get_name() == p_static_string.ptr)
  181. break;
  182. _data = _data->next;
  183. }
  184. if (_data) {
  185. if (_data->refcount.ref()) {
  186. // exists
  187. _global_unlock();
  188. return;
  189. } else {
  190. }
  191. }
  192. _data = memnew(_Data);
  193. _data->refcount.init();
  194. _data->hash = hash;
  195. _data->idx = idx;
  196. _data->cname = p_static_string.ptr;
  197. _data->next = _table[idx];
  198. _data->prev = NULL;
  199. if (_table[idx])
  200. _table[idx]->prev = _data;
  201. _table[idx] = _data;
  202. _global_unlock();
  203. }
  204. StringName::StringName(const String &p_name) {
  205. _data = NULL;
  206. ERR_FAIL_COND(!configured);
  207. if (p_name.empty())
  208. return;
  209. _global_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. _data = _data->next;
  217. }
  218. if (_data) {
  219. if (_data->refcount.ref()) {
  220. // exists
  221. _global_unlock();
  222. return;
  223. } else {
  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 = NULL;
  232. _data->next = _table[idx];
  233. _data->prev = NULL;
  234. if (_table[idx])
  235. _table[idx]->prev = _data;
  236. _table[idx] = _data;
  237. _global_unlock();
  238. }
  239. StringName StringName::search(const char *p_name) {
  240. ERR_FAIL_COND_V(!configured, StringName());
  241. ERR_FAIL_COND_V(!p_name, StringName());
  242. if (!p_name[0])
  243. return StringName();
  244. _global_lock();
  245. uint32_t hash = String::hash(p_name);
  246. uint32_t idx = hash & STRING_TABLE_MASK;
  247. _Data *_data = _table[idx];
  248. while (_data) {
  249. // compare hash first
  250. if (_data->hash == hash && _data->get_name() == p_name)
  251. break;
  252. _data = _data->next;
  253. }
  254. if (_data && _data->refcount.ref()) {
  255. _global_unlock();
  256. return StringName(_data);
  257. }
  258. _global_unlock();
  259. return StringName(); //does not exist
  260. }
  261. StringName StringName::search(const CharType *p_name) {
  262. ERR_FAIL_COND_V(!configured, StringName());
  263. ERR_FAIL_COND_V(!p_name, StringName());
  264. if (!p_name[0])
  265. return StringName();
  266. _global_lock();
  267. uint32_t hash = String::hash(p_name);
  268. uint32_t idx = hash & STRING_TABLE_MASK;
  269. _Data *_data = _table[idx];
  270. while (_data) {
  271. // compare hash first
  272. if (_data->hash == hash && _data->get_name() == p_name)
  273. break;
  274. _data = _data->next;
  275. }
  276. if (_data && _data->refcount.ref()) {
  277. _global_unlock();
  278. return StringName(_data);
  279. }
  280. _global_unlock();
  281. return StringName(); //does not exist
  282. }
  283. StringName StringName::search(const String &p_name) {
  284. ERR_FAIL_COND_V(p_name == "", StringName());
  285. _global_lock();
  286. uint32_t hash = p_name.hash();
  287. uint32_t idx = hash & STRING_TABLE_MASK;
  288. _Data *_data = _table[idx];
  289. while (_data) {
  290. // compare hash first
  291. if (_data->hash == hash && p_name == _data->get_name())
  292. break;
  293. _data = _data->next;
  294. }
  295. if (_data && _data->refcount.ref()) {
  296. _global_unlock();
  297. return StringName(_data);
  298. }
  299. _global_unlock();
  300. return StringName(); //does not exist
  301. }
  302. StringName::StringName() {
  303. _data = NULL;
  304. }
  305. StringName::~StringName() {
  306. unref();
  307. }