summaryrefslogtreecommitdiff
path: root/src/vm/stringliteralmap.cpp
blob: faa5a6db1b140d4d6091e8e9b2783c0f8b99f1e3 (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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//


/*============================================================
**
** Header:  Map used for interning of string literals.
**
===========================================================*/

#include "common.h"
#include "eeconfig.h"
#include "stringliteralmap.h"

/*
    Thread safety in GlobalStringLiteralMap / StringLiteralMap

    A single lock protects the N StringLiteralMap objects and single
    GlobalStringLiteralMap rooted in the SystemDomain at any time. It is

    SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal

    At one time each StringLiteralMap had it's own lock to protect
    the entry hash table as well, and Interlocked operations were done on the
    ref count of the contained StringLiteralEntries. But anything of import
    needed to be done under the global lock mentioned above or races would
    result. (For example, an app domain shuts down, doing final release on
    a StringLiteralEntry, but at that moment the entry is being handed out
    in another appdomain and addref'd only after the count went to 0.)

    The rule is:

    Any AddRef()/Release() calls on StringLiteralEntry need to be under the lock.
    Any insert/deletes from the StringLiteralMap or GlobalStringLiteralMap
    need to be done under the lock.

    The only thing you can do without the lock is look up an existing StringLiteralEntry
    in an StringLiteralMap hash table. This is true because these lookup calls
    will all come before destruction of the map, the hash table is safe for multiple readers,
    and we know the StringLiteralEntry so found 1) can't be destroyed because that table keeps
    an AddRef on it and 2) isn't internally modified once created.
*/
    
#define GLOBAL_STRING_TABLE_BUCKET_SIZE 128
#define INIT_NUM_APP_DOMAIN_STRING_BUCKETS 59
#define INIT_NUM_GLOBAL_STRING_BUCKETS 131

// assumes that memory pools's per block data is same as sizeof (StringLiteralEntry) 
#define EEHASH_MEMORY_POOL_GROW_COUNT 128

StringLiteralEntryArray *StringLiteralEntry::s_EntryList = NULL;
DWORD StringLiteralEntry::s_UsedEntries = NULL;
StringLiteralEntry *StringLiteralEntry::s_FreeEntryList = NULL;

StringLiteralMap::StringLiteralMap()
: m_StringToEntryHashTable(NULL)
, m_MemoryPool(NULL)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;
}

void StringLiteralMap::Init()
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        PRECONDITION(CheckPointer(this));
        INJECT_FAULT(ThrowOutOfMemory());
    }
    CONTRACTL_END;

    // Allocate the memory pool and set the initial count to quarter as grow count
    m_MemoryPool = new MemoryPool (SIZEOF_EEHASH_ENTRY, EEHASH_MEMORY_POOL_GROW_COUNT, EEHASH_MEMORY_POOL_GROW_COUNT/4);

    m_StringToEntryHashTable =  new EEUnicodeStringLiteralHashTable ();

    LockOwner lock = {&(SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal), IsOwnerOfCrst};
    if (!m_StringToEntryHashTable->Init(INIT_NUM_APP_DOMAIN_STRING_BUCKETS, &lock, m_MemoryPool))
        ThrowOutOfMemory();
}

StringLiteralMap::~StringLiteralMap()
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
    }
    CONTRACTL_END;

    // We do need to take the globalstringliteralmap lock because we are manipulating
    // StringLiteralEntry objects that belong to it.
    // Note that we remember the current entry and relaese it only when the 
    // enumerator has advanced to the next entry so that we don't endup deleteing the
    // current entry itself and killing the enumerator.

    if (m_StringToEntryHashTable != NULL)
    {
        // We need the global lock anytime we release StringLiteralEntry objects
        CrstHolder gch(&(SystemDomain::GetGlobalStringLiteralMapNoCreate()->m_HashTableCrstGlobal));

        StringLiteralEntry *pEntry = NULL;
        EEHashTableIteration Iter;

#ifdef _DEBUG
        m_StringToEntryHashTable->SuppressSyncCheck();
#endif

        m_StringToEntryHashTable->IterateStart(&Iter);
        if (m_StringToEntryHashTable->IterateNext(&Iter))
        {
            pEntry = (StringLiteralEntry*)m_StringToEntryHashTable->IterateGetValue(&Iter);

            while (m_StringToEntryHashTable->IterateNext(&Iter))
            {
                // Release the previous entry
                _ASSERTE(pEntry);
                pEntry->Release();

                // Set the 
                pEntry = (StringLiteralEntry*)m_StringToEntryHashTable->IterateGetValue(&Iter);
            }
            // Release the last entry
            _ASSERTE(pEntry);
            pEntry->Release();
        }
        // else there were no entries.

        // Delete the hash table first. The dtor of the hash table would clean up all the entries.
        delete m_StringToEntryHashTable;
    }

    // Delete the pool later, since the dtor above would need it.
    if (m_MemoryPool != NULL)
        delete m_MemoryPool;
}



STRINGREF *StringLiteralMap::GetStringLiteral(EEStringData *pStringData, BOOL bAddIfNotFound, BOOL bAppDomainWontUnload)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(this));
        PRECONDITION(CheckPointer(pStringData));
    }
    CONTRACTL_END;

    HashDatum Data;

    DWORD dwHash = m_StringToEntryHashTable->GetHash(pStringData);
    if (m_StringToEntryHashTable->GetValue(pStringData, &Data, dwHash))
    {
        STRINGREF *pStrObj = NULL;
        pStrObj = ((StringLiteralEntry*)Data)->GetStringObject();
        _ASSERTE(!bAddIfNotFound || pStrObj);
        return pStrObj;

    }
    else
    {
        // Retrieve the string literal from the global string literal map.
        CrstHolder gch(&(SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal));

        // TODO: We can be more efficient by checking our local hash table now to see if
        // someone beat us to inserting it. (m_StringToEntryHashTable->GetValue(pStringData, &Data))
        // (Rather than waiting until after we look the string up in the global map) 
        
        StringLiteralEntryHolder pEntry(SystemDomain::GetGlobalStringLiteralMap()->GetStringLiteral(pStringData, dwHash, bAddIfNotFound));

        _ASSERTE(pEntry || !bAddIfNotFound);

        // If pEntry is non-null then the entry exists in the Global map. (either we retrieved it or added it just now)
        if (pEntry)
        {
            // If the entry exists in the Global map and the appdomain wont ever unload then we really don't need to add a
            // hashentry in the appdomain specific map.
            // TODO: except that by not inserting into our local table we always take the global map lock
            // and come into this path, when we could succeed at a lock free lookup above.
            
            if (!bAppDomainWontUnload)
            {                
                // Make sure some other thread has not already added it.
                if (!m_StringToEntryHashTable->GetValue(pStringData, &Data))
                {
                    // Insert the handle to the string into the hash table.
                    m_StringToEntryHashTable->InsertValue(pStringData, (LPVOID)pEntry, FALSE);
                }
                else
                {
                    pEntry.Release(); //while we're still under lock
                }
            }
#ifdef _DEBUG
            else
            {
                LOG((LF_APPDOMAIN, LL_INFO10000, "Avoided adding String literal to appdomain map: size: %d bytes\n", pStringData->GetCharCount()));
            }
#endif
            pEntry.SuppressRelease();
            STRINGREF *pStrObj = NULL;
            // Retrieve the string objectref from the string literal entry.
            pStrObj = pEntry->GetStringObject();
            _ASSERTE(!bAddIfNotFound || pStrObj);
            return pStrObj;
        }
    }
    // If the bAddIfNotFound flag is set then we better have a string
    // string object at this point.
    _ASSERTE(!bAddIfNotFound);
    return NULL;
}

STRINGREF *StringLiteralMap::GetInternedString(STRINGREF *pString, BOOL bAddIfNotFound, BOOL bAppDomainWontUnload)
{
    CONTRACTL
    {
        GC_TRIGGERS;
        THROWS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(this));
        PRECONDITION(CheckPointer(pString));
    }
    CONTRACTL_END;

    HashDatum Data;
    EEStringData StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());

    DWORD dwHash = m_StringToEntryHashTable->GetHash(&StringData);
    if (m_StringToEntryHashTable->GetValue(&StringData, &Data, dwHash))
    {
        STRINGREF *pStrObj = NULL;
        pStrObj = ((StringLiteralEntry*)Data)->GetStringObject();
        _ASSERTE(!bAddIfNotFound || pStrObj);
        return pStrObj;

    }
    else
    {
        CrstHolder gch(&(SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal));

        // TODO: We can be more efficient by checking our local hash table now to see if
        // someone beat us to inserting it. (m_StringToEntryHashTable->GetValue(pStringData, &Data))
        // (Rather than waiting until after we look the string up in the global map) 
        
        // Retrieve the string literal from the global string literal map.
        StringLiteralEntryHolder pEntry(SystemDomain::GetGlobalStringLiteralMap()->GetInternedString(pString, dwHash, bAddIfNotFound));

        _ASSERTE(pEntry || !bAddIfNotFound);

        // If pEntry is non-null then the entry exists in the Global map. (either we retrieved it or added it just now)
        if (pEntry)
        {
            // If the entry exists in the Global map and the appdomain wont ever unload then we really don't need to add a
            // hashentry in the appdomain specific map.
            // TODO: except that by not inserting into our local table we always take the global map lock
            // and come into this path, when we could succeed at a lock free lookup above.

            if (!bAppDomainWontUnload)
            {
                // Since GlobalStringLiteralMap::GetInternedString() could have caused a GC,
                // we need to recreate the string data.
                StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());

                // Make sure some other thread has not already added it.
                if (!m_StringToEntryHashTable->GetValue(&StringData, &Data))
                {
                    // Insert the handle to the string into the hash table.
                    m_StringToEntryHashTable->InsertValue(&StringData, (LPVOID)pEntry, FALSE);
                }
                else
                {
                    pEntry.Release(); // while we're under lock
                }
            }
            pEntry.SuppressRelease();
            // Retrieve the string objectref from the string literal entry.
            STRINGREF *pStrObj = NULL;
            pStrObj = pEntry->GetStringObject();
            return pStrObj;
        }
    }
    // If the bAddIfNotFound flag is set then we better have a string
    // string object at this point.
    _ASSERTE(!bAddIfNotFound);

    return NULL;
}

GlobalStringLiteralMap::GlobalStringLiteralMap()
: m_StringToEntryHashTable(NULL)
, m_MemoryPool(NULL)
, m_HashTableCrstGlobal(CrstGlobalStrLiteralMap)
, m_LargeHeapHandleTable(SystemDomain::System(), GLOBAL_STRING_TABLE_BUCKET_SIZE)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
    }
    CONTRACTL_END;

#ifdef _DEBUG
    m_LargeHeapHandleTable.RegisterCrstDebug(&m_HashTableCrstGlobal);
#endif
}

GlobalStringLiteralMap::~GlobalStringLiteralMap()
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
    }
    CONTRACTL_END;
    
    // if we are deleting the map then either it is shutdown time or else there was a race trying to create
    // the initial map and this one was the loser 
    // (i.e. two threads made a map and the InterlockedCompareExchange failed for one of them and 
    // now it is deleting the map)
    //
    // if it's not the main map, then the map we are deleting better be empty!

    // there must be *some* global table
    _ASSERTE(SystemDomain::GetGlobalStringLiteralMapNoCreate()  != NULL);

    if (SystemDomain::GetGlobalStringLiteralMapNoCreate() != this)
    {
        // if this isn't the real global table then it must be empty
        _ASSERTE(m_StringToEntryHashTable->IsEmpty());  

        // Delete the hash table first. The dtor of the hash table would clean up all the entries.
        delete m_StringToEntryHashTable;
        // Delete the pool later, since the dtor above would need it.
        delete m_MemoryPool;        
    }
    else
    {
        // We are shutting down, the OS will reclaim the memory from the StringLiteralEntries,
        // m_MemoryPool and m_StringToEntryHashTable.
        _ASSERTE(g_fProcessDetach);
    }        
}

void GlobalStringLiteralMap::Init()
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        PRECONDITION(CheckPointer(this));
        INJECT_FAULT(ThrowOutOfMemory());
    } 
    CONTRACTL_END;

    // Allocate the memory pool and set the initial count to quarter as grow count
    m_MemoryPool = new MemoryPool (SIZEOF_EEHASH_ENTRY, EEHASH_MEMORY_POOL_GROW_COUNT, EEHASH_MEMORY_POOL_GROW_COUNT/4);

    m_StringToEntryHashTable =  new EEUnicodeStringLiteralHashTable ();

    LockOwner lock = {&m_HashTableCrstGlobal, IsOwnerOfCrst};
    if (!m_StringToEntryHashTable->Init(INIT_NUM_GLOBAL_STRING_BUCKETS, &lock, m_MemoryPool))
        ThrowOutOfMemory();
}

StringLiteralEntry *GlobalStringLiteralMap::GetStringLiteral(EEStringData *pStringData, DWORD dwHash, BOOL bAddIfNotFound)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(this));
        PRECONDITION(CheckPointer(pStringData));
        PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());        
    }
    CONTRACTL_END;

    HashDatum Data;
    StringLiteralEntry *pEntry = NULL;

    if (m_StringToEntryHashTable->GetValue(pStringData, &Data, dwHash))
    {
        pEntry = (StringLiteralEntry*)Data;
        // If the entry is already in the table then addref it before we return it.
        if (pEntry)
            pEntry->AddRef();
    }
    else
    {
        if (bAddIfNotFound)
            pEntry = AddStringLiteral(pStringData);
    }

    return pEntry;
}

StringLiteralEntry *GlobalStringLiteralMap::GetInternedString(STRINGREF *pString, DWORD dwHash, BOOL bAddIfNotFound)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(this));
        PRECONDITION(CheckPointer(pString));
        PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());
    }
    CONTRACTL_END;  

    EEStringData StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());

    HashDatum Data;
    StringLiteralEntry *pEntry = NULL;

    if (m_StringToEntryHashTable->GetValue(&StringData, &Data, dwHash))
    {
        pEntry = (StringLiteralEntry*)Data;
        // If the entry is already in the table then addref it before we return it.
        if (pEntry)
            pEntry->AddRef();
    }
    else
    {
        if (bAddIfNotFound)
            pEntry = AddInternedString(pString);
    }

    return pEntry;
}

#ifdef LOGGING
static void LogStringLiteral(__in_z const char* action, EEStringData *pStringData)
{
    STATIC_CONTRACT_NOTHROW;
    STATIC_CONTRACT_GC_NOTRIGGER;
    STATIC_CONTRACT_FORBID_FAULT;

    int length = pStringData->GetCharCount();
    length = min(length, 100);
    WCHAR *szString = (WCHAR *)_alloca((length + 1) * sizeof(WCHAR));
    memcpyNoGCRefs((void*)szString, (void*)pStringData->GetStringBuffer(), length * sizeof(WCHAR));
    szString[length] = '\0';
    LOG((LF_APPDOMAIN, LL_INFO10000, "String literal \"%S\" %s to Global map, size %d bytes\n", szString, action, pStringData->GetCharCount()));
}
#endif

STRINGREF AllocateStringObject(EEStringData *pStringData)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;  
    }
    CONTRACTL_END;  
   
    // Create the COM+ string object.
    DWORD cCount = pStringData->GetCharCount();
    
    STRINGREF strObj = AllocateString(cCount);

    GCPROTECT_BEGIN(strObj)
    {
        // Copy the string constant into the COM+ string object.  The code
        // will add an extra null at the end for safety purposes, but since
        // we support embedded nulls, one should never treat the string as
        // null termianted.
        LPWSTR strDest = strObj->GetBuffer();
        memcpyNoGCRefs(strDest, pStringData->GetStringBuffer(), cCount*sizeof(WCHAR));
        strDest[cCount] = 0;

        // IsOnlyLowChars actually incidicates if we can sort the string in a fast way.
        // Take a look RegMeta::DefineUserString to see how we set the flag.
        // The flag is persisited to assembly containing the string literals.
        // We restore the flag when we load strings from assembly (MDInternalRO::GetUserString.) 
        //
        if (pStringData->GetIsOnlyLowChars()) 
        {
            strObj->SetHighCharState(STRING_STATE_FAST_OPS);
        }
    }
    GCPROTECT_END();

    return strObj;
}
StringLiteralEntry *GlobalStringLiteralMap::AddStringLiteral(EEStringData *pStringData)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(this));
        PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());        
    }
    CONTRACTL_END;  

    StringLiteralEntry *pRet;

    {
    LargeHeapHandleBlockHolder pStrObj(&m_LargeHeapHandleTable,1);
    // Create the COM+ string object.
    STRINGREF strObj = AllocateStringObject(pStringData);
                
    // Allocate a handle for the string.
    SetObjectReference(pStrObj[0], (OBJECTREF) strObj, NULL);
   

    // Allocate the StringLiteralEntry.
    StringLiteralEntryHolder pEntry(StringLiteralEntry::AllocateEntry(pStringData, (STRINGREF*)pStrObj[0]));
    pStrObj.SuppressRelease();
    // Insert the handle to the string into the hash table.
    m_StringToEntryHashTable->InsertValue(pStringData, (LPVOID)pEntry, FALSE);
    pEntry.SuppressRelease();
    pRet = pEntry;

#ifdef LOGGING
    LogStringLiteral("added", pStringData);
#endif
    }

    return pRet;
}

StringLiteralEntry *GlobalStringLiteralMap::AddInternedString(STRINGREF *pString)
{
 
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(this));
        PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());        
    }
    CONTRACTL_END;  

    EEStringData StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());    
    StringLiteralEntry *pRet;

    {
    LargeHeapHandleBlockHolder pStrObj(&m_LargeHeapHandleTable,1);
    SetObjectReference(pStrObj[0], (OBJECTREF) *pString, NULL);

    // Since the allocation might have caused a GC we need to re-get the
    // string data.
    StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());

    StringLiteralEntryHolder pEntry(StringLiteralEntry::AllocateEntry(&StringData, (STRINGREF*)pStrObj[0]));
    pStrObj.SuppressRelease();

    // Insert the handle to the string into the hash table.
    m_StringToEntryHashTable->InsertValue(&StringData, (LPVOID)pEntry, FALSE);
    pEntry.SuppressRelease();
    pRet = pEntry;
    }

    return pRet;
}

void GlobalStringLiteralMap::RemoveStringLiteralEntry(StringLiteralEntry *pEntry)
{
   CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        PRECONDITION(CheckPointer(pEntry)); 
        PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());
        PRECONDITION(CheckPointer(this));
    }
    CONTRACTL_END;      

    // Remove the entry from the hash table.
    {
        GCX_COOP();

        EEStringData StringData;    
        pEntry->GetStringData(&StringData);

        BOOL bSuccess;
        bSuccess = m_StringToEntryHashTable->DeleteValue(&StringData);
        // this assert is comented out to accomodate case when StringLiteralEntryHolder 
        // releases this object after failed insertion into hash
        //_ASSERTE(bSuccess);

#ifdef LOGGING
        // We need to do this logging within the GCX_COOP(), as a gc will render
        // our StringData pointers stale.
        if (bSuccess)
        {
            LogStringLiteral("removed", &StringData);
        }
#endif

        // Release the object handle that the entry was using.
        STRINGREF *pObjRef = pEntry->GetStringObject();
        m_LargeHeapHandleTable.ReleaseHandles((OBJECTREF*)pObjRef, 1);
    }

    // We do not delete the StringLiteralEntry itself that will be done in the
    // release method of the StringLiteralEntry.
}

StringLiteralEntry *StringLiteralEntry::AllocateEntry(EEStringData *pStringData, STRINGREF *pStringObj)
{
   CONTRACTL
    {
        THROWS;
        GC_TRIGGERS; // GC_TRIGGERS because in the precondition below GetGlobalStringLiteralMap() might need to create the map
        MODE_COOPERATIVE;
        PRECONDITION(SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal.OwnedByCurrentThread());
    }
    CONTRACTL_END; 

    // Note: we don't synchronize here because allocateEntry is called when HashCrst is held.
    void *pMem = NULL;
    if (s_FreeEntryList != NULL)
    {
        pMem = s_FreeEntryList;
        s_FreeEntryList = s_FreeEntryList->m_pNext;
        _ASSERTE (((StringLiteralEntry*)pMem)->m_bDeleted);        
    }
    else
    {
        if (s_EntryList == NULL || (s_UsedEntries >= MAX_ENTRIES_PER_CHUNK))
        {
            StringLiteralEntryArray *pNew = new StringLiteralEntryArray();
            pNew->m_pNext = s_EntryList;
            s_EntryList = pNew;
            s_UsedEntries = 0;
        }
        pMem = &(s_EntryList->m_Entries[s_UsedEntries++*sizeof(StringLiteralEntry)]);
    }
    _ASSERTE (pMem && "Unable to allocate String literal Entry");

    return new (pMem) StringLiteralEntry (pStringData, pStringObj);
}

void StringLiteralEntry::DeleteEntry (StringLiteralEntry *pEntry)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        PRECONDITION(SystemDomain::GetGlobalStringLiteralMapNoCreate()->m_HashTableCrstGlobal.OwnedByCurrentThread());
    }
    CONTRACTL_END; 

    _ASSERTE (VolatileLoad(&pEntry->m_dwRefCount) == 0);
    
#ifdef _DEBUG
    memset (pEntry, 0xc, sizeof(StringLiteralEntry));
#endif

#ifdef _DEBUG        
    pEntry->m_bDeleted = TRUE;
#endif
    
    // The free list needs protection from the m_HashTableCrstGlobal
    pEntry->m_pNext = s_FreeEntryList;
    s_FreeEntryList = pEntry;
}