forked from catid/siamese
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSiameseCommon.cpp
More file actions
111 lines (85 loc) · 3.13 KB
/
Copy pathSiameseCommon.cpp
File metadata and controls
111 lines (85 loc) · 3.13 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
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
// SiameseCommon.cpp
// Siamese FEC Implementation: Codec Common Definitions
#include "SiameseCommon.h"
#include "SiameseSerializers.h"
#include "SiameseTypes.h"
namespace siamese {
//------------------------------------------------------------------------------
// GrowingAlignedByteMatrix
void GrowingAlignedByteMatrix::Free(pktalloc::Allocator* allocator)
{
SIAMESE_DEBUG_ASSERT(allocator);
if (Data)
{
allocator->Free(Data);
Data = nullptr;
AllocatedRows = 0;
AllocatedColumns = 0;
}
}
bool GrowingAlignedByteMatrix::Initialize(pktalloc::Allocator* allocator, unsigned rows, unsigned columns)
{
Rows = rows;
Columns = columns;
AllocatedRows = rows + kExtraRows;
AllocatedColumns = pktalloc::NextAlignedOffset(columns + kMinExtraColumns);
Data = allocator->Reallocate(Data, AllocatedRows * AllocatedColumns, pktalloc::ReallocType::Uninitialized);
return Data != nullptr;
}
bool GrowingAlignedByteMatrix::Resize(pktalloc::Allocator* allocator, unsigned rows, unsigned columns)
{
SIAMESE_DEBUG_ASSERT(allocator && rows > 0 && columns > 0 && columns <= kColumnPeriod);
if (rows <= AllocatedRows && columns <= AllocatedColumns)
{
Rows = rows;
Columns = columns;
return true;
}
const unsigned newAllocatedRows = rows + kExtraRows;
const unsigned newAllocatedColumns = pktalloc::NextAlignedOffset(columns + kMinExtraColumns);
uint8_t* newBuffer = allocator->Allocate(newAllocatedRows * newAllocatedColumns);
if (!newBuffer)
{
Free(allocator);
return false;
}
if (Data)
{
const unsigned oldColumns = Columns;
const unsigned oldRows = Rows;
const unsigned oldStride = AllocatedColumns;
unsigned copyCount = oldColumns;
if (copyCount > columns)
{
SIAMESE_DEBUG_BREAK(); // Should never happen
copyCount = columns;
}
uint8_t* destRow = newBuffer;
uint8_t* srcRow = Data;
for (unsigned i = 0; i < oldRows; ++i, destRow += newAllocatedColumns, srcRow += oldStride)
memcpy(destRow, srcRow, copyCount);
allocator->Free(Data);
}
AllocatedRows = newAllocatedRows;
AllocatedColumns = newAllocatedColumns;
Rows = rows;
Columns = columns;
Data = newBuffer;
return true;
}
//------------------------------------------------------------------------------
// OriginalPacket
unsigned OriginalPacket::Initialize(pktalloc::Allocator* allocator, const SiameseOriginalPacket& packet)
{
SIAMESE_DEBUG_ASSERT(allocator && packet.data && packet.dataBytes > 0 && packet.packetNum < kColumnPeriod);
const unsigned bufferSize = kMaxPacketLengthFieldBytes + packet.dataBytes;
if (!Buffer.Initialize(allocator, bufferSize))
return 0;
HeaderBytes = SerializeHeader_PacketLength(packet.dataBytes, Buffer.Data);
SIAMESE_DEBUG_ASSERT(HeaderBytes <= kMaxPacketLengthFieldBytes);
memcpy(Buffer.Data + HeaderBytes, packet.data, packet.dataBytes);
Buffer.Bytes = HeaderBytes + packet.dataBytes;
Column = packet.packetNum;
return HeaderBytes;
}
} // namespace siamese