forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutlallocation.h
134 lines (114 loc) · 2.33 KB
/
utlallocation.h
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
// The CUtlAllocation class:
// A single allocation in the style of CUtlMemory/CUtlString/CUtlBuffer
// as compact as possible, no virtuals or extraneous data
// to be used primarily to replace CUtlBuffer
//=============================================================================
#ifndef UTLALLOCATION_H
#define UTLALLOCATION_H
#ifdef _WIN32
#pragma once
#endif
#include "tier1/utlmemory.h"
class CUtlAllocation
{
public:
// constructor, destructor
CUtlAllocation()
{
m_pMemory = NULL;
}
CUtlAllocation( const void *pMemory, int cub )
{
m_pMemory = NULL;
Copy( pMemory, cub );
}
CUtlAllocation( CUtlAllocation const &src )
{
m_pMemory = NULL;
Copy( src );
}
~CUtlAllocation()
{
Purge();
}
CUtlAllocation &operator=( CUtlAllocation const &src )
{
Copy( src );
return *this;
}
bool operator==( CUtlAllocation const &src )
{
if ( Count() != src.Count() )
return false;
return Q_memcmp( Base(), src.Base(), Count() ) == 0;
}
void Copy( const void *pMemory, int cub )
{
if ( cub == 0 || pMemory == NULL )
{
Purge();
return;
}
if ( cub != Count() )
{
Purge();
m_pMemory = (ActualMemory_t *)malloc( cub + sizeof( int ) );
m_pMemory->cub = cub;
}
Q_memcpy( Base(), pMemory, cub );
}
// Gets the base address
uint8* Base()
{
if ( m_pMemory == NULL )
return NULL;
return m_pMemory->rgub;
}
const uint8* Base() const
{
if ( m_pMemory == NULL )
return NULL;
return m_pMemory->rgub;
}
// Size
int Count() const
{
if ( m_pMemory == NULL )
return 0;
return m_pMemory->cub;
}
// Memory deallocation
void Purge()
{
if ( m_pMemory )
free(m_pMemory);
m_pMemory = NULL;
}
void Copy( const CUtlAllocation &alloc )
{
Copy( alloc.Base(), alloc.Count() );
}
void Swap( CUtlAllocation &alloc )
{
ActualMemory_t *pTemp = m_pMemory;
m_pMemory = alloc.m_pMemory;
alloc.m_pMemory = pTemp;
}
void Alloc( int cub )
{
Purge();
m_pMemory = (ActualMemory_t *)malloc( cub + sizeof( int ) );
m_pMemory->cub = cub;
}
private:
struct ActualMemory_t
{
int cub;
uint8 rgub[4]; // i'd prefer to make this 0 but the compiler whines when i do
};
ActualMemory_t *m_pMemory;
};
#endif // UTLALLOCATION_H