-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgobuff.h
80 lines (68 loc) · 1.17 KB
/
gobuff.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
#ifndef gobuff_h
#define gobuff_h
#include "myutils.h"
template<class T, unsigned SizeInc = 32, bool CopyOnGrow = false,
bool ZeroOnGrow = false> class GoBuff
{
public:
unsigned MaxSize;
unsigned Size;
T *Data;
public:
GoBuff()
{
MaxSize = 0;
Size = 0;
Data = 0;
}
~GoBuff() { Free(); }
void Free()
{
myfree(Data);
Size = 0;
Data = 0;
}
void Clear()
{
Size = 0;
}
void Alloc(unsigned n)
{
if (n <= MaxSize)
return;
unsigned NewMaxSize = n + SizeInc;
T *NewBuffer = myalloc(T, NewMaxSize);
if (Size > 0)
{
if (CopyOnGrow)
memcpy(NewBuffer, Data, Size*sizeof(T));
myfree(Data);
}
if (ZeroOnGrow)
memset(NewBuffer, 0, NewMaxSize*sizeof(T));
Data = NewBuffer;
MaxSize = NewMaxSize;
}
unsigned GetMemUseBytes() const
{
return (MaxSize*sizeof(T));
}
void Append(T t)
{
if (Size == MaxSize)
{
unsigned NewMaxSize = Size + SizeInc;
T *NewBuffer = myalloc(T, NewMaxSize);
if (Size > 0)
{
memcpy(NewBuffer, Data, Size*sizeof(T));
myfree(Data);
}
Data = NewBuffer;
MaxSize = NewMaxSize;
}
Data[Size++] = t;
}
};
const unsigned GROW64K = 0x10000;
#endif // gobuff_h