1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 #ifndef __FASTSERIALIZER_H__
6 #define __FASTSERIALIZER_H__
8 #define ALIGNMENT_SIZE 4
10 #ifdef FEATURE_PERFTRACING
12 #include "fastserializableobject.h"
17 // the enumeration has a specific set of values to keep it compatible with consumer library
18 // it's sibling is defined in https://github.com/Microsoft/perfview/blob/10d1f92b242c98073b3817ac5ee6d98cd595d39b/src/FastSerialization/FastSerialization.cs#L2295
19 enum class FastSerializerTags : uint8_t
21 Error = 0, // To improve debugabilty, 0 is an illegal tag.
22 NullReference = 1, // Tag for a null object forwardReference.
23 ObjectReference = 2, // Followed by StreamLabel
24 // 3 used to belong to ForwardReference, which got removed in V3
25 BeginObject = 4, // Followed by Type object, object data, tagged EndObject
26 BeginPrivateObject = 5, // Like beginObject, but not placed in interning table on deserialiation
27 EndObject = 6, // Placed after an object to mark its end.
28 // 7 used to belong to ForwardDefinition, which got removed in V3
36 Limit // Just past the last valid tag, used for asserts.
40 //! Provides a generic interface for writing a sequence of bytes to a stream.
45 StreamWriter() = default;
46 virtual ~StreamWriter() = default;
47 virtual bool Write(const void *lpBuffer, const uint32_t nBytesToWrite, uint32_t &nBytesWritten) const = 0;
51 //! Implements a StreamWriter for writing bytes to an IPC.
53 class IpcStreamWriter final : public StreamWriter
56 IpcStreamWriter(uint64_t id, IpcStream *pStream);
58 bool Write(const void *lpBuffer, const uint32_t nBytesToWrite, uint32_t &nBytesWritten) const;
65 //! Implements a StreamWriter for writing bytes to a File.
67 class FileStreamWriter final : public StreamWriter
70 FileStreamWriter(const SString &outputFilePath);
72 bool Write(const void *lpBuffer, const uint32_t nBytesToWrite, uint32_t &nBytesWritten) const;
75 CFileStream *m_pFileStream;
81 FastSerializer(StreamWriter *pStreamWriter);
84 void WriteObject(FastSerializableObject *pObject);
85 void WriteBuffer(BYTE *pBuffer, unsigned int length);
86 void WriteTag(FastSerializerTags tag, BYTE *payload = NULL, unsigned int payloadLength = 0);
87 void WriteString(const char *strContents, unsigned int length);
89 size_t GetCurrentPosition() const
91 LIMITED_METHOD_CONTRACT;
95 bool HasWriteErrors() const
97 LIMITED_METHOD_CONTRACT;
98 return m_writeErrorEncountered;
102 void WriteSerializationType(FastSerializableObject *pObject);
103 void WriteFileHeader();
105 StreamWriter *const m_pStreamWriter;
106 bool m_writeErrorEncountered;
110 #endif // FEATURE_PERFTRACING
112 #endif // __FASTSERIALIZER_H__