string_db.cpp 8.6 KB

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