CompoundString.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. // JScriptDiag does not link with Runtime.lib and does not include .cpp files, so this file will be included as a header
  6. #include "RuntimeLibraryPch.h"
  7. namespace Js
  8. {
  9. #pragma region CompoundString::Block
  10. #ifndef IsJsDiag
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12. const uint CompoundString::Block::MaxChainedBlockSize = HeapConstants::MaxSmallObjectSize; // TODO: LargeAlloc seems to be significantly slower, hence this threshold
  13. const uint CompoundString::Block::ChainSizeThreshold = MaxChainedBlockSize / 2;
  14. // TODO: Once the above LargeAlloc issue is fixed, experiment with forcing resizing as long as the string has only direct chars
  15. CompoundString::Block::Block(const CharCount charCapacity, const Block *const previous)
  16. : bufferOwner(this), charLength(0), charCapacity(charCapacity), previous(previous)
  17. {
  18. Assert(HeapInfo::IsAlignedSize(ChainSizeThreshold));
  19. Assert(ChainSizeThreshold <= MaxChainedBlockSize);
  20. Assert(HeapInfo::IsAlignedSize(MaxChainedBlockSize));
  21. Assert((MaxChainedBlockSize << 1) > MaxChainedBlockSize);
  22. Assert(charCapacity != 0);
  23. Assert(GrowSize(SizeFromCharCapacity(charCapacity)) != 0);
  24. }
  25. CompoundString::Block::Block(
  26. const void *const buffer,
  27. const CharCount charLength,
  28. const CharCount charCapacity)
  29. : bufferOwner(this), charLength(charLength), charCapacity(charCapacity), previous(nullptr)
  30. {
  31. Assert(buffer);
  32. Assert(charLength <= charCapacity);
  33. js_wmemcpy_s(Chars(), charLength, Chars(buffer), charLength);
  34. }
  35. CompoundString::Block::Block(const Block &other, const CharCount usedCharLength)
  36. : bufferOwner(other.bufferOwner),
  37. charLength(usedCharLength),
  38. charCapacity(other.charCapacity),
  39. previous(other.previous)
  40. {
  41. // This only does a shallow copy. The metadata is copied, and a reference to the other block is included in this copy
  42. // for access to the other block's buffer.
  43. Assert(usedCharLength <= other.charCapacity);
  44. }
  45. CompoundString::Block *CompoundString::Block::New(
  46. const uint size,
  47. const Block *const previous,
  48. Recycler *const recycler)
  49. {
  50. Assert(HeapInfo::IsAlignedSize(size));
  51. Assert(recycler);
  52. return RecyclerNewPlus(recycler, size - sizeof(Block), Block, CharCapacityFromSize(size), previous);
  53. }
  54. CompoundString::Block *CompoundString::Block::New(
  55. const void *const buffer,
  56. const CharCount usedCharLength,
  57. const bool reserveMoreSpace,
  58. Recycler *const recycler)
  59. {
  60. Assert(buffer);
  61. Assert(recycler);
  62. uint size = SizeFromUsedCharLength(usedCharLength);
  63. if(reserveMoreSpace)
  64. size = GrowSize(size);
  65. return RecyclerNewPlus(recycler, size - sizeof(Block), Block, buffer, usedCharLength, CharCapacityFromSize(size));
  66. }
  67. CompoundString::Block *CompoundString::Block::Clone(
  68. const CharCount usedCharLength,
  69. Recycler *const recycler) const
  70. {
  71. Assert(recycler);
  72. return RecyclerNew(recycler, Block, *this, usedCharLength);
  73. }
  74. CharCount CompoundString::Block::CharCapacityFromSize(const uint size)
  75. {
  76. Assert(size >= sizeof(Block));
  77. return (size - sizeof(Block)) / sizeof(char16);
  78. }
  79. uint CompoundString::Block::SizeFromCharCapacity(const CharCount charCapacity)
  80. {
  81. Assert(IsValidCharCount(charCapacity));
  82. return UInt32Math::Add(sizeof(Block), charCapacity * sizeof(char16));
  83. }
  84. #endif
  85. inline CharCount CompoundString::Block::PointerAlign(const CharCount charLength)
  86. {
  87. const CharCount alignedCharLength = ::Math::Align(charLength, static_cast<CharCount>(sizeof(void *) / sizeof(char16)));
  88. Assert(alignedCharLength >= charLength);
  89. return alignedCharLength;
  90. }
  91. inline const char16 *CompoundString::Block::Chars(const void *const buffer)
  92. {
  93. return static_cast<const char16 *>(buffer);
  94. }
  95. #ifndef IsJsDiag
  96. char16 *CompoundString::Block::Chars(void *const buffer)
  97. {
  98. return static_cast<char16 *>(buffer);
  99. }
  100. #endif
  101. inline void *const *CompoundString::Block::Pointers(const void *const buffer)
  102. {
  103. return static_cast<void *const *>(buffer);
  104. }
  105. #ifndef IsJsDiag
  106. void **CompoundString::Block::Pointers(void *const buffer)
  107. {
  108. return static_cast<void **>(buffer);
  109. }
  110. CharCount CompoundString::Block::PointerCapacityFromCharCapacity(const CharCount charCapacity)
  111. {
  112. return charCapacity / (sizeof(void *) / sizeof(char16));
  113. }
  114. CharCount CompoundString::Block::CharCapacityFromPointerCapacity(const CharCount pointerCapacity)
  115. {
  116. return pointerCapacity * (sizeof(void *) / sizeof(char16));
  117. }
  118. #endif
  119. // ChakraDiag includes CompoundString.cpp as a header file so this method needs to be marked as inline
  120. // to handle that case
  121. JS_DIAG_INLINE CharCount CompoundString::Block::PointerLengthFromCharLength(const CharCount charLength)
  122. {
  123. return PointerAlign(charLength) / (sizeof(void *) / sizeof(char16));
  124. }
  125. #ifndef IsJsDiag
  126. CharCount CompoundString::Block::CharLengthFromPointerLength(const CharCount pointerLength)
  127. {
  128. return pointerLength * (sizeof(void *) / sizeof(char16));
  129. }
  130. uint CompoundString::Block::SizeFromUsedCharLength(const CharCount usedCharLength)
  131. {
  132. const size_t usedSize = SizeFromCharCapacity(usedCharLength);
  133. const size_t alignedUsedSize = HeapInfo::GetAlignedSizeNoCheck(usedSize);
  134. if (alignedUsedSize != (uint)alignedUsedSize)
  135. {
  136. Js::Throw::OutOfMemory();
  137. }
  138. return (uint)alignedUsedSize;
  139. }
  140. bool CompoundString::Block::ShouldAppendChars(
  141. const CharCount appendCharLength,
  142. const uint additionalSizeForPointerAppend)
  143. {
  144. // Append characters instead of pointers when it would save space. Add some buffer as well, as flattening becomes more
  145. // expensive after the switch to pointer mode.
  146. //
  147. // 'additionalSizeForPointerAppend' should be provided when appending a pointer also involves creating a string object
  148. // or some other additional space (such as LiteralString, in which case this parameter should be sizeof(LiteralString)),
  149. // as that additional size also needs to be taken into account.
  150. return appendCharLength <= (sizeof(void *) * 2 + additionalSizeForPointerAppend) / sizeof(char16);
  151. }
  152. const void *CompoundString::Block::Buffer() const
  153. {
  154. return bufferOwner + 1;
  155. }
  156. void *CompoundString::Block::Buffer()
  157. {
  158. return bufferOwner + 1;
  159. }
  160. const CompoundString::Block *CompoundString::Block::Previous() const
  161. {
  162. return previous;
  163. }
  164. const char16 *CompoundString::Block::Chars() const
  165. {
  166. return Chars(Buffer());
  167. }
  168. char16 *CompoundString::Block::Chars()
  169. {
  170. return Chars(Buffer());
  171. }
  172. CharCount CompoundString::Block::CharLength() const
  173. {
  174. return charLength;
  175. }
  176. void CompoundString::Block::SetCharLength(const CharCount charLength)
  177. {
  178. Assert(charLength <= CharCapacity());
  179. this->charLength = charLength;
  180. }
  181. CharCount CompoundString::Block::CharCapacity() const
  182. {
  183. return charCapacity;
  184. }
  185. void *const *CompoundString::Block::Pointers() const
  186. {
  187. return Pointers(Buffer());
  188. }
  189. void **CompoundString::Block::Pointers()
  190. {
  191. return Pointers(Buffer());
  192. }
  193. CharCount CompoundString::Block::PointerLength() const
  194. {
  195. return PointerLengthFromCharLength(CharLength());
  196. }
  197. CharCount CompoundString::Block::PointerCapacity() const
  198. {
  199. return PointerCapacityFromCharCapacity(CharCapacity());
  200. }
  201. uint CompoundString::Block::GrowSize(const uint size)
  202. {
  203. Assert(size >= sizeof(Block));
  204. Assert(HeapInfo::IsAlignedSize(size));
  205. const uint newSize = size << 1;
  206. Assert(newSize > size);
  207. return newSize;
  208. }
  209. uint CompoundString::Block::GrowSizeForChaining(const uint size)
  210. {
  211. const uint newSize = GrowSize(size);
  212. return min(MaxChainedBlockSize, newSize);
  213. }
  214. CompoundString::Block *CompoundString::Block::Chain(Recycler *const recycler)
  215. {
  216. return New(GrowSizeForChaining(SizeFromUsedCharLength(CharLength())), this, recycler);
  217. }
  218. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  219. #endif
  220. #pragma endregion
  221. #pragma region CompoundString::BlockInfo
  222. #ifndef IsJsDiag
  223. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  224. CompoundString::BlockInfo::BlockInfo() : buffer(nullptr), charLength(0), charCapacity(0)
  225. {
  226. }
  227. CompoundString::BlockInfo::BlockInfo(Block *const block)
  228. {
  229. CopyFrom(block);
  230. }
  231. char16 *CompoundString::BlockInfo::Chars() const
  232. {
  233. return Block::Chars(buffer);
  234. }
  235. CharCount CompoundString::BlockInfo::CharLength() const
  236. {
  237. return charLength;
  238. }
  239. void CompoundString::BlockInfo::SetCharLength(const CharCount charLength)
  240. {
  241. Assert(charLength <= CharCapacity());
  242. this->charLength = charLength;
  243. }
  244. CharCount CompoundString::BlockInfo::CharCapacity() const
  245. {
  246. return charCapacity;
  247. }
  248. void **CompoundString::BlockInfo::Pointers() const
  249. {
  250. return Block::Pointers(buffer);
  251. }
  252. CharCount CompoundString::BlockInfo::PointerLength() const
  253. {
  254. return Block::PointerLengthFromCharLength(CharLength());
  255. }
  256. void CompoundString::BlockInfo::SetPointerLength(const CharCount pointerLength)
  257. {
  258. Assert(pointerLength <= PointerCapacity());
  259. charLength = Block::CharLengthFromPointerLength(pointerLength);
  260. }
  261. CharCount CompoundString::BlockInfo::PointerCapacity() const
  262. {
  263. return Block::PointerCapacityFromCharCapacity(CharCapacity());
  264. }
  265. CharCount CompoundString::BlockInfo::AlignCharCapacityForAllocation(const CharCount charCapacity)
  266. {
  267. const CharCount alignedCharCapacity =
  268. ::Math::AlignOverflowCheck(
  269. charCapacity == 0 ? static_cast<CharCount>(1) : charCapacity,
  270. static_cast<CharCount>(HeapConstants::ObjectGranularity / sizeof(char16)));
  271. Assert(alignedCharCapacity != 0);
  272. return alignedCharCapacity;
  273. }
  274. CharCount CompoundString::BlockInfo::GrowCharCapacity(const CharCount charCapacity)
  275. {
  276. Assert(charCapacity != 0);
  277. Assert(AlignCharCapacityForAllocation(charCapacity) == charCapacity);
  278. const CharCount newCharCapacity = UInt32Math::Mul<2>(charCapacity);
  279. Assert(newCharCapacity > charCapacity);
  280. return newCharCapacity;
  281. }
  282. bool CompoundString::BlockInfo::ShouldAllocateBuffer(const CharCount charCapacity)
  283. {
  284. Assert(charCapacity != 0);
  285. Assert(AlignCharCapacityForAllocation(charCapacity) == charCapacity);
  286. return charCapacity < Block::ChainSizeThreshold / sizeof(char16);
  287. }
  288. void CompoundString::BlockInfo::AllocateBuffer(const CharCount charCapacity, Recycler *const recycler)
  289. {
  290. Assert(!buffer);
  291. Assert(CharLength() == 0);
  292. Assert(CharCapacity() == 0);
  293. Assert(ShouldAllocateBuffer(charCapacity));
  294. Assert(recycler);
  295. buffer = RecyclerNewArray(recycler, char16, charCapacity);
  296. this->charCapacity = charCapacity;
  297. }
  298. CompoundString::Block *CompoundString::BlockInfo::CopyBuffer(
  299. const void *const buffer,
  300. const CharCount usedCharLength,
  301. const bool reserveMoreSpace,
  302. Recycler *const recycler)
  303. {
  304. Assert(buffer);
  305. Assert(recycler);
  306. CharCount charCapacity = AlignCharCapacityForAllocation(usedCharLength);
  307. if(reserveMoreSpace)
  308. charCapacity = GrowCharCapacity(charCapacity);
  309. if(ShouldAllocateBuffer(charCapacity))
  310. {
  311. AllocateBuffer(charCapacity, recycler);
  312. charLength = usedCharLength;
  313. js_wmemcpy_s((char16*)(this->buffer), charCapacity, (char16*)(buffer), usedCharLength);
  314. return nullptr;
  315. }
  316. Block *const block = Block::New(buffer, usedCharLength, reserveMoreSpace, recycler);
  317. CopyFrom(block);
  318. return block;
  319. }
  320. CompoundString::Block *CompoundString::BlockInfo::Resize(Recycler *const recycler)
  321. {
  322. Assert(recycler);
  323. const CharCount newCharCapacity = GrowCharCapacity(AlignCharCapacityForAllocation(CharLength()));
  324. if(ShouldAllocateBuffer(newCharCapacity))
  325. {
  326. void *const newBuffer = RecyclerNewArray(recycler, char16, newCharCapacity);
  327. charCapacity = newCharCapacity;
  328. const CharCount charLength = CharLength();
  329. js_wmemcpy_s((char16*)newBuffer, charCapacity, (char16*)buffer, charLength);
  330. buffer = newBuffer;
  331. return nullptr;
  332. }
  333. Block *const block = Block::New(buffer, CharLength(), true, recycler);
  334. CopyFrom(block);
  335. return block;
  336. }
  337. void CompoundString::BlockInfo::CopyFrom(Block *const block)
  338. {
  339. buffer = block->Buffer();
  340. charLength = block->CharLength();
  341. charCapacity = block->CharCapacity();
  342. }
  343. void CompoundString::BlockInfo::CopyTo(Block *const block)
  344. {
  345. Assert(block->Buffer() == buffer);
  346. Assert(block->CharLength() <= charLength);
  347. Assert(block->CharCapacity() == charCapacity);
  348. block->SetCharLength(charLength);
  349. }
  350. void CompoundString::BlockInfo::Unreference()
  351. {
  352. buffer = nullptr;
  353. charLength = 0;
  354. charCapacity = 0;
  355. }
  356. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  357. #endif
  358. #pragma endregion
  359. #pragma region CompoundString
  360. #ifndef IsJsDiag
  361. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  362. CompoundString::CompoundString(const CharCount initialCharCapacity, JavascriptLibrary *const library)
  363. : LiteralString(library->GetStringTypeStatic()),
  364. directCharLength(static_cast<CharCount>(-1)),
  365. ownsLastBlock(true),
  366. lastBlock(nullptr)
  367. {
  368. Assert(library);
  369. lastBlockInfo.AllocateBuffer(initialCharCapacity, library->GetRecycler());
  370. }
  371. CompoundString::CompoundString(
  372. const CharCount initialBlockSize,
  373. const bool allocateBlock,
  374. JavascriptLibrary *const library)
  375. : LiteralString(library->GetStringTypeStatic()),
  376. directCharLength(static_cast<CharCount>(-1)),
  377. ownsLastBlock(true)
  378. {
  379. Assert(allocateBlock);
  380. Assert(library);
  381. Block *const block = Block::New(initialBlockSize, nullptr, library->GetRecycler());
  382. lastBlockInfo.CopyFrom(block);
  383. lastBlock = block;
  384. }
  385. CompoundString::CompoundString(
  386. const CharCount stringLength,
  387. const CharCount directCharLength,
  388. const void *const buffer,
  389. const CharCount usedCharLength,
  390. const bool reserveMoreSpace,
  391. JavascriptLibrary *const library)
  392. : LiteralString(library->GetStringTypeStatic()),
  393. directCharLength(directCharLength),
  394. ownsLastBlock(true)
  395. {
  396. Assert(directCharLength == static_cast<CharCount>(-1) || directCharLength <= stringLength);
  397. Assert(buffer);
  398. Assert(library);
  399. SetLength(stringLength);
  400. lastBlock = lastBlockInfo.CopyBuffer(buffer, usedCharLength, reserveMoreSpace, library->GetRecycler());
  401. }
  402. CompoundString::CompoundString(CompoundString &other, const bool forAppending)
  403. : LiteralString(other.GetLibrary()->GetStringTypeStatic()),
  404. lastBlockInfo(other.lastBlockInfo),
  405. directCharLength(other.directCharLength),
  406. lastBlock(other.lastBlock)
  407. {
  408. Assert(!other.IsFinalized());
  409. SetLength(other.GetLength());
  410. if(forAppending)
  411. {
  412. // This compound string will be used for appending, so take ownership of the last block. Appends are fast for a
  413. // compound string that owns the last block.
  414. const bool ownsLastBlock = other.ownsLastBlock;
  415. other.ownsLastBlock = false;
  416. this->ownsLastBlock = ownsLastBlock;
  417. if(ownsLastBlock)
  418. return;
  419. TakeOwnershipOfLastBlock();
  420. return;
  421. }
  422. ownsLastBlock = false;
  423. }
  424. CompoundString *CompoundString::NewWithCharCapacity(
  425. const CharCount initialCharCapacity,
  426. JavascriptLibrary *const library)
  427. {
  428. const CharCount alignedInitialCharCapacity = BlockInfo::AlignCharCapacityForAllocation(initialCharCapacity);
  429. if(BlockInfo::ShouldAllocateBuffer(alignedInitialCharCapacity))
  430. return NewWithBufferCharCapacity(alignedInitialCharCapacity, library);
  431. return NewWithBlockSize(Block::SizeFromUsedCharLength(initialCharCapacity), library);
  432. }
  433. CompoundString *CompoundString::NewWithPointerCapacity(
  434. const CharCount initialPointerCapacity,
  435. JavascriptLibrary *const library)
  436. {
  437. return NewWithCharCapacity(Block::CharCapacityFromPointerCapacity(initialPointerCapacity), library);
  438. }
  439. CompoundString *CompoundString::NewWithBufferCharCapacity(const CharCount initialCharCapacity, JavascriptLibrary *const library)
  440. {
  441. Assert(library);
  442. return RecyclerNew(library->GetRecycler(), CompoundString, initialCharCapacity, library);
  443. }
  444. CompoundString *CompoundString::NewWithBlockSize(const CharCount initialBlockSize, JavascriptLibrary *const library)
  445. {
  446. Assert(library);
  447. return RecyclerNew(library->GetRecycler(), CompoundString, initialBlockSize, true, library);
  448. }
  449. CompoundString *CompoundString::New(
  450. const CharCount stringLength,
  451. const CharCount directCharLength,
  452. const void *const buffer,
  453. const CharCount usedCharLength,
  454. const bool reserveMoreSpace,
  455. JavascriptLibrary *const library)
  456. {
  457. Assert(library);
  458. return
  459. RecyclerNew(
  460. library->GetRecycler(),
  461. CompoundString,
  462. stringLength,
  463. directCharLength,
  464. buffer,
  465. usedCharLength,
  466. reserveMoreSpace,
  467. library);
  468. }
  469. CompoundString *CompoundString::Clone(const bool forAppending)
  470. {
  471. return RecyclerNew(GetLibrary()->GetRecycler(), CompoundString, *this, forAppending);
  472. }
  473. CompoundString * CompoundString::JitClone(CompoundString * cs)
  474. {
  475. Assert(Is(cs));
  476. return cs->Clone(false);
  477. }
  478. CompoundString * CompoundString::JitCloneForAppending(CompoundString * cs)
  479. {
  480. Assert(Is(cs));
  481. return cs->Clone(true);
  482. }
  483. bool CompoundString::Is(RecyclableObject *const object)
  484. {
  485. return VirtualTableInfo<CompoundString>::HasVirtualTable(object);
  486. }
  487. bool CompoundString::Is(const Var var)
  488. {
  489. return RecyclableObject::Is(var) && Is(RecyclableObject::FromVar(var));
  490. }
  491. CompoundString *CompoundString::FromVar(RecyclableObject *const object)
  492. {
  493. Assert(Is(object));
  494. CompoundString *const cs = static_cast<CompoundString *>(object);
  495. Assert(!cs->IsFinalized());
  496. return cs;
  497. }
  498. CompoundString *CompoundString::FromVar(const Var var)
  499. {
  500. return FromVar(RecyclableObject::FromVar(var));
  501. }
  502. JavascriptString *CompoundString::GetImmutableOrScriptUnreferencedString(JavascriptString *const s)
  503. {
  504. Assert(s);
  505. // The provided string may be referenced by script code. A script-unreferenced version of the string is being requested,
  506. // likely because the provided string will be referenced directly in a concatenation operation (by ConcatString or
  507. // another CompoundString, for instance). If the provided string is a CompoundString, it must not be mutated by script
  508. // code after the concatenation operation. In that case, clone the string to ensure that it is not referenced by script
  509. // code. If the clone is never handed back to script code, it effectively behaves as an immutable string.
  510. return Is(s) ? FromVar(s)->Clone(false) : s;
  511. }
  512. bool CompoundString::ShouldAppendChars(const CharCount appendCharLength)
  513. {
  514. return Block::ShouldAppendChars(appendCharLength);
  515. }
  516. bool CompoundString::HasOnlyDirectChars() const
  517. {
  518. return directCharLength == static_cast<CharCount>(-1);
  519. }
  520. void CompoundString::SwitchToPointerMode()
  521. {
  522. Assert(HasOnlyDirectChars());
  523. directCharLength = GetLength();
  524. if(PHASE_TRACE_StringConcat)
  525. {
  526. Output::Print(_u("CompoundString::SwitchToPointerMode()\n"));
  527. Output::Flush();
  528. }
  529. }
  530. bool CompoundString::OwnsLastBlock() const
  531. {
  532. return ownsLastBlock;
  533. }
  534. const char16 *CompoundString::GetAppendStringBuffer(JavascriptString *const s) const
  535. {
  536. Assert(s);
  537. // A compound string cannot flatten itself while appending itself to itself since flattening would make the append
  538. // illegal. Clone the string being appended if necessary, before flattening.
  539. return s == this ? FromVar(s)->Clone(false)->GetSz() : s->GetString();
  540. }
  541. char16 *CompoundString::LastBlockChars() const
  542. {
  543. return lastBlockInfo.Chars();
  544. }
  545. CharCount CompoundString::LastBlockCharLength() const
  546. {
  547. return lastBlockInfo.CharLength();
  548. }
  549. void CompoundString::SetLastBlockCharLength(const CharCount charLength)
  550. {
  551. lastBlockInfo.SetCharLength(charLength);
  552. }
  553. CharCount CompoundString::LastBlockCharCapacity() const
  554. {
  555. return lastBlockInfo.CharCapacity();
  556. }
  557. void **CompoundString::LastBlockPointers() const
  558. {
  559. return lastBlockInfo.Pointers();
  560. }
  561. CharCount CompoundString::LastBlockPointerLength() const
  562. {
  563. return lastBlockInfo.PointerLength();
  564. }
  565. void CompoundString::SetLastBlockPointerLength(const CharCount pointerLength)
  566. {
  567. lastBlockInfo.SetPointerLength(pointerLength);
  568. }
  569. CharCount CompoundString::LastBlockPointerCapacity() const
  570. {
  571. return lastBlockInfo.PointerCapacity();
  572. }
  573. void CompoundString::PackSubstringInfo(
  574. const CharCount startIndex,
  575. const CharCount length,
  576. void * *const packedSubstringInfoRef,
  577. void * *const packedSubstringInfo2Ref)
  578. {
  579. Assert(static_cast<int32>(startIndex) >= 0);
  580. Assert(static_cast<int32>(length) >= 0);
  581. Assert(packedSubstringInfoRef);
  582. Assert(packedSubstringInfo2Ref);
  583. #if defined(_M_X64_OR_ARM64)
  584. // On 64-bit architectures, two nonnegative 32-bit ints fit completely in a tagged pointer
  585. *packedSubstringInfoRef =
  586. reinterpret_cast<void *>(
  587. (static_cast<uintptr_t>(startIndex) << 32) +
  588. (static_cast<uintptr_t>(length) << 1) +
  589. 1);
  590. *packedSubstringInfo2Ref = nullptr;
  591. #else
  592. CompileAssert(sizeof(void *) == sizeof(int32));
  593. // On 32-bit architectures, it will be attempted to fit both pieces of into one pointer by using 16 bits for the
  594. // start index, 15 for the length, and 1 for the tag. If it does not fit, an additional pointer will be used.
  595. if(startIndex <= static_cast<CharCount>(0xffff) && length <= static_cast<CharCount>(0x7fff))
  596. {
  597. *packedSubstringInfoRef =
  598. reinterpret_cast<void *>(
  599. (static_cast<uintptr_t>(startIndex) << 16) +
  600. (static_cast<uintptr_t>(length) << 1) +
  601. 1);
  602. *packedSubstringInfo2Ref = nullptr;
  603. }
  604. else
  605. {
  606. *packedSubstringInfoRef = reinterpret_cast<void *>((static_cast<uintptr_t>(startIndex) << 1) + 1);
  607. *packedSubstringInfo2Ref = reinterpret_cast<void *>((static_cast<uintptr_t>(length) << 1) + 1);
  608. }
  609. #endif
  610. #if DBG
  611. CharCount unpackedStartIndex, unpackedLength;
  612. UnpackSubstringInfo(*packedSubstringInfoRef, *packedSubstringInfo2Ref, &unpackedStartIndex, &unpackedLength);
  613. Assert(unpackedStartIndex == startIndex);
  614. Assert(unpackedLength == length);
  615. #endif
  616. }
  617. #endif
  618. inline bool CompoundString::IsPackedInfo(void *const pointer)
  619. {
  620. Assert(pointer);
  621. return reinterpret_cast<uintptr_t>(pointer) & 1;
  622. }
  623. inline void CompoundString::UnpackSubstringInfo(
  624. void *const pointer,
  625. void *const pointer2,
  626. CharCount *const startIndexRef,
  627. CharCount *const lengthRef)
  628. {
  629. Assert(pointer);
  630. Assert(startIndexRef);
  631. Assert(lengthRef);
  632. const uintptr_t packedSubstringInfo = reinterpret_cast<uintptr_t>(pointer);
  633. Assert(packedSubstringInfo & 1);
  634. #if defined(_M_X64_OR_ARM64)
  635. // On 64-bit architectures, two nonnegative 32-bit ints fit completely in a tagged pointer
  636. Assert(!pointer2);
  637. *startIndexRef = static_cast<CharCount>(packedSubstringInfo >> 32);
  638. *lengthRef = static_cast<CharCount>(static_cast<uint32>(packedSubstringInfo) >> 1);
  639. #else
  640. CompileAssert(sizeof(void *) == sizeof(int32));
  641. // On 32-bit architectures, it will be attempted to fit both pieces of into one pointer by using 16 bits for the
  642. // start index, 15 for the length, and 1 for the tag. If it does not fit, an additional pointer will be used.
  643. if(!pointer2)
  644. {
  645. *startIndexRef = static_cast<CharCount>(packedSubstringInfo >> 16);
  646. *lengthRef = static_cast<CharCount>(static_cast<uint16>(packedSubstringInfo) >> 1);
  647. }
  648. else
  649. {
  650. *startIndexRef = static_cast<CharCount>(packedSubstringInfo >> 1);
  651. const uintptr_t packedSubstringInfo2 = reinterpret_cast<uintptr_t>(pointer2);
  652. Assert(packedSubstringInfo2 & 1);
  653. *lengthRef = static_cast<CharCount>(packedSubstringInfo2 >> 1);
  654. }
  655. #endif
  656. }
  657. #ifndef IsJsDiag
  658. void CompoundString::AppendSlow(const char16 c)
  659. {
  660. Grow();
  661. const bool appended =
  662. HasOnlyDirectChars()
  663. ? TryAppendGeneric(c, this)
  664. : TryAppendGeneric(GetLibrary()->GetCharStringCache().GetStringForChar(c), 1, this);
  665. Assert(appended);
  666. }
  667. void CompoundString::AppendSlow(JavascriptString *const s)
  668. {
  669. Grow();
  670. const bool appended = TryAppendGeneric(s, s->GetLength(), this);
  671. Assert(appended);
  672. }
  673. void CompoundString::AppendSlow(
  674. __in_xcount(appendCharLength) const char16 *const s,
  675. const CharCount appendCharLength)
  676. {
  677. Assert(!IsFinalized());
  678. Assert(OwnsLastBlock());
  679. Assert(HasOnlyDirectChars());
  680. Assert(s);
  681. // In case of exception, save enough state to revert back to the current state
  682. const BlockInfo savedLastBlockInfo(lastBlockInfo);
  683. Block *const savedLastBlock = lastBlock;
  684. const CharCount savedStringLength = GetLength();
  685. SetLength(savedStringLength + appendCharLength);
  686. CharCount copiedCharLength = 0;
  687. while(true)
  688. {
  689. const CharCount blockCharLength = LastBlockCharLength();
  690. const CharCount copyCharLength =
  691. min(LastBlockCharCapacity() - blockCharLength, appendCharLength - copiedCharLength);
  692. CopyHelper(&LastBlockChars()[blockCharLength], &s[copiedCharLength], copyCharLength);
  693. SetLastBlockCharLength(blockCharLength + copyCharLength);
  694. copiedCharLength += copyCharLength;
  695. if(copiedCharLength >= appendCharLength)
  696. break;
  697. try
  698. {
  699. Grow();
  700. }
  701. catch(...)
  702. {
  703. lastBlockInfo = savedLastBlockInfo;
  704. if(savedLastBlock)
  705. savedLastBlock->SetCharLength(savedLastBlockInfo.CharLength());
  706. lastBlock = savedLastBlock;
  707. SetLength(savedStringLength);
  708. throw;
  709. }
  710. }
  711. Assert(copiedCharLength == appendCharLength);
  712. }
  713. void CompoundString::AppendSlow(
  714. JavascriptString *const s,
  715. void *const packedSubstringInfo,
  716. void *const packedSubstringInfo2,
  717. const CharCount appendCharLength)
  718. {
  719. Grow();
  720. const bool appended = TryAppendGeneric(s, packedSubstringInfo, packedSubstringInfo2, appendCharLength, this);
  721. Assert(appended);
  722. }
  723. void CompoundString::PrepareForAppend()
  724. {
  725. Assert(!IsFinalized());
  726. if(OwnsLastBlock())
  727. return;
  728. TakeOwnershipOfLastBlock();
  729. }
  730. void CompoundString::Append(const char16 c)
  731. {
  732. AppendGeneric(c, this, false);
  733. }
  734. void CompoundString::AppendChars(const char16 c)
  735. {
  736. AppendGeneric(c, this, true);
  737. }
  738. void CompoundString::Append(JavascriptString *const s)
  739. {
  740. AppendGeneric(s, this, false);
  741. }
  742. void CompoundString::AppendChars(JavascriptString *const s)
  743. {
  744. AppendGeneric(s, this, true);
  745. }
  746. void CompoundString::Append(
  747. JavascriptString *const s,
  748. const CharCount startIndex,
  749. const CharCount appendCharLength)
  750. {
  751. AppendGeneric(s, startIndex, appendCharLength, this, false);
  752. }
  753. void CompoundString::AppendChars(
  754. JavascriptString *const s,
  755. const CharCount startIndex,
  756. const CharCount appendCharLength)
  757. {
  758. AppendGeneric(s, startIndex, appendCharLength, this, true);
  759. }
  760. void CompoundString::Append(
  761. __in_xcount(appendCharLength) const char16 *const s,
  762. const CharCount appendCharLength)
  763. {
  764. AppendGeneric(s, appendCharLength, this, false);
  765. }
  766. void CompoundString::AppendChars(
  767. __in_xcount(appendCharLength) const char16 *const s,
  768. const CharCount appendCharLength)
  769. {
  770. AppendGeneric(s, appendCharLength, this, true);
  771. }
  772. void CompoundString::AppendCharsSz(__in_z const char16 *const s)
  773. {
  774. size_t len = wcslen(s);
  775. // We limit the length of the string to MaxCharCount,
  776. // so just OOM if we are appending a string that exceed this limit already
  777. if (!IsValidCharCount(len))
  778. {
  779. JavascriptExceptionOperators::ThrowOutOfMemory(this->GetScriptContext());
  780. }
  781. AppendChars(s, (CharCount)len);
  782. }
  783. void CompoundString::Grow()
  784. {
  785. Assert(!IsFinalized());
  786. Assert(OwnsLastBlock());
  787. Block *const lastBlock = this->lastBlock;
  788. if(!lastBlock)
  789. {
  790. // There is no last block. Only the buffer was allocated, and is held in 'lastBlockInfo'. In that case it is always
  791. // within the threshold to resize. Resize the buffer or resize it into a new block depending on its size.
  792. this->lastBlock = lastBlockInfo.Resize(GetLibrary()->GetRecycler());
  793. return;
  794. }
  795. lastBlockInfo.CopyTo(lastBlock);
  796. Block *const newLastBlock = lastBlock->Chain(GetLibrary()->GetRecycler());
  797. lastBlockInfo.CopyFrom(newLastBlock);
  798. this->lastBlock = newLastBlock;
  799. }
  800. void CompoundString::TakeOwnershipOfLastBlock()
  801. {
  802. Assert(!IsFinalized());
  803. Assert(!OwnsLastBlock());
  804. // Another string object owns the last block's buffer. The buffer must be copied, or another block must be chained.
  805. Block *const lastBlock = this->lastBlock;
  806. if(!lastBlock)
  807. {
  808. // There is no last block. Only the buffer was allocated, and is held in 'lastBlockInfo'. In that case it is always
  809. // within the threshold to resize. Resize the buffer or resize it into a new block depending on its size.
  810. this->lastBlock = lastBlockInfo.Resize(GetLibrary()->GetRecycler());
  811. ownsLastBlock = true;
  812. return;
  813. }
  814. // The last block is already in a chain, or is over the threshold to resize. Shallow-clone the last block (clone
  815. // just its metadata, while still pointing to the original buffer), and chain it to a new last block.
  816. Recycler *const recycler = GetLibrary()->GetRecycler();
  817. Block *const newLastBlock = lastBlock->Clone(LastBlockCharLength(), recycler)->Chain(recycler);
  818. lastBlockInfo.CopyFrom(newLastBlock);
  819. ownsLastBlock = true;
  820. this->lastBlock = newLastBlock;
  821. }
  822. void CompoundString::Unreference()
  823. {
  824. lastBlockInfo.Unreference();
  825. directCharLength = 0;
  826. ownsLastBlock = false;
  827. lastBlock = nullptr;
  828. }
  829. const char16 *CompoundString::GetSz()
  830. {
  831. Assert(!IsFinalized());
  832. const CharCount totalCharLength = GetLength();
  833. switch(totalCharLength)
  834. {
  835. case 0:
  836. {
  837. Unreference();
  838. const char16 *const buffer = _u("");
  839. SetBuffer(buffer);
  840. VirtualTableInfo<LiteralString>::SetVirtualTable(this);
  841. return buffer;
  842. }
  843. case 1:
  844. {
  845. Assert(HasOnlyDirectChars());
  846. Assert(LastBlockCharLength() == 1);
  847. const char16 *const buffer = GetLibrary()->GetCharStringCache().GetStringForChar(LastBlockChars()[0])->UnsafeGetBuffer();
  848. Unreference();
  849. SetBuffer(buffer);
  850. VirtualTableInfo<LiteralString>::SetVirtualTable(this);
  851. return buffer;
  852. }
  853. }
  854. if(OwnsLastBlock() && HasOnlyDirectChars() && !lastBlock && TryAppendGeneric(_u('\0'), this)) // GetSz() requires null termination
  855. {
  856. // There is no last block. Only the buffer was allocated, and is held in 'lastBlockInfo'. Since this string owns the
  857. // last block, has only direct chars, and the buffer was allocated directly (buffer pointer is not an internal
  858. // pointer), there is no need to copy the buffer.
  859. SetLength(totalCharLength); // terminating null should not count towards the string length
  860. const char16 *const buffer = LastBlockChars();
  861. Unreference();
  862. SetBuffer(buffer);
  863. VirtualTableInfo<LiteralString>::SetVirtualTable(this);
  864. return buffer;
  865. }
  866. char16 *const buffer = RecyclerNewArrayLeaf(GetScriptContext()->GetRecycler(), char16, SafeSzSize(totalCharLength));
  867. buffer[totalCharLength] = _u('\0'); // GetSz() requires null termination
  868. Copy<CompoundString>(buffer, totalCharLength);
  869. Assert(buffer[totalCharLength] == _u('\0'));
  870. Unreference();
  871. SetBuffer(buffer);
  872. VirtualTableInfo<LiteralString>::SetVirtualTable(this);
  873. return buffer;
  874. }
  875. void CompoundString::CopyVirtual(
  876. _Out_writes_(m_charLength) char16 *const buffer,
  877. StringCopyInfoStack &nestedStringTreeCopyInfos,
  878. const byte recursionDepth)
  879. {
  880. Assert(!IsFinalized());
  881. Assert(buffer);
  882. const CharCount totalCharLength = GetLength();
  883. switch(totalCharLength)
  884. {
  885. case 0:
  886. return;
  887. case 1:
  888. Assert(HasOnlyDirectChars());
  889. Assert(LastBlockCharLength() == 1);
  890. buffer[0] = LastBlockChars()[0];
  891. return;
  892. }
  893. // Copy buffers from string pointers
  894. const bool hasOnlyDirectChars = HasOnlyDirectChars();
  895. const CharCount directCharLength = hasOnlyDirectChars ? totalCharLength : this->directCharLength;
  896. CharCount remainingCharLengthToCopy = totalCharLength;
  897. const Block *const lastBlock = this->lastBlock;
  898. const Block *block = lastBlock;
  899. void *const *blockPointers = LastBlockPointers();
  900. CharCount pointerIndex = LastBlockPointerLength();
  901. while(remainingCharLengthToCopy > directCharLength)
  902. {
  903. while(pointerIndex == 0)
  904. {
  905. Assert(block);
  906. block = block->Previous();
  907. Assert(block);
  908. blockPointers = block->Pointers();
  909. pointerIndex = block->PointerLength();
  910. }
  911. void *const pointer = blockPointers[--pointerIndex];
  912. if(IsPackedInfo(pointer))
  913. {
  914. Assert(pointerIndex != 0);
  915. void *pointer2 = blockPointers[--pointerIndex];
  916. JavascriptString *s;
  917. #if defined(_M_X64_OR_ARM64)
  918. Assert(!IsPackedInfo(pointer2));
  919. #else
  920. if(IsPackedInfo(pointer2))
  921. {
  922. Assert(pointerIndex != 0);
  923. s = JavascriptString::FromVar(blockPointers[--pointerIndex]);
  924. }
  925. else
  926. #endif
  927. {
  928. s = JavascriptString::FromVar(pointer2);
  929. pointer2 = nullptr;
  930. }
  931. CharCount startIndex, copyCharLength;
  932. UnpackSubstringInfo(pointer, pointer2, &startIndex, &copyCharLength);
  933. Assert(startIndex <= s->GetLength());
  934. Assert(copyCharLength <= s->GetLength() - startIndex);
  935. Assert(remainingCharLengthToCopy >= copyCharLength);
  936. remainingCharLengthToCopy -= copyCharLength;
  937. CopyHelper(&buffer[remainingCharLengthToCopy], &s->GetString()[startIndex], copyCharLength);
  938. }
  939. else
  940. {
  941. JavascriptString *const s = JavascriptString::FromVar(pointer);
  942. const CharCount copyCharLength = s->GetLength();
  943. Assert(remainingCharLengthToCopy >= copyCharLength);
  944. remainingCharLengthToCopy -= copyCharLength;
  945. if(recursionDepth == MaxCopyRecursionDepth && s->IsTree())
  946. {
  947. // Don't copy nested string trees yet, as that involves a recursive call, and the recursion can become
  948. // excessive. Just collect the nested string trees and the buffer location where they should be copied, and
  949. // the caller can deal with those after returning.
  950. nestedStringTreeCopyInfos.Push(StringCopyInfo(s, &buffer[remainingCharLengthToCopy]));
  951. }
  952. else
  953. {
  954. Assert(recursionDepth <= MaxCopyRecursionDepth);
  955. s->Copy(&buffer[remainingCharLengthToCopy], nestedStringTreeCopyInfos, recursionDepth + 1);
  956. }
  957. }
  958. }
  959. Assert(remainingCharLengthToCopy == directCharLength);
  960. if(remainingCharLengthToCopy != 0)
  961. {
  962. // Determine the number of direct chars in the current block
  963. CharCount blockCharLength;
  964. if(pointerIndex == 0)
  965. {
  966. // The string switched to pointer mode at the beginning of the current block, or the string never switched to
  967. // pointer mode and the last block is empty. In either case, direct chars span to the end of the previous block.
  968. Assert(block);
  969. block = block->Previous();
  970. Assert(block);
  971. blockCharLength = block->CharLength();
  972. }
  973. else if(hasOnlyDirectChars)
  974. {
  975. // The string never switched to pointer mode, so the current block's char length is where direct chars end
  976. blockCharLength = block == lastBlock ? LastBlockCharLength() : block->CharLength();
  977. }
  978. else
  979. {
  980. // The string switched to pointer mode somewhere in the middle of the current block. To determine where direct
  981. // chars end in this block, all previous blocks are scanned and their char lengths discounted.
  982. blockCharLength = remainingCharLengthToCopy;
  983. if(block)
  984. {
  985. for(const Block *previousBlock = block->Previous();
  986. previousBlock;
  987. previousBlock = previousBlock->Previous())
  988. {
  989. Assert(blockCharLength >= previousBlock->CharLength());
  990. blockCharLength -= previousBlock->CharLength();
  991. }
  992. }
  993. Assert(Block::PointerLengthFromCharLength(blockCharLength) == pointerIndex);
  994. }
  995. // Copy direct chars
  996. const char16 *blockChars = block == lastBlock ? LastBlockChars() : block->Chars();
  997. while(true)
  998. {
  999. if(blockCharLength != 0)
  1000. {
  1001. Assert(remainingCharLengthToCopy >= blockCharLength);
  1002. remainingCharLengthToCopy -= blockCharLength;
  1003. js_wmemcpy_s(&buffer[remainingCharLengthToCopy], blockCharLength, blockChars, blockCharLength);
  1004. if(remainingCharLengthToCopy == 0)
  1005. break;
  1006. }
  1007. Assert(block);
  1008. block = block->Previous();
  1009. Assert(block);
  1010. blockChars = block->Chars();
  1011. blockCharLength = block->CharLength();
  1012. }
  1013. }
  1014. #if DBG
  1015. // Verify that all nonempty blocks have been visited
  1016. if(block)
  1017. {
  1018. while(true)
  1019. {
  1020. block = block->Previous();
  1021. if(!block)
  1022. break;
  1023. Assert(block->CharLength() == 0);
  1024. }
  1025. }
  1026. #endif
  1027. Assert(remainingCharLengthToCopy == 0);
  1028. }
  1029. bool CompoundString::IsTree() const
  1030. {
  1031. Assert(!IsFinalized());
  1032. return !HasOnlyDirectChars();
  1033. }
  1034. DEFINE_RECYCLER_TRACKER_PERF_COUNTER(CompoundString);
  1035. CompileAssert(static_cast<CharCount>(-1) > static_cast<CharCount>(0)); // CharCount is assumed to be unsigned
  1036. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1037. #endif
  1038. #pragma endregion
  1039. }