netStringTable.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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(!String::compare(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. dsize_t stringLen = dStrlen(string) + 1;
  91. table[e].string = (char *) allocator->alloc(stringLen);
  92. dStrcpy(table[e].string, string, stringLen);
  93. table[e].next = hashTable[bucket];
  94. hashTable[bucket] = e;
  95. table[e].link = firstValid;
  96. table[firstValid].prevLink = e;
  97. firstValid = e;
  98. table[e].prevLink = 0;
  99. return e;
  100. }
  101. U32 GameAddTaggedString(const char *string)
  102. {
  103. return gNetStringTable->addString(string);
  104. }
  105. const char *NetStringTable::lookupString(U32 id)
  106. {
  107. if(table[id].refCount == 0 && table[id].scriptRefCount == 0)
  108. return NULL;
  109. return table[id].string;
  110. }
  111. void NetStringTable::removeString(U32 id, bool script)
  112. {
  113. if(!script)
  114. {
  115. AssertFatal(table[id].refCount != 0, "Error, ref count is already 0!!");
  116. if(--table[id].refCount)
  117. return;
  118. if(table[id].scriptRefCount)
  119. return;
  120. }
  121. else
  122. {
  123. // If both ref counts are already 0, this id is not valid. Ignore
  124. // the remove
  125. if (table[id].scriptRefCount == 0 && table[id].refCount == 0)
  126. return;
  127. if(table[id].scriptRefCount == 0 && table[id].refCount)
  128. {
  129. Con::errorf("removeTaggedString failed! Ref count is already 0 for string: %s", table[id].string);
  130. return;
  131. }
  132. if(--table[id].scriptRefCount)
  133. return;
  134. if(table[id].refCount)
  135. return;
  136. }
  137. // unlink first:
  138. U32 prev = table[id].prevLink;
  139. U32 next = table[id].link;
  140. if(next)
  141. table[next].prevLink = prev;
  142. if(prev)
  143. table[prev].link = next;
  144. else
  145. firstValid = next;
  146. // remove it from the hash table
  147. U32 hash = _StringTable::hashString(table[id].string);
  148. U32 bucket = hash % HashTableSize;
  149. for(U32 *walk = &hashTable[bucket];*walk; walk = &table[*walk].next)
  150. {
  151. if(*walk == id)
  152. {
  153. *walk = table[id].next;
  154. break;
  155. }
  156. }
  157. table[id].next = firstFree;
  158. firstFree = id;
  159. }
  160. void NetStringTable::repack()
  161. {
  162. DataChunker *newAllocator = new DataChunker(DataChunkerSize);
  163. for(U32 walk = firstValid; walk; walk = table[walk].link)
  164. {
  165. const char *prevStr = table[walk].string;
  166. dsize_t prevStrLen = dStrlen(prevStr) + 1;
  167. table[walk].string = (char *) newAllocator->alloc(prevStrLen);
  168. dStrcpy(table[walk].string, prevStr, prevStrLen);
  169. }
  170. delete allocator;
  171. allocator = newAllocator;
  172. }
  173. void NetStringTable::create()
  174. {
  175. AssertFatal(gNetStringTable == NULL, "Error, calling NetStringTable::create twice.");
  176. gNetStringTable = new NetStringTable();
  177. }
  178. void NetStringTable::destroy()
  179. {
  180. AssertFatal(gNetStringTable != NULL, "Error, not calling NetStringTable::create.");
  181. delete gNetStringTable;
  182. gNetStringTable = NULL;
  183. }
  184. void NetStringTable::expandString(NetStringHandle &inString, char *buf, U32 bufSize, U32 argc, const char **argv)
  185. {
  186. buf[0] = StringTagPrefixByte;
  187. dSprintf(buf + 1, bufSize - 1, "%d ", inString.getIndex());
  188. const char *string = inString.getString();
  189. if (string != NULL) {
  190. U32 index = dStrlen(buf);
  191. while(index < bufSize)
  192. {
  193. char c = *string++;
  194. if(c == '%')
  195. {
  196. c = *string++;
  197. if(c >= '1' && c <= '9')
  198. {
  199. U32 strIndex = c - '1';
  200. if(strIndex >= argc)
  201. continue;
  202. // start copying out of arg index
  203. const char *copy = argv[strIndex];
  204. // skip past any tags:
  205. if(*copy == StringTagPrefixByte)
  206. {
  207. while(*copy && *copy != ' ')
  208. copy++;
  209. if(*copy)
  210. copy++;
  211. }
  212. while(*copy && index < bufSize)
  213. buf[index++] = *copy++;
  214. continue;
  215. }
  216. }
  217. buf[index++] = c;
  218. if(!c)
  219. break;
  220. }
  221. buf[bufSize - 1] = 0;
  222. } else {
  223. dStrcat(buf, "<NULL>", bufSize);
  224. }
  225. }
  226. #ifdef TORQUE_DEBUG
  227. void NetStringTable::dumpToConsole()
  228. {
  229. U32 count = 0;
  230. S32 maxIndex = -1;
  231. for ( U32 i = 0; i < size; i++ )
  232. {
  233. if ( table[i].refCount > 0 || table[i].scriptRefCount > 0)
  234. {
  235. Con::printf( "%d: \"%c%s%c\" REF: %d", i, 0x10, table[i].string, 0x11, table[i].refCount );
  236. if ( maxIndex == -1 || table[i].refCount > table[maxIndex].refCount )
  237. maxIndex = i;
  238. count++;
  239. }
  240. }
  241. Con::printf( ">> STRINGS: %d MAX REF COUNT: %d \"%c%s%c\" <<",
  242. count,
  243. ( maxIndex == -1 ) ? 0 : table[maxIndex].refCount,
  244. 0x10,
  245. ( maxIndex == -1 ) ? "" : table[maxIndex].string,
  246. 0x11 );
  247. }
  248. #endif // DEBUG
  249. DefineEngineFunction( dumpNetStringTable, void, (),,
  250. "@brief Dump the current contents of the networked string table to the console.\n\n"
  251. "The results are returned in three columns. The first column is the network string ID. "
  252. "The second column is the string itself. The third column is the reference count to the "
  253. "network string.\n\n"
  254. "@note This function is available only in debug builds.\n\n"
  255. "@ingroup Networking" )
  256. {
  257. #ifdef TORQUE_DEBUG
  258. gNetStringTable->dumpToConsole();
  259. #endif
  260. }