summaryrefslogtreecommitdiff
path: root/src/jit/jitexpandarray.h
blob: 03d81f50f3c4b36eea4d3f6749715380ae7f1d6b (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// 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.

#pragma once

// An array of T that expands automatically (and never shrinks) to accomodate
// any index access. Elements added as a result of automatic expansion are
// value-initialized (that is, they are assigned T()).
template <class T>
class JitExpandArray
{
protected:
    CompAllocator* m_alloc;   // The allocator object that should be used to allocate members.
    T*             m_members; // Pointer to the element array.
    unsigned       m_size;    // The size of the element array.
    unsigned       m_minSize; // The minimum size of the element array.

    // Ensure that the element array is large enough for the specified index to be valid.
    void EnsureCoversInd(unsigned idx);

    //------------------------------------------------------------------------
    // InitializeRange: Value-initialize the specified range of elements.
    //
    // Arguments:
    //    low  - inclusive lower bound of the range to initialize
    //    high - exclusive upper bound of the range to initialize
    //
    // Assumptions:
    //    Assumes that the element array has aready been allocated
    //    and that low and high are valid indices. The array is not
    //    expanded to accomodate invalid indices.
    //
    void InitializeRange(unsigned low, unsigned high)
    {
        assert(m_members != nullptr);
        assert((low <= high) && (high <= m_size));
        for (unsigned i = low; i < high; i++)
        {
            m_members[i] = T();
        }
    }

public:
    //------------------------------------------------------------------------
    // JitExpandArray: Construct an empty JitExpandArray object.
    //
    // Arguments:
    //    alloc   - the allocator used to allocate the element array
    //    minSize - the initial size of the element array
    //
    // Notes:
    //    Initially no memory is allocated for the element array. The first
    //    time an array element (having index `idx`) is accessed, an array
    //    of size max(`minSize`, `idx`) is allocated.
    //
    JitExpandArray(CompAllocator* alloc, unsigned minSize = 1)
        : m_alloc(alloc), m_members(nullptr), m_size(0), m_minSize(minSize)
    {
        assert(minSize > 0);
    }

    //------------------------------------------------------------------------
    // ~JitExpandArray: Destruct the JitExpandArray object.
    //
    // Notes:
    //    Frees the element array. Destructors of elements stored in the
    //    array are NOT invoked.
    //
    ~JitExpandArray()
    {
        if (m_members != nullptr)
        {
            m_alloc->Free(m_members);
        }
    }

    //------------------------------------------------------------------------
    // Init: Re-initialize the array to the empty state.
    //
    // Arguments:
    //    alloc   - the allocator used to allocate the element array
    //    minSize - the initial size of the element array
    //
    // Notes:
    //    This is equivalent to calling the destructor and then constructing
    //    the array again.
    //
    void Init(CompAllocator* alloc, unsigned minSize = 1)
    {
        if (m_members != nullptr)
        {
            m_alloc->Free(m_members);
        }
        m_alloc   = alloc;
        m_members = nullptr;
        m_size    = 0;
        m_minSize = minSize;
    }

    //------------------------------------------------------------------------
    // Reset: Change the minimum size and value-initialize all the elements.
    //
    // Arguments:
    //    minSize - the initial size of the element array
    //
    // Notes:
    //    Ensures that an element array of at least `minSize` elements
    //    has been allocated.
    //
    void Reset(unsigned minSize)
    {
        m_minSize = minSize;
        Reset();
    }

    //------------------------------------------------------------------------
    // Reset: Value-initialize all the array elements.
    //
    // Notes:
    //    Ensures that an element array of at least `m_minSize` elements
    //    has been allocated.
    //
    void Reset()
    {
        if (m_minSize > m_size)
        {
            EnsureCoversInd(m_minSize - 1);
        }
        InitializeRange(0, m_size);
    }

    //------------------------------------------------------------------------
    // Get: Get a copy of the element at index `idx`.
    //
    // Arguments:
    //    idx - the element index
    //
    // Return Value:
    //    A copy of the element at index `idx`.
    //
    // Notes:
    //    Expands the element array, if necessary, to contain `idx`.
    //    The result will be a value-initialized T if a value wasn't
    //    previously assigned to the specififed index.
    //
    T Get(unsigned idx)
    {
        EnsureCoversInd(idx);
        return m_members[idx];
    }

    //------------------------------------------------------------------------
    // GetRef: Get a reference to the element at index `idx`.
    //
    // Arguments:
    //    idx - the element index
    //
    // Return Value:
    //    A reference to the element at index `idx`.
    //
    // Notes:
    //    Like `Get`, but returns a reference, so suitable for use as
    //    the LHS of an assignment.
    //
    T& GetRef(unsigned idx)
    {
        EnsureCoversInd(idx);
        return m_members[idx];
    }

    //------------------------------------------------------------------------
    // Set: Assign a copy of `val` to the element at index `idx`.
    //
    // Arguments:
    //    idx - the element index
    //    val - the value to assign
    //
    // Notes:
    //    Expands the element array, if necessary, to contain `idx`.
    //
    void Set(unsigned idx, T val)
    {
        EnsureCoversInd(idx);
        m_members[idx] = val;
    }

    //------------------------------------------------------------------------
    // operator[]: Get a reference to the element at index `idx`.
    //
    // Arguments:
    //    idx - the element index
    //
    // Return Value:
    //    A reference to the element at index `idx`.
    //
    // Notes:
    //    Same as `GetRef`.
    //
    T& operator[](unsigned idx)
    {
        EnsureCoversInd(idx);
        return m_members[idx];
    }
};

template <class T>
class JitExpandArrayStack : public JitExpandArray<T>
{
    unsigned m_used; // The stack depth

public:
    //------------------------------------------------------------------------
    // JitExpandArrayStack: Construct an empty JitExpandArrayStack object.
    //
    // Arguments:
    //    alloc   - the allocator used to allocate the element array
    //    minSize - the initial size of the element array
    //
    // Notes:
    //    See JitExpandArray constructor notes.
    //
    JitExpandArrayStack(CompAllocator* alloc, unsigned minSize = 1) : JitExpandArray<T>(alloc, minSize), m_used(0)
    {
    }

    //------------------------------------------------------------------------
    // Set: Assign value a copy of `val` to the element at index `idx`.
    //
    // Arguments:
    //    idx - the index of element
    //    val - the value to assign
    //
    // Notes:
    //    Expands the element array, if necessary, to contain `idx`.
    //    If `idx` is larger than the current stack depth then this
    //    is the equivalent of series of Push(T()) followed by a Push(val).
    //
    void Set(unsigned idx, T val)
    {
        JitExpandArray<T>::Set(idx, val);
        m_used = max((idx + 1), m_used);
    }

    //------------------------------------------------------------------------
    // Reset: Remove all the elements from the stack.
    //
    void Reset()
    {
        JitExpandArray<T>::Reset();
        m_used = 0;
    }

    //------------------------------------------------------------------------
    // Push: Push a copy of the specified value onto the stack.
    //
    // Arguments:
    //    val - the value
    //
    // Return Value:
    //    The index of the pushed value.
    //
    unsigned Push(T val)
    {
        unsigned res = m_used;
        JitExpandArray<T>::Set(m_used, val);
        m_used++;
        return res;
    }

    //------------------------------------------------------------------------
    // Pop: Remove the top element of the stack.
    //
    // Return Value:
    //    A copy of the removed element.
    //
    // Assumptions:
    //    The stack must not be empty.
    //
    T Pop()
    {
        assert(Size() > 0);
        m_used--;
        return this->m_members[m_used];
    }

    //------------------------------------------------------------------------
    // Top: Get a copy of the top element.
    //
    // Return Value:
    //    A copy of the top element.
    //
    // Assumptions:
    //    The stack must not be empty.
    //
    T Top()
    {
        assert(Size() > 0);
        return this->m_members[m_used - 1];
    }

    //------------------------------------------------------------------------
    // TopRef: Get a reference to the top element.
    //
    // Return Value:
    //    A reference to the top element.
    //
    // Assumptions:
    //    The stack must not be empty.
    //
    T& TopRef()
    {
        assert(Size() > 0);
        return this->m_members[m_used - 1];
    }

    //------------------------------------------------------------------------
    // GetNoExpand: Get a copy of the element at index `idx`.
    //
    // Arguments:
    //    idx - the element index
    //
    // Return Value:
    //    A copy of the element at index `idx`.
    //
    // Notes:
    //    Unlike `Get` this does not expand the array if the index is not valid.
    //
    // Assumptions:
    //    The element index does not exceed the current stack depth.
    //
    T GetNoExpand(unsigned idx)
    {
        assert(idx < m_used);
        return this->m_members[idx];
    }

    //------------------------------------------------------------------------
    // Remove: Remove the element at index `idx`.
    //
    // Arguments:
    //    idx - the element index
    //
    // Notes:
    //    Shifts contents of the array beyond `idx`, if any, to occupy the free
    //    slot created at `idx`. O(n) worst case operation, no memory is allocated.
    //    Elements are bitwise copied, copy constructors are NOT invoked.
    //
    // Assumptions:
    //    The element index does not exceed the current stack depth.
    //
    void Remove(unsigned idx)
    {
        assert(idx < m_used);
        if (idx < m_used - 1)
        {
            memmove(&this->m_members[idx], &this->m_members[idx + 1], (m_used - idx - 1) * sizeof(T));
        }
        m_used--;
    }

    //------------------------------------------------------------------------
    // Size: Get the current stack depth.
    //
    // Return Value:
    //    The stack depth.
    //
    unsigned Size()
    {
        return m_used;
    }
};

//------------------------------------------------------------------------
// EnsureCoversInd: Ensure that the array is large enough for the specified
// index to be valid.
//
// Arguments:
//    idx - the element index
//
// Notes:
//    If the array is expanded then
//      - the existing elements are bitwise copied (copy constructors are NOT invoked)
//      - the newly added elements are value-initialized
//
template <class T>
void JitExpandArray<T>::EnsureCoversInd(unsigned idx)
{
    if (idx >= m_size)
    {
        unsigned oldSize    = m_size;
        T*       oldMembers = m_members;
        m_size              = max(idx + 1, max(m_minSize, m_size * 2));
        if (sizeof(T) < sizeof(int))
        {
            m_members = (T*)m_alloc->ArrayAlloc(ALIGN_UP(m_size * sizeof(T), sizeof(int)), sizeof(BYTE));
        }
        else
        {
            m_members = (T*)m_alloc->ArrayAlloc(m_size, sizeof(T));
        }
        if (oldMembers != nullptr)
        {
            memcpy(m_members, oldMembers, oldSize * sizeof(T));
            m_alloc->Free(oldMembers);
        }
        InitializeRange(oldSize, m_size);
    }
}