string_db.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*************************************************************************/
  2. /* string_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "string_db.h"
  30. #include "print_string.h"
  31. #include "os/os.h"
  32. StaticCString StaticCString::create(const char *p_ptr) {
  33. StaticCString scs; scs.ptr=p_ptr; return scs;
  34. }
  35. StringName::_Data *StringName::_table[STRING_TABLE_LEN];
  36. StringName _scs_create(const char *p_chr) {
  37. return (p_chr[0]?StringName(StaticCString::create(p_chr)):StringName());
  38. }
  39. bool StringName::configured=false;
  40. void StringName::setup() {
  41. ERR_FAIL_COND(configured);
  42. for(int i=0;i<STRING_TABLE_LEN;i++) {
  43. _table[i]=NULL;
  44. }
  45. configured=true;
  46. }
  47. void StringName::cleanup() {
  48. _global_lock();
  49. int lost_strings=0;
  50. for(int i=0;i<STRING_TABLE_LEN;i++) {
  51. while(_table[i]) {
  52. _Data*d=_table[i];
  53. lost_strings++;
  54. if (OS::get_singleton()->is_stdout_verbose()) {
  55. if (d->cname) {
  56. print_line("Orphan StringName: "+String(d->cname));
  57. } else {
  58. print_line("Orphan StringName: "+String(d->name));
  59. }
  60. }
  61. _table[i]=_table[i]->next;
  62. memdelete(d);
  63. }
  64. }
  65. if (OS::get_singleton()->is_stdout_verbose() && lost_strings) {
  66. print_line("StringName: "+itos(lost_strings)+" unclaimed string names at exit.");
  67. }
  68. _global_unlock();
  69. }
  70. void StringName::unref() {
  71. ERR_FAIL_COND(!configured);
  72. if (_data && _data->refcount.unref()) {
  73. _global_lock();
  74. if (_data->prev) {
  75. _data->prev->next=_data->next;
  76. } else {
  77. if (_table[_data->idx]!=_data) {
  78. ERR_PRINT("BUG!");
  79. }
  80. _table[_data->idx]=_data->next;
  81. }
  82. if (_data->next) {
  83. _data->next->prev=_data->prev;
  84. }
  85. memdelete(_data);
  86. _global_unlock();
  87. }
  88. _data=NULL;
  89. }
  90. bool StringName::operator==(const String& p_name) const {
  91. if (!_data) {
  92. return (p_name.length()==0);
  93. }
  94. return (_data->get_name()==p_name);
  95. }
  96. bool StringName::operator==(const char* p_name) const {
  97. if (!_data) {
  98. return (p_name[0]==0);
  99. }
  100. return (_data->get_name()==p_name);
  101. }
  102. bool StringName::operator!=(const String& p_name) const {
  103. return !(operator==(p_name));
  104. }
  105. bool StringName::operator!=(const StringName& p_name) const {
  106. // the real magic of all this mess happens here.
  107. // this is why path comparisons are very fast
  108. return _data!=p_name._data;
  109. }
  110. void StringName::operator=(const StringName& p_name) {
  111. if (this==&p_name)
  112. return;
  113. unref();
  114. if (p_name._data && p_name._data->refcount.ref()) {
  115. _data = p_name._data;
  116. }
  117. }
  118. /* was inlined
  119. StringName::operator String() const {
  120. if (_data)
  121. return _data->get_name();
  122. return "";
  123. }
  124. */
  125. StringName::StringName(const StringName& p_name) {
  126. ERR_FAIL_COND(!configured);
  127. _data=NULL;
  128. if (p_name._data && p_name._data->refcount.ref()) {
  129. _data = p_name._data;
  130. }
  131. }
  132. StringName::StringName(const char *p_name) {
  133. _data=NULL;
  134. ERR_FAIL_COND(!configured);
  135. ERR_FAIL_COND( !p_name || !p_name[0]);
  136. _global_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. _global_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. _global_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. _global_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. _global_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. _global_unlock();
  200. }
  201. StringName::StringName(const String& p_name) {
  202. _data=NULL;
  203. ERR_FAIL_COND(!configured);
  204. _global_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. _global_unlock();
  217. return;
  218. } else {
  219. }
  220. }
  221. _data = memnew( _Data );
  222. _data->name=p_name;
  223. _data->refcount.init();
  224. _data->hash=hash;
  225. _data->idx=idx;
  226. _data->cname=NULL;
  227. _data->next=_table[idx];
  228. _data->prev=NULL;
  229. if (_table[idx])
  230. _table[idx]->prev=_data;
  231. _table[idx]=_data;
  232. _global_unlock();
  233. }
  234. StringName StringName::search(const char *p_name) {
  235. ERR_FAIL_COND_V(!configured,StringName());
  236. ERR_FAIL_COND_V( !p_name, StringName() );
  237. if (!p_name[0])
  238. return StringName();
  239. _global_lock();
  240. uint32_t hash = String::hash(p_name);
  241. uint32_t idx=hash&STRING_TABLE_MASK;
  242. _Data *_data=_table[idx];
  243. while(_data) {
  244. // compare hash first
  245. if (_data->hash==hash && _data->get_name()==p_name)
  246. break;
  247. _data=_data->next;
  248. }
  249. if (_data && _data->refcount.ref()) {
  250. _global_unlock();
  251. return StringName(_data);
  252. }
  253. _global_unlock();
  254. return StringName(); //does not exist
  255. }
  256. StringName StringName::search(const CharType *p_name) {
  257. ERR_FAIL_COND_V(!configured,StringName());
  258. ERR_FAIL_COND_V( !p_name, StringName() );
  259. if (!p_name[0])
  260. return StringName();
  261. _global_lock();
  262. uint32_t hash = String::hash(p_name);
  263. uint32_t idx=hash&STRING_TABLE_MASK;
  264. _Data *_data=_table[idx];
  265. while(_data) {
  266. // compare hash first
  267. if (_data->hash==hash && _data->get_name()==p_name)
  268. break;
  269. _data=_data->next;
  270. }
  271. if (_data && _data->refcount.ref()) {
  272. _global_unlock();
  273. return StringName(_data);
  274. }
  275. _global_unlock();
  276. return StringName(); //does not exist
  277. }
  278. StringName StringName::search(const String &p_name) {
  279. ERR_FAIL_COND_V( p_name=="", StringName() );
  280. _global_lock();
  281. uint32_t hash = p_name.hash();
  282. uint32_t idx=hash&STRING_TABLE_MASK;
  283. _Data *_data=_table[idx];
  284. while(_data) {
  285. // compare hash first
  286. if (_data->hash==hash && p_name==_data->get_name())
  287. break;
  288. _data=_data->next;
  289. }
  290. if (_data && _data->refcount.ref()) {
  291. _global_unlock();
  292. return StringName(_data);
  293. }
  294. _global_unlock();
  295. return StringName(); //does not exist
  296. }
  297. StringName::StringName() {
  298. _data=NULL;
  299. }
  300. StringName::~StringName() {
  301. unref();
  302. }