-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteQueue.cpp
More file actions
514 lines (444 loc) · 16.1 KB
/
ByteQueue.cpp
File metadata and controls
514 lines (444 loc) · 16.1 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
ByteQueue is a high-performance data structure for managing queues of bytes
in a small, fixed amount of memory (2048 bytes). It supports the functions:
create_queue, enqueue_byte, dequeue_byte, and destroy_queue in O(1) time.
~
by Nicolas Ayllon
*/
#include <iostream>
// Classes
class ByteQueueFragment;
class FragmentPool;
// Errors
void on_out_of_memory() {
printf("[!] out of memory, no queue created\n");
}
void on_illegal_operation() {
printf("[!] queue empty, no byte dequeued\n");
}
/***************************/
/* D E C L A R A T I O N S */
/***************************/
/*
Pool
┌┄┄┄┄┄┄┄┄┄┄┄┄┄ 64 fragments ┄┄┄┄┄┄┄┄┄┄┄┄┐
┌────────┬────────┬────────┬───┬────────┐
│fragment│fragment│fragment│...│fragment│ = 2048 bytes
└────────┴────────┴────────┴───┴────────┘
↑ ↑ ↑ ↑
32 32 32 32
FragmentPool holds a 2048-byte data array (unsigned char data[2048]).
It allocates and deallocates 32-byte chunks for ByteQueueFragments.
64 fragments fit into the pool, enough for the assumed max of 64 queues.
FragmentPool also stores a pointer to the head the free list of unallocated
fragments, allowing for fast O(1) allocation.
*/
//
class FragmentPool {
public:
// construction and allocation
FragmentPool();
ByteQueueFragment* allocate();
void deallocate(void* ptr);
// memory calculations
char getIndexInPool(void* ptr);
ByteQueueFragment* getPointerAtIndex(char idx);
// erase
void eraseFragment(void* ptr);
void erasePool();
private:
unsigned char data[2048];
// bool used[64]; // testing only
ByteQueueFragment* nextFreeFragment;
// testing
friend void printDataBlock();
};
/*
A queue is made from a linked list of ByteQueueFragments.
┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ Queue ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┐
┌────────┐ ┌────────┐ ┌────────┐
│fragment│->│fragment│->...->│fragment│
└────────┘ └────────┘ └────────┘
↑ ↑
front back
When in use, the 32-byte fragment uses 28 bytes for bytes in the queue
and 4 bytes for tracking.
┌┄┄┄┄┄┄┄┄┄┄┄┄┄┄ Fragment ┄┄┄┄┄┄┄┄┄┄┄┄┄┐
┌─┬─┬─┬─┬─────────────────────────────┐
│B│N│f│b│ queue bytes │ = 32 bytes
└─┴─┴─┴─┴─────────────────────────────┘
↑ ↑ ↑ ↑ ↑
1 1 1 1 28
The 4 tracking bytes store indices in the pool and fragment's byte array:
B index of the back fragment in the pool (0-63)
N index of the next fragment in the pool (0-63)
f index of the front byte in the fragment's byte array (0-27)
b index of the back byte in the fragment's byte array (0-27)
Note: -1 refers to no index. For example, the last fragment
in the queue has no next fragment, so N = -1.
When not in use, the same memory acts as a free list, storing a pointer to
the next unallocated fragment.
*/
//
class ByteQueueFragment {
friend class FragmentPool;
private:
// A static instance of FragmentPool handles memory allocation
// and deallocation for ByteQueueFragments.
static FragmentPool pool;
// ByteQueueFragment's constructor is private,
// FragmentPool handles creation
ByteQueueFragment() {};
// Use union to use the same 32-bytes to store queue data when used,
// or as part of a free list when unused.
union {
// filled when used
struct {
char m_backFragmentIdx; // 1 byte, range 0-63
char m_nextFragmentIdx; // 1 byte, range 0-63
char m_frontItemIdx; // 1 byte, range 0-27
char m_backItemIdx; // 1 byte, range 0-27
unsigned char bytes[28]; // 28 bytes
} whenUsed;
// when not used, points to next available memory
ByteQueueFragment* next;
} chunk;
// Get
char getBackFragmentIdx();
char getNextFragmentIdx();
char getFrontItemIdx();
char getBackItemIdx();
unsigned char getByte(char idx);
unsigned char getFrontByte();
ByteQueueFragment* getBackFragment();
ByteQueueFragment* getNextFragment();
// Set
void setBackFragmentIdx(char backFragmentIdx);
void setNextFragmentIdx(char nextFragmentIdx);
void setFrontItemIdx(char frontItemIdx);
void setBackItemIdx(char backItemIdx);
void incrementFrontItemIdx();
void incrementBackItemIdx();
void clearBytes();
void setByte(char idx, char byte);
// Testing & state
bool isEmpty();
bool isFrontItemAtEnd();
bool isBackItemAtEnd();
bool isValidByteIndex(char idx);
bool isValidFragmentIndex(char idx);
// Sets an unused fragment's memory pointing to next in free list
void setNextFree(ByteQueueFragment* nextFragment);
// Operations
friend ByteQueueFragment* create_queue();
friend void enqueue_byte(ByteQueueFragment*& front, unsigned char byte);
friend unsigned char dequeue_byte(ByteQueueFragment*& front);
friend void destroy_queue(ByteQueueFragment*& front);
// Testing
friend void printDataBlock();
};
// static member initialized out of class
FragmentPool ByteQueueFragment::pool = FragmentPool();
/*************************/
/* D E F I N I T I O N S */
/*************************/
/* * * * * * * * Fragment Pool * * * * * * * */
FragmentPool::FragmentPool() {
erasePool();
// memset(&used, false, 64); // (testing only) initialize used array
// Set each 32-byte unused memory chunks pointing to next in free list
size_t numFragments = sizeof(data) / sizeof(ByteQueueFragment);
ByteQueueFragment* start = reinterpret_cast<ByteQueueFragment*>(&data);
ByteQueueFragment* fragment = start;
for(int i = 1; i < numFragments; ++i) {
fragment[i-1].setNextFree(&fragment[i]);
}
fragment[numFragments-1].setNextFree(nullptr);
nextFreeFragment = start;
}
ByteQueueFragment* FragmentPool::allocate() {
if(nextFreeFragment == nullptr) {
on_out_of_memory();
return nullptr;
}
ByteQueueFragment* freeFragment = nextFreeFragment;
nextFreeFragment = nextFreeFragment->chunk.next;
// used[getIndexInPool(freeFragment)] = true; // testing only
return freeFragment;
}
void FragmentPool::deallocate(void* ptr) {
eraseFragment(ptr);
reinterpret_cast<ByteQueueFragment*>(ptr)->setNextFree(nextFreeFragment);
nextFreeFragment = reinterpret_cast<ByteQueueFragment*>(ptr);
// used[getIndexInPool(ptr)] = false; // testing only
}
char FragmentPool::getIndexInPool(void* ptr) {
return
reinterpret_cast<ByteQueueFragment*>(ptr)
- reinterpret_cast<ByteQueueFragment*>(&data);
}
ByteQueueFragment* FragmentPool::getPointerAtIndex(char idx) {
return
reinterpret_cast<ByteQueueFragment*>(&data)
+ idx;
}
void FragmentPool::eraseFragment(void* ptr) {
memset(ptr, 0, sizeof(ByteQueueFragment));
}
void FragmentPool::erasePool() {
memset(&data, 0, sizeof(data));
}
/* * * * * * * * ByteQueueFragment * * * * * * * */
// Get
char ByteQueueFragment::getBackFragmentIdx() {
return chunk.whenUsed.m_backFragmentIdx;
}
char ByteQueueFragment::getNextFragmentIdx() {
return chunk.whenUsed.m_nextFragmentIdx;
}
char ByteQueueFragment::getFrontItemIdx() {
return chunk.whenUsed.m_frontItemIdx;
}
char ByteQueueFragment::getBackItemIdx() {
return chunk.whenUsed.m_backItemIdx;
}
unsigned char ByteQueueFragment::getByte(char idx) {
return chunk.whenUsed.bytes[idx];
}
unsigned char ByteQueueFragment::getFrontByte() {
return getByte(getFrontItemIdx());
}
ByteQueueFragment* ByteQueueFragment::getBackFragment() {
if(getBackFragmentIdx() == -1) return nullptr;
return ByteQueueFragment::pool.getPointerAtIndex(getBackFragmentIdx());
}
ByteQueueFragment* ByteQueueFragment::getNextFragment() {
if(getNextFragmentIdx() == -1) return nullptr;
return ByteQueueFragment::pool.getPointerAtIndex(getNextFragmentIdx());
}
// Set
void ByteQueueFragment::setBackFragmentIdx(char backFragmentIdx) {
chunk.whenUsed.m_backFragmentIdx = backFragmentIdx;
}
void ByteQueueFragment::setNextFragmentIdx(char nextFragmentIdx) {
chunk.whenUsed.m_nextFragmentIdx = nextFragmentIdx;
}
void ByteQueueFragment::setFrontItemIdx(char frontItemIdx) {
chunk.whenUsed.m_frontItemIdx = frontItemIdx;
}
void ByteQueueFragment::setBackItemIdx(char backItemIdx) {
chunk.whenUsed.m_backItemIdx = backItemIdx;
}
void ByteQueueFragment::incrementFrontItemIdx() {
chunk.whenUsed.m_frontItemIdx++;
}
void ByteQueueFragment::incrementBackItemIdx() {
chunk.whenUsed.m_backItemIdx++;
}
void ByteQueueFragment::clearBytes() {
memset(chunk.whenUsed.bytes, 0, 28);
}
void ByteQueueFragment::setByte(char idx, char byte) {
// if(!isValidByteIndex(idx)) return; // testing
chunk.whenUsed.bytes[idx] = byte;
}
// Testing & State
bool ByteQueueFragment::isEmpty() {
return getFrontItemIdx() == -1 || getBackItemIdx() < getFrontItemIdx();
}
bool ByteQueueFragment::isFrontItemAtEnd() {
return getFrontItemIdx() == sizeof(chunk.whenUsed.bytes) - 1; // == 27
}
bool ByteQueueFragment::isBackItemAtEnd() {
return getBackItemIdx() == sizeof(chunk.whenUsed.bytes) - 1; // == 27
}
bool ByteQueueFragment::isValidByteIndex(char idx) {
bool isValidIndex = (0 <= idx && idx < 28);
if(!isValidIndex) {
printf("invalid byte index %i not in range [0, 27]\n", idx);
}
return isValidIndex;
}
bool ByteQueueFragment::isValidFragmentIndex(char idx) {
bool isValidIndex = (0 <= idx && idx < 64);
if(!isValidIndex) {
printf("invalid fragment index %i not in range [0, 63]\n", idx);
}
return isValidIndex;
}
// For using the same memory in the free list
void ByteQueueFragment::setNextFree(ByteQueueFragment* nextFragment) {
chunk.next = nextFragment;
}
/* * * * * * * * * * Operations * * * * * * * * * */
/* * * * * * * * (Friend Functions) * * * * * * * */
ByteQueueFragment* create_queue() {
// Allocate memory
ByteQueueFragment* newFragment = ByteQueueFragment::pool.allocate();
if(newFragment == nullptr) {
return nullptr;
}
// Construct fragment
char indexInPool = ByteQueueFragment::pool.getIndexInPool(newFragment);
newFragment->setBackFragmentIdx(indexInPool);
newFragment->setNextFragmentIdx(-1);
newFragment->setFrontItemIdx(-1);
newFragment->setBackItemIdx(-1);
newFragment->clearBytes();
return newFragment;
}
// Pass by reference to update front in case it was nullptr and got allocated
void enqueue_byte(ByteQueueFragment*& front, unsigned char byte) {
// If front points to no queue (it's been deallocated)
if(front == nullptr) {
// First try to create it.
front = create_queue();
// If there really is no more memory, give up.
if(front == nullptr) return;
}
ByteQueueFragment* currentBack = front->getBackFragment();
// If back fragment has last byte at end of array, allocate new fragment
if(currentBack->isBackItemAtEnd()) {
ByteQueueFragment* newBack = ByteQueueFragment::pool.allocate();
if(newBack == nullptr) return; // avoid crash for failed allocation
// Update indices in front and old back to point to new back
char newBackFragmentIdx =
ByteQueueFragment::pool.getIndexInPool(newBack);
front->setBackFragmentIdx(newBackFragmentIdx);
currentBack->setNextFragmentIdx(newBackFragmentIdx);
// Initialize new back fragment
newBack->setBackFragmentIdx(-1);
newBack->setNextFragmentIdx(-1);
newBack->setFrontItemIdx(-1);
newBack->setBackItemIdx(0); // first item in the new back fragment
newBack->clearBytes();
newBack->setByte(0, byte);
return;
}
// Front fragment empty, so set byte at index 0, update frontItem & backItem
if(front->getFrontItemIdx() == -1) {
front->setFrontItemIdx(0);
front->setBackItemIdx(0);
front->setByte(0, byte);
return;
}
// Current back fragment is not empty
currentBack->incrementBackItemIdx(); // -1 -> 0 in empty fragment
currentBack->setByte(currentBack->getBackItemIdx(), byte);
}
// Note:
// dequeue_byte preemptively deallocates memory as soon as the queue is empty,
// which sets front = nullptr.
// enqueue_byte will reallocate memory if bytes are added to the empty queue.
//
// (Pass by reference to update front when last byte in fragment is dequeued.)
unsigned char dequeue_byte(ByteQueueFragment*& front) {
// Handle nullptr or empty queue
if(front == nullptr || front->isEmpty()) {
on_illegal_operation();
return 0;
}
unsigned char dequeuedByte = front->getFrontByte();
// Dequeued byte was the last in the fragment
if(front->isFrontItemAtEnd()) {
// There is no next fragment.
// Deallocate. A new one will be allocated on next enqueue.
if(front->getNextFragmentIdx() == -1) {
ByteQueueFragment::pool.deallocate(front);
front = nullptr;
}
// There is a next fragment.
// Update the next fragment with front's data, and set it as the new front.
else {
ByteQueueFragment* newFront = front->getNextFragment();
newFront->setBackFragmentIdx(front->getBackFragmentIdx());
// No need for setNextFragment() of next fragment. Unaffected by dequeue.
newFront->setFrontItemIdx(0);
// No need for setBackItemIdx(), done in enqueue_byte
ByteQueueFragment::pool.deallocate(front);
front = newFront;
}
return dequeuedByte;
}
// Dequeued byte was NOT the last item in fragment, so increment index
front->incrementFrontItemIdx();
// If the queue is now empty, deallocate it.
if(front->isEmpty()) {
ByteQueueFragment::pool.deallocate(front);
front = nullptr;
}
return dequeuedByte;
}
// Pass by reference to update front to nullptr once queue is destroyed
void destroy_queue(ByteQueueFragment*& front) {
while(front != nullptr) {
ByteQueueFragment* fragmentToDeallocate = front;
front = front->getNextFragment();
ByteQueueFragment::pool.deallocate(fragmentToDeallocate);
}
}
/*****************/
/* T E S T I N G */
/*****************/
void printDataBlock() {
// print j values
std::cout << " j:";
for(int j = 0; j < 32; ++j) { printf("%4i", j); }
std::cout << '\n';
// print box top
std::cout << " ┌─";
for(int j = 0; j < 32; ++j) { printf("────"); }
std::cout << '\n';
// print i labels
for(int i = 0; i < 64; ++i) {
// bool used = ByteQueueFragment::pool.used[i];
// std::string icon = used ? "●" : "○";
// printf("%s i:%3i│", &icon, i);
bool used = true; // when used array is commented out
printf(" i:%3i│", i); // when used array is commented out
// print bytes in row i
if(used) {
for(int j = 0; j < 32; ++j) {
unsigned char val = ByteQueueFragment::pool.data[32*i + j];
if(j < 4) {
printf("%4i", (char)val);
continue;
}
printf("%4i", val);
}
}
else {
for(int j = 0; j < 32; ++j) {
unsigned char val = ByteQueueFragment::pool.data[32*i + j];
printf("%4x", val);
}
}
std::cout << '\n';
}
}
/***********/
/* M A I N */
/***********/
int main() {
// Test
ByteQueueFragment* q0 = create_queue();
enqueue_byte(q0, 0);
enqueue_byte(q0, 1);
ByteQueueFragment* q1 = create_queue();
enqueue_byte(q1, 3);
enqueue_byte(q0, 2);
enqueue_byte(q1, 4);
printf("%d", dequeue_byte(q0));
printf("%d\n", dequeue_byte(q0));
enqueue_byte(q0, 5);
enqueue_byte(q1, 6);
printf("%d", dequeue_byte(q0));
printf("%d\n", dequeue_byte(q0));
destroy_queue(q0);
printf("%d", dequeue_byte(q1));
printf("%d", dequeue_byte(q1));
printf("%d\n", dequeue_byte(q1));
destroy_queue(q1);
//printDataBlock();
return 0;
}