forked from mcneel/rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLBPSimpleByteArray.h
83 lines (67 loc) · 1.93 KB
/
LBPSimpleByteArray.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
#pragma once
template <typename T>
class CLBPSimpleArray
{
public:
CLBPSimpleArray(size_t iInitialAllocationOfObjects = 100)
: m_pBuffer((T*)malloc(iInitialAllocationOfObjects * sizeof(T))),
m_dwSizeOfAllocationInObjects(iInitialAllocationOfObjects),
m_iNumObjects(0)
{}
virtual ~CLBPSimpleArray()
{
if (m_pBuffer)
{
free(m_pBuffer);
}
}
public:
void Append(const T* p, size_t iSize)
{
Allocate(m_iNumObjects + iSize);
memcpy(m_pBuffer + m_iNumObjects, p, iSize*sizeof(T));
m_iNumObjects += iSize;
}
void Append(T b) { Append(&b, 1); }
size_t Count(void) const { return m_iNumObjects; }
//operator const BYTE*() const { return m_pBuffer; }
const T* Bytes(void) const { return m_pBuffer; }
private:
size_t Allocate(size_t iTotalObjects, bool bExact = false)
{
if (iTotalObjects < m_dwSizeOfAllocationInObjects)
return m_dwSizeOfAllocationInObjects;
size_t dwNewAllocationInObjects = iTotalObjects;
if (!bExact)
{
if (dwNewAllocationInObjects < 32)
{
// Never allocate less than 32 bytes.
dwNewAllocationInObjects = 32;
}
else
if (dwNewAllocationInObjects > 100000)
{
// If the allocation is really big, don't bloat it - just add a reasonable amount on the end.
dwNewAllocationInObjects = dwNewAllocationInObjects + 10000;
}
else
{
dwNewAllocationInObjects = dwNewAllocationInObjects * 4;
}
}
if (dwNewAllocationInObjects > m_dwSizeOfAllocationInObjects)
{
m_dwSizeOfAllocationInObjects = dwNewAllocationInObjects;
// realloc size plus 1 to make sure we never realloc zero bytes; probably can't happen.
const size_t dwNumBytes = (m_dwSizeOfAllocationInObjects + 1) * sizeof(T);
m_pBuffer = (T*)LBPREALLOC(m_pBuffer, dwNumBytes);
}
return m_dwSizeOfAllocationInObjects;
}
private:
T* m_pBuffer;
size_t m_iNumObjects = 0;
size_t m_dwSizeOfAllocationInObjects;
};
using CLBPSimpleByteArray = CLBPSimpleArray<BYTE>;