-
Notifications
You must be signed in to change notification settings - Fork 22
/
utlbuffer_mini.h
90 lines (74 loc) · 1.54 KB
/
utlbuffer_mini.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
#ifndef UTLBUFFER_MINI_H
#define UTLBUFFER_MINI_H
#ifdef _WIN32
#pragma once
#endif
class CUtlBuffer
{
public:
typedef bool (CUtlBuffer::*OverflowFunc_t)(int nSize);
CUtlBuffer(int growSize = 0, int initSize = 0)
: m_pMemory(nullptr), m_nAllocationCount(0), m_nGrowSize(growSize)
{
EnsureCapacity(initSize);
m_GetOverflowFunc = &EnsureCapacity;
m_PutOverflowFunc = &EnsureCapacity;
m_dummy1 = m_dummy2 = m_dummy3 = m_dummy4 = m_dummy5 = m_dummy6 = m_Byteswap = 0;
}
~CUtlBuffer()
{
Purge();
}
unsigned char *Base()
{
return m_pMemory;
}
int Size()
{
return m_nAllocationCount;
}
bool EnsureCapacity(int num)
{
if (m_nAllocationCount >= num)
return true;
if (m_nGrowSize < 0)
return false;
m_nAllocationCount = num;
if (m_pMemory)
{
m_pMemory = (unsigned char *)realloc(m_pMemory, m_nAllocationCount * sizeof(unsigned char));
}
else
{
m_pMemory = (unsigned char *)malloc(m_nAllocationCount * sizeof(unsigned char));
}
return true;
}
void Purge()
{
if (m_nGrowSize >= 0)
{
if (m_pMemory)
{
free(m_pMemory);
m_pMemory = nullptr;
}
m_nAllocationCount = 0;
}
m_dummy1 = m_dummy2 = m_dummy3 = m_dummy4 = m_dummy5 = m_dummy6 = m_Byteswap = 0;
}
private:
int m_dummy1;
unsigned char *m_pMemory;
int m_nAllocationCount;
int m_nGrowSize;
int m_dummy2; // 2
int m_dummy3; // dummy
int m_dummy4; // 4
int m_dummy5; // me
int m_dummy6; // ¯\_(ツ)_/¯
OverflowFunc_t m_GetOverflowFunc;
OverflowFunc_t m_PutOverflowFunc;
int m_Byteswap;
};
#endif // UTLBUFFER_MINI_H