Adding Fidelius manual.
[iotcloud.git] / version2 / src / C / SlotBuffer.cpp
1 #include "SlotBuffer.h"
2 #include "Slot.h"
3 /**
4  * Circular buffer that holds the live set of slots.
5  * @author Brian Demsky
6  * @version 1.0
7  */
8
9 SlotBuffer::SlotBuffer() :
10         array(new Array<Slot *>(SlotBuffer_DEFAULT_SIZE + 1)),
11         head(0),
12         tail(0),
13         oldestseqn(0) {
14 }
15
16 SlotBuffer::~SlotBuffer() {
17         int32_t index = tail;
18         while (index != head) {
19                 delete array->get(index);
20                 index++;
21                 if (index == (int32_t) array->length())
22                         index = 0;
23         }
24         delete array;
25 }
26
27 int SlotBuffer::size() {
28         if (head >= tail)
29                 return head - tail;
30         return (array->length() + head) - tail;
31 }
32
33 int SlotBuffer::capacity() {
34         return array->length() - 1;
35 }
36
37 void SlotBuffer::resize(int newsize) {
38         if ((uint32_t)newsize == (array->length() - 1))
39                 return;
40
41         Array<Slot *> *newarray = new Array<Slot *>(newsize + 1);
42         int currsize = size();
43         int index = tail;
44         for (int i = 0; i < currsize; i++) {
45                 newarray->set(i, array->get(index));
46                 if (((uint32_t)++ index) == array->length())
47                         index = 0;
48         }
49         array = newarray;
50         tail = 0;
51         head = currsize;
52 }
53
54 void SlotBuffer::incrementHead() {
55         head++;
56         if (((uint32_t)head) >= array->length())
57                 head = 0;
58 }
59
60 void SlotBuffer::incrementTail() {
61         delete array->get(tail);
62         tail++;
63         if (((uint32_t)tail) >= array->length())
64                 tail = 0;
65 }
66
67 void SlotBuffer::putSlot(Slot *s) {
68         int64_t checkNum = (getNewestSeqNum() + 1);
69
70         if (checkNum != s->getSequenceNumber()) {
71                 int32_t index = tail;
72                 while (index != head) {
73                         delete array->get(index);
74                         index++;
75                         if (index == (int32_t) array->length())
76                                 index = 0;
77                 }
78                 oldestseqn = s->getSequenceNumber();
79                 tail = 0;
80                 head = 1;
81                 array->set(0, s);
82                 return;
83         }
84
85         array->set(head, s);
86         incrementHead();
87
88         if (oldestseqn == 0) {
89                 oldestseqn = s->getSequenceNumber();
90         }
91
92         if (head == tail) {
93                 incrementTail();
94                 oldestseqn++;
95         }
96 }
97
98 Slot *SlotBuffer::getSlot(int64_t seqnum) {
99         int32_t diff = (int32_t) (seqnum - oldestseqn);
100         int32_t index = diff + tail;
101
102         if (index < 0) {
103                 return NULL;
104         }
105
106         if (((uint32_t)index) >= array->length()) {
107                 if (head >= tail) {
108                         return NULL;
109                 }
110                 index -= (int32_t) array->length();
111         }
112
113         if (((uint32_t)index) >= array->length()) {
114                 return NULL;
115         }
116
117         if (head >= tail && index >= head) {
118                 return NULL;
119         }
120
121         return array->get(index);
122 }