summaryrefslogtreecommitdiff
path: root/engine/traceinit.cpp
blob: c3a26e552fc372a340b30b0930ac58609e0dc215 (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
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//


#include "basetypes.h"
#include "traceinit.h"
#include "utlvector.h"
#include "sys.h"
#include "common.h"

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

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
class CInitTracker
{
public:
	enum
	{
		NUM_LISTS = 4,
	};

	//-----------------------------------------------------------------------------
	// Purpose: 
	//-----------------------------------------------------------------------------
	class InitFunc
	{
	public:
		const char	*initname;
		const char	*shutdownname;
		int			referencecount;
		int			sequence;
		bool		warningprinted;
		// Profiling data
		float		inittime;
		float		shutdowntime;
	};

								CInitTracker( void );
								~CInitTracker( void );

	void						Init( const char *init, const char *shutdown, int listnum );
	void						Shutdown( const char *shutdown, int listnum );

private:
	int							m_nNumFuncs[ NUM_LISTS ];
	CUtlVector < InitFunc * >	m_Funcs[ NUM_LISTS ];
};

static CInitTracker g_InitTracker;

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CInitTracker::CInitTracker( void )
{
	for ( int l = 0; l < NUM_LISTS; l++ )
	{
		m_nNumFuncs[ l ] = 0;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CInitTracker::~CInitTracker( void )
{
	for ( int l = 0; l < NUM_LISTS; l++ )
	{
		//assert( m_nNumFuncs[l] == 0 );
		for ( int i = 0; i < m_nNumFuncs[l]; i++ )
		{
			InitFunc *f = m_Funcs[ l ][ i ];
			if ( f->referencecount )
			{
				Msg( "Missing shutdown function for %s : %s\n", f->initname, f->shutdownname );
			}
			delete f;
		}
		m_Funcs[ l ].RemoveAll();
		m_nNumFuncs[ l ] = 0;
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *init - 
//			*shutdown - 
//-----------------------------------------------------------------------------
void CInitTracker::Init( const char *init, const char *shutdown, int listnum )
{
	InitFunc *f = new InitFunc;
	Assert( f );
	f->initname			= init;
	f->shutdownname		= shutdown;
	f->sequence			= m_nNumFuncs[ listnum ];
	f->referencecount	= 1;
	f->warningprinted	= false;
	f->inittime			= 0.0; //Sys_FloatTime();
	f->shutdowntime		= 0.0;

	m_Funcs[ listnum ].AddToHead( f );
	m_nNumFuncs[ listnum ]++;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *shutdown - 
//-----------------------------------------------------------------------------
void CInitTracker::Shutdown( const char *shutdown, int listnum )
{
	if( !m_nNumFuncs[ listnum ] )
	{
		Msg( "Mismatched shutdown function %s\n", shutdown );
		return;
	}
	
	int i = 0;
	InitFunc *f = NULL;
	for ( i = 0; i < m_nNumFuncs[ listnum ]; i++ )
	{
		f = m_Funcs[ listnum ][ i ];
		if ( f->referencecount )
			break;
	}
	
	if ( f && f->referencecount && stricmp( f->shutdownname, shutdown ) )
	{
		if ( !f->warningprinted )
		{
			f->warningprinted = true;
			//Msg( "Shutdown function %s called out of order, expecting %s\n", shutdown, f->shutdownname );
		}
	}

	for ( i = 0; i < m_nNumFuncs[ listnum ]; i++ )
	{
		f = m_Funcs[ listnum ][ i ];

		if ( !stricmp( f->shutdownname, shutdown ) )
		{
			Assert( f->referencecount );
			//f->shutdowntime = Sys_FloatTime();
			f->referencecount--;
			return;
		}
	}

	Msg( "Shutdown function %s not in list!!!\n", shutdown );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *i - 
//			*s - 
//-----------------------------------------------------------------------------
void TraceInit( const char *i, const char *s, int listnum )
{
	g_InitTracker.Init( i, s, listnum );

	COM_TimestampedLog( "%s", i );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *s - 
//-----------------------------------------------------------------------------
void TraceShutdown( const char *s, int listnum )
{
	g_InitTracker.Shutdown( s, listnum );
}