netStringTable.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/dnet.h"
  23. #include "core/strings/stringFunctions.h"
  24. #include "core/stringTable.h"
  25. #include "console/engineAPI.h"
  26. #include "sim/netStringTable.h"
  27. NetStringTable *gNetStringTable = NULL;
  28. NetStringTable::NetStringTable()
  29. {
  30. firstFree = 1;
  31. firstValid = 1;
  32. table = (Entry *) dMalloc(sizeof(Entry) * InitialSize);
  33. size = InitialSize;
  34. for(U32 i = 0; i < InitialSize; i++)
  35. {
  36. table[i].next = i + 1;
  37. table[i].refCount = 0;
  38. table[i].scriptRefCount = 0;
  39. }
  40. table[InitialSize-1].next = InvalidEntry;
  41. for(U32 j = 0; j < HashTableSize; j++)
  42. hashTable[j] = 0;
  43. allocator = new DataChunker(DataChunkerSize);
  44. }
  45. NetStringTable::~NetStringTable()
  46. {
  47. delete allocator;
  48. dFree( table );
  49. }
  50. void NetStringTable::incStringRef(U32 id)
  51. {
  52. AssertFatal(table[id].refCount != 0 || table[id].scriptRefCount != 0 , "Cannot inc ref count from zero.");
  53. table[id].refCount++;
  54. }
  55. void NetStringTable::incStringRefScript(U32 id)
  56. {
  57. AssertFatal(table[id].refCount != 0 || table[id].scriptRefCount != 0 , "Cannot inc ref count from zero.");
  58. table[id].scriptRefCount++;
  59. }
  60. U32 NetStringTable::addString(const char *string)
  61. {
  62. U32 hash = _StringTable::hashString(string);
  63. U32 bucket = hash % HashTableSize;
  64. for(U32 walk = hashTable[bucket];walk; walk = table[walk].next)
  65. {
  66. if(!dStrcmp(table[walk].string, string))
  67. {
  68. table[walk].refCount++;
  69. return walk;
  70. }
  71. }
  72. U32 e = firstFree;
  73. firstFree = table[e].next;
  74. if(firstFree == InvalidEntry)
  75. {
  76. // in this case, we should expand the table for next time...
  77. U32 newSize = size * 2;
  78. table = (Entry *) dRealloc(table, newSize * sizeof(Entry));
  79. for(U32 i = size; i < newSize; i++)
  80. {
  81. table[i].next = i + 1;
  82. table[i].refCount = 0;
  83. table[i].scriptRefCount = 0;
  84. }
  85. firstFree = size;
  86. table[newSize - 1].next = InvalidEntry;
  87. size = newSize;
  88. }
  89. table[e].refCount++;
  90. table[e].string = (char *) allocator->alloc(dStrlen(string) + 1);
  91. dStrcpy(table[e].string, string);
  92. table[e].next = hashTable[bucket];
  93. hashTable[bucket] = e;
  94. table[e].link = firstValid;
  95. table[firstValid].prevLink = e;
  96. firstValid = e;
  97. table[e].prevLink = 0;
  98. return e;
  99. }
  100. U32 GameAddTaggedString(const char *string)
  101. {
  102. return gNetStringTable->addString(string);
  103. }
  104. const char *NetStringTable::lookupString(U32 id)
  105. {
  106. if(table[id].refCount == 0 && table[id].scriptRefCount == 0)
  107. return NULL;
  108. return table[id].string;
  109. }
  110. void NetStringTable::removeString(U32 id, bool script)
  111. {
  112. if(!script)
  113. {
  114. AssertFatal(table[id].refCount != 0, "Error, ref count is already 0!!");
  115. if(--table[id].refCount)
  116. return;
  117. if(table[id].scriptRefCount)
  118. return;
  119. }
  120. else
  121. {
  122. // If both ref counts are already 0, this id is not valid. Ignore
  123. // the remove
  124. if (table[id].scriptRefCount == 0 && table[id].refCount == 0)
  125. return;
  126. if(table[id].scriptRefCount == 0 && table[id].refCount)
  127. {
  128. Con::errorf("removeTaggedString failed! Ref count is already 0 for string: %s", table[id].string);
  129. return;
  130. }
  131. if(--table[id].scriptRefCount)
  132. return;
  133. if(table[id].refCount)
  134. return;
  135. }
  136. // unlink first:
  137. U32 prev = table[id].prevLink;
  138. U32 next = table[id].link;
  139. if(next)
  140. table[next].prevLink = prev;
  141. if(prev)
  142. table[prev].link = next;
  143. else
  144. firstValid = next;
  145. // remove it from the hash table
  146. U32 hash = _StringTable::hashString(table[id].string);
  147. U32 bucket = hash % HashTableSize;
  148. for(U32 *walk = &hashTable[bucket];*walk; walk = &table[*walk].next)
  149. {
  150. if(*walk == id)
  151. {
  152. *walk = table[id].next;
  153. break;
  154. }
  155. }
  156. table[id].next = firstFree;
  157. firstFree = id;
  158. }
  159. void NetStringTable::repack()
  160. {
  161. DataChunker *newAllocator = new DataChunker(DataChunkerSize);
  162. for(U32 walk = firstValid; walk; walk = table[walk].link)
  163. {
  164. const char *prevStr = table[walk].string;
  165. table[walk].string = (char *) newAllocator->alloc(dStrlen(prevStr) + 1);
  166. dStrcpy(table[walk].string, prevStr);
  167. }
  168. delete allocator;
  169. allocator = newAllocator;
  170. }
  171. void NetStringTable::create()
  172. {
  173. AssertFatal(gNetStringTable == NULL, "Error, calling NetStringTable::create twice.");
  174. gNetStringTable = new NetStringTable();
  175. }
  176. void NetStringTable::destroy()
  177. {
  178. AssertFatal(gNetStringTable != NULL, "Error, not calling NetStringTable::create.");
  179. delete gNetStringTable;
  180. gNetStringTable = NULL;
  181. }
  182. void NetStringTable::expandString(NetStringHandle &inString, char *buf, U32 bufSize, U32 argc, const char **argv)
  183. {
  184. buf[0] = StringTagPrefixByte;
  185. dSprintf(buf + 1, bufSize - 1, "%d ", inString.getIndex());
  186. const char *string = inString.getString();
  187. if (string != NULL) {
  188. U32 index = dStrlen(buf);
  189. while(index < bufSize)
  190. {
  191. char c = *string++;
  192. if(c == '%')
  193. {
  194. c = *string++;
  195. if(c >= '1' && c <= '9')
  196. {
  197. U32 strIndex = c - '1';
  198. if(strIndex >= argc)
  199. continue;
  200. // start copying out of arg index
  201. const char *copy = argv[strIndex];
  202. // skip past any tags:
  203. if(*copy == StringTagPrefixByte)
  204. {
  205. while(*copy && *copy != ' ')
  206. copy++;
  207. if(*copy)
  208. copy++;
  209. }
  210. while(*copy && index < bufSize)
  211. buf[index++] = *copy++;
  212. continue;
  213. }
  214. }
  215. buf[index++] = c;
  216. if(!c)
  217. break;
  218. }
  219. buf[bufSize - 1] = 0;
  220. } else {
  221. dStrcat(buf, "<NULL>");
  222. }
  223. }
  224. #ifdef TORQUE_DEBUG
  225. void NetStringTable::dumpToConsole()
  226. {
  227. U32 count = 0;
  228. S32 maxIndex = -1;
  229. for ( U32 i = 0; i < size; i++ )
  230. {
  231. if ( table[i].refCount > 0 || table[i].scriptRefCount > 0)
  232. {
  233. Con::printf( "%d: \"%c%s%c\" REF: %d", i, 0x10, table[i].string, 0x11, table[i].refCount );
  234. if ( maxIndex == -1 || table[i].refCount > table[maxIndex].refCount )
  235. maxIndex = i;
  236. count++;
  237. }
  238. }
  239. Con::printf( ">> STRINGS: %d MAX REF COUNT: %d \"%c%s%c\" <<",
  240. count,
  241. ( maxIndex == -1 ) ? 0 : table[maxIndex].refCount,
  242. 0x10,
  243. ( maxIndex == -1 ) ? "" : table[maxIndex].string,
  244. 0x11 );
  245. }
  246. #endif // DEBUG
  247. DefineEngineFunction( dumpNetStringTable, void, (),,
  248. "@brief Dump the current contents of the networked string table to the console.\n\n"
  249. "The results are returned in three columns. The first column is the network string ID. "
  250. "The second column is the string itself. The third column is the reference count to the "
  251. "network string.\n\n"
  252. "@note This function is available only in debug builds.\n\n"
  253. "@ingroup Networking" )
  254. {
  255. #ifdef TORQUE_DEBUG
  256. gNetStringTable->dumpToConsole();
  257. #endif
  258. }