-
Notifications
You must be signed in to change notification settings - Fork 13
/
BipBuffer.h
335 lines (285 loc) · 7.62 KB
/
BipBuffer.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#pragma once
/*
Copyright (c) 2003 Simon Cooke, All Rights Reserved
Licensed royalty-free for commercial and non-commercial
use. All that I ask is that you send me an email
telling me that you're using my code. It'll make me
feel warm and fuzzy inside. [email protected]
*/
class BipBuffer
{
private:
uint8* pBuffer;
int ixa;
int sza;
int ixb;
int szb;
int buflen;
int ixResrv;
int szResrv;
public:
BipBuffer() : pBuffer(NULL), ixa(0), sza(0), ixb(0), szb(0), buflen(0), ixResrv(0), szResrv(0)
{
}
~BipBuffer()
{
// We don't call FreeBuffer, because we don't need to reset our variables - our object is dying
if (pBuffer != NULL)
{
free(pBuffer);
}
}
// Allocate Buffer
//
// Allocates a buffer in virtual memory, to the nearest page size (rounded up)
//
// Parameters:
// int buffersize size of buffer to allocate, in bytes (default: 4096)
//
// Returns:
// bool true if successful, false if buffer cannot be allocated
bool AllocateBuffer(int buffersize = 4096)
{
if (buffersize <= 0) return false;
if (pBuffer != NULL) FreeBuffer();
pBuffer = (uint8*)malloc(buffersize);
if (pBuffer == NULL) return false;
buflen = buffersize;
return true;
}
///
/// \brief Clears the buffer of any allocations.
///
/// Clears the buffer of any allocations or reservations. Note; it
/// does not wipe the buffer memory; it merely resets all pointers,
/// returning the buffer to a completely empty state ready for new
/// allocations.
///
void Clear()
{
ixa = sza = ixb = szb = ixResrv = szResrv = 0;
}
// Free Buffer
//
// Frees a previously allocated buffer, resetting all internal pointers to 0.
//
// Parameters:
// none
//
// Returns:
// void
void FreeBuffer()
{
if (pBuffer == NULL) return;
ixa = sza = ixb = szb = buflen = 0;
free(pBuffer);
pBuffer = NULL;
}
// Reserve
//
// Reserves space in the buffer for a memory write operation
//
// Parameters:
// int size amount of space to reserve
// OUT int& reserved size of space actually reserved
//
// Returns:
// BYTE* pointer to the reserved block
//
// Notes:
// Will return NULL for the pointer if no space can be allocated.
// Can return any value from 1 to size in reserved.
// Will return NULL if a previous reservation has not been committed.
uint8* Reserve(int size, int& reserved)
{
// We always allocate on B if B exists; this means we have two blocks and our buffer is filling.
if (szb)
{
int freespace = GetBFreeSpace();
if (size < freespace) freespace = size;
if (freespace == 0) return NULL;
szResrv = freespace;
reserved = freespace;
ixResrv = ixb + szb;
return pBuffer + ixResrv;
}
else
{
// Block b does not exist, so we can check if the space AFTER a is bigger than the space
// before A, and allocate the bigger one.
int freespace = GetSpaceAfterA();
if (freespace >= ixa)
{
if (freespace == 0) return NULL;
if (size < freespace) freespace = size;
szResrv = freespace;
reserved = freespace;
ixResrv = ixa + sza;
return pBuffer + ixResrv;
}
else
{
if (ixa == 0) return NULL;
if (ixa < size) size = ixa;
szResrv = size;
reserved = size;
ixResrv = 0;
return pBuffer;
}
}
}
// Commit
//
// Commits space that has been written to in the buffer
//
// Parameters:
// int size number of bytes to commit
//
// Notes:
// Committing a size > than the reserved size will cause an assert in a debug build;
// in a release build, the actual reserved size will be used.
// Committing a size < than the reserved size will commit that amount of data, and release
// the rest of the space.
// Committing a size of 0 will release the reservation.
//
void Commit(int size)
{
if (size == 0)
{
// decommit any reservation
szResrv = ixResrv = 0;
return;
}
// If we try to commit more space than we asked for, clip to the size we asked for.
if (size > szResrv)
{
size = szResrv;
}
// If we have no blocks being used currently, we create one in A.
if (sza == 0 && szb == 0)
{
ixa = ixResrv;
sza = size;
ixResrv = 0;
szResrv = 0;
return;
}
if (ixResrv == sza + ixa)
{
sza += size;
}
else
{
szb += size;
}
ixResrv = 0;
szResrv = 0;
}
// GetContiguousBlock
//
// Gets a pointer to the first contiguous block in the buffer, and returns the size of that block.
//
// Parameters:
// OUT int & size returns the size of the first contiguous block
//
// Returns:
// BYTE* pointer to the first contiguous block, or NULL if empty.
uint8* GetContiguousBlock(int& size)
{
if (sza == 0)
{
size = 0;
return NULL;
}
size = sza;
return pBuffer + ixa;
}
// DecommitBlock
//
// Decommits space from the first contiguous block
//
// Parameters:
// int size amount of memory to decommit
//
// Returns:
// nothing
void DecommitBlock(int size)
{
if (size >= sza)
{
ixa = ixb;
sza = szb;
ixb = 0;
szb = 0;
}
else
{
sza -= size;
ixa += size;
}
}
// GetCommittedSize
//
// Queries how much data (in total) has been committed in the buffer
//
// Parameters:
// none
//
// Returns:
// int total amount of committed data in the buffer
int GetCommittedSize() const
{
return sza + szb;
}
// GetReservationSize
//
// Queries how much space has been reserved in the buffer.
//
// Parameters:
// none
//
// Returns:
// int number of bytes that have been reserved
//
// Notes:
// A return value of 0 indicates that no space has been reserved
int GetReservationSize() const
{
return szResrv;
}
// GetBufferSize
//
// Queries the maximum total size of the buffer
//
// Parameters:
// none
//
// Returns:
// int total size of buffer
int GetBufferSize() const
{
return buflen;
}
// IsInitialized
//
// Queries whether or not the buffer has been allocated
//
// Parameters:
// none
//
// Returns:
// bool true if the buffer has been allocated
bool IsInitialized() const
{
return pBuffer != NULL;
}
private:
int GetSpaceAfterA() const
{
return buflen - ixa - sza;
}
int GetBFreeSpace() const
{
return ixa - ixb - szb;
}
};