forked from catid/siamese
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrowingAlignedDataBuffer.cpp
More file actions
65 lines (53 loc) · 1.19 KB
/
Copy pathGrowingAlignedDataBuffer.cpp
File metadata and controls
65 lines (53 loc) · 1.19 KB
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
#include "GrowingAlignedDataBuffer.h"
GrowingAlignedDataBuffer::GrowingAlignedDataBuffer()
: Data(nullptr), Size(0), Capacity(0) {}
GrowingAlignedDataBuffer::~GrowingAlignedDataBuffer()
{
Clear();
}
bool GrowingAlignedDataBuffer::Resize(unsigned bytes)
{
if (bytes <= Capacity)
{
Size = bytes;
return true;
}
uint8_t* newData = (uint8_t*)aligned_alloc(16, bytes);
if (!newData) return false;
if (Data)
{
memcpy(newData, Data, Size);
free(Data);
}
Data = newData;
Size = bytes;
Capacity = bytes;
return true;
}
bool GrowingAlignedDataBuffer::Append(const uint8_t* data, unsigned bytes)
{
if (!Resize(Size + bytes))
return false;
memcpy((uint8_t*)Data + (Size - bytes), data, bytes);
return true;
}
bool GrowingAlignedDataBuffer::GrowZeroPadded(unsigned bytes)
{
if (bytes <= Size)
return true;
unsigned oldSize = Size;
if (!Resize(bytes))
return false;
memset((uint8_t*)Data + oldSize, 0, bytes - oldSize);
return true;
}
void GrowingAlignedDataBuffer::Clear()
{
if (Data)
{
free(Data);
Data = nullptr;
}
Size = 0;
Capacity = 0;
}