aboutsummaryrefslogtreecommitdiff
path: root/sp/src/game/shared/gamestringpool.cpp
blob: f75d4ff989300dd9ce824c0acc3d69eea2f030c0 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"

#include "utlhashtable.h"
#ifndef GC
#include "igamesystem.h"
#endif
#include "gamestringpool.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//-----------------------------------------------------------------------------
// Purpose: The actual storage for pooled per-level strings
//-----------------------------------------------------------------------------
#ifdef GC
class CGameStringPool
#else
class CGameStringPool : public CBaseGameSystem
#endif
{
	virtual char const *Name() { return "CGameStringPool"; }
	virtual void LevelShutdownPostEntity() { FreeAll(); }

	void FreeAll()
	{
#if 0 && _DEBUG
		m_Strings.DbgCheckIntegrity();
		m_KeyLookupCache.DbgCheckIntegrity();
#endif
		m_Strings.Purge();
		m_KeyLookupCache.Purge();
	}

	CUtlHashtable<CUtlConstString> m_Strings;
	CUtlHashtable<const void*, const char*> m_KeyLookupCache;

public:

	CGameStringPool() : m_Strings(256) { }

	~CGameStringPool() { FreeAll(); }

	void Dump( void )
	{
		CUtlVector<const char*> strings( 0, m_Strings.Count() );
		for (UtlHashHandle_t i = m_Strings.FirstHandle(); i != m_Strings.InvalidHandle(); i = m_Strings.NextHandle(i))
		{
			strings.AddToTail( strings[i] );
		}
		struct _Local {
			static int __cdecl F(const char * const *a, const char * const *b) { return strcmp(*a, *b); }
		};
		strings.Sort( _Local::F );
		
		for ( int i = 0; i < strings.Count(); ++i )
		{
			DevMsg( "  %d (0x%p) : %s\n", i, strings[i], strings[i] );
		}
		DevMsg( "\n" );
		DevMsg( "Size:  %d items\n", strings.Count() );
	}

	const char *Find(const char *string)
	{
		UtlHashHandle_t i = m_Strings.Find( string );
		return i == m_Strings.InvalidHandle() ? NULL : m_Strings[ i ].Get();
	}

	const char *Allocate(const char *string)
	{
		return m_Strings[ m_Strings.Insert( string ) ].Get();
	}

	const char *AllocateWithKey(const char *string, const void* key)
	{
		const char * &cached = m_KeyLookupCache[ m_KeyLookupCache.Insert( key, NULL ) ];
		if (cached == NULL)
		{
			cached = Allocate( string );
		}
		return cached;
	}
};

static CGameStringPool g_GameStringPool;

#ifndef GC
//-----------------------------------------------------------------------------
// String system accessor
//-----------------------------------------------------------------------------
IGameSystem *GameStringSystem()
{
	return &g_GameStringPool;
}
#endif


//-----------------------------------------------------------------------------
// Purpose: The public accessor for the level-global pooled strings
//-----------------------------------------------------------------------------
string_t AllocPooledString( const char * pszValue )
{
	if (pszValue && *pszValue)
		return MAKE_STRING( g_GameStringPool.Allocate( pszValue ) );
	return NULL_STRING;
}

string_t AllocPooledString_StaticConstantStringPointer( const char * pszGlobalConstValue )
{
	Assert(pszGlobalConstValue && *pszGlobalConstValue);
	return MAKE_STRING( g_GameStringPool.AllocateWithKey( pszGlobalConstValue, pszGlobalConstValue ) );
}

string_t FindPooledString( const char *pszValue )
{
	return MAKE_STRING( g_GameStringPool.Find( pszValue ) );
}

#if !defined(CLIENT_DLL) && !defined( GC )
//------------------------------------------------------------------------------
// Purpose: 
//------------------------------------------------------------------------------
void CC_DumpGameStringTable( void )
{
	if ( !UTIL_IsCommandIssuedByServerAdmin() )
		return;

	g_GameStringPool.Dump();
}
static ConCommand dumpgamestringtable("dumpgamestringtable", CC_DumpGameStringTable, "Dump the contents of the game string table to the console.", FCVAR_CHEAT);
#endif