2c669f0a2520e88807fdbfc04ca5d554be7e4923
[platform/upstream/coreclr.git] / src / vm / fastserializer.h
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.
4
5 #ifndef __FASTSERIALIZER_H__
6 #define __FASTSERIALIZER_H__
7
8 #define ALIGNMENT_SIZE 4
9
10 #ifdef FEATURE_PERFTRACING
11
12 #include "fastserializableobject.h"
13 #include "fstream.h"
14
15 class IpcStream;
16
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
20 {
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
29     Byte               = 8,
30     Int16,
31     Int32,
32     Int64,
33     SkipRegion,
34     String,
35     Blob,
36     Limit                   // Just past the last valid tag, used for asserts.
37 };
38
39 //!
40 //! Provides a generic interface for writing a sequence of bytes to a stream.
41 //!
42 class StreamWriter
43 {
44 public:
45     StreamWriter() = default;
46     virtual ~StreamWriter() = default;
47     virtual bool Write(const void *lpBuffer, const uint32_t nBytesToWrite, uint32_t &nBytesWritten) const = 0;
48 };
49
50 //!
51 //! Implements a StreamWriter for writing bytes to an IPC.
52 //!
53 class IpcStreamWriter final : public StreamWriter
54 {
55 public:
56     IpcStreamWriter(uint64_t id, IpcStream *pStream);
57     ~IpcStreamWriter();
58     bool Write(const void *lpBuffer, const uint32_t nBytesToWrite, uint32_t &nBytesWritten) const;
59
60 private:
61     IpcStream *_pStream;
62 };
63
64 //!
65 //! Implements a StreamWriter for writing bytes to an File.
66 //!
67 class FileStreamWriter final : public StreamWriter
68 {
69 public:
70     FileStreamWriter(const SString &outputFilePath);
71     ~FileStreamWriter();
72     bool Write(const void *lpBuffer, const uint32_t nBytesToWrite, uint32_t &nBytesWritten) const;
73
74 private:
75     CFileStream *m_pFileStream;
76 };
77
78 class FastSerializer
79 {
80 public:
81     FastSerializer(StreamWriter *pStreamWriter);
82     ~FastSerializer();
83
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);
88
89     size_t GetCurrentPosition() const
90     {
91         LIMITED_METHOD_CONTRACT;
92         return m_currentPos;
93     }
94
95     bool HasWriteErrors() const
96     {
97         LIMITED_METHOD_CONTRACT;
98         return m_writeErrorEncountered;
99     }
100
101 private:
102     void WriteSerializationType(FastSerializableObject *pObject);
103     void WriteFileHeader();
104
105     StreamWriter *const m_pStreamWriter;
106     bool m_writeErrorEncountered;
107     size_t m_currentPos;
108 };
109
110 #endif // FEATURE_PERFTRACING
111
112 #endif // __FASTSERIALIZER_H__