netStringTable.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "platform/platform.h"
  23. #include "network/connectionProtocol.h"
  24. #include "sim/simBase.h"
  25. #include "network/netStringTable.h"
  26. #include "string/stringTable.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. }
  49. void NetStringTable::incStringRef(U32 id)
  50. {
  51. AssertFatal(table[id].refCount != 0 || table[id].scriptRefCount != 0 , "Cannot inc ref count from zero.");
  52. table[id].refCount++;
  53. }
  54. void NetStringTable::incStringRefScript(U32 id)
  55. {
  56. AssertFatal(table[id].refCount != 0 || table[id].scriptRefCount != 0 , "Cannot inc ref count from zero.");
  57. table[id].scriptRefCount++;
  58. }
  59. U32 NetStringTable::addString(const char *string)
  60. {
  61. U32 hash = _StringTable::hashString(string);
  62. U32 bucket = hash % HashTableSize;
  63. for(U32 walk = hashTable[bucket];walk; walk = table[walk].next)
  64. {
  65. if(!dStrcmp(table[walk].string, string))
  66. {
  67. table[walk].refCount++;
  68. return walk;
  69. }
  70. }
  71. U32 e = firstFree;
  72. firstFree = table[e].next;
  73. if(firstFree == InvalidEntry)
  74. {
  75. // in this case, we should expand the table for next time...
  76. U32 newSize = size * 2;
  77. table = (Entry *) dRealloc(table, newSize * sizeof(Entry));
  78. for(U32 i = size; i < newSize; i++)
  79. {
  80. table[i].next = i + 1;
  81. table[i].refCount = 0;
  82. table[i].scriptRefCount = 0;
  83. }
  84. firstFree = size;
  85. table[newSize - 1].next = InvalidEntry;
  86. size = newSize;
  87. }
  88. table[e].refCount++;
  89. table[e].string = (char *) allocator->alloc(dStrlen(string) + 1);
  90. dStrcpy(table[e].string, string);
  91. table[e].next = hashTable[bucket];
  92. hashTable[bucket] = e;
  93. table[e].link = firstValid;
  94. table[firstValid].prevLink = e;
  95. firstValid = e;
  96. table[e].prevLink = 0;
  97. return e;
  98. }
  99. U32 GameAddTaggedString(const char *string)
  100. {
  101. return gNetStringTable->addString(string);
  102. }
  103. const char *NetStringTable::lookupString(U32 id)
  104. {
  105. if(table[id].refCount == 0 && table[id].scriptRefCount == 0)
  106. return NULL;
  107. return table[id].string;
  108. }
  109. void NetStringTable::removeString(U32 id, bool script)
  110. {
  111. if(!script)
  112. {
  113. AssertFatal(table[id].refCount != 0, "Error, ref count is already 0!!");
  114. if(--table[id].refCount)
  115. return;
  116. if(table[id].scriptRefCount)
  117. return;
  118. }
  119. else
  120. {
  121. // If both ref counts are already 0, this id is not valid. Ignore
  122. // the remove
  123. if (table[id].scriptRefCount == 0 && table[id].refCount == 0)
  124. return;
  125. if(table[id].scriptRefCount == 0 && table[id].refCount)
  126. {
  127. Con::errorf("removeTaggedString failed! Ref count is already 0 for string: %s", table[id].string);
  128. return;
  129. }
  130. if(--table[id].scriptRefCount)
  131. return;
  132. if(table[id].refCount)
  133. return;
  134. }
  135. // unlink first:
  136. U32 prev = table[id].prevLink;
  137. U32 next = table[id].link;
  138. if(next)
  139. table[next].prevLink = prev;
  140. if(prev)
  141. table[prev].link = next;
  142. else
  143. firstValid = next;
  144. // remove it from the hash table
  145. U32 hash = _StringTable::hashString(table[id].string);
  146. U32 bucket = hash % HashTableSize;
  147. for(U32 *walk = &hashTable[bucket];*walk; walk = &table[*walk].next)
  148. {
  149. if(*walk == id)
  150. {
  151. *walk = table[id].next;
  152. break;
  153. }
  154. }
  155. table[id].next = firstFree;
  156. firstFree = id;
  157. }
  158. void NetStringTable::repack()
  159. {
  160. DataChunker *newAllocator = new DataChunker(DataChunkerSize);
  161. for(U32 walk = firstValid; walk; walk = table[walk].link)
  162. {
  163. const char *prevStr = table[walk].string;
  164. table[walk].string = (char *) newAllocator->alloc(dStrlen(prevStr) + 1);
  165. dStrcpy(table[walk].string, prevStr);
  166. }
  167. delete allocator;
  168. allocator = newAllocator;
  169. }
  170. void NetStringTable::create()
  171. {
  172. AssertFatal(gNetStringTable == NULL, "Error, calling NetStringTable::create twice.");
  173. gNetStringTable = new NetStringTable();
  174. }
  175. void NetStringTable::destroy()
  176. {
  177. AssertFatal(gNetStringTable != NULL, "Error, not calling NetStringTable::create.");
  178. delete gNetStringTable;
  179. gNetStringTable = NULL;
  180. }
  181. void NetStringTable::expandString(NetStringHandle &inString, char *buf, U32 bufSize, U32 argc, const char **argv)
  182. {
  183. buf[0] = StringTagPrefixByte;
  184. dSprintf(buf + 1, bufSize - 1, "%d ", inString.getIndex());
  185. const char *string = inString.getString();
  186. if (string != NULL) {
  187. U32 index = dStrlen(buf);
  188. while(index < bufSize)
  189. {
  190. char c = *string++;
  191. if(c == '%')
  192. {
  193. c = *string++;
  194. if(c >= '1' && c <= '9')
  195. {
  196. U32 strIndex = c - '1';
  197. if(strIndex >= argc)
  198. continue;
  199. // start copying out of arg index
  200. const char *copy = argv[strIndex];
  201. // skip past any tags:
  202. if(*copy == StringTagPrefixByte)
  203. {
  204. while(*copy && *copy != ' ')
  205. copy++;
  206. if(*copy)
  207. copy++;
  208. }
  209. while(*copy && index < bufSize)
  210. buf[index++] = *copy++;
  211. continue;
  212. }
  213. }
  214. buf[index++] = c;
  215. if(!c)
  216. break;
  217. }
  218. buf[bufSize - 1] = 0;
  219. } else {
  220. dStrcat(buf, "<NULL>");
  221. }
  222. }
  223. #if defined(TORQUE_DEBUG)
  224. void NetStringTable::dumpToConsole()
  225. {
  226. U32 count = 0;
  227. S32 maxIndex = -1;
  228. for ( U32 i = 0; i < size; i++ )
  229. {
  230. if ( table[i].refCount > 0 || table[i].scriptRefCount > 0)
  231. {
  232. Con::printf( "%d: \"%c%s%c\" REF: %d", i, 0x10, table[i].string, 0x11, table[i].refCount );
  233. if ( maxIndex == -1 || table[i].refCount > table[maxIndex].refCount )
  234. maxIndex = i;
  235. count++;
  236. }
  237. }
  238. Con::printf( ">> STRINGS: %d MAX REF COUNT: %d \"%c%s%c\" <<",
  239. count,
  240. ( maxIndex == -1 ) ? 0 : table[maxIndex].refCount,
  241. 0x10,
  242. ( maxIndex == -1 ) ? "" : table[maxIndex].string,
  243. 0x11 );
  244. }
  245. ConsoleFunctionGroupBegin(NetStringTable, "Debug functions for the NetStringTable.");
  246. ConsoleFunction( dumpNetStringTable, void, 1, 1, "() Use the dumpNetStringTable function to dump a list of all the currently registered NetStringTable entries, including the times each has been referenced, the total entry count, and the current 'highest' reference string.\n"
  247. "For this to work, the engine must have been compiled with TORQUE_DEBUG defined\n"
  248. "@return No return value.")
  249. {
  250. gNetStringTable->dumpToConsole();
  251. }
  252. ConsoleFunctionGroupEnd(NetStringTable);
  253. #endif // DEBUG