string_db.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*************************************************************************/
  2. /* string_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "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. } else {
  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 = NULL;
  160. _data->next = _table[idx];
  161. _data->prev = NULL;
  162. if (_table[idx])
  163. _table[idx]->prev = _data;
  164. _table[idx] = _data;
  165. lock->unlock();
  166. }
  167. StringName::StringName(const StaticCString &p_static_string) {
  168. _data = NULL;
  169. ERR_FAIL_COND(!configured);
  170. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  171. lock->lock();
  172. uint32_t hash = String::hash(p_static_string.ptr);
  173. uint32_t idx = hash & STRING_TABLE_MASK;
  174. _data = _table[idx];
  175. while (_data) {
  176. // compare hash first
  177. if (_data->hash == hash && _data->get_name() == p_static_string.ptr)
  178. break;
  179. _data = _data->next;
  180. }
  181. if (_data) {
  182. if (_data->refcount.ref()) {
  183. // exists
  184. lock->unlock();
  185. return;
  186. } else {
  187. }
  188. }
  189. _data = memnew(_Data);
  190. _data->refcount.init();
  191. _data->hash = hash;
  192. _data->idx = idx;
  193. _data->cname = p_static_string.ptr;
  194. _data->next = _table[idx];
  195. _data->prev = NULL;
  196. if (_table[idx])
  197. _table[idx]->prev = _data;
  198. _table[idx] = _data;
  199. lock->unlock();
  200. }
  201. StringName::StringName(const String &p_name) {
  202. _data = NULL;
  203. ERR_FAIL_COND(!configured);
  204. if (p_name == String())
  205. return;
  206. lock->lock();
  207. uint32_t hash = p_name.hash();
  208. uint32_t idx = hash & STRING_TABLE_MASK;
  209. _data = _table[idx];
  210. while (_data) {
  211. if (_data->hash == hash && _data->get_name() == p_name)
  212. break;
  213. _data = _data->next;
  214. }
  215. if (_data) {
  216. if (_data->refcount.ref()) {
  217. // exists
  218. lock->unlock();
  219. return;
  220. } else {
  221. }
  222. }
  223. _data = memnew(_Data);
  224. _data->name = p_name;
  225. _data->refcount.init();
  226. _data->hash = hash;
  227. _data->idx = idx;
  228. _data->cname = NULL;
  229. _data->next = _table[idx];
  230. _data->prev = NULL;
  231. if (_table[idx])
  232. _table[idx]->prev = _data;
  233. _table[idx] = _data;
  234. lock->unlock();
  235. }
  236. StringName StringName::search(const char *p_name) {
  237. ERR_FAIL_COND_V(!configured, StringName());
  238. ERR_FAIL_COND_V(!p_name, StringName());
  239. if (!p_name[0])
  240. return StringName();
  241. lock->lock();
  242. uint32_t hash = String::hash(p_name);
  243. uint32_t idx = hash & STRING_TABLE_MASK;
  244. _Data *_data = _table[idx];
  245. while (_data) {
  246. // compare hash first
  247. if (_data->hash == hash && _data->get_name() == p_name)
  248. break;
  249. _data = _data->next;
  250. }
  251. if (_data && _data->refcount.ref()) {
  252. lock->unlock();
  253. return StringName(_data);
  254. }
  255. lock->unlock();
  256. return StringName(); //does not exist
  257. }
  258. StringName StringName::search(const CharType *p_name) {
  259. ERR_FAIL_COND_V(!configured, StringName());
  260. ERR_FAIL_COND_V(!p_name, StringName());
  261. if (!p_name[0])
  262. return StringName();
  263. lock->lock();
  264. uint32_t hash = String::hash(p_name);
  265. uint32_t idx = hash & STRING_TABLE_MASK;
  266. _Data *_data = _table[idx];
  267. while (_data) {
  268. // compare hash first
  269. if (_data->hash == hash && _data->get_name() == p_name)
  270. break;
  271. _data = _data->next;
  272. }
  273. if (_data && _data->refcount.ref()) {
  274. lock->unlock();
  275. return StringName(_data);
  276. }
  277. lock->unlock();
  278. return StringName(); //does not exist
  279. }
  280. StringName StringName::search(const String &p_name) {
  281. ERR_FAIL_COND_V(p_name == "", StringName());
  282. lock->lock();
  283. uint32_t hash = p_name.hash();
  284. uint32_t idx = hash & STRING_TABLE_MASK;
  285. _Data *_data = _table[idx];
  286. while (_data) {
  287. // compare hash first
  288. if (_data->hash == hash && p_name == _data->get_name())
  289. break;
  290. _data = _data->next;
  291. }
  292. if (_data && _data->refcount.ref()) {
  293. lock->unlock();
  294. return StringName(_data);
  295. }
  296. lock->unlock();
  297. return StringName(); //does not exist
  298. }
  299. StringName::StringName() {
  300. _data = NULL;
  301. }
  302. StringName::~StringName() {
  303. unref();
  304. }