TTSupport.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeDebugPch.h"
  6. #if ENABLE_TTD
  7. void TTDAbort_unrecoverable_error(const char* msg)
  8. {
  9. Output::Print(_u("TTD assert failed: %S\n"), msg);
  10. int scenario = 101;
  11. ReportFatalException(NULL, E_UNEXPECTED, Fatal_TTDAbort, scenario);
  12. }
  13. namespace TTD
  14. {
  15. TTModeStack::TTModeStack()
  16. : m_stackEntries(nullptr), m_stackTop(0), m_stackMax(16)
  17. {
  18. this->m_stackEntries = TT_HEAP_ALLOC_ARRAY_ZERO(TTDMode, 16);
  19. }
  20. TTModeStack::~TTModeStack()
  21. {
  22. TT_HEAP_FREE_ARRAY(TTDMode, this->m_stackEntries, this->m_stackMax);
  23. }
  24. uint32 TTModeStack::Count() const
  25. {
  26. return this->m_stackTop;
  27. }
  28. TTDMode TTModeStack::GetAt(uint32 index) const
  29. {
  30. TTDAssert(index < this->m_stackTop, "index is out of range");
  31. return this->m_stackEntries[index];
  32. }
  33. void TTModeStack::SetAt(uint32 index, TTDMode m)
  34. {
  35. TTDAssert(index < this->m_stackTop, "index is out of range");
  36. this->m_stackEntries[index] = m;
  37. }
  38. void TTModeStack::Push(TTDMode m)
  39. {
  40. if(this->m_stackTop == this->m_stackMax)
  41. {
  42. uint32 newMax = this->m_stackMax + 16;
  43. TTDMode* newStack = TT_HEAP_ALLOC_ARRAY_ZERO(TTDMode, newMax);
  44. js_memcpy_s(newStack, newMax * sizeof(TTDMode), this->m_stackEntries, this->m_stackMax * sizeof(TTDMode));
  45. TT_HEAP_FREE_ARRAY(TTDMode, this->m_stackEntries, this->m_stackMax);
  46. this->m_stackMax = newMax;
  47. this->m_stackEntries = newStack;
  48. }
  49. this->m_stackEntries[this->m_stackTop] = m;
  50. this->m_stackTop++;
  51. }
  52. TTDMode TTModeStack::Peek() const
  53. {
  54. TTDAssert(this->m_stackTop > 0, "Underflow in stack pop.");
  55. return this->m_stackEntries[this->m_stackTop - 1];
  56. }
  57. void TTModeStack::Pop()
  58. {
  59. TTDAssert(this->m_stackTop > 0, "Underflow in stack pop.");
  60. this->m_stackTop--;
  61. }
  62. namespace UtilSupport
  63. {
  64. TTAutoString::TTAutoString()
  65. : m_allocSize(-1), m_contents(nullptr), m_optFormatBuff(nullptr)
  66. {
  67. ;
  68. }
  69. TTAutoString::TTAutoString(const char16* str)
  70. : m_allocSize(-1), m_contents(nullptr), m_optFormatBuff(nullptr)
  71. {
  72. size_t clen = wcslen(str) + 1;
  73. this->m_contents = TT_HEAP_ALLOC_ARRAY_ZERO(char16, clen);
  74. this->m_allocSize = (int32)clen;
  75. js_memcpy_s(this->m_contents, clen * sizeof(char16), str, clen * sizeof(char16));
  76. }
  77. TTAutoString::TTAutoString(const TTAutoString& str)
  78. : m_allocSize(-1), m_contents(nullptr), m_optFormatBuff(nullptr)
  79. {
  80. this->Append(str.m_contents);
  81. }
  82. TTAutoString& TTAutoString::operator=(const TTAutoString& str)
  83. {
  84. if(this != &str)
  85. {
  86. this->Clear();
  87. this->Append(str.GetStrValue());
  88. }
  89. return *this;
  90. }
  91. TTAutoString::~TTAutoString()
  92. {
  93. this->Clear();
  94. }
  95. void TTAutoString::Clear()
  96. {
  97. if(this->m_contents != nullptr)
  98. {
  99. TT_HEAP_FREE_ARRAY(char16, this->m_contents, (size_t)this->m_allocSize);
  100. this->m_allocSize = -1;
  101. this->m_contents = nullptr;
  102. }
  103. if(this->m_optFormatBuff != nullptr)
  104. {
  105. TT_HEAP_FREE_ARRAY(char16, this->m_optFormatBuff, 64);
  106. this->m_optFormatBuff = nullptr;
  107. }
  108. }
  109. bool TTAutoString::IsNullString() const
  110. {
  111. return this->m_contents == nullptr;
  112. }
  113. void TTAutoString::Append(const char16* str, size_t start, size_t end)
  114. {
  115. Assert(end > start);
  116. if(this->m_contents == nullptr && str == nullptr)
  117. {
  118. return;
  119. }
  120. size_t origsize = (this->m_contents != nullptr ? wcslen(this->m_contents) : 0);
  121. size_t strsize = 0;
  122. if(start == 0 && end == SIZE_T_MAX)
  123. {
  124. strsize = (str != nullptr ? wcslen(str) : 0);
  125. }
  126. else
  127. {
  128. strsize = (end - start) + 1;
  129. }
  130. size_t nsize = origsize + strsize + 1;
  131. char16* nbuff = TT_HEAP_ALLOC_ARRAY_ZERO(char16, nsize);
  132. if(this->m_contents != nullptr)
  133. {
  134. js_memcpy_s(nbuff, nsize * sizeof(char16), this->m_contents, origsize * sizeof(char16));
  135. TT_HEAP_FREE_ARRAY(char16, this->m_contents, origsize + 1);
  136. this->m_allocSize = -1;
  137. this->m_contents = nullptr;
  138. }
  139. if(str != nullptr)
  140. {
  141. size_t curr = origsize;
  142. for(size_t i = start; i <= end && str[i] != '\0'; ++i)
  143. {
  144. nbuff[curr] = str[i];
  145. curr++;
  146. }
  147. nbuff[curr] = _u('\0');
  148. }
  149. this->m_contents = nbuff;
  150. this->m_allocSize = (int64)nsize;
  151. }
  152. void TTAutoString::Append(const TTAutoString& str, size_t start, size_t end)
  153. {
  154. this->Append(str.GetStrValue(), start, end);
  155. }
  156. void TTAutoString::Append(uint64 val)
  157. {
  158. if(this->m_optFormatBuff == nullptr)
  159. {
  160. this->m_optFormatBuff = TT_HEAP_ALLOC_ARRAY_ZERO(char16, 64);
  161. }
  162. swprintf_s(this->m_optFormatBuff, 32, _u("%I64u"), val); //64 char16s is 32 words
  163. this->Append(this->m_optFormatBuff);
  164. }
  165. void TTAutoString::Append(LPCUTF8 strBegin, LPCUTF8 strEnd)
  166. {
  167. int32 strCount = (int32)((strEnd - strBegin) + 1);
  168. char16* buff = TT_HEAP_ALLOC_ARRAY_ZERO(char16, (size_t)strCount);
  169. LPCUTF8 curr = strBegin;
  170. int32 i = 0;
  171. while(curr != strEnd)
  172. {
  173. buff[i] = (char16)*curr;
  174. i++;
  175. curr++;
  176. }
  177. TTDAssert(i + 1 == strCount, "Our indexing is off.");
  178. buff[i] = _u('\0');
  179. this->Append(buff);
  180. TT_HEAP_FREE_ARRAY(char16, buff, (size_t)strCount);
  181. }
  182. int32 TTAutoString::GetLength() const
  183. {
  184. TTDAssert(!this->IsNullString(), "That doesn't make sense.");
  185. return (int32)wcslen(this->m_contents);
  186. }
  187. char16 TTAutoString::GetCharAt(int32 pos) const
  188. {
  189. TTDAssert(!this->IsNullString(), "That doesn't make sense.");
  190. TTDAssert(0 <= pos && pos < this->GetLength(), "Not in valid range.");
  191. return this->m_contents[pos];
  192. }
  193. const char16* TTAutoString::GetStrValue() const
  194. {
  195. return this->m_contents;
  196. }
  197. }
  198. //////////////////
  199. void LoadValuesForHashTables(uint32 targetSize, uint32* powerOf2, uint32* closePrime, uint32* midPrime)
  200. {
  201. TTD_TABLE_FACTORLOAD_BASE(128, 127, 61)
  202. TTD_TABLE_FACTORLOAD(256, 251, 127)
  203. TTD_TABLE_FACTORLOAD(512, 511, 251)
  204. TTD_TABLE_FACTORLOAD(1024, 1021, 511)
  205. TTD_TABLE_FACTORLOAD(2048, 2039, 1021)
  206. TTD_TABLE_FACTORLOAD(4096, 4093, 2039)
  207. TTD_TABLE_FACTORLOAD(8192, 8191, 4093)
  208. TTD_TABLE_FACTORLOAD(16384, 16381, 8191)
  209. TTD_TABLE_FACTORLOAD(32768, 32749, 16381)
  210. TTD_TABLE_FACTORLOAD(65536, 65521, 32749)
  211. TTD_TABLE_FACTORLOAD(131072, 131071, 65521)
  212. TTD_TABLE_FACTORLOAD(262144, 262139, 131071)
  213. TTD_TABLE_FACTORLOAD(524288, 524287, 262139)
  214. TTD_TABLE_FACTORLOAD(1048576, 1048573, 524287)
  215. TTD_TABLE_FACTORLOAD(2097152, 2097143, 1048573)
  216. TTD_TABLE_FACTORLOAD(4194304, 4194301, 2097143)
  217. TTD_TABLE_FACTORLOAD(8388608, 8388593, 4194301)
  218. TTD_TABLE_FACTORLOAD_FINAL(16777216, 16777213, 8388593)
  219. }
  220. //////////////////
  221. void InitializeAsNullPtrTTString(TTString& str)
  222. {
  223. str.Length = 0;
  224. str.Contents = nullptr;
  225. }
  226. bool IsNullPtrTTString(const TTString& str)
  227. {
  228. return str.Contents == nullptr;
  229. }
  230. #if ENABLE_TTD_INTERNAL_DIAGNOSTICS
  231. bool TTStringEQForDiagnostics(const TTString& str1, const TTString& str2)
  232. {
  233. if(IsNullPtrTTString(str1) || IsNullPtrTTString(str2))
  234. {
  235. return IsNullPtrTTString(str1) && IsNullPtrTTString(str2);
  236. }
  237. if(str1.Length != str2.Length)
  238. {
  239. return false;
  240. }
  241. for(uint32 i = 0; i < str1.Length; ++i)
  242. {
  243. if(str1.Contents[i] != str2.Contents[i])
  244. {
  245. return false;
  246. }
  247. }
  248. return true;
  249. }
  250. #endif
  251. //////////////////
  252. MarkTable::MarkTable()
  253. : m_capcity(TTD_MARK_TABLE_INIT_SIZE), m_h2Prime(TTD_MARK_TABLE_INIT_H2PRIME), m_count(0), m_iterPos(0)
  254. {
  255. this->m_addrArray = TT_HEAP_ALLOC_ARRAY_ZERO(uint64, this->m_capcity);
  256. this->m_markArray = TT_HEAP_ALLOC_ARRAY_ZERO(MarkTableTag, this->m_capcity);
  257. memset(this->m_handlerCounts, 0, ((uint32)MarkTableTag::KindTagCount) * sizeof(uint32));
  258. }
  259. MarkTable::~MarkTable()
  260. {
  261. TT_HEAP_FREE_ARRAY(uint64, this->m_addrArray, this->m_capcity);
  262. TT_HEAP_FREE_ARRAY(MarkTableTag, this->m_markArray, this->m_capcity);
  263. }
  264. void MarkTable::Clear()
  265. {
  266. if(this->m_capcity == TTD_MARK_TABLE_INIT_SIZE)
  267. {
  268. memset(this->m_addrArray, 0, TTD_MARK_TABLE_INIT_SIZE * sizeof(uint64));
  269. memset(this->m_markArray, 0, TTD_MARK_TABLE_INIT_SIZE * sizeof(MarkTableTag));
  270. }
  271. else
  272. {
  273. TT_HEAP_FREE_ARRAY(uint64, this->m_addrArray, this->m_capcity);
  274. TT_HEAP_FREE_ARRAY(MarkTableTag, this->m_markArray, this->m_capcity);
  275. this->m_capcity = TTD_MARK_TABLE_INIT_SIZE;
  276. this->m_h2Prime = TTD_MARK_TABLE_INIT_H2PRIME;
  277. this->m_addrArray = TT_HEAP_ALLOC_ARRAY_ZERO(uint64, this->m_capcity);
  278. this->m_markArray = TT_HEAP_ALLOC_ARRAY_ZERO(MarkTableTag, this->m_capcity);
  279. }
  280. this->m_count = 0;
  281. this->m_iterPos = 0;
  282. memset(this->m_handlerCounts, 0, ((uint32)MarkTableTag::KindTagCount) * sizeof(uint32));
  283. }
  284. }
  285. #endif