-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathmem.cpp
100 lines (86 loc) · 2.35 KB
/
mem.cpp
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Memory allocation!
//
// $NoKeywords: $
//=============================================================================//
#include "pch_tier0.h"
#include "tier0/mem.h"
#ifdef OSX
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#include "tier0/dbg.h"
#include "tier0/minidump.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifndef STEAM
#define PvRealloc realloc
#define PvAlloc malloc
#define PvExpand _expand
#endif
enum
{
MAX_STACK_DEPTH = 32
};
static uint8 *s_pBuf = NULL;
static int s_pBufStackDepth[MAX_STACK_DEPTH];
static int s_nBufDepth = -1;
static int s_nBufCurSize = 0;
static int s_nBufAllocSize = 0;
static bool s_oomerror_called = false;
void MemAllocOOMError( size_t nSize )
{
if ( !s_oomerror_called )
{
s_oomerror_called = true;
MinidumpUserStreamInfoAppend( "MemAllocOOMError: %u bytes\n", (uint)nSize );
//$ TODO: Need a good error message here.
// A basic advice to try lowering texture settings is just most-likely to help users who are exhausting address
// space, but not necessarily the cause. Ideally the engine wouldn't let you get here because of too-high settings.
Error( "Out of memory or address space. Texture quality setting may be too high.\n" );
}
}
//-----------------------------------------------------------------------------
// Other DLL-exported methods for particular kinds of memory
//-----------------------------------------------------------------------------
void *MemAllocScratch( int nMemSize )
{
// Minimally allocate 1M scratch
if (s_nBufAllocSize < s_nBufCurSize + nMemSize)
{
s_nBufAllocSize = s_nBufCurSize + nMemSize;
if (s_nBufAllocSize < 1024 * 1024)
{
s_nBufAllocSize = 1024 * 1024;
}
if (s_pBuf)
{
s_pBuf = (uint8*)PvRealloc( s_pBuf, s_nBufAllocSize );
Assert( s_pBuf );
}
else
{
s_pBuf = (uint8*)PvAlloc( s_nBufAllocSize );
}
}
int nBase = s_nBufCurSize;
s_nBufCurSize += nMemSize;
++s_nBufDepth;
Assert( s_nBufDepth < MAX_STACK_DEPTH );
s_pBufStackDepth[s_nBufDepth] = nMemSize;
return &s_pBuf[nBase];
}
void MemFreeScratch()
{
Assert( s_nBufDepth >= 0 );
s_nBufCurSize -= s_pBufStackDepth[s_nBufDepth];
--s_nBufDepth;
}
#ifdef POSIX
void ZeroMemory( void *mem, size_t length )
{
memset( mem, 0x0, length );
}
#endif