GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — node/src/blob_serializer_deserializer-inl.h at main · bezuglov/node Expand file tree/ Copy pathblob_serializer_deserializer-inl.hMore file actions 385 lines (327 loc) · 11.2 KB / Copy pathblob_serializer_deserializer-inl.hFile metadata and controls385 lines (327 loc) · 11.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 #ifndef SRC_BLOB_SERIALIZER_DESERIALIZER_INL_H_ #define SRC_BLOB_SERIALIZER_DESERIALIZER_INL_H_ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #include "blob_serializer_deserializer.h" #include <ostream> #include <sstream> #include <string> #include <type_traits> #include <utility> #include "debug_utils-inl.h" // This is related to the blob that is used in snapshots and single executable // applications and has nothing to do with `node_blob.h`. namespace node { struct EnvSerializeInfo; struct PropInfo; struct RealmSerializeInfo; namespace builtins { struct CodeCacheInfo; } // namespace builtins // These operator<< overload declarations are needed because // BlobSerializerDeserializer::ToStr() uses these. std::ostream& operator<<(std::ostream& output, const builtins::CodeCacheInfo& info); std::ostream& operator<<(std::ostream& output, const std::vector<builtins::CodeCacheInfo>& vec); std::ostream& operator<<(std::ostream& output, const std::vector<uint8_t>& vec); std::ostream& operator<<(std::ostream& output, const std::vector<PropInfo>& vec); std::ostream& operator<<(std::ostream& output, const PropInfo& info); std::ostream& operator<<(std::ostream& output, const std::vector<std::string>& vec); std::ostream& operator<<(std::ostream& output, const RealmSerializeInfo& i); std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& i); template <typename... Args> void BlobSerializerDeserializer::Debug(const char* format, Args&&... args) const { if (is_debug) { FPrintF(stderr, format, std::forward<Args>(args)...); } } template <typename T> std::string BlobSerializerDeserializer::ToStr(const T& arg) const { std::stringstream ss; ss << arg; return ss.str(); } template <typename T> std::string BlobSerializerDeserializer::GetName() const { #define TYPE_LIST(V) \ V(builtins::CodeCacheInfo) \ V(PropInfo) \ V(std::string) #define V(TypeName) \ if constexpr (std::is_same_v<T, TypeName>) { \ return #TypeName; \ } else // NOLINT(readability/braces) TYPE_LIST(V) #undef V if constexpr (std::is_arithmetic_v<T>) { return (std::is_unsigned_v<T> ? "uint" : std::is_integral_v<T> ? "int" : "float") + std::to_string(sizeof(T) * 8) + "_t"; } return ""; } // Helper for reading numeric types. template <typename Impl> template <typename T> T BlobDeserializer<Impl>::ReadArithmetic() { static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type"); T result; ReadArithmetic(&result, 1); return result; } // Layout of vectors: // [ 4/8 bytes ] count // [ ... ] contents (count * size of individual elements) template <typename Impl> template <typename T> std::vector<T> BlobDeserializer<Impl>::ReadVector() { if (is_debug) { std::string name = GetName<T>(); Debug("\nReadVector<%s>()(%d-byte)\n", name, sizeof(T)); } size_t count = static_cast<size_t>(ReadArithmetic<size_t>()); if (count == 0) { return std::vector<T>(); } if (is_debug) { Debug("Reading %d vector elements...\n", count); } std::vector<T> result; if constexpr (std::is_arithmetic_v<T>) { result = ReadArithmeticVector<T>(count); } else { result = ReadNonArithmeticVector<T>(count); } if (is_debug) { std::string str = std::is_arithmetic_v<T> ? "" : ToStr(result); std::string name = GetName<T>(); Debug("ReadVector<%s>() read %s\n", name, str); } return result; } template <typename Impl> std::string BlobDeserializer<Impl>::ReadString() { std::string_view view = ReadStringView(StringLogMode::kAddressAndContent); return std::string(view); } template <typename Impl> std::string_view BlobDeserializer<Impl>::ReadStringView(StringLogMode mode) { size_t length = ReadArithmetic<size_t>(); Debug("ReadStringView(), length=%zu: ", length); if (length == 0) { Debug("ReadStringView() read an empty view\n"); return std::string_view(); } std::string_view result(sink.data() + read_total, length); Debug("%p, read %zu bytes", result.data(), result.size()); if (mode == StringLogMode::kAddressAndContent) { Debug(", content:%s%s", length > 32 ? "\n" : " ", result); } Debug("\n"); read_total += length; return result; } // Helper for reading an array of numeric types. template <typename Impl> template <typename T> void BlobDeserializer<Impl>::ReadArithmetic(T* out, size_t count) { static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type"); DCHECK_GT(count, 0); // Should not read contents for vectors of size 0. if (is_debug) { std::string name = GetName<T>(); Debug("Read<%s>()(%d-byte), count=%d: ", name, sizeof(T), count); } size_t size = sizeof(T) * count; memcpy(out, sink.data() + read_total, size); if (is_debug) { std::string str = "{ " + std::to_string(out[0]) + (count > 1 ? ", ... }" : " }"); Debug("%s, read %zu bytes\n", str, size); } read_total += size; } // Helper for reading numeric vectors. template <typename Impl> template <typename Number> std::vector<Number> BlobDeserializer<Impl>::ReadArithmeticVector(size_t count) { static_assert(std::is_arithmetic_v<Number>, "Not an arithmetic type"); DCHECK_GT(count, 0); // Should not read contents for vectors of size 0. std::vector<Number> result(count); ReadArithmetic(result.data(), count); return result; } // Helper for reading non-numeric vectors. template <typename Impl> template <typename T> std::vector<T> BlobDeserializer<Impl>::ReadNonArithmeticVector(size_t count) { static_assert(!std::is_arithmetic_v<T>, "Arithmetic type"); DCHECK_GT(count, 0); // Should not read contents for vectors of size 0. std::vector<T> result; result.reserve(count); bool original_is_debug = is_debug; is_debug = original_is_debug && !std::is_same_v<T, std::string>; for (size_t i = 0; i < count; ++i) { if (is_debug) { Debug("\n[%d] ", i); } result.push_back(ReadElement<T>()); } is_debug = original_is_debug; return result; } template <typename Impl> template <typename T> T BlobDeserializer<Impl>::ReadElement() { if constexpr (std::is_arithmetic_v<T>) { return ReadArithmetic<T>(); } else if constexpr (std::is_same_v<T, std::string>) { return ReadString(); } else { return impl()->template Read<T>(); } } // Helper for writing numeric types. template <typename Impl> template <typename T> size_t BlobSerializer<Impl>::WriteArithmetic(const T& data) { static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type"); return WriteArithmetic(&data, 1); } // Layout of vectors: // [ 4/8 bytes ] count // [ ... ] contents (count * size of individual elements) template <typename Impl> template <typename T> size_t BlobSerializer<Impl>::WriteVector(const std::vector<T>& data) { if (is_debug) { std::string str = std::is_arithmetic_v<T> ? "" : ToStr(data); std::string name = GetName<T>(); Debug("\nAt 0x%x: WriteVector<%s>() (%d-byte), count=%d: %s\n", sink.size(), name, sizeof(T), data.size(), str); } size_t written_total = WriteArithmetic<size_t>(data.size()); if (data.empty()) { return written_total; } if constexpr (std::is_arithmetic_v<T>) { written_total += WriteArithmeticVector<T>(data); } else { written_total += WriteNonArithmeticVector<T>(data); } if (is_debug) { std::string name = GetName<T>(); Debug("WriteVector<%s>() wrote %d bytes\n", name, written_total); } return written_total; } // The layout of a written string: // [ 4/8 bytes ] length // [ |length| bytes ] contents template <typename Impl> size_t BlobSerializer<Impl>::WriteStringView(std::string_view data, StringLogMode mode) { Debug("At 0x%x: WriteStringView(), length=%zu: %p\n", sink.size(), data.size(), data.data()); size_t written_total = WriteArithmetic<size_t>(data.size()); size_t length = data.size(); if (length == 0) { Debug("WriteStringView() wrote an empty view\n"); return written_total; } sink.insert(sink.end(), data.data(), data.data() + length); written_total += length; Debug("WriteStringView() wrote %zu bytes\n", written_total); if (mode == StringLogMode::kAddressAndContent) { Debug("%s", data); } return written_total; } template <typename Impl> size_t BlobSerializer<Impl>::WriteString(const std::string& data) { return WriteStringView(data, StringLogMode::kAddressAndContent); } static size_t kPreviewCount = 16; // Helper for writing an array of numeric types. template <typename Impl> template <typename T> size_t BlobSerializer<Impl>::WriteArithmetic(const T* data, size_t count) { static_assert(std::is_arithmetic_v<T>, "Arithmetic type"); DCHECK_GT(count, 0); // Should not write contents for vectors of size 0. if (is_debug) { size_t preview_count = count < kPreviewCount ? count : kPreviewCount; std::string str = "{ "; for (size_t i = 0; i < preview_count; ++i) { str += (std::to_string(data[i]) + ","); } if (count > preview_count) { str += "..."; } str += "}"; std::string name = GetName<T>(); Debug("At 0x%x: Write<%s>() (%zu-byte), count=%zu: %s", sink.size(), name, sizeof(T), count, str); } size_t size = sizeof(T) * count; const char* pos = reinterpret_cast<const char*>(data); sink.insert(sink.end(), pos, pos + size); if (is_debug) { Debug(", wrote %zu bytes\n", size); } return size; } // Helper for writing numeric vectors. template <typename Impl> template <typename Number> size_t BlobSerializer<Impl>::WriteArithmeticVector( const std::vector<Number>& data) { static_assert(std::is_arithmetic_v<Number>, "Arithmetic type"); return WriteArithmetic(data.data(), data.size()); } // Helper for writing non-numeric vectors. template <typename Impl> template <typename T> size_t BlobSerializer<Impl>::WriteNonArithmeticVector( const std::vector<T>& data) { static_assert(!std::is_arithmetic_v<T>, "Arithmetic type"); DCHECK_GT(data.size(), 0); // Should not write contents for vectors of size 0. size_t written_total = 0; bool original_is_debug = is_debug; is_debug = original_is_debug && !std::is_same_v<T, std::string>; for (size_t i = 0; i < data.size(); ++i) { if (is_debug) { Debug("\n[%d] ", i); } written_total += WriteElement<T>(data[i]); } is_debug = original_is_debug; return written_total; } template <typename Impl> template <typename T> size_t BlobSerializer<Impl>::WriteElement(const T& data) { if constexpr (std::is_arithmetic_v<T>) { return WriteArithmetic<T>(data); } else if constexpr (std::is_same_v<T, std::string>) { return WriteString(data); } else { return impl()->template Write<T>(data); } } } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS #endif // SRC_BLOB_SERIALIZER_DESERIALIZER_INL_H_ |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |