edits
[iotcloud.git] / version2 / src / C / SlotBuffer.cc
index 91ac22edc1e6667c2fce73d7cfd3dbba06c03402..06c46e901d8f60a5970025476480c29da18d7e66 100644 (file)
@@ -1,4 +1,5 @@
-#include"SlotBuffer.h"
+#include "SlotBuffer.h"
+#include "Slot.h"
 /**
  * Circular buffer that holds the live set of slots.
  * @author Brian Demsky
@@ -12,6 +13,17 @@ SlotBuffer::SlotBuffer() :
        oldestseqn(0) {
 }
 
+SlotBuffer::~SlotBuffer() {
+       int32_t index = tail;
+       while (index != head) {
+               delete array->get(index);
+               index++;
+               if (index == (int32_t) array->length())
+                       index = 0;
+       }
+       delete array;
+}
+
 int SlotBuffer::size() {
        if (head >= tail)
                return head - tail;
@@ -23,15 +35,15 @@ int SlotBuffer::capacity() {
 }
 
 void SlotBuffer::resize(int newsize) {
-       if (newsize == (array->length() - 1))
+       if ((uint32_t)newsize == (array->length() - 1))
                return;
-       
-       Array<Slot *> * newarray = new Array<Slot *>(newsize + 1);
+
+       Array<Slot *> *newarray = new Array<Slot *>(newsize + 1);
        int currsize = size();
        int index = tail;
        for (int i = 0; i < currsize; i++) {
                newarray->set(i, array->get(index));
-               if ((++index) == array->length())
+               if (((uint32_t)++ index) == array->length())
                        index = 0;
        }
        array = newarray;
@@ -41,31 +53,38 @@ void SlotBuffer::resize(int newsize) {
 
 void SlotBuffer::incrementHead() {
        head++;
-       if (head >= array->length())
+       if (((uint32_t)head) >= array->length())
                head = 0;
 }
 
 void SlotBuffer::incrementTail() {
+       delete array->get(tail);
        tail++;
-       if (tail >= array->length())
+       if (((uint32_t)tail) >= array->length())
                tail = 0;
 }
 
-void SlotBuffer::putSlot(Slot * s) {
+void SlotBuffer::putSlot(Slot *s) {
        int64_t checkNum = (getNewestSeqNum() + 1);
-       
+
        if (checkNum != s->getSequenceNumber()) {
-               // We have a gap so expunge all our slots
+               int32_t index = tail;
+               while (index != head) {
+                       delete array->get(index);
+                       index++;
+                       if (index == (int32_t) array->length())
+                               index = 0;
+               }
                oldestseqn = s->getSequenceNumber();
                tail = 0;
                head = 1;
                array->set(0, s);
                return;
        }
-       
+
        array->set(head, s);
        incrementHead();
-       
+
        if (oldestseqn == 0) {
                oldestseqn = s->getSequenceNumber();
        }
@@ -76,26 +95,25 @@ void SlotBuffer::putSlot(Slot * s) {
        }
 }
 
-Slot SlotBuffer::getSlot(int64_t seqnum) {
+Slot *SlotBuffer::getSlot(int64_t seqnum) {
        int32_t diff = (int32_t) (seqnum - oldestseqn);
        int32_t index = diff + tail;
 
        if (index < 0) {
-               // Really old message so we dont have it anymore
                return NULL;
        }
 
-       if (index >= array->length()) {
+       if (((uint32_t)index) >= array->length()) {
                if (head >= tail) {
                        return NULL;
                }
-               index -= array->length();
+               index -= (int32_t) array->length();
        }
 
-       if (index >= array->length) {
-
+       if (((uint32_t)index) >= array->length()) {
                return NULL;
        }
+
        if (head >= tail && index >= head) {
                return NULL;
        }