-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBuffer.h
286 lines (259 loc) · 9.94 KB
/
Buffer.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#pragma once
#include <CppCore/Root.h>
#include <CppCore/Memory.h>
namespace CppCore
{
/// <summary>
/// Fixed size memory buffer with 16-byte alignment.
/// Keep SIZE a multiple of 16.
/// </summary>
template<size_t SIZE = 8192U, bool ZEROMEM = false>
class CPPCORE_ALIGN16 Buffer
{
protected:
const static size_t N128 = SIZE >> 4U;
const static size_t N64 = SIZE >> 5U;
const static size_t N32 = SIZE >> 6U;
union {
struct {
union {
struct {
CPPCORE_ALIGN8 size_t mLength; // length in 32/64 bit but aligned to 64
CPPCORE_ALIGN8 size_t mLengthRead; // length read in 32/64 bit but aligned to 64
};
#if defined(CPPCORE_CPUFEAT_SSE2)
CPPCORE_ALIGN16 __m128i mLengthSSE; // both length as single SSE word
#endif
};
union {
CPPCORE_ALIGN16 char mData[SIZE]; // data in 8-bit words
CPPCORE_ALIGN16 uint32_t mData32[N32]; // data in 32-bit words
CPPCORE_ALIGN16 uint64_t mData64[N64]; // data in 64-bit words
#if defined(CPPCORE_CPUFEAT_SSE2)
CPPCORE_ALIGN16 __m128i mDataSSE[N128]; // data in 128-bit words
#endif
};
};
#if defined(CPPCORE_CPUFEAT_SSE2)
CPPCORE_ALIGN16 __m128i mSSE[N128+1]; // both length + data in SSE words
#endif
};
public:
/// <summary>
/// Total number of bytes that can be stored in this Buffer class.
/// </summary>
static INLINE size_t getSize() { return SIZE; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////
INLINE size_t getLength() const { return mLength; }
INLINE size_t getLengthRead() const { return mLengthRead; }
INLINE size_t getRemaining() const { return SIZE - mLength; }
INLINE size_t getRemainingRead() const { return mLength - mLengthRead; }
INLINE char* getPtr() { return &mData[0]; }
INLINE char* getPtrRead() { return &mData[mLengthRead]; }
INLINE char* getPtrRemaining() { return &mData[mLength]; }
INLINE void setLength(const size_t length) { mLength = (length <= SIZE) ? length : SIZE; }
INLINE void setLengthRead(const size_t length) { mLengthRead = (length <= mLength) ? length : mLength; }
INLINE void* operator new (const size_t size) { return CPPCORE_ALIGNED_ALLOC(size, 16U); }
INLINE void* operator new[] (const size_t size) { return CPPCORE_ALIGNED_ALLOC(size, 16U); }
INLINE void operator delete (void* ptr) { return CPPCORE_ALIGNED_FREE(ptr); }
INLINE void operator delete[] (void* ptr) { return CPPCORE_ALIGNED_FREE(ptr); }
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Default Constructor
/// </summary>
INLINE Buffer()
{
#if defined(CPPCORE_CPUFEAT_SSE2)
if (ZEROMEM) Memory::streamclear128x1(&mSSE, 16U + SIZE);
else Memory::streamclear128x1(&mLengthSSE, 16U);
#else
mLength = 0U;
mLengthRead = 0U;
#if defined(CPPCORE_CPU_64BIT)
if (ZEROMEM) Memory::clear64(mData64, N64);
#else
if (ZEROMEM) Memory::clear32(mData32, N32);
#endif
#endif
}
/// <summary>
/// Copy Constructor
/// </summary>
INLINE Buffer(const Buffer& other)
{
CPPCORE_ASSERT_MULTIPLE16(SIZE);
#if defined(CPPCORE_CPUFEAT_SSE2)
Memory::streamcopy128x1(mSSE, other.mSSE, 16U + other.mLength);
#else
mLength = other.mLength;
mLengthRead = other.mLengthRead;
Memory::copy(mData, other.mData, other.mLength);
#endif
}
/// <summary>
/// Set length to 0 (and maybe clear data).
/// </summary>
INLINE void clear()
{
#if defined(CPPCORE_CPUFEAT_SSE2)
if (ZEROMEM) Memory::streamclear128x1(&mSSE, 16U + mLength);
else Memory::streamclear128x1(&mLengthSSE, 16U);
#else
mLength = 0U;
mLengthRead = 0U;
#if defined(CPPCORE_CPU_64BIT)
if (ZEROMEM) Memory::clear64(mData64, N64);
#else
if (ZEROMEM) Memory::clear32(mData32, N32);
#endif
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// References a specific typed value from custom index.
/// This is a reference, hence changing the reference
/// changes the underlying bytes in the buffer.
/// </summary>
template<typename T>
INLINE T& ref(const size_t index)
{
return *((T*)&mData[index]);
}
/// <summary>
/// Like other variant, but constant.
/// </summary>
template<typename T>
INLINE const T& ref(const size_t index) const
{
return *((T*)&mData[index]);
}
/// <summary>
/// References a specific typed value at current read index.
/// This is a reference, hence changing the reference
/// changes the underlying bytes in the buffer.
/// </summary>
template<typename T>
INLINE T& refRead()
{
return *((T*)&mData[mLengthRead]);
}
/// <summary>
/// Like other variant, but constant.
/// </summary>
template<typename T>
INLINE const T& refRead() const
{
return *((T*)&mData[mLengthRead]);
}
/// <summary>
/// References a specific typed value at current write index.
/// This is a reference, hence changing the reference
/// changes the underlying bytes in the buffer.
/// </summary>
template<typename T>
INLINE T& refWrite()
{
return *((T*)&mData[mLength]);
}
/// <summary>
/// Like other variant, but constant.
/// </summary>
template<typename T>
INLINE const T& refWrite() const
{
return *((T*)&mData[mLength]);
}
///////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Writes generic data to custom index.
/// Does not touch current length. Uses memcpy().
/// </summary>
template<bool CHECK = true>
INLINE bool writeData(const char* data, const size_t length, const size_t index)
{
const bool OK = !CHECK || index + length <= SIZE;
if (OK) memcpy(&mData[index], data, length);
return OK;
}
/// <summary>
/// Writes generic data to next free index.
/// Increments length by length param.
/// </summary>
template<bool CHECK = true>
INLINE bool writeData(const char* data, const size_t length)
{
const bool OK = writeData<CHECK>(data, length, mLength);
if (OK) mLength += length;
return OK;
}
///////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Reads generic data from custom index.
/// Does not touch current length. Uses memcpy().
/// </summary>
template<bool CHECK = true>
INLINE bool readData(char* data, const size_t length, const size_t index) const
{
const bool OK = !CHECK || index + length <= SIZE;
if (OK) memcpy(data, &mData[index], length);
return OK;
}
/// <summary>
/// Reads generic data from current read index.
/// Increments read index by length param.
/// </summary>
template<bool CHECK = true>
INLINE bool readData(char* data, const size_t length)
{
const bool OK = readData<CHECK>(data, length, mLengthRead);
if (OK) mLengthRead += length;
return OK;
}
///////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Write a specific typed value to custom index.
/// Does not touch current length.
/// </summary>
template<typename T, bool CHECK = true>
INLINE bool write(const T& data, const size_t index)
{
const bool OK = !CHECK || index + sizeof(T) <= SIZE;
if (OK) *((T*)&mData[index]) = data;
return OK;
}
/// <summary>
/// Writes a specific typed value to next free index.
/// Increments length by sizeof(T)
/// </summary>
template<typename T, bool CHECK = true>
INLINE bool write(const T& data)
{
const bool OK = write<T, CHECK>(data, mLength);
if (OK) mLength += sizeof(T);
return OK;
}
///////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Read a specific typed value from custom index.
/// Does not touch current read index.
/// </summary>
template<typename T, bool CHECK = true>
INLINE bool read(T& data, const size_t index) const
{
const bool OK = !CHECK || index + sizeof(T) <= SIZE;
if (OK) data = *((T*)&mData[index]);
return OK;
}
/// <summary>
/// Reads a specific typed value from current read index.
/// Increments read index by sizeof(T)
/// </summary>
template<typename T, bool CHECK = true>
INLINE bool read(T& data)
{
const bool OK = read<T, CHECK>(data, mLengthRead);
if (OK) mLengthRead += sizeof(T);
return OK;
}
};
}