blob: be0b9c3fd62c2238cd3e44980e061704d43de823 (
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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#ifndef ZONE_H
#define ZONE_H
#pragma once
#include "tier0/dbg.h"
void Memory_Init (void);
void Memory_Shutdown( void );
void *Hunk_Alloc(int size, bool bClear = true );
void *Hunk_AllocName (int size, const char *name, bool bClear = true );
int Hunk_LowMark (void);
void Hunk_FreeToLowMark (int mark);
void Hunk_Check (void);
int Hunk_MallocSize();
int Hunk_Size();
void Hunk_Print();
template< typename T >
class CHunkMemory
{
public:
// constructor, destructor
CHunkMemory( int nGrowSize = 0, int nInitSize = 0 ) { m_pMemory = NULL; m_nAllocated = 0; if ( nInitSize ) Grow( nInitSize ); }
CHunkMemory( T* pMemory, int numElements ) { Assert( 0 ); }
// Can we use this index?
bool IsIdxValid( int i ) const { return (i >= 0) && (i < m_nAllocated); }
// Gets the base address
T* Base() { return (T*)m_pMemory; }
const T* Base() const { return (T*)m_pMemory; }
// element access
T& operator[]( int i ) { Assert( IsIdxValid(i) ); return Base()[i]; }
const T& operator[]( int i ) const { Assert( IsIdxValid(i) ); return Base()[i]; }
T& Element( int i ) { Assert( IsIdxValid(i) ); return Base()[i]; }
const T& Element( int i ) const { Assert( IsIdxValid(i) ); return Base()[i]; }
// Attaches the buffer to external memory....
void SetExternalBuffer( T* pMemory, int numElements ) { Assert( 0 ); }
// Size
int NumAllocated() const { return m_nAllocated; }
int Count() const { return m_nAllocated; }
// Grows the memory, so that at least allocated + num elements are allocated
void Grow( int num = 1 ) { Assert( !m_nAllocated ); m_pMemory = (T *)Hunk_Alloc( num * sizeof(T), false ); m_nAllocated = num; }
// Makes sure we've got at least this much memory
void EnsureCapacity( int num ) { Assert( num <= m_nAllocated ); }
// Memory deallocation
void Purge() { m_nAllocated = 0; }
// Purge all but the given number of elements (NOT IMPLEMENTED IN )
void Purge( int numElements ) { Assert( 0 ); }
// is the memory externally allocated?
bool IsExternallyAllocated() const { return false; }
// Set the size by which the memory grows
void SetGrowSize( int size ) {}
private:
T *m_pMemory;
int m_nAllocated;
};
#endif // ZONE_H
|