Skip to content

Commit c221bec

Browse files
committed
Added patched public/tier0/memalloc.h file for ob-valve SDK
1 parent 468caf2 commit c221bec

File tree

1 file changed

+361
-0
lines changed
  • src/patches/hl2sdk-ob-valve/public/tier0

1 file changed

+361
-0
lines changed
Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
2+
//
3+
// Purpose: This header should never be used directly from leaf code!!!
4+
// Instead, just add the file memoverride.cpp into your project and all this
5+
// will automagically be used
6+
//
7+
// $NoKeywords: $
8+
//=============================================================================//
9+
10+
#ifndef TIER0_MEMALLOC_H
11+
#define TIER0_MEMALLOC_H
12+
13+
#ifdef _WIN32
14+
#pragma once
15+
#endif
16+
17+
#if defined(_LINUX) || defined(__APPLE__)
18+
#define NO_MALLOC_OVERRIDE
19+
#endif
20+
21+
// Define this in release to get memory tracking even in release builds
22+
//#define USE_MEM_DEBUG 1
23+
24+
#if defined( _MEMTEST )
25+
#define USE_MEM_DEBUG 1
26+
#endif
27+
28+
// Undefine this if using a compiler lacking threadsafe RTTI (like vc6)
29+
#define MEM_DEBUG_CLASSNAME 1
30+
31+
#if !defined(STEAM) && !defined(NO_MALLOC_OVERRIDE)
32+
33+
#include <stddef.h>
34+
#include "tier0/mem.h"
35+
#include "commonmacros.h"
36+
37+
struct _CrtMemState;
38+
39+
#define MEMALLOC_VERSION 1
40+
41+
typedef size_t (*MemAllocFailHandler_t)( size_t );
42+
43+
//-----------------------------------------------------------------------------
44+
// NOTE! This should never be called directly from leaf code
45+
// Just use new,delete,malloc,free etc. They will call into this eventually
46+
//-----------------------------------------------------------------------------
47+
abstract_class IMemAlloc
48+
{
49+
public:
50+
inline void *IndirectAlloc( size_t nSize ) { return Alloc( nSize ); }
51+
52+
// Release versions
53+
virtual void *Alloc( size_t nSize ) = 0;
54+
virtual void *Realloc( void *pMem, size_t nSize ) = 0;
55+
virtual void Free( void *pMem ) = 0;
56+
virtual void *Expand_NoLongerSupported( void *pMem, size_t nSize ) = 0;
57+
58+
// Debug versions
59+
virtual void *Alloc( size_t nSize, const char *pFileName, int nLine ) = 0;
60+
virtual void *Realloc( void *pMem, size_t nSize, const char *pFileName, int nLine ) = 0;
61+
virtual void Free( void *pMem, const char *pFileName, int nLine ) = 0;
62+
virtual void *Expand_NoLongerSupported( void *pMem, size_t nSize, const char *pFileName, int nLine ) = 0;
63+
64+
// Returns size of a particular allocation
65+
virtual size_t GetSize( void *pMem ) = 0;
66+
67+
// Force file + line information for an allocation
68+
virtual void PushAllocDbgInfo( const char *pFileName, int nLine ) = 0;
69+
virtual void PopAllocDbgInfo() = 0;
70+
71+
// FIXME: Remove when we have our own allocator
72+
// these methods of the Crt debug code is used in our codebase currently
73+
virtual long CrtSetBreakAlloc( long lNewBreakAlloc ) = 0;
74+
virtual int CrtSetReportMode( int nReportType, int nReportMode ) = 0;
75+
virtual int CrtIsValidHeapPointer( const void *pMem ) = 0;
76+
virtual int CrtIsValidPointer( const void *pMem, unsigned int size, int access ) = 0;
77+
virtual int CrtCheckMemory( void ) = 0;
78+
virtual int CrtSetDbgFlag( int nNewFlag ) = 0;
79+
virtual void CrtMemCheckpoint( _CrtMemState *pState ) = 0;
80+
81+
// FIXME: Make a better stats interface
82+
virtual void DumpStats() = 0;
83+
virtual void DumpStatsFileBase( char const *pchFileBase ) = 0;
84+
85+
// FIXME: Remove when we have our own allocator
86+
virtual void* CrtSetReportFile( int nRptType, void* hFile ) = 0;
87+
virtual void* CrtSetReportHook( void* pfnNewHook ) = 0;
88+
virtual int CrtDbgReport( int nRptType, const char * szFile,
89+
int nLine, const char * szModule, const char * pMsg ) = 0;
90+
91+
virtual int heapchk() = 0;
92+
93+
virtual bool IsDebugHeap() = 0;
94+
95+
virtual void GetActualDbgInfo( const char *&pFileName, int &nLine ) = 0;
96+
virtual void RegisterAllocation( const char *pFileName, int nLine, int nLogicalSize, int nActualSize, unsigned nTime ) = 0;
97+
virtual void RegisterDeallocation( const char *pFileName, int nLine, int nLogicalSize, int nActualSize, unsigned nTime ) = 0;
98+
99+
virtual int GetVersion() = 0;
100+
101+
virtual void CompactHeap() = 0;
102+
103+
// Function called when malloc fails or memory limits hit to attempt to free up memory (can come in any thread)
104+
virtual MemAllocFailHandler_t SetAllocFailHandler( MemAllocFailHandler_t pfnMemAllocFailHandler ) = 0;
105+
106+
virtual void DumpBlockStats( void * ) = 0;
107+
108+
#if defined( _MEMTEST )
109+
virtual void SetStatsExtraInfo( const char *pMapName, const char *pComment ) = 0;
110+
#endif
111+
112+
// Returns 0 if no failure, otherwise the size_t of the last requested chunk
113+
virtual size_t MemoryAllocFailed() = 0;
114+
};
115+
116+
//-----------------------------------------------------------------------------
117+
// Singleton interface
118+
//-----------------------------------------------------------------------------
119+
MEM_INTERFACE IMemAlloc *g_pMemAlloc;
120+
121+
//-----------------------------------------------------------------------------
122+
123+
inline void *MemAlloc_AllocAligned( size_t size, size_t align )
124+
{
125+
unsigned char *pAlloc, *pResult;
126+
127+
if (!IsPowerOfTwo(uint(align)))
128+
return NULL;
129+
130+
align = (align > sizeof(void *) ? align : sizeof(void *)) - 1;
131+
132+
if ( (pAlloc = (unsigned char*)g_pMemAlloc->Alloc( sizeof(void *) + align + size ) ) == (unsigned char*)NULL)
133+
return NULL;
134+
135+
pResult = (unsigned char*)( (size_t)(pAlloc + sizeof(void *) + align ) & ~align );
136+
((unsigned char**)(pResult))[-1] = pAlloc;
137+
138+
return (void *)pResult;
139+
}
140+
141+
inline void *MemAlloc_AllocAligned( size_t size, size_t align, const char *pszFile, int nLine )
142+
{
143+
unsigned char *pAlloc, *pResult;
144+
145+
if (!IsPowerOfTwo(uint(align)))
146+
return NULL;
147+
148+
align = (align > sizeof(void *) ? align : sizeof(void *)) - 1;
149+
150+
if ( (pAlloc = (unsigned char*)g_pMemAlloc->Alloc( sizeof(void *) + align + size, pszFile, nLine ) ) == (unsigned char*)NULL)
151+
return NULL;
152+
153+
pResult = (unsigned char*)( (size_t)(pAlloc + sizeof(void *) + align ) & ~align );
154+
((unsigned char**)(pResult))[-1] = pAlloc;
155+
156+
return (void *)pResult;
157+
}
158+
159+
inline void *MemAlloc_ReallocAligned( void *ptr, size_t size, size_t align )
160+
{
161+
if ( !IsPowerOfTwo( uint(align) ) )
162+
return NULL;
163+
164+
// Don't change alignment between allocation + reallocation.
165+
if ( ( (size_t)ptr & ( align - 1 ) ) != 0 )
166+
return NULL;
167+
168+
if ( !ptr )
169+
return MemAlloc_AllocAligned( size, align );
170+
171+
void *pAlloc, *pResult;
172+
173+
// Figure out the actual allocation point
174+
pAlloc = ptr;
175+
pAlloc = (void *)(((size_t)pAlloc & ~( sizeof(void *) - 1 ) ) - sizeof(void *));
176+
pAlloc = *( (void **)pAlloc );
177+
178+
// See if we have enough space
179+
size_t nOffset = (size_t)ptr - (size_t)pAlloc;
180+
size_t nOldSize = g_pMemAlloc->GetSize( pAlloc );
181+
if ( nOldSize >= size + nOffset )
182+
return ptr;
183+
184+
pResult = MemAlloc_AllocAligned( size, align );
185+
memcpy( pResult, ptr, nOldSize - nOffset );
186+
g_pMemAlloc->Free( pAlloc );
187+
return pResult;
188+
}
189+
190+
inline void MemAlloc_FreeAligned( void *pMemBlock )
191+
{
192+
void *pAlloc;
193+
194+
if ( pMemBlock == NULL )
195+
return;
196+
197+
pAlloc = pMemBlock;
198+
199+
// pAlloc points to the pointer to starting of the memory block
200+
pAlloc = (void *)(((size_t)pAlloc & ~( sizeof(void *) - 1 ) ) - sizeof(void *));
201+
202+
// pAlloc is the pointer to the start of memory block
203+
pAlloc = *( (void **)pAlloc );
204+
g_pMemAlloc->Free( pAlloc );
205+
}
206+
207+
inline size_t MemAlloc_GetSizeAligned( void *pMemBlock )
208+
{
209+
void *pAlloc;
210+
211+
if ( pMemBlock == NULL )
212+
return 0;
213+
214+
pAlloc = pMemBlock;
215+
216+
// pAlloc points to the pointer to starting of the memory block
217+
pAlloc = (void *)(((size_t)pAlloc & ~( sizeof(void *) - 1 ) ) - sizeof(void *));
218+
219+
// pAlloc is the pointer to the start of memory block
220+
pAlloc = *((void **)pAlloc );
221+
return g_pMemAlloc->GetSize( pAlloc ) - ( (byte *)pMemBlock - (byte *)pAlloc );
222+
}
223+
224+
//-----------------------------------------------------------------------------
225+
226+
#if (defined(_DEBUG) || defined(USE_MEM_DEBUG))
227+
#define MEM_ALLOC_CREDIT_(tag) CMemAllocAttributeAlloction memAllocAttributeAlloction( tag, __LINE__ )
228+
#define MemAlloc_PushAllocDbgInfo( pszFile, line ) g_pMemAlloc->PushAllocDbgInfo( pszFile, line )
229+
#define MemAlloc_PopAllocDbgInfo() g_pMemAlloc->PopAllocDbgInfo()
230+
#define MemAlloc_RegisterAllocation( pFileName, nLine, nLogicalSize, nActualSize, nTime ) g_pMemAlloc->RegisterAllocation( pFileName, nLine, nLogicalSize, nActualSize, nTime )
231+
#define MemAlloc_RegisterDeallocation( pFileName, nLine, nLogicalSize, nActualSize, nTime ) g_pMemAlloc->RegisterDeallocation( pFileName, nLine, nLogicalSize, nActualSize, nTime )
232+
#else
233+
#define MEM_ALLOC_CREDIT_(tag) ((void)0)
234+
#define MemAlloc_PushAllocDbgInfo( pszFile, line ) ((void)0)
235+
#define MemAlloc_PopAllocDbgInfo() ((void)0)
236+
#define MemAlloc_RegisterAllocation( pFileName, nLine, nLogicalSize, nActualSize, nTime ) ((void)0)
237+
#define MemAlloc_RegisterDeallocation( pFileName, nLine, nLogicalSize, nActualSize, nTime ) ((void)0)
238+
#endif
239+
240+
//-----------------------------------------------------------------------------
241+
242+
class CMemAllocAttributeAlloction
243+
{
244+
public:
245+
CMemAllocAttributeAlloction( const char *pszFile, int line )
246+
{
247+
MemAlloc_PushAllocDbgInfo( pszFile, line );
248+
}
249+
250+
~CMemAllocAttributeAlloction()
251+
{
252+
MemAlloc_PopAllocDbgInfo();
253+
}
254+
};
255+
256+
#define MEM_ALLOC_CREDIT() MEM_ALLOC_CREDIT_(__FILE__)
257+
258+
//-----------------------------------------------------------------------------
259+
260+
#if defined(_WIN32) && ( defined(_DEBUG) || defined(USE_MEM_DEBUG) )
261+
262+
#pragma warning(disable:4290)
263+
#pragma warning(push)
264+
#include <typeinfo.h>
265+
266+
// MEM_DEBUG_CLASSNAME is opt-in.
267+
// Note: typeid().name() is not threadsafe, so if the project needs to access it in multiple threads
268+
// simultaneously, it'll need a mutex.
269+
#if defined(_CPPRTTI) && defined(MEM_DEBUG_CLASSNAME)
270+
#define MEM_ALLOC_CREDIT_CLASS() MEM_ALLOC_CREDIT_( typeid(*this).name() )
271+
#define MEM_ALLOC_CLASSNAME(type) (typeid((type*)(0)).name())
272+
#else
273+
#define MEM_ALLOC_CREDIT_CLASS() MEM_ALLOC_CREDIT_( __FILE__ )
274+
#define MEM_ALLOC_CLASSNAME(type) (__FILE__)
275+
#endif
276+
277+
// MEM_ALLOC_CREDIT_FUNCTION is used when no this pointer is available ( inside 'new' overloads, for example )
278+
#ifdef _MSC_VER
279+
#define MEM_ALLOC_CREDIT_FUNCTION() MEM_ALLOC_CREDIT_( __FUNCTION__ )
280+
#else
281+
#define MEM_ALLOC_CREDIT_FUNCTION() (__FILE__)
282+
#endif
283+
284+
#pragma warning(pop)
285+
#else
286+
#define MEM_ALLOC_CREDIT_CLASS()
287+
#define MEM_ALLOC_CLASSNAME(type) NULL
288+
#define MEM_ALLOC_CREDIT_FUNCTION()
289+
#endif
290+
291+
//-----------------------------------------------------------------------------
292+
293+
#if (defined(_DEBUG) || defined(USE_MEM_DEBUG))
294+
struct MemAllocFileLine_t
295+
{
296+
const char *pszFile;
297+
int line;
298+
};
299+
300+
#define MEMALLOC_DEFINE_EXTERNAL_TRACKING( tag ) \
301+
static CUtlMap<void *, MemAllocFileLine_t, int> g_##tag##Allocs( DefLessFunc( void *) ); \
302+
static const char *g_psz##tag##Alloc = strcpy( (char *)g_pMemAlloc->Alloc( strlen( #tag "Alloc" ) + 1, "intentional leak", 0 ), #tag "Alloc" );
303+
304+
#define MemAlloc_RegisterExternalAllocation( tag, p, size ) \
305+
if ( !p ) \
306+
; \
307+
else \
308+
{ \
309+
MemAllocFileLine_t fileLine = { g_psz##tag##Alloc, 0 }; \
310+
g_pMemAlloc->GetActualDbgInfo( fileLine.pszFile, fileLine.line ); \
311+
if ( fileLine.pszFile != g_psz##tag##Alloc ) \
312+
{ \
313+
g_##tag##Allocs.Insert( p, fileLine ); \
314+
} \
315+
\
316+
MemAlloc_RegisterAllocation( fileLine.pszFile, fileLine.line, size, size, 0); \
317+
}
318+
319+
#define MemAlloc_RegisterExternalDeallocation( tag, p, size ) \
320+
if ( !p ) \
321+
; \
322+
else \
323+
{ \
324+
MemAllocFileLine_t fileLine = { g_psz##tag##Alloc, 0 }; \
325+
CUtlMap<void *, MemAllocFileLine_t, int>::IndexType_t iRecordedFileLine = g_##tag##Allocs.Find( p ); \
326+
if ( iRecordedFileLine != g_##tag##Allocs.InvalidIndex() ) \
327+
{ \
328+
fileLine = g_##tag##Allocs[iRecordedFileLine]; \
329+
g_##tag##Allocs.RemoveAt( iRecordedFileLine ); \
330+
} \
331+
\
332+
MemAlloc_RegisterDeallocation( fileLine.pszFile, fileLine.line, size, size, 0); \
333+
}
334+
335+
#else
336+
337+
#define MEMALLOC_DEFINE_EXTERNAL_TRACKING( tag )
338+
#define MemAlloc_RegisterExternalAllocation( tag, p, size ) ((void)0)
339+
#define MemAlloc_RegisterExternalDeallocation( tag, p, size ) ((void)0)
340+
341+
#endif
342+
343+
//-----------------------------------------------------------------------------
344+
345+
#endif // !STEAM && !NO_MALLOC_OVERRIDE
346+
347+
//-----------------------------------------------------------------------------
348+
349+
#if !defined(STEAM) && defined(NO_MALLOC_OVERRIDE)
350+
351+
#define MEM_ALLOC_CREDIT_(tag) ((void)0)
352+
#define MEM_ALLOC_CREDIT() MEM_ALLOC_CREDIT_(__FILE__)
353+
#define MEM_ALLOC_CREDIT_CLASS()
354+
#define MEM_ALLOC_CLASSNAME(type) NULL
355+
#define MEM_ALLOC_CREDIT_FUNCTION()
356+
357+
#endif // !STEAM && NO_MALLOC_OVERRIDE
358+
359+
//-----------------------------------------------------------------------------
360+
361+
#endif /* TIER0_MEMALLOC_H */

0 commit comments

Comments
 (0)