1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
6 /*============================================================
8 ** Header: Map used for interning of string literals.
10 ===========================================================*/
14 #include "stringliteralmap.h"
17 Thread safety in GlobalStringLiteralMap / StringLiteralMap
19 A single lock protects the N StringLiteralMap objects and single
20 GlobalStringLiteralMap rooted in the SystemDomain at any time. It is
22 SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal
24 At one time each StringLiteralMap had it's own lock to protect
25 the entry hash table as well, and Interlocked operations were done on the
26 ref count of the contained StringLiteralEntries. But anything of import
27 needed to be done under the global lock mentioned above or races would
28 result. (For example, an app domain shuts down, doing final release on
29 a StringLiteralEntry, but at that moment the entry is being handed out
30 in another appdomain and addref'd only after the count went to 0.)
34 Any AddRef()/Release() calls on StringLiteralEntry need to be under the lock.
35 Any insert/deletes from the StringLiteralMap or GlobalStringLiteralMap
36 need to be done under the lock.
38 The only thing you can do without the lock is look up an existing StringLiteralEntry
39 in an StringLiteralMap hash table. This is true because these lookup calls
40 will all come before destruction of the map, the hash table is safe for multiple readers,
41 and we know the StringLiteralEntry so found 1) can't be destroyed because that table keeps
42 an AddRef on it and 2) isn't internally modified once created.
45 #define GLOBAL_STRING_TABLE_BUCKET_SIZE 128
46 #define INIT_NUM_APP_DOMAIN_STRING_BUCKETS 59
47 #define INIT_NUM_GLOBAL_STRING_BUCKETS 131
49 // assumes that memory pools's per block data is same as sizeof (StringLiteralEntry)
50 #define EEHASH_MEMORY_POOL_GROW_COUNT 128
52 StringLiteralEntryArray *StringLiteralEntry::s_EntryList = NULL;
53 DWORD StringLiteralEntry::s_UsedEntries = NULL;
54 StringLiteralEntry *StringLiteralEntry::s_FreeEntryList = NULL;
56 StringLiteralMap::StringLiteralMap()
57 : m_StringToEntryHashTable(NULL)
68 void StringLiteralMap::Init()
74 PRECONDITION(CheckPointer(this));
75 INJECT_FAULT(ThrowOutOfMemory());
79 // Allocate the memory pool and set the initial count to quarter as grow count
80 m_MemoryPool = new MemoryPool (SIZEOF_EEHASH_ENTRY, EEHASH_MEMORY_POOL_GROW_COUNT, EEHASH_MEMORY_POOL_GROW_COUNT/4);
82 m_StringToEntryHashTable = new EEUnicodeStringLiteralHashTable ();
84 LockOwner lock = {&(SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal), IsOwnerOfCrst};
85 if (!m_StringToEntryHashTable->Init(INIT_NUM_APP_DOMAIN_STRING_BUCKETS, &lock, m_MemoryPool))
89 StringLiteralMap::~StringLiteralMap()
98 // We do need to take the globalstringliteralmap lock because we are manipulating
99 // StringLiteralEntry objects that belong to it.
100 // Note that we remember the current entry and relaese it only when the
101 // enumerator has advanced to the next entry so that we don't endup deleteing the
102 // current entry itself and killing the enumerator.
104 if (m_StringToEntryHashTable != NULL)
106 // We need the global lock anytime we release StringLiteralEntry objects
107 CrstHolder gch(&(SystemDomain::GetGlobalStringLiteralMapNoCreate()->m_HashTableCrstGlobal));
109 StringLiteralEntry *pEntry = NULL;
110 EEHashTableIteration Iter;
113 m_StringToEntryHashTable->SuppressSyncCheck();
116 m_StringToEntryHashTable->IterateStart(&Iter);
117 if (m_StringToEntryHashTable->IterateNext(&Iter))
119 pEntry = (StringLiteralEntry*)m_StringToEntryHashTable->IterateGetValue(&Iter);
121 while (m_StringToEntryHashTable->IterateNext(&Iter))
123 // Release the previous entry
128 pEntry = (StringLiteralEntry*)m_StringToEntryHashTable->IterateGetValue(&Iter);
130 // Release the last entry
134 // else there were no entries.
136 // Delete the hash table first. The dtor of the hash table would clean up all the entries.
137 delete m_StringToEntryHashTable;
140 // Delete the pool later, since the dtor above would need it.
141 if (m_MemoryPool != NULL)
147 STRINGREF *StringLiteralMap::GetStringLiteral(EEStringData *pStringData, BOOL bAddIfNotFound, BOOL bAppDomainWontUnload)
154 PRECONDITION(CheckPointer(this));
155 PRECONDITION(CheckPointer(pStringData));
161 DWORD dwHash = m_StringToEntryHashTable->GetHash(pStringData);
162 // Retrieve the string literal from the global string literal map.
163 CrstHolder gch(&(SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal));
165 // TODO: We can be more efficient by checking our local hash table now to see if
166 // someone beat us to inserting it. (m_StringToEntryHashTable->GetValue(pStringData, &Data))
167 // (Rather than waiting until after we look the string up in the global map)
169 StringLiteralEntryHolder pEntry(SystemDomain::GetGlobalStringLiteralMap()->GetStringLiteral(pStringData, dwHash, bAddIfNotFound));
171 _ASSERTE(pEntry || !bAddIfNotFound);
173 // If pEntry is non-null then the entry exists in the Global map. (either we retrieved it or added it just now)
176 // If the entry exists in the Global map and the appdomain wont ever unload then we really don't need to add a
177 // hashentry in the appdomain specific map.
178 // TODO: except that by not inserting into our local table we always take the global map lock
179 // and come into this path, when we could succeed at a lock free lookup above.
181 if (!bAppDomainWontUnload)
183 // Make sure some other thread has not already added it.
184 if (!m_StringToEntryHashTable->GetValue(pStringData, &Data))
186 // Insert the handle to the string into the hash table.
187 m_StringToEntryHashTable->InsertValue(pStringData, (LPVOID)pEntry, FALSE);
191 pEntry.Release(); //while we're still under lock
197 LOG((LF_APPDOMAIN, LL_INFO10000, "Avoided adding String literal to appdomain map: size: %d bytes\n", pStringData->GetCharCount()));
200 pEntry.SuppressRelease();
201 STRINGREF *pStrObj = NULL;
202 // Retrieve the string objectref from the string literal entry.
203 pStrObj = pEntry->GetStringObject();
204 _ASSERTE(!bAddIfNotFound || pStrObj);
207 // If the bAddIfNotFound flag is set then we better have a string
208 // string object at this point.
209 _ASSERTE(!bAddIfNotFound);
213 STRINGREF *StringLiteralMap::GetInternedString(STRINGREF *pString, BOOL bAddIfNotFound, BOOL bAppDomainWontUnload)
220 PRECONDITION(CheckPointer(this));
221 PRECONDITION(CheckPointer(pString));
226 EEStringData StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());
228 DWORD dwHash = m_StringToEntryHashTable->GetHash(&StringData);
229 if (m_StringToEntryHashTable->GetValue(&StringData, &Data, dwHash))
231 STRINGREF *pStrObj = NULL;
232 pStrObj = ((StringLiteralEntry*)Data)->GetStringObject();
233 _ASSERTE(!bAddIfNotFound || pStrObj);
239 CrstHolder gch(&(SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal));
241 // TODO: We can be more efficient by checking our local hash table now to see if
242 // someone beat us to inserting it. (m_StringToEntryHashTable->GetValue(pStringData, &Data))
243 // (Rather than waiting until after we look the string up in the global map)
245 // Retrieve the string literal from the global string literal map.
246 StringLiteralEntryHolder pEntry(SystemDomain::GetGlobalStringLiteralMap()->GetInternedString(pString, dwHash, bAddIfNotFound));
248 _ASSERTE(pEntry || !bAddIfNotFound);
250 // If pEntry is non-null then the entry exists in the Global map. (either we retrieved it or added it just now)
253 // If the entry exists in the Global map and the appdomain wont ever unload then we really don't need to add a
254 // hashentry in the appdomain specific map.
255 // TODO: except that by not inserting into our local table we always take the global map lock
256 // and come into this path, when we could succeed at a lock free lookup above.
258 if (!bAppDomainWontUnload)
260 // Since GlobalStringLiteralMap::GetInternedString() could have caused a GC,
261 // we need to recreate the string data.
262 StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());
264 // Make sure some other thread has not already added it.
265 if (!m_StringToEntryHashTable->GetValue(&StringData, &Data))
267 // Insert the handle to the string into the hash table.
268 m_StringToEntryHashTable->InsertValue(&StringData, (LPVOID)pEntry, FALSE);
272 pEntry.Release(); // while we're under lock
275 pEntry.SuppressRelease();
276 // Retrieve the string objectref from the string literal entry.
277 STRINGREF *pStrObj = NULL;
278 pStrObj = pEntry->GetStringObject();
282 // If the bAddIfNotFound flag is set then we better have a string
283 // string object at this point.
284 _ASSERTE(!bAddIfNotFound);
289 GlobalStringLiteralMap::GlobalStringLiteralMap()
290 : m_StringToEntryHashTable(NULL)
292 , m_HashTableCrstGlobal(CrstGlobalStrLiteralMap)
293 , m_LargeHeapHandleTable(SystemDomain::System(), GLOBAL_STRING_TABLE_BUCKET_SIZE)
303 m_LargeHeapHandleTable.RegisterCrstDebug(&m_HashTableCrstGlobal);
307 GlobalStringLiteralMap::~GlobalStringLiteralMap()
316 // if we are deleting the map then either it is shutdown time or else there was a race trying to create
317 // the initial map and this one was the loser
318 // (i.e. two threads made a map and the InterlockedCompareExchange failed for one of them and
319 // now it is deleting the map)
321 // if it's not the main map, then the map we are deleting better be empty!
323 // there must be *some* global table
324 _ASSERTE(SystemDomain::GetGlobalStringLiteralMapNoCreate() != NULL);
326 if (SystemDomain::GetGlobalStringLiteralMapNoCreate() != this)
328 // if this isn't the real global table then it must be empty
329 _ASSERTE(m_StringToEntryHashTable->IsEmpty());
331 // Delete the hash table first. The dtor of the hash table would clean up all the entries.
332 delete m_StringToEntryHashTable;
333 // Delete the pool later, since the dtor above would need it.
338 // We are shutting down, the OS will reclaim the memory from the StringLiteralEntries,
339 // m_MemoryPool and m_StringToEntryHashTable.
340 _ASSERTE(g_fProcessDetach);
344 void GlobalStringLiteralMap::Init()
350 PRECONDITION(CheckPointer(this));
351 INJECT_FAULT(ThrowOutOfMemory());
355 // Allocate the memory pool and set the initial count to quarter as grow count
356 m_MemoryPool = new MemoryPool (SIZEOF_EEHASH_ENTRY, EEHASH_MEMORY_POOL_GROW_COUNT, EEHASH_MEMORY_POOL_GROW_COUNT/4);
358 m_StringToEntryHashTable = new EEUnicodeStringLiteralHashTable ();
360 LockOwner lock = {&m_HashTableCrstGlobal, IsOwnerOfCrst};
361 if (!m_StringToEntryHashTable->Init(INIT_NUM_GLOBAL_STRING_BUCKETS, &lock, m_MemoryPool))
365 StringLiteralEntry *GlobalStringLiteralMap::GetStringLiteral(EEStringData *pStringData, DWORD dwHash, BOOL bAddIfNotFound)
372 PRECONDITION(CheckPointer(this));
373 PRECONDITION(CheckPointer(pStringData));
374 PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());
379 StringLiteralEntry *pEntry = NULL;
381 if (m_StringToEntryHashTable->GetValueSpeculative(pStringData, &Data, dwHash)) // Since we hold the critical section here, we can safely use the speculative variant of GetValue
383 pEntry = (StringLiteralEntry*)Data;
384 // If the entry is already in the table then addref it before we return it.
391 pEntry = AddStringLiteral(pStringData);
397 StringLiteralEntry *GlobalStringLiteralMap::GetInternedString(STRINGREF *pString, DWORD dwHash, BOOL bAddIfNotFound)
404 PRECONDITION(CheckPointer(this));
405 PRECONDITION(CheckPointer(pString));
406 PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());
410 EEStringData StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());
413 StringLiteralEntry *pEntry = NULL;
415 if (m_StringToEntryHashTable->GetValue(&StringData, &Data, dwHash))
417 pEntry = (StringLiteralEntry*)Data;
418 // If the entry is already in the table then addref it before we return it.
425 pEntry = AddInternedString(pString);
432 static void LogStringLiteral(__in_z const char* action, EEStringData *pStringData)
434 STATIC_CONTRACT_NOTHROW;
435 STATIC_CONTRACT_GC_NOTRIGGER;
436 STATIC_CONTRACT_FORBID_FAULT;
438 int length = pStringData->GetCharCount();
439 length = min(length, 100);
440 WCHAR *szString = (WCHAR *)_alloca((length + 1) * sizeof(WCHAR));
441 memcpyNoGCRefs((void*)szString, (void*)pStringData->GetStringBuffer(), length * sizeof(WCHAR));
442 szString[length] = '\0';
443 LOG((LF_APPDOMAIN, LL_INFO10000, "String literal \"%S\" %s to Global map, size %d bytes\n", szString, action, pStringData->GetCharCount()));
447 STRINGREF AllocateStringObject(EEStringData *pStringData)
457 // Create the COM+ string object.
458 DWORD cCount = pStringData->GetCharCount();
460 STRINGREF strObj = AllocateString(cCount);
462 GCPROTECT_BEGIN(strObj)
464 // Copy the string constant into the COM+ string object. The code
465 // will add an extra null at the end for safety purposes, but since
466 // we support embedded nulls, one should never treat the string as
468 LPWSTR strDest = strObj->GetBuffer();
469 memcpyNoGCRefs(strDest, pStringData->GetStringBuffer(), cCount*sizeof(WCHAR));
476 StringLiteralEntry *GlobalStringLiteralMap::AddStringLiteral(EEStringData *pStringData)
483 PRECONDITION(CheckPointer(this));
484 PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());
488 StringLiteralEntry *pRet;
491 LargeHeapHandleBlockHolder pStrObj(&m_LargeHeapHandleTable,1);
492 // Create the COM+ string object.
493 STRINGREF strObj = AllocateStringObject(pStringData);
495 // Allocate a handle for the string.
496 SetObjectReference(pStrObj[0], (OBJECTREF) strObj);
499 // Allocate the StringLiteralEntry.
500 StringLiteralEntryHolder pEntry(StringLiteralEntry::AllocateEntry(pStringData, (STRINGREF*)pStrObj[0]));
501 pStrObj.SuppressRelease();
502 // Insert the handle to the string into the hash table.
503 m_StringToEntryHashTable->InsertValue(pStringData, (LPVOID)pEntry, FALSE);
504 pEntry.SuppressRelease();
508 LogStringLiteral("added", pStringData);
515 StringLiteralEntry *GlobalStringLiteralMap::AddInternedString(STRINGREF *pString)
523 PRECONDITION(CheckPointer(this));
524 PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());
528 EEStringData StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());
529 StringLiteralEntry *pRet;
532 LargeHeapHandleBlockHolder pStrObj(&m_LargeHeapHandleTable,1);
533 SetObjectReference(pStrObj[0], (OBJECTREF) *pString);
535 // Since the allocation might have caused a GC we need to re-get the
537 StringData = EEStringData((*pString)->GetStringLength(), (*pString)->GetBuffer());
539 StringLiteralEntryHolder pEntry(StringLiteralEntry::AllocateEntry(&StringData, (STRINGREF*)pStrObj[0]));
540 pStrObj.SuppressRelease();
542 // Insert the handle to the string into the hash table.
543 m_StringToEntryHashTable->InsertValue(&StringData, (LPVOID)pEntry, FALSE);
544 pEntry.SuppressRelease();
551 void GlobalStringLiteralMap::RemoveStringLiteralEntry(StringLiteralEntry *pEntry)
557 PRECONDITION(CheckPointer(pEntry));
558 PRECONDITION(m_HashTableCrstGlobal.OwnedByCurrentThread());
559 PRECONDITION(CheckPointer(this));
563 // Remove the entry from the hash table.
567 EEStringData StringData;
568 pEntry->GetStringData(&StringData);
571 bSuccess = m_StringToEntryHashTable->DeleteValue(&StringData);
572 // this assert is comented out to accomodate case when StringLiteralEntryHolder
573 // releases this object after failed insertion into hash
574 //_ASSERTE(bSuccess);
577 // We need to do this logging within the GCX_COOP(), as a gc will render
578 // our StringData pointers stale.
581 LogStringLiteral("removed", &StringData);
585 // Release the object handle that the entry was using.
586 STRINGREF *pObjRef = pEntry->GetStringObject();
587 m_LargeHeapHandleTable.ReleaseHandles((OBJECTREF*)pObjRef, 1);
590 // We do not delete the StringLiteralEntry itself that will be done in the
591 // release method of the StringLiteralEntry.
594 StringLiteralEntry *StringLiteralEntry::AllocateEntry(EEStringData *pStringData, STRINGREF *pStringObj)
599 GC_TRIGGERS; // GC_TRIGGERS because in the precondition below GetGlobalStringLiteralMap() might need to create the map
601 PRECONDITION(SystemDomain::GetGlobalStringLiteralMap()->m_HashTableCrstGlobal.OwnedByCurrentThread());
605 // Note: we don't synchronize here because allocateEntry is called when HashCrst is held.
607 if (s_FreeEntryList != NULL)
609 pMem = s_FreeEntryList;
610 s_FreeEntryList = s_FreeEntryList->m_pNext;
611 _ASSERTE (((StringLiteralEntry*)pMem)->m_bDeleted);
615 if (s_EntryList == NULL || (s_UsedEntries >= MAX_ENTRIES_PER_CHUNK))
617 StringLiteralEntryArray *pNew = new StringLiteralEntryArray();
618 pNew->m_pNext = s_EntryList;
622 pMem = &(s_EntryList->m_Entries[s_UsedEntries++*sizeof(StringLiteralEntry)]);
624 _ASSERTE (pMem && "Unable to allocate String literal Entry");
626 return new (pMem) StringLiteralEntry (pStringData, pStringObj);
629 void StringLiteralEntry::DeleteEntry (StringLiteralEntry *pEntry)
635 PRECONDITION(SystemDomain::GetGlobalStringLiteralMapNoCreate()->m_HashTableCrstGlobal.OwnedByCurrentThread());
639 _ASSERTE (VolatileLoad(&pEntry->m_dwRefCount) == 0);
642 memset (pEntry, 0xc, sizeof(StringLiteralEntry));
646 pEntry->m_bDeleted = TRUE;
649 // The free list needs protection from the m_HashTableCrstGlobal
650 pEntry->m_pNext = s_FreeEntryList;
651 s_FreeEntryList = pEntry;