BufferBuilder.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. // Buffer builder is used to layout out binary content which contains offsets
  6. // from one part of the content to another.
  7. // It works in two-pass fashion:
  8. // - Pass one fixes the real offset of each element (BufferBuilder) of the
  9. // content.
  10. // - Pass two writes the actual content including any relative offset values.
  11. //----------------------------------------------------------------------------
  12. #pragma once
  13. #if _M_IX86
  14. #define serialization_alignment
  15. #elif _M_X64 || defined(_M_ARM32_OR_ARM64)
  16. #define serialization_alignment __unaligned
  17. #else
  18. #error Must define alignment capabilities for processor
  19. #endif
  20. struct _SIMDValue;
  21. typedef _SIMDValue SIMDValue;
  22. namespace Js
  23. {
  24. // The base buffer builder class
  25. class BufferBuilder
  26. {
  27. protected:
  28. LPCWSTR clue;
  29. BufferBuilder(LPCWSTR clue)
  30. : clue(clue), offset(0xffffffff) { }
  31. public:
  32. uint32 offset;
  33. virtual uint32 FixOffset(uint32 offset) = 0;
  34. virtual void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const = 0;
  35. #if DBG
  36. protected:
  37. void TraceOutput(byte * buffer, uint32 size) const;
  38. #endif
  39. };
  40. #if VARIABLE_INT_ENCODING
  41. #define VARIABLE_INT_TAGBIT_COUNT (1)
  42. #define VARIABLE_INT_BYTE_SHIFT (8 - VARIABLE_INT_TAGBIT_COUNT)
  43. #define VARIABLE_INT_BYTE_MAX (1 << VARIABLE_INT_BYTE_SHIFT)
  44. #define VARIABLE_INT_BYTE_MASK ~((byte) VARIABLE_INT_BYTE_MAX)
  45. #define SENTINEL_BYTE_COUNT 1
  46. #define ONE_BYTE_MAX ((byte) 0xfd)
  47. #define TWO_BYTE_MAX ((uint16) 0xffff)
  48. #define TWO_BYTE_SENTINEL ONE_BYTE_MAX + 1
  49. #define FOUR_BYTE_SENTINEL ONE_BYTE_MAX + 2
  50. #define MIN_SENTINEL TWO_BYTE_SENTINEL
  51. #endif
  52. #if INSTRUMENT_BUFFER_INTS
  53. static uint Counts[] = { 0, 0, 0, 0 };
  54. #endif
  55. // Templatized buffer builder for writing fixed-size content
  56. template<typename T, bool useVariableIntEncoding>
  57. struct BufferBuilderOf : BufferBuilder
  58. {
  59. typedef serialization_alignment T value_type;
  60. value_type value;
  61. BufferBuilderOf(LPCWSTR clue, const T & value)
  62. : BufferBuilder(clue), value(value)
  63. { }
  64. // Assume that the value is 0- for negative values of value, we'll just use the default encoding
  65. bool UseOneByte() const
  66. {
  67. return value >= 0 && value <= ONE_BYTE_MAX;
  68. }
  69. bool UseTwoBytes() const
  70. {
  71. return value > ONE_BYTE_MAX && value <= TWO_BYTE_MAX;
  72. }
  73. uint32 FixOffset(uint32 offset) override
  74. {
  75. this->offset = offset;
  76. if (useVariableIntEncoding)
  77. {
  78. if (UseOneByte())
  79. {
  80. return this->offset + sizeof(serialization_alignment byte);
  81. }
  82. else if (UseTwoBytes())
  83. {
  84. return this->offset + sizeof(serialization_alignment uint16) + SENTINEL_BYTE_COUNT;
  85. }
  86. return this->offset + sizeof(serialization_alignment T) + SENTINEL_BYTE_COUNT;
  87. }
  88. else
  89. {
  90. return this->offset + sizeof(serialization_alignment T);
  91. }
  92. }
  93. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  94. {
  95. DebugOnly(uint32 size = sizeof(T));
  96. #if INSTRUMENT_BUFFER_INTS
  97. if (value < ((1 << 8)))
  98. {
  99. Counts[0]++;
  100. }
  101. else if (value < ((1 << 16)))
  102. {
  103. Counts[1]++;
  104. }
  105. else if (value < ((1 << 24)))
  106. {
  107. Counts[2]++;
  108. }
  109. else
  110. {
  111. Counts[3]++;
  112. }
  113. #endif
  114. if (useVariableIntEncoding)
  115. {
  116. if (UseOneByte())
  117. {
  118. if (bufferSize - this->offset < sizeof(serialization_alignment byte))
  119. {
  120. Throw::FatalInternalError();
  121. }
  122. DebugOnly(size = sizeof(byte));
  123. *(serialization_alignment byte*)(buffer + this->offset) = (byte) value;
  124. }
  125. else if (UseTwoBytes())
  126. {
  127. if (bufferSize - this->offset < sizeof(serialization_alignment uint16) + sizeof(serialization_alignment byte))
  128. {
  129. Throw::FatalInternalError();
  130. }
  131. DebugOnly(size = sizeof(uint16) + 1);
  132. *(serialization_alignment byte*)(buffer + this->offset) = TWO_BYTE_SENTINEL;
  133. *(serialization_alignment uint16*)(buffer + this->offset + SENTINEL_BYTE_COUNT) = (uint16) this->value;
  134. }
  135. else
  136. {
  137. if (bufferSize - this->offset < sizeof(serialization_alignment T) + sizeof(serialization_alignment byte))
  138. {
  139. Throw::FatalInternalError();
  140. }
  141. *(serialization_alignment byte*)(buffer + this->offset) = FOUR_BYTE_SENTINEL;
  142. *(serialization_alignment T*)(buffer + this->offset + SENTINEL_BYTE_COUNT) = this->value;
  143. DebugOnly(size = sizeof(T) + 1);
  144. #if INSTRUMENT_BUFFER_INTS
  145. Output::Print(_u("[BCGENSTATS] %d, %d\n"), value, sizeof(T));
  146. #endif
  147. }
  148. }
  149. else
  150. {
  151. if (bufferSize - this->offset<sizeof(serialization_alignment T))
  152. {
  153. Throw::FatalInternalError();
  154. }
  155. *(serialization_alignment T*)(buffer + this->offset) = value;
  156. }
  157. DebugOnly(TraceOutput(buffer, size));
  158. }
  159. };
  160. template<typename T>
  161. struct BufferBuilderOf<T, false>: BufferBuilder
  162. {
  163. typedef serialization_alignment T value_type;
  164. value_type value;
  165. BufferBuilderOf(LPCWSTR clue, const T & value)
  166. : BufferBuilder(clue), value(value)
  167. { }
  168. uint32 FixOffset(uint32 offset) override
  169. {
  170. this->offset = offset;
  171. return this->offset + sizeof(serialization_alignment T);
  172. }
  173. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  174. {
  175. if (bufferSize - this->offset<sizeof(serialization_alignment T))
  176. {
  177. Throw::FatalInternalError();
  178. }
  179. *(serialization_alignment T*)(buffer + this->offset) = value;
  180. DebugOnly(TraceOutput(buffer, sizeof(T)));
  181. }
  182. };
  183. template <typename T>
  184. struct ConstantSizedBufferBuilderOf : BufferBuilderOf<T, false>
  185. {
  186. ConstantSizedBufferBuilderOf(LPCWSTR clue, const T & value)
  187. : BufferBuilderOf<T, false>(clue, value)
  188. { }
  189. };
  190. #if VARIABLE_INT_ENCODING
  191. typedef BufferBuilderOf<int16, true> BufferBuilderInt16;
  192. typedef BufferBuilderOf<int, true> BufferBuilderInt32;
  193. typedef ConstantSizedBufferBuilderOf<byte> BufferBuilderByte;
  194. typedef ConstantSizedBufferBuilderOf<float> BufferBuilderFloat;
  195. typedef ConstantSizedBufferBuilderOf<double> BufferBuilderDouble;
  196. typedef ConstantSizedBufferBuilderOf<SIMDValue> BufferBuilderSIMD;
  197. #else
  198. typedef ConstantSizedBufferBuilderOf<int16> BufferBuilderInt16;
  199. typedef ConstantSizedBufferBuilderOf<int> BufferBuilderInt32;
  200. typedef ConstantSizedBufferBuilderOf<byte> BufferBuilderByte;
  201. typedef ConstantSizedBufferBuilderOf<float> BufferBuilderFloat;
  202. typedef ConstantSizedBufferBuilderOf<double> BufferBuilderDouble;
  203. typedef ConstantSizedBufferBuilderOf<SIMDValue> BufferBuilderSIMD;
  204. #endif
  205. // A buffer builder which contains a list of buffer builders
  206. struct BufferBuilderList : BufferBuilder
  207. {
  208. regex::ImmutableList<BufferBuilder*> * list;
  209. BufferBuilderList(LPCWSTR clue)
  210. : BufferBuilder(clue), list(nullptr)
  211. { }
  212. uint32 FixOffset(uint32 offset) override
  213. {
  214. this->offset = offset;
  215. return list->Accumulate(offset,[](uint32 size, BufferBuilder * builder)->uint32 {
  216. return builder->FixOffset(size);
  217. });
  218. }
  219. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  220. {
  221. return list->Iterate([&](BufferBuilder * builder) {
  222. builder->Write(buffer, bufferSize);
  223. });
  224. }
  225. };
  226. // A buffer builder which points to another buffer builder.
  227. // At write time, it will write the offset from the start of the raw buffer to
  228. // the pointed-to location.
  229. struct BufferBuilderRelativeOffset : BufferBuilder
  230. {
  231. BufferBuilder * pointsTo;
  232. uint32 additionalOffset;
  233. BufferBuilderRelativeOffset(LPCWSTR clue, BufferBuilder * pointsTo, uint32 additionalOffset)
  234. : BufferBuilder(clue), pointsTo(pointsTo), additionalOffset(additionalOffset)
  235. { }
  236. BufferBuilderRelativeOffset(LPCWSTR clue, BufferBuilder * pointsTo)
  237. : BufferBuilder(clue), pointsTo(pointsTo), additionalOffset(0)
  238. { }
  239. uint32 FixOffset(uint32 offset) override
  240. {
  241. this->offset = offset;
  242. return this->offset + sizeof(int);
  243. }
  244. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  245. {
  246. if (bufferSize - this->offset<sizeof(int))
  247. {
  248. Throw::FatalInternalError();
  249. }
  250. int offsetOfPointedTo = pointsTo->offset;
  251. *(int*)(buffer + this->offset) = offsetOfPointedTo + additionalOffset;
  252. DebugOnly(TraceOutput(buffer, sizeof(int)));
  253. }
  254. };
  255. // A buffer builder which holds a raw byte buffer
  256. struct BufferBuilderRaw : BufferBuilder
  257. {
  258. uint32 size;
  259. const byte * raw;
  260. BufferBuilderRaw(LPCWSTR clue, __in uint32 size, __in_bcount(size) const byte * raw)
  261. : BufferBuilder(clue), size(size), raw(raw)
  262. { }
  263. uint32 FixOffset(uint32 offset) override
  264. {
  265. this->offset = offset;
  266. return this->offset + size;
  267. }
  268. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  269. {
  270. if (bufferSize - this->offset<size)
  271. {
  272. Throw::FatalInternalError();
  273. }
  274. js_memcpy_s(buffer + this->offset, bufferSize-this->offset, raw, size);
  275. DebugOnly(TraceOutput(buffer, size));
  276. }
  277. };
  278. // A buffer builder which aligns its contents to the specified alignment
  279. struct BufferBuilderAligned : BufferBuilder
  280. {
  281. BufferBuilder * content;
  282. uint32 alignment;
  283. uint32 padding;
  284. BufferBuilderAligned(LPCWSTR clue, BufferBuilder * content, uint32 alignment)
  285. : BufferBuilder(clue), content(content), alignment(alignment), padding(0)
  286. { }
  287. uint32 FixOffset(uint32 offset) override
  288. {
  289. this->offset = offset;
  290. // Calculate padding
  291. offset = ::Math::Align(this->offset, this->alignment);
  292. this->padding = offset - this->offset;
  293. Assert(this->padding < this->alignment);
  294. return content->FixOffset(offset);
  295. }
  296. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  297. {
  298. if (bufferSize - this->offset < this->padding)
  299. {
  300. Throw::FatalInternalError();
  301. }
  302. for (uint32 i = 0; i < this->padding; i++)
  303. {
  304. buffer[this->offset + i] = 0;
  305. }
  306. this->content->Write(buffer, bufferSize);
  307. }
  308. };
  309. }