Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/public/tier1/utlsymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ class CUtlSymbolTable
unsigned short m_iOffset; // Index into the string pool.
};

// Helper to store search string on the stack for safe thread access
class CStringPoolIndexSearch : public CStringPoolIndex
{
public:
CStringPoolIndexSearch( const char* pString ) : CStringPoolIndex( 0xFFFF, 0xFFFF )
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick, thanks!

{
m_pUserSearchString = pString;
}
const char* m_pUserSearchString;
};

class CLess
{
public:
Expand Down
8 changes: 4 additions & 4 deletions src/tier1/utlsymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ bool CUtlSymbolTable::CLess::operator()( const CStringPoolIndex &i1, const CStri
// right now at least, because m_LessFunc is the first member of CUtlRBTree, and m_Lookup
// is the first member of CUtlSymbolTabke, this == pTable
CUtlSymbolTable *pTable = (CUtlSymbolTable *)( (byte *)this - offsetof(CUtlSymbolTable::CTree, m_LessFunc) ) - offsetof(CUtlSymbolTable, m_Lookup );
const char* str1 = (i1 == INVALID_STRING_INDEX) ? pTable->m_pUserSearchString :
const char* str1 = (i1 == INVALID_STRING_INDEX) ? static_cast< const CUtlSymbolTable::CStringPoolIndexSearch& >( i1 ).m_pUserSearchString :
pTable->StringFromIndex( i1 );
const char* str2 = (i2 == INVALID_STRING_INDEX) ? pTable->m_pUserSearchString :
const char* str2 = (i2 == INVALID_STRING_INDEX) ? static_cast< const CUtlSymbolTable::CStringPoolIndexSearch& >( i2 ).m_pUserSearchString :
pTable->StringFromIndex( i2 );

if ( !str1 && str2 )
Expand Down Expand Up @@ -181,11 +181,11 @@ CUtlSymbol CUtlSymbolTable::Find( const char* pString ) const
return CUtlSymbol();

// Store a special context used to help with insertion
m_pUserSearchString = pString;
CStringPoolIndexSearch search( pString );

// Passing this special invalid symbol makes the comparison function
// use the string passed in the context
UtlSymId_t idx = m_Lookup.Find( INVALID_STRING_INDEX );
UtlSymId_t idx = m_Lookup.Find( search );

#ifdef _DEBUG
m_pUserSearchString = NULL;
Expand Down