PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Common/DataStructures/BufferBuilder.h

https://gitlab.com/Srijancse/ChakraCore
C Header | 357 lines | 287 code | 49 blank | 21 comment | 26 complexity | 80bd2b488e59e5f5737c699e0b6fff02 MD5 | raw file
  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))
  128. {
  129. Throw::FatalInternalError();
  130. }
  131. DebugOnly(size = sizeof(uint16));
  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))
  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. #if INSTRUMENT_BUFFER_INTS
  144. printf("[BCGENSTATS] %d, %d\n", value, sizeof(T));
  145. #endif
  146. }
  147. }
  148. else
  149. {
  150. if (bufferSize - this->offset<sizeof(serialization_alignment T))
  151. {
  152. Throw::FatalInternalError();
  153. }
  154. *(serialization_alignment T*)(buffer + this->offset) = value;
  155. }
  156. DebugOnly(TraceOutput(buffer, size));
  157. }
  158. };
  159. template<typename T>
  160. struct BufferBuilderOf<T, false>: BufferBuilder
  161. {
  162. typedef serialization_alignment T value_type;
  163. value_type value;
  164. BufferBuilderOf(LPCWSTR clue, const T & value)
  165. : BufferBuilder(clue), value(value)
  166. { }
  167. uint32 FixOffset(uint32 offset) override
  168. {
  169. this->offset = offset;
  170. return this->offset + sizeof(serialization_alignment T);
  171. }
  172. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  173. {
  174. if (bufferSize - this->offset<sizeof(serialization_alignment T))
  175. {
  176. Throw::FatalInternalError();
  177. }
  178. *(serialization_alignment T*)(buffer + this->offset) = value;
  179. DebugOnly(TraceOutput(buffer, sizeof(T)));
  180. }
  181. };
  182. template <typename T>
  183. struct ConstantSizedBufferBuilderOf : BufferBuilderOf<T, false>
  184. {
  185. ConstantSizedBufferBuilderOf(LPCWSTR clue, const T & value)
  186. : BufferBuilderOf(clue, value)
  187. { }
  188. };
  189. #if VARIABLE_INT_ENCODING
  190. typedef BufferBuilderOf<int16, true> BufferBuilderInt16;
  191. typedef BufferBuilderOf<int, true> BufferBuilderInt32;
  192. typedef ConstantSizedBufferBuilderOf<byte> BufferBuilderByte;
  193. typedef ConstantSizedBufferBuilderOf<float> BufferBuilderFloat;
  194. typedef ConstantSizedBufferBuilderOf<double> BufferBuilderDouble;
  195. typedef ConstantSizedBufferBuilderOf<SIMDValue> BufferBuilderSIMD;
  196. #else
  197. typedef ConstantSizedBufferBuilderOf<int16> BufferBuilderInt16;
  198. typedef ConstantSizedBufferBuilderOf<int> BufferBuilderInt32;
  199. typedef ConstantSizedBufferBuilderOf<byte> BufferBuilderByte;
  200. typedef ConstantSizedBufferBuilderOf<float> BufferBuilderFloat;
  201. typedef ConstantSizedBufferBuilderOf<double> BufferBuilderDouble;
  202. typedef ConstantSizedBufferBuilderOf<SIMDValue> BufferBuilderSIMD;
  203. #endif
  204. // A buffer builder which contains a list of buffer builders
  205. struct BufferBuilderList : BufferBuilder
  206. {
  207. regex::ImmutableList<BufferBuilder*> * list;
  208. BufferBuilderList(LPCWSTR clue)
  209. : BufferBuilder(clue), list(nullptr)
  210. { }
  211. uint32 FixOffset(uint32 offset) override
  212. {
  213. this->offset = offset;
  214. return list->Accumulate(offset,[](uint32 size, BufferBuilder * builder)->uint32 {
  215. return builder->FixOffset(size);
  216. });
  217. }
  218. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  219. {
  220. return list->Iterate([&](BufferBuilder * builder) {
  221. builder->Write(buffer, bufferSize);
  222. });
  223. }
  224. };
  225. // A buffer builder which points to another buffer builder.
  226. // At write time, it will write the offset from the start of the raw buffer to
  227. // the pointed-to location.
  228. struct BufferBuilderRelativeOffset : BufferBuilder
  229. {
  230. BufferBuilder * pointsTo;
  231. uint32 additionalOffset;
  232. BufferBuilderRelativeOffset(LPCWSTR clue, BufferBuilder * pointsTo, uint32 additionalOffset)
  233. : BufferBuilder(clue), pointsTo(pointsTo), additionalOffset(additionalOffset)
  234. { }
  235. BufferBuilderRelativeOffset(LPCWSTR clue, BufferBuilder * pointsTo)
  236. : BufferBuilder(clue), pointsTo(pointsTo), additionalOffset(0)
  237. { }
  238. uint32 FixOffset(uint32 offset) override
  239. {
  240. this->offset = offset;
  241. return this->offset + sizeof(int);
  242. }
  243. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  244. {
  245. if (bufferSize - this->offset<sizeof(int))
  246. {
  247. Throw::FatalInternalError();
  248. }
  249. int offsetOfPointedTo = pointsTo->offset;
  250. *(int*)(buffer + this->offset) = offsetOfPointedTo + additionalOffset;
  251. DebugOnly(TraceOutput(buffer, sizeof(int)));
  252. }
  253. };
  254. // A buffer builder which holds a raw byte buffer
  255. struct BufferBuilderRaw : BufferBuilder
  256. {
  257. uint32 size;
  258. const byte * raw;
  259. BufferBuilderRaw(LPCWSTR clue, __in uint32 size, __in_bcount(size) const byte * raw)
  260. : BufferBuilder(clue), size(size), raw(raw)
  261. { }
  262. uint32 FixOffset(uint32 offset) override
  263. {
  264. this->offset = offset;
  265. return this->offset + size;
  266. }
  267. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  268. {
  269. if (bufferSize - this->offset<size)
  270. {
  271. Throw::FatalInternalError();
  272. }
  273. js_memcpy_s(buffer + this->offset, bufferSize-this->offset, raw, size);
  274. DebugOnly(TraceOutput(buffer, size));
  275. }
  276. };
  277. // A buffer builder which aligns its contents to the specified alignment
  278. struct BufferBuilderAligned : BufferBuilder
  279. {
  280. BufferBuilder * content;
  281. uint32 alignment;
  282. uint32 padding;
  283. BufferBuilderAligned(LPCWSTR clue, BufferBuilder * content, uint32 alignment)
  284. : BufferBuilder(clue), content(content), alignment(alignment), padding(0)
  285. { }
  286. uint32 FixOffset(uint32 offset) override
  287. {
  288. this->offset = offset;
  289. // Calculate padding
  290. offset = ::Math::Align(this->offset, this->alignment);
  291. this->padding = offset - this->offset;
  292. Assert(this->padding < this->alignment);
  293. return content->FixOffset(offset);
  294. }
  295. void Write(__in_bcount(bufferSize) byte * buffer, __in uint32 bufferSize) const
  296. {
  297. if (bufferSize - this->offset < this->padding)
  298. {
  299. Throw::FatalInternalError();
  300. }
  301. for (uint32 i = 0; i < this->padding; i++)
  302. {
  303. buffer[this->offset + i] = 0;
  304. }
  305. this->content->Write(buffer, bufferSize);
  306. }
  307. };
  308. }