SimpleStringPool.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_CRYSYSTEM_SIMPLESTRINGPOOL_H
  9. #define CRYINCLUDE_CRYSYSTEM_SIMPLESTRINGPOOL_H
  10. #pragma once
  11. #include "ISystem.h"
  12. #include <StlUtils.h>
  13. //TODO: Pull most of this into a cpp file!
  14. struct SStringData
  15. {
  16. SStringData(const char* szString, int nStrLen)
  17. : m_szString(szString)
  18. , m_nStrLen(nStrLen)
  19. {
  20. }
  21. const char* m_szString;
  22. int m_nStrLen;
  23. bool operator==(const SStringData& other) const
  24. {
  25. if (m_nStrLen != other.m_nStrLen)
  26. {
  27. return false;
  28. }
  29. return strcmp(m_szString, other.m_szString) == 0;
  30. }
  31. private:
  32. };
  33. template<>
  34. inline const char* stl::constchar_cast(const SStringData& in)
  35. {
  36. return in.m_szString;
  37. }
  38. /////////////////////////////////////////////////////////////////////
  39. // String pool implementation.
  40. // Inspired by expat implementation.
  41. /////////////////////////////////////////////////////////////////////
  42. class CSimpleStringPool
  43. {
  44. public:
  45. enum
  46. {
  47. STD_BLOCK_SIZE = 1u << 16
  48. };
  49. struct BLOCK
  50. {
  51. BLOCK* next;
  52. int size;
  53. char s[1];
  54. };
  55. unsigned int m_blockSize;
  56. BLOCK* m_blocks;
  57. BLOCK* m_free_blocks;
  58. const char* m_end;
  59. char* m_ptr;
  60. char* m_start;
  61. int nUsedSpace;
  62. int nUsedBlocks;
  63. bool m_reuseStrings;
  64. typedef AZStd::unordered_map<SStringData, char*, stl::hash_string<SStringData> > TStringToExistingStringMap;
  65. TStringToExistingStringMap m_stringToExistingStringMap;
  66. static size_t g_nTotalAllocInXmlStringPools;
  67. CSimpleStringPool()
  68. {
  69. m_blockSize = STD_BLOCK_SIZE - offsetof(BLOCK, s);
  70. m_blocks = 0;
  71. m_start = 0;
  72. m_ptr = 0;
  73. m_end = 0;
  74. nUsedSpace = 0;
  75. nUsedBlocks = 0;
  76. m_free_blocks = 0;
  77. m_reuseStrings = false;
  78. }
  79. explicit CSimpleStringPool(bool reuseStrings)
  80. : m_blockSize(STD_BLOCK_SIZE - offsetof(BLOCK, s))
  81. , m_blocks(NULL)
  82. , m_free_blocks(NULL)
  83. , m_end(0)
  84. , m_ptr(0)
  85. , m_start(0)
  86. , nUsedSpace(0)
  87. , nUsedBlocks(0)
  88. , m_reuseStrings(reuseStrings)
  89. {
  90. }
  91. ~CSimpleStringPool()
  92. {
  93. BLOCK* pBlock = m_blocks;
  94. while (pBlock)
  95. {
  96. BLOCK* temp = pBlock->next;
  97. g_nTotalAllocInXmlStringPools -= (offsetof(BLOCK, s) + pBlock->size * sizeof(char));
  98. azfree(pBlock);
  99. pBlock = temp;
  100. }
  101. pBlock = m_free_blocks;
  102. while (pBlock)
  103. {
  104. BLOCK* temp = pBlock->next;
  105. g_nTotalAllocInXmlStringPools -= (offsetof(BLOCK, s) + pBlock->size * sizeof(char));
  106. azfree(pBlock);
  107. pBlock = temp;
  108. }
  109. m_blocks = 0;
  110. m_ptr = 0;
  111. m_start = 0;
  112. m_end = 0;
  113. }
  114. void SetBlockSize(unsigned int nBlockSize)
  115. {
  116. if (nBlockSize > 1024 * 1024)
  117. {
  118. nBlockSize = 1024 * 1024;
  119. }
  120. unsigned int size = 512;
  121. while (size < nBlockSize)
  122. {
  123. size *= 2;
  124. }
  125. m_blockSize = size - offsetof(BLOCK, s);
  126. }
  127. void Clear()
  128. {
  129. BLOCK* pLast = m_free_blocks;
  130. if (pLast)
  131. {
  132. while (pLast->next)
  133. {
  134. pLast = pLast->next;
  135. }
  136. pLast->next = m_blocks;
  137. }
  138. else
  139. {
  140. m_free_blocks = m_blocks;
  141. }
  142. m_blocks = 0;
  143. m_start = 0;
  144. m_ptr = 0;
  145. m_end = 0;
  146. nUsedSpace = 0;
  147. if (m_reuseStrings)
  148. {
  149. m_stringToExistingStringMap.clear();
  150. }
  151. }
  152. char* Append(const char* ptr, int nStrLen)
  153. {
  154. // If a string does not fit within the remainder of the string pool, a new pool will be allocated with at least
  155. // nStrLen + 1 size, which means this code does take care of incredibly large strings.
  156. if (m_reuseStrings)
  157. {
  158. if (char* existingString = FindExistingString(ptr, nStrLen))
  159. {
  160. return existingString;
  161. }
  162. }
  163. char* ret = m_ptr;
  164. if (m_ptr && nStrLen + 1 < (m_end - m_ptr))
  165. {
  166. memcpy(m_ptr, ptr, nStrLen);
  167. m_ptr = m_ptr + nStrLen;
  168. *m_ptr++ = 0; // add null termination.
  169. }
  170. else
  171. {
  172. int nNewBlockSize = std::max(nStrLen + 1, (int)m_blockSize);
  173. AllocBlock(nNewBlockSize, nStrLen + 1);
  174. PREFAST_ASSUME(m_ptr);
  175. memcpy(m_ptr, ptr, nStrLen);
  176. m_ptr = m_ptr + nStrLen;
  177. *m_ptr++ = 0; // add null termination.
  178. ret = m_start;
  179. }
  180. if (m_reuseStrings)
  181. {
  182. assert(!FindExistingString(ptr, nStrLen));
  183. m_stringToExistingStringMap[SStringData(ret, nStrLen)] = ret;
  184. }
  185. nUsedSpace += nStrLen;
  186. return ret;
  187. }
  188. char* ReplaceString(const char* str1, const char* str2)
  189. {
  190. if (m_reuseStrings)
  191. {
  192. CryFatalError("Can't replace strings in an xml node that reuses strings");
  193. }
  194. int nStrLen1 = static_cast<int>(strlen(str1));
  195. int nStrLen2 = static_cast<int>(strlen(str2));
  196. // undo ptr1 add.
  197. if (m_ptr != m_start)
  198. {
  199. m_ptr = m_ptr - nStrLen1 - 1;
  200. }
  201. assert(m_ptr == str1);
  202. int nStrLen = nStrLen1 + nStrLen2;
  203. char* ret = m_ptr;
  204. if (m_ptr && nStrLen + 1 < (m_end - m_ptr))
  205. {
  206. if (m_ptr != str1)
  207. {
  208. memcpy(m_ptr, str1, nStrLen1);
  209. }
  210. memcpy(m_ptr + nStrLen1, str2, nStrLen2);
  211. m_ptr = m_ptr + nStrLen;
  212. *m_ptr++ = 0; // add null termination.
  213. }
  214. else
  215. {
  216. int nNewBlockSize = std::max(nStrLen + 1, (int)m_blockSize);
  217. if (m_ptr == m_start)
  218. {
  219. ReallocBlock(nNewBlockSize * 2); // Reallocate current block.
  220. PREFAST_ASSUME(m_ptr);
  221. memcpy(m_ptr + nStrLen1, str2, nStrLen2);
  222. }
  223. else
  224. {
  225. AllocBlock(nNewBlockSize, nStrLen + 1);
  226. PREFAST_ASSUME(m_ptr);
  227. memcpy(m_ptr, str1, nStrLen1);
  228. memcpy(m_ptr + nStrLen1, str2, nStrLen2);
  229. }
  230. m_ptr = m_ptr + nStrLen;
  231. *m_ptr++ = 0; // add null termination.
  232. ret = m_start;
  233. }
  234. nUsedSpace += nStrLen;
  235. return ret;
  236. }
  237. private:
  238. CSimpleStringPool(const CSimpleStringPool&);
  239. CSimpleStringPool& operator = (const CSimpleStringPool&);
  240. private:
  241. void AllocBlock(int blockSize, int nMinBlockSize)
  242. {
  243. if (m_free_blocks)
  244. {
  245. BLOCK* pBlock = m_free_blocks;
  246. BLOCK* pPrev = 0;
  247. while (pBlock)
  248. {
  249. if (pBlock->size >= nMinBlockSize)
  250. {
  251. // Reuse free block
  252. if (pPrev)
  253. {
  254. pPrev->next = pBlock->next;
  255. }
  256. else
  257. {
  258. m_free_blocks = pBlock->next;
  259. }
  260. pBlock->next = m_blocks;
  261. m_blocks = pBlock;
  262. m_ptr = pBlock->s;
  263. m_start = pBlock->s;
  264. m_end = pBlock->s + pBlock->size;
  265. return;
  266. }
  267. pPrev = pBlock;
  268. pBlock = pBlock->next;
  269. }
  270. }
  271. size_t nMallocSize = offsetof(BLOCK, s) + blockSize * sizeof(char);
  272. g_nTotalAllocInXmlStringPools += nMallocSize;
  273. BLOCK* pBlock = (BLOCK*)azmalloc(nMallocSize);
  274. ;
  275. assert(pBlock);
  276. pBlock->size = blockSize;
  277. pBlock->next = m_blocks;
  278. m_blocks = pBlock;
  279. m_ptr = pBlock->s;
  280. m_start = pBlock->s;
  281. m_end = pBlock->s + blockSize;
  282. nUsedBlocks++;
  283. }
  284. void ReallocBlock(int blockSize)
  285. {
  286. if (m_reuseStrings)
  287. {
  288. CryFatalError("Can't replace strings in an xml node that reuses strings");
  289. }
  290. BLOCK* pThisBlock = m_blocks;
  291. BLOCK* pPrevBlock = m_blocks->next;
  292. m_blocks = pPrevBlock;
  293. size_t nMallocSize = offsetof(BLOCK, s) + blockSize * sizeof(char);
  294. if (pThisBlock)
  295. {
  296. g_nTotalAllocInXmlStringPools -= (offsetof(BLOCK, s) + pThisBlock->size * sizeof(char));
  297. }
  298. g_nTotalAllocInXmlStringPools += nMallocSize;
  299. BLOCK* pBlock = (BLOCK*)azrealloc(pThisBlock, nMallocSize);
  300. assert(pBlock);
  301. pBlock->size = blockSize;
  302. pBlock->next = m_blocks;
  303. m_blocks = pBlock;
  304. m_ptr = pBlock->s;
  305. m_start = pBlock->s;
  306. m_end = pBlock->s + blockSize;
  307. }
  308. char* FindExistingString(const char* szString, int nStrLen)
  309. {
  310. SStringData testData(szString, nStrLen);
  311. char* szResult = stl::find_in_map(m_stringToExistingStringMap, testData, NULL);
  312. assert(!szResult || !_stricmp(szResult, szString));
  313. return szResult;
  314. }
  315. };
  316. #endif // CRYINCLUDE_CRYSYSTEM_SIMPLESTRINGPOOL_H