string_db.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*************************************************************************/
  2. /* string_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. 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 (OS::get_singleton()->is_stdout_verbose() && lost_strings) {
  71. print_line("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. /* was inlined
  125. StringName::operator String() const {
  126. if (_data)
  127. return _data->get_name();
  128. return "";
  129. }
  130. */
  131. StringName::StringName(const StringName &p_name) {
  132. ERR_FAIL_COND(!configured);
  133. _data = NULL;
  134. if (p_name._data && p_name._data->refcount.ref()) {
  135. _data = p_name._data;
  136. }
  137. }
  138. StringName::StringName(const char *p_name) {
  139. _data = NULL;
  140. ERR_FAIL_COND(!configured);
  141. if (!p_name || p_name[0] == 0)
  142. return; //empty, ignore
  143. lock->lock();
  144. uint32_t hash = String::hash(p_name);
  145. uint32_t idx = hash & STRING_TABLE_MASK;
  146. _data = _table[idx];
  147. while (_data) {
  148. // compare hash first
  149. if (_data->hash == hash && _data->get_name() == p_name)
  150. break;
  151. _data = _data->next;
  152. }
  153. if (_data) {
  154. if (_data->refcount.ref()) {
  155. // exists
  156. lock->unlock();
  157. return;
  158. } else {
  159. }
  160. }
  161. _data = memnew(_Data);
  162. _data->name = p_name;
  163. _data->refcount.init();
  164. _data->hash = hash;
  165. _data->idx = idx;
  166. _data->cname = NULL;
  167. _data->next = _table[idx];
  168. _data->prev = NULL;
  169. if (_table[idx])
  170. _table[idx]->prev = _data;
  171. _table[idx] = _data;
  172. lock->unlock();
  173. }
  174. StringName::StringName(const StaticCString &p_static_string) {
  175. _data = NULL;
  176. ERR_FAIL_COND(!configured);
  177. ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
  178. lock->lock();
  179. uint32_t hash = String::hash(p_static_string.ptr);
  180. uint32_t idx = hash & STRING_TABLE_MASK;
  181. _data = _table[idx];
  182. while (_data) {
  183. // compare hash first
  184. if (_data->hash == hash && _data->get_name() == p_static_string.ptr)
  185. break;
  186. _data = _data->next;
  187. }
  188. if (_data) {
  189. if (_data->refcount.ref()) {
  190. // exists
  191. lock->unlock();
  192. return;
  193. } else {
  194. }
  195. }
  196. _data = memnew(_Data);
  197. _data->refcount.init();
  198. _data->hash = hash;
  199. _data->idx = idx;
  200. _data->cname = p_static_string.ptr;
  201. _data->next = _table[idx];
  202. _data->prev = NULL;
  203. if (_table[idx])
  204. _table[idx]->prev = _data;
  205. _table[idx] = _data;
  206. lock->unlock();
  207. }
  208. StringName::StringName(const String &p_name) {
  209. _data = NULL;
  210. ERR_FAIL_COND(!configured);
  211. if (p_name == String())
  212. return;
  213. lock->lock();
  214. uint32_t hash = p_name.hash();
  215. uint32_t idx = hash & STRING_TABLE_MASK;
  216. _data = _table[idx];
  217. while (_data) {
  218. if (_data->hash == hash && _data->get_name() == p_name)
  219. break;
  220. _data = _data->next;
  221. }
  222. if (_data) {
  223. if (_data->refcount.ref()) {
  224. // exists
  225. lock->unlock();
  226. return;
  227. } else {
  228. }
  229. }
  230. _data = memnew(_Data);
  231. _data->name = p_name;
  232. _data->refcount.init();
  233. _data->hash = hash;
  234. _data->idx = idx;
  235. _data->cname = NULL;
  236. _data->next = _table[idx];
  237. _data->prev = NULL;
  238. if (_table[idx])
  239. _table[idx]->prev = _data;
  240. _table[idx] = _data;
  241. lock->unlock();
  242. }
  243. StringName StringName::search(const char *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. lock->lock();
  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. lock->unlock();
  260. return StringName(_data);
  261. }
  262. lock->unlock();
  263. return StringName(); //does not exist
  264. }
  265. StringName StringName::search(const CharType *p_name) {
  266. ERR_FAIL_COND_V(!configured, StringName());
  267. ERR_FAIL_COND_V(!p_name, StringName());
  268. if (!p_name[0])
  269. return StringName();
  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. _data = _data->next;
  279. }
  280. if (_data && _data->refcount.ref()) {
  281. lock->unlock();
  282. return StringName(_data);
  283. }
  284. lock->unlock();
  285. return StringName(); //does not exist
  286. }
  287. StringName StringName::search(const String &p_name) {
  288. ERR_FAIL_COND_V(p_name == "", StringName());
  289. lock->lock();
  290. uint32_t hash = p_name.hash();
  291. uint32_t idx = hash & STRING_TABLE_MASK;
  292. _Data *_data = _table[idx];
  293. while (_data) {
  294. // compare hash first
  295. if (_data->hash == hash && p_name == _data->get_name())
  296. break;
  297. _data = _data->next;
  298. }
  299. if (_data && _data->refcount.ref()) {
  300. lock->unlock();
  301. return StringName(_data);
  302. }
  303. lock->unlock();
  304. return StringName(); //does not exist
  305. }
  306. StringName::StringName() {
  307. _data = NULL;
  308. }
  309. StringName::~StringName() {
  310. unref();
  311. }