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
15class FastSerializer;
16
17typedef unsigned int StreamLabel;
18
19// the enumeration has a specific set of values to keep it compatible with consumer library
20// it's sibling is defined in https://github.com/Microsoft/perfview/blob/10d1f92b242c98073b3817ac5ee6d98cd595d39b/src/FastSerialization/FastSerialization.cs#L2295
21enum class FastSerializerTags : BYTE
22{
23 Error = 0, // To improve debugabilty, 0 is an illegal tag.
24 NullReference = 1, // Tag for a null object forwardReference.
25 ObjectReference = 2, // Followed by StreamLabel
26 // 3 used to belong to ForwardReference, which got removed in V3
27 BeginObject = 4, // Followed by Type object, object data, tagged EndObject
28 BeginPrivateObject = 5, // Like beginObject, but not placed in interning table on deserialiation
29 EndObject = 6, // Placed after an object to mark its end.
30 // 7 used to belong to ForwardDefinition, which got removed in V3
31 Byte = 8,
32 Int16,
33 Int32,
34 Int64,
35 SkipRegion,
36 String,
37 Blob,
38 Limit // Just past the last valid tag, used for asserts.
39};
40
41class FastSerializer
42{
43public:
44
45 FastSerializer(SString &outputFilePath);
46 ~FastSerializer();
47
48 StreamLabel GetStreamLabel() const;
49
50 void WriteObject(FastSerializableObject *pObject);
51 void WriteBuffer(BYTE *pBuffer, unsigned int length);
52 void WriteTag(FastSerializerTags tag, BYTE *payload = NULL, unsigned int payloadLength = 0);
53 void WriteString(const char *strContents, unsigned int length);
54
55 size_t GetCurrentPosition() const
56 {
57 LIMITED_METHOD_CONTRACT;
58
59 return m_currentPos;
60 }
61
62private:
63
64 void WriteSerializationType(FastSerializableObject *pObject);
65 void WriteFileHeader();
66
67 CFileStream *m_pFileStream;
68 bool m_writeErrorEncountered;
69 size_t m_currentPos;
70};
71
72#endif // FEATURE_PERFTRACING
73
74#endif // __FASTSERIALIZER_H__
75