summaryrefslogtreecommitdiff
path: root/src/md/heaps/stringheap.h
blob: e674675f2239c200c5a3c9794beebe36da0b05d5 (plain)
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
// 
// File: StringHeap.h
// 

// 
// Classes code:MetaData::StringHeapRO and code:MetaData::StringHeapRW represent #String heap.
// The #String heap stores null-terminated UTF-8 strings (as defined in CLI ECMA specification). Elements 
// are indexed by code:#StringHeapIndex.
// 
//#StringHeapIndex
// String heap indexes are 0-based. They are stored the same way in the table columns (i.e. there is no 
// 0-based vs. 1-based index difference as in table record indexes code:TableRecordStorage).
// 
// ======================================================================================

#pragma once

#include "external.h"

namespace MetaData
{

// --------------------------------------------------------------------------------------
// 
// This class represents read-only #String heap with all utility methods.
// 
class StringHeapRO
{
    friend class StringHeapRW;
    
private:
    // 
    // Private data
    // 
    
    // The storage of strings.
    StgPoolReadOnly m_StringPool;
    
public:
    // 
    // Initialization
    // 
    
    __checkReturn 
    inline HRESULT Initialize(
        DataBlob sourceData, 
        BOOL     fCopyData)
    {
        _ASSERTE(!fCopyData);
        return m_StringPool.InitOnMemReadOnly((void *)sourceData.GetDataPointer(), sourceData.GetSize());
    }
    
#ifdef FEATURE_PREJIT
    // Can be called multiple times.
    inline void InitializeHotData(
        HotHeap hotHeap)
    {
        m_StringPool.InitHotData(hotHeap);
    }
#endif //FEATURE_PREJIT
    
    inline void Delete()
    {
        return m_StringPool.Uninit();
    }
    
public:
    // 
    // Getters
    // 
    
    __checkReturn 
    inline HRESULT GetString(
                      UINT32  nIndex, 
        __deref_out_z LPCSTR *pszString) const
    {
        return const_cast<StgPoolReadOnly &>(m_StringPool).GetString(
            nIndex, 
            pszString);
    }
    
    // Gets raw size (in bytes) of the represented strings.
    inline UINT32 GetUnalignedSize() const
    {
        return m_StringPool.GetPoolSize();
    }
    
};  // class StringHeapRO

// --------------------------------------------------------------------------------------
// 
// This class represents read-write #String heap with all utility methods.
// 
class StringHeapRW
{
private:
    // 
    // Private data
    // 
    
    // The storage of strings.
    StgStringPool m_StringPool;
    
public:
    // 
    // Initialization
    // 
    
    __checkReturn 
    inline HRESULT InitializeEmpty(
                         UINT32 cbAllocationSize 
        COMMA_INDEBUG_MD(BOOL   debug_fIsReadWrite))
    {
        return m_StringPool.InitNew(cbAllocationSize, 0);
    }
    __checkReturn 
    inline HRESULT InitializeEmpty_WithItemsCount(
                         UINT32 cbAllocationSize, 
                         UINT32 cItemsCount 
        COMMA_INDEBUG_MD(BOOL   debug_fIsReadWrite))
    {
        return m_StringPool.InitNew(cbAllocationSize, cItemsCount);
    }
    __checkReturn 
    inline HRESULT Initialize(
        DataBlob sourceData, 
        BOOL     fCopyData)
    {
        return m_StringPool.InitOnMem((void *)sourceData.GetDataPointer(), sourceData.GetSize(), !fCopyData);
    }
    __checkReturn 
    inline HRESULT InitializeFromStringHeap(
        const StringHeapRO *pSourceStringHeap, 
        BOOL                fCopyData)
    {
        return m_StringPool.InitOnMem(
            (void *)pSourceStringHeap->m_StringPool.GetSegData(), 
            pSourceStringHeap->m_StringPool.GetDataSize(), 
            !fCopyData);
    }
    __checkReturn 
    inline HRESULT InitializeFromStringHeap(
        const StringHeapRW *pSourceStringHeap, 
        BOOL                fCopyData)
    {
        return m_StringPool.InitOnMem(
            (void *)pSourceStringHeap->m_StringPool.GetSegData(), 
            pSourceStringHeap->m_StringPool.GetDataSize(), 
            !fCopyData);
    }
    
    // Destroys the string heap and all its allocated data. Can run on uninitialized string heap.
    inline void Delete()
    {
        return m_StringPool.Uninit();
    }
    
public:
    // 
    // Getters
    // 
    
    __checkReturn 
    inline HRESULT GetString(
                      UINT32  nIndex, 
        __deref_out_z LPCSTR *pszString) const
    {
        return const_cast<StgStringPool &>(m_StringPool).GetString(
            nIndex, 
            pszString);
    }
    
    // Gets raw size (in bytes) of the represented strings. Doesn't align the size as code:GetAlignedSize.
    inline UINT32 GetUnalignedSize() const
    {
        return m_StringPool.GetRawSize();
    }
    // Gets size (in bytes) aligned up to 4-bytes of the represented strings.
    // Fills *pcbSize with 0 on error.
    __checkReturn 
    inline HRESULT GetAlignedSize(
        __out UINT32 *pcbSize) const
    {
        return m_StringPool.GetSaveSize(pcbSize);
    }
    // Returns TRUE if the string heap is empty (even if it contains only default empty string).
    inline BOOL IsEmpty() const
    {
        return const_cast<StgStringPool &>(m_StringPool).IsEmpty();
    }
    
    // Returns TRUE if the string index (nIndex, see code:#StringHeapIndex) is valid (i.e. in the string 
    // heap).
    inline BOOL IsValidIndex(UINT32 nIndex) const
    {
        return const_cast<StgStringPool &>(m_StringPool).IsValidCookie(nIndex);
    }
    
    __checkReturn 
    inline HRESULT SaveToStream_Aligned(
             UINT32   nStartIndex, 
        __in IStream *pStream) const
    {
        if (nStartIndex == 0)
        {
            return const_cast<StgStringPool &>(m_StringPool).PersistToStream(pStream);
        }
        
        if (nStartIndex == m_StringPool.GetRawSize())
        {
            _ASSERTE(!m_StringPool.HaveEdits());
            return S_OK;
        }
        _ASSERTE(m_StringPool.HaveEdits());
        _ASSERTE(nStartIndex == m_StringPool.GetOffsetOfEdit());
        return const_cast<StgStringPool &>(m_StringPool).PersistPartialToStream(pStream, nStartIndex);
    }
    
public:
    // 
    // Heap modifications
    // 
    
    // Adds null-terminated UTF-8 string (szString) to the end of the heap (incl. its null-terminator).
    // Returns S_OK and index of added string (*pnIndex).
    // Returns error code otherwise (and fills *pnIndex with 0).
    __checkReturn 
    inline HRESULT AddString(
        __in_z LPCSTR  szString, 
        __out  UINT32 *pnIndex)
    {
        return m_StringPool.AddString(szString, pnIndex);
    }
    // Adds null-terminated UTF-16 string (wszString) to the end of the heap (incl. its null-terminator).
    // Returns S_OK and index of added string (*pnIndex).
    // Returns error code otherwise (and fills *pnIndex with 0).
    __checkReturn 
    inline HRESULT AddStringW(
        __in_z LPCWSTR wszString, 
        __out  UINT32 *pnIndex)
    {
        return m_StringPool.AddStringW(wszString, pnIndex);
    }
    
    // Adds data from *pSourceStringHeap starting at index (nStartSourceIndex) to the string heap.
    // Returns S_OK (even if the source is empty) or error code.
    __checkReturn 
    inline HRESULT AddStringHeap(
        const StringHeapRW *pSourceStringHeap, 
        UINT32              nStartSourceIndex)
    {
        return m_StringPool.CopyPool(
            nStartSourceIndex, 
            &pSourceStringHeap->m_StringPool);
    } // StringHeapRW::AddStringHeap
    
    __checkReturn 
    inline HRESULT MakeWritable()
    {
        return m_StringPool.ConvertToRW();
    }
    
public:
    // 
    // Tracking of heap modifications for EnC
    // 
    
    //#EnCSessionTracking
    // EnC session starts automatically with initialization (code:Initialize or code:InitializeEmpty) or by 
    // user's explicit call to code:StartNewEnCSession. The heap stores its actual data size, so we can find 
    // out if some data were added later.
    
    // Gets heap size (in bytes) from the beginning of the last EnC session (code:#EnCSessionTracking).
    inline UINT32 GetEnCSessionStartHeapSize() const
    {
        if (m_StringPool.HaveEdits())
        {
            return m_StringPool.GetOffsetOfEdit();
        }
        
        return m_StringPool.GetRawSize();
    }
    // Starts new EnC session (code:#EnCSessionTracking).
    inline void StartNewEnCSession()
    {
        m_StringPool.ResetOffsetOfEdit();
    }
    // Gets size (in bytes) aligned to 4-bytes of adds made from the beginning of the last EnC session.
    __checkReturn 
    inline HRESULT GetEnCSessionAddedHeapSize_Aligned(
        __out UINT32 *pcbSize) const
    {
        if (m_StringPool.HaveEdits())
        {
            return m_StringPool.GetEditSaveSize(pcbSize);
        }
        
        *pcbSize = 0;
        return S_OK;
    }
    
};  // class StringHeapRW

};  // namespace MetaData