summaryrefslogtreecommitdiff
path: root/src/vm/gcheaphashtable.inl
blob: f2256bea1e121b9765c737f0dd5eabb7388a39a3 (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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// 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.

#ifdef GCHEAPHASHTABLE_H
#ifndef GCHEAPHASHTABLE_INL
#define GCHEAPHASHTABLE_INL

template <bool removeSupported>
/*static */bool DefaultGCHeapHashTraits<removeSupported>::IsNull(PTRARRAYREF arr, INT32 index)
{
    LIMITED_METHOD_CONTRACT;

    return arr->GetAt(index) == 0;
}

template <bool removeSupported>
/*static */bool DefaultGCHeapHashTraits<removeSupported>::IsDeleted(PTRARRAYREF arr, INT32 index, GCHEAPHASHOBJECTREF gcHeap)
{
    LIMITED_METHOD_CONTRACT;

    if (removeSupported)
    {
        return gcHeap == arr->GetAt(index);
    }
    else
    {
        return false;
    }
}

#ifndef DACCESS_COMPILE
template <bool removeSupported>
/*static*/ typename DefaultGCHeapHashTraits<removeSupported>::THashArrayType DefaultGCHeapHashTraits<removeSupported>::AllocateArray(INT32 size)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    return (THashArrayType)AllocateObjectArray(size, g_pObjectClass);
}
#endif // !DACCESS_COMPILE

    // Not a part of the traits api, but used to allow derived traits to save on code
template <bool removeSupported>
/*static*/ OBJECTREF DefaultGCHeapHashTraits<removeSupported>::GetValueAtIndex(GCHEAPHASHOBJECTREF *pgcHeap, INT32 index)
{
    LIMITED_METHOD_CONTRACT;

    PTRARRAYREF arr((PTRARRAYREF)(*pgcHeap)->GetData());

    OBJECTREF value = arr->GetAt(index);

    return value;
}

#ifndef DACCESS_COMPILE
template <bool removeSupported>
/*static*/ void DefaultGCHeapHashTraits<removeSupported>::CopyValue(THashArrayType srcArray, INT32 indexSrc, THashArrayType destinationArray, INT32 indexDest)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    if (srcArray == NULL)
        COMPlusThrow(kNullReferenceException);

    if ((INT32)srcArray->GetNumComponents() < indexSrc)
        COMPlusThrow(kIndexOutOfRangeException);

    OBJECTREF value = srcArray->GetAt(indexSrc);

    if ((INT32)destinationArray->GetNumComponents() < indexDest)
        COMPlusThrow(kIndexOutOfRangeException);

    destinationArray->SetAt(indexDest, value);
}
#endif // !DACCESS_COMPILE

#ifndef DACCESS_COMPILE
template <bool removeSupported>
/*static*/ void DefaultGCHeapHashTraits<removeSupported>::DeleteEntry(GCHEAPHASHOBJECTREF *pgcHeap, INT32 index)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    static_assert(removeSupported, "This hash doesn't support remove");

    PTRARRAYREF arr((PTRARRAYREF)(*pgcHeap)->GetData());

    if (arr == NULL)
        COMPlusThrow(kNullReferenceException);

    if ((INT32)arr->GetNumComponents() < index)
        COMPlusThrow(kIndexOutOfRangeException);

    // The deleted sentinel is a self-pointer
    arr->SetAt(index, *pgcHeap);
}
#endif // !DACCESS_COMPILE

template <bool removeSupported>
template<class TElement>
/*static*/ void DefaultGCHeapHashTraits<removeSupported>::GetElement(GCHEAPHASHOBJECTREF *pgcHeap, INT32 index, TElement& foundElement)
{
    LIMITED_METHOD_CONTRACT;

    foundElement = (TElement)GetValueAtIndex(pgcHeap, index);
}

#ifndef DACCESS_COMPILE
template <bool removeSupported>
template<class TElement>
/*static*/ void DefaultGCHeapHashTraits<removeSupported>::SetElement(GCHEAPHASHOBJECTREF *pgcHeap, INT32 index, TElement& foundElement)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    PTRARRAYREF arr((PTRARRAYREF)(*pgcHeap)->GetData());

    if (arr == NULL)
        COMPlusThrow(kNullReferenceException);

    if ((INT32)arr->GetNumComponents() < index)
        COMPlusThrow(kIndexOutOfRangeException);

    arr->SetAt(index, foundElement);
}
#endif // !DACCESS_COMPILE

template <class PtrTypeKey, bool supports_remove>
/*static */INT32 GCHeapHashTraitsPointerToPointerList<PtrTypeKey, supports_remove>::Hash(PtrTypeKey *pValue)
{
    LIMITED_METHOD_CONTRACT;
    return (INT32)*pValue;
}

template <class PtrTypeKey, bool supports_remove>
/*static */INT32 GCHeapHashTraitsPointerToPointerList<PtrTypeKey, supports_remove>::Hash(PTRARRAYREF arr, INT32 index)
{
    LIMITED_METHOD_CONTRACT;

    UPTRARRAYREF value = (UPTRARRAYREF)arr->GetAt(index);
    
    return (INT32)*value->GetDirectConstPointerToNonObjectElements();
}

template <class PtrTypeKey, bool supports_remove>
/*static */bool GCHeapHashTraitsPointerToPointerList<PtrTypeKey, supports_remove>::DoesEntryMatchKey(PTRARRAYREF arr, INT32 index, PtrTypeKey *pKey)
{
    LIMITED_METHOD_CONTRACT;

    UPTRARRAYREF value = (UPTRARRAYREF)arr->GetAt(index);
    UPTR uptrValue = *value->GetDirectConstPointerToNonObjectElements();

    return ((UPTR)*pKey) == uptrValue;
}

template <class TRAITS>
template<class TKey, class TValueSetter>
void GCHeapHash<TRAITS>::Insert(TKey *pKey, const TValueSetter &valueSetter)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    count_t hash = CallHash(pKey);
    count_t tableSize = m_gcHeapHash->GetCapacity();
    count_t index = hash % tableSize; 
    count_t increment = 0; // delay computation

    while (TRUE)
    {
        THashArrayType arr((THashArrayType)(m_gcHeapHash)->GetData());

        bool isNull = TRAITS::IsNull(arr, index);
        bool isDeleted = false;
        if (!isNull && TRAITS::IsDeleted(arr, index, m_gcHeapHash))
            isDeleted = true;

        if (isNull || isDeleted)
        {
            if (arr == NULL)
                COMPlusThrow(kNullReferenceException);

            if ((INT32)arr->GetNumComponents() < index)
                COMPlusThrow(kIndexOutOfRangeException);

            valueSetter(arr, index);
            m_gcHeapHash->IncrementCount(isDeleted);
            return;
        }
    
        if (increment == 0)
            increment = (hash % (tableSize-1)) + 1; 
    
        index += increment;
        if (index >= tableSize)
            index -= tableSize;
    }
}

template <class TRAITS>
void GCHeapHash<TRAITS>::CheckGrowth()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    count_t tableMax = (count_t) (m_gcHeapHash->GetCapacity() * TRAITS::s_density_factor_numerator / TRAITS::s_density_factor_denominator);
    if (m_gcHeapHash->GetCount() == tableMax)
        Grow();
    else
    {
        tableMax = (count_t) (m_gcHeapHash->GetCapacity() * TRAITS::s_densitywithdeletes_factor_numerator / TRAITS::s_densitywithdeletes_factor_denominator);
        if ((m_gcHeapHash->GetCount() + m_gcHeapHash->GetDeletedCount()) >= tableMax)
        {
            THashArrayType newTable = TRAITS::AllocateArray(m_gcHeapHash->GetCapacity());
            ReplaceTable(newTable);
        }
    }
}

template <class TRAITS>
void GCHeapHash<TRAITS>::Grow()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    THashArrayType newTable = Grow_OnlyAllocateNewTable();
    ReplaceTable(newTable);
}

template <class TRAITS>
typename GCHeapHash<TRAITS>::THashArrayType GCHeapHash<TRAITS>::Grow_OnlyAllocateNewTable()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        INSTANCE_CHECK;
    }
    CONTRACTL_END;

    count_t newSize = (count_t) (m_gcHeapHash->GetCount()
                                * TRAITS::s_growth_factor_numerator / TRAITS::s_growth_factor_denominator
                                * TRAITS::s_density_factor_denominator / TRAITS::s_density_factor_numerator);
    if (newSize < TRAITS::s_minimum_allocation)
        newSize = TRAITS::s_minimum_allocation;

    // handle potential overflow
    if (newSize < m_gcHeapHash->GetCount())
        ThrowOutOfMemory();

    return TRAITS::AllocateArray(NextPrime(newSize));
}

template <class TRAITS>
bool GCHeapHash<TRAITS>::IsPrime(count_t number)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    // This is a very low-tech check for primality, which doesn't scale very well.  
    // There are more efficient tests if this proves to be burdensome for larger
    // tables.

    if ((number & 1) == 0)
        return false;

    count_t factor = 3;
    while (factor * factor <= number)
    {
        if ((number % factor) == 0)
            return false;
        factor += 2;
    }

    return true;
}

template <class TRAITS>
typename GCHeapHash<TRAITS>::count_t GCHeapHash<TRAITS>::NextPrime(count_t number)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    static_assert(sizeof(INT32) == sizeof(g_shash_primes[0]), "the cast below of g_shash_primes[] to INT32 isn't safe due to loss of precision.");

    for (int i = 0; i < (int) (sizeof(g_shash_primes) / sizeof(g_shash_primes[0])); i++) {
        if ((INT32)g_shash_primes[i] >= number)
            return (INT32)g_shash_primes[i];
    }

    if ((number&1) == 0)
        number++;

    while (number != 1) {
        if (IsPrime(number))
            return number;
        number +=2;
    }

    // overflow
    ThrowOutOfMemory();
}

template <class TRAITS>
void GCHeapHash<TRAITS>::ReplaceTable(THashArrayType newTable)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    GCPROTECT_BEGIN(newTable);
    {
        count_t newTableSize = (count_t)newTable->GetNumComponents();
        count_t oldTableSize = m_gcHeapHash->GetCapacity();

        // Move all entries over to the new table
        count_t capacity = m_gcHeapHash->GetCapacity();

        for (count_t index = 0; index < capacity; ++index)
        {
            THashArrayType arr((THashArrayType)(m_gcHeapHash)->GetData());
            if (!TRAITS::IsNull(arr, index) && !TRAITS::IsDeleted(arr, index, m_gcHeapHash))
            {
                count_t hash = CallHash(arr, index);
                count_t tableSize = (count_t)newTable->GetNumComponents();
                count_t newIndex = hash % tableSize; 
                count_t increment = 0; // delay computation

                // Value to copy is in index
                while (TRUE)
                {
                    if (TRAITS::IsNull(newTable, newIndex))
                    {
                        arr = (THashArrayType)(m_gcHeapHash)->GetData();
                        TRAITS::CopyValue(arr, index, newTable, newIndex);
                        break;
                    }
                
                    if (increment == 0)
                        increment = (hash % (tableSize-1)) + 1; 
                
                    newIndex += increment;
                    if (newIndex >= tableSize)
                        newIndex -= tableSize;
                }
            }
        }

        m_gcHeapHash->SetTable((BASEARRAYREF)newTable);
        
        // We've just copied the table to a new table. There are no deleted items as
        // we skipped them all, so reset the deleted count to zero
        m_gcHeapHash->SetDeletedCountToZero(); 
    }
    GCPROTECT_END();
}

template <class TRAITS>
template<class TVisitor>
bool GCHeapHash<TRAITS>::VisitAllEntryIndices(TVisitor &visitor)
{
    WRAPPER_NO_CONTRACT;

    count_t capacity = m_gcHeapHash->GetCapacity();

    for (count_t index = 0; index < capacity; ++index)
    {
        THashArrayType arr((THashArrayType)(m_gcHeapHash)->GetData());
        if (!TRAITS::IsNull(arr, index) && !TRAITS::IsDeleted(arr, index, m_gcHeapHash))
        {
            if (!visitor(index))
                return false;
        }
    }

    return true;
}

template <class TRAITS>
template<class TKey, class TValueSetter>
void GCHeapHash<TRAITS>::Add(TKey *pKey, const TValueSetter &valueSetter)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    CheckGrowth();
    Insert(pKey, valueSetter);
}

    // Get the index in the hashtable of the value which matches key, or -1 if there are no matches
template <class TRAITS>
template<class TKey>
INT32 GCHeapHash<TRAITS>::GetValueIndex(TKey *pKey)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    count_t hash = CallHash(pKey);
    count_t tableSize = m_gcHeapHash->GetCapacity();

    // If the table is empty, then there aren't any entries. Just return.
    if (m_gcHeapHash->GetCount() == 0)
        return -1;

    count_t index = hash % tableSize; 
    count_t increment = 0; // delay computation

    THashArrayType arr((THashArrayType)(m_gcHeapHash)->GetData());

    while (m_gcHeapHash->GetCount() != 0) /* the TRAITS::IsDeleted function is allowed to reduce the count */
    {
        if (TRAITS::IsNull(arr, index))
        {
            return -1;
        }

        if (!TRAITS::IsDeleted(arr, index, m_gcHeapHash) && TRAITS::DoesEntryMatchKey(arr, index, pKey))
        {
            return index;
        }
    
        if (increment == 0)
            increment = (hash % (tableSize-1)) + 1; 

        index += increment;
        if (index >= tableSize)
            index -= tableSize;
    }

    return -1;
}

template <class TRAITS>
template<class TElement>
void GCHeapHash<TRAITS>::GetElement(INT32 index, TElement& foundElement)
{
    WRAPPER_NO_CONTRACT;

    TRAITS::GetElement(&m_gcHeapHash, index, foundElement);
}

    // Use this to update an value within the hashtable directly. 
    // It is ONLY safe to do if the index already points at an element
    // which already exists and has the same key as the newElementValue
template <class TRAITS>
template<class TElement>
void GCHeapHash<TRAITS>::SetElement(INT32 index, TElement& newElementValue)
{
    TRAITS::SetElement(&m_gcHeapHash, index, newElementValue);
}

template <class TRAITS>
template<class TKey>
void GCHeapHash<TRAITS>::DeleteEntry(TKey *pKey)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    INT32 index = GetValueIndex(pKey);
    if (index != -1)
    {
        TRAITS::DeleteEntry(&m_gcHeapHash, index);
        m_gcHeapHash->DecrementCount(true);
    }
}

#endif // GCHEAPHASHTABLE_INL
#endif // GCHEAPHASHTABLE_H