Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StringPool.cs « Shared « src « System.Private.Interop « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eea2db4a6661da96ab351a675417bc5983eb7cb1 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;

namespace System.Runtime.InteropServices
{
    /// <summary>
    /// Commpressed collection of strings represented by two byte arrays + one ushort array
    ///    m_typeNamespaces are shared substrings, limited to 128 strings, each represented by an index. Currently only namespaces are stored. E.g. "System.Runtime."
    ///    m_typeNames are compressed strings, each represented by an index. Within m_typeNames, [0x80 .. 0xFF] bytes are used to represented shared sub strings.
    ///    m_indices array maps index to start position
    ///
    /// All strings are zero-terminated. Tail reusing is possible, but not implemented by MCG.
    /// If a string in m_typeNames start with 0x01, it's stored as complete UNICODE string (0x01 + two bytes for each char + two 0 bytes). lsb byte goes first.
    ///
    /// Functions:
    ///   1. GetString converts compressed string represented by an index to original System.String
    ///   2. StableStringHash computes hash code without decoding to System.String
    ///   3. IsStringEqual compares compressed string represented by an index with System.String
    ///
    /// TODO:
    ///   1. More string reuse
    /// </summary>
    internal class StringPool
    {
        byte[] m_typeNamespaces;       // Big byte array of all class/interface namespaces
        byte[] m_typeNames;            // Big byte array of all class/interface/value-type names
        UInt16[] m_indices;              // Map from >=0x80 bytes to first char in m_typeNamespaces array

        internal StringPool(
            byte[] typeNamespaces,
            byte[] typeNames,
            UInt16[] indices)
        {
            m_typeNamespaces = typeNamespaces;
            m_typeNames = typeNames;
            m_indices = indices;
        }

        internal const byte Escape_Start = 0x80;
        internal const byte Unicode_Mark = 0x01;    // If first byte is 0x01, whole string is UNICODE: two bytes for each char + two 0 bytes

        /// <summary>
        /// Convert string represented by an index back to original form, by expanding Unicode private use area characters to namespace names
        /// </summary>
        internal unsafe string GetString(UInt32 nameIdx)
        {
            Debug.Assert((nameIdx >= 0) && (nameIdx < m_typeNames.Length));

            fixed (byte* pNs = m_typeNamespaces)
            fixed (byte* pN = m_typeNames)
            {
                int len = 0;

                bool unicode = false;

                if (pN[nameIdx] == Unicode_Mark) // Check for UNICODE mark
                {
                    unicode = true;
                    nameIdx++;
                }

                // Calculate final string length
                for (byte* p = pN + nameIdx; ; p++)
                {
                    int c = *p;

                    if (unicode)
                    {
                        c |= (*(++p)) << 8; // read the 2nd byte, forming a completer UTF16 char
                    }

                    if (c == 0)
                        break;

                    if (!unicode && (c >= Escape_Start)) // If not UNICODE mode, for char in [0x80 .. 0xFF] range read substring from m_typeNamespace array
                    {
                        int namespaceIndex = m_indices[c - Escape_Start];

                        Debug.Assert((namespaceIndex >= 0) && (namespaceIndex < m_typeNamespaces.Length));

                        for (byte* q = pNs + namespaceIndex; *q != 0; q++)
                        {
                            len++;
                        }
                    }
                    else
                    {
                        len++;
                    }
                }

                // Allocate string, TODO make FastAllocString accessible
                string result = new string(' ', len);

                // Fill characters
                fixed (char* pResult = result)
                {
                    char* pDest = pResult;

                    for (byte* p = pN + nameIdx; ; p++)
                    {
                        int c = *p;

                        if (unicode)
                        {
                            c |= (*(++p)) << 8;
                        }

                        if (c == 0)
                            break;

                        if (!unicode && (c >= Escape_Start))
                        {
                            int namespaceIndex = m_indices[c - Escape_Start];

                            Debug.Assert((namespaceIndex >= 0) && (namespaceIndex < m_typeNamespaces.Length));

                            for (byte* q = pNs + namespaceIndex; *q != 0; q++)
                            {
                                *pDest++ = (char)*q;
                                len++;
                            }
                        }
                        else
                        {
                            *pDest++ = (char)c;
                        }
                    }
                }

                return result;
            }
        }

        const int Hash_Init = 5381;

        /// <summary>
        /// Should be inlined
        /// </summary>
        internal static int HashAccumulate(int hash, char val)
        {
            return ((hash << 5) + hash) ^ val;
        }

        /// <summary>
        /// This version needs to be the same as in StableStringHash(int nameIdx)
        /// </summary>
        internal static int StableStringHash(string str)
        {
            int hash = Hash_Init;

            if (str != null)
            {
                hash = HashAccumulate(hash, (char)0); // Make null and "" different

                for (int i = 0; i < str.Length; i++)
                {
                    hash = HashAccumulate(hash, str[i]);
                }
            }

            return hash & 0x7FFFFFFF;
        }

        /// <summary>
        /// Compute hash code same as StableStringHash(System.String)
        /// </summary>
        /// <param name="nameIdx"></param>
        /// <returns></returns>
        internal unsafe int StableStringHash(UInt32 nameIdx)
        {
            int hash = Hash_Init;

            fixed (byte* pNs = m_typeNamespaces)
            fixed (byte* pN = m_typeNames)
            {
                hash = HashAccumulate(hash, (char)0);

                bool unicode = false;

                if (pN[nameIdx] == Unicode_Mark)
                {
                    unicode = true;
                    nameIdx++;
                }

                for (byte* p = pN + nameIdx; ; p++)
                {
                    int c = *p;

                    if (unicode)
                    {
                        c |= (*(++p)) << 8;
                    }

                    if (c == 0)
                    {
                        break;
                    }

                    if (!unicode && c >= Escape_Start)
                    {
                        int namespaceIndex = m_indices[c - Escape_Start];

                        Debug.Assert((namespaceIndex >= 0) && (namespaceIndex < m_typeNamespaces.Length));

                        for (byte* q = pNs + namespaceIndex; *q != 0; q++)
                        {
                            hash = HashAccumulate(hash, (char)*q);
                        }
                    }
                    else
                    {
                        hash = HashAccumulate(hash, (char)c);
                    }
                }
            }

            return hash & 0x7FFFFFFF;
        }

        /// <summary>
        /// Check if name is the same as encoded string represented by nameIdx
        /// </summary>
        internal unsafe bool IsStringEqual(string name, UInt32 nameIdx)
        {
            Debug.Assert(nameIdx < m_typeNames.Length);

            fixed (char* pNameStart = name)
            fixed (byte* pNBlob = m_typeNames, pNsBlob = m_typeNamespaces)
            {
                bool unicode = false;

                if (pNBlob[nameIdx] == Unicode_Mark)
                {
                    unicode = true;
                    nameIdx++;
                }

                char* pName = pNameStart;
                byte* pN = pNBlob + nameIdx;

                for (; ; pN++)
                {
                    int c = *pN;

                    if (unicode)
                    {
                        c |= (*(++pN)) << 8;
                    }

                    if (c == 0)
                        break;

                    if (!unicode && (c >= Escape_Start))
                    {
                        byte* pNs = pNsBlob + m_indices[c - Escape_Start];

                        for (; ; pNs++, pName++)
                        {
                            byte d = *pNs;

                            if (d == 0)
                                break;

                            if (d != *pName)
                                goto NoMatch;
                        }
                    }
                    else
                    {
                        if (c != *pName)
                            goto NoMatch;

                        pName++;
                    }
                }

                if (*pName != '\0')
                    goto NoMatch;
            }

            Debug.Assert(GetString(nameIdx) == name, "IsStringEqual returned a bad result");

            return true;

        NoMatch:
            Debug.Assert(GetString(nameIdx) != name, "IsStringEqual returned a bad result");

            return false;
        }
    }
}