An implementation of a circular array allowing direct write and read.

Files at this revision

API Documentation at this revision

Comitter:
GLemasson
Date:
Wed Feb 12 15:51:51 2014 +0000
Parent:
0:28766f5a758e
Commit message:
change to char version

Changed in this revision

CircularArray.cpp Show annotated file Show diff for this revision Revisions of this file
CircularArray.h Show annotated file Show diff for this revision Revisions of this file
diff -r 28766f5a758e -r 5036a532fe62 CircularArray.cpp
--- a/CircularArray.cpp	Tue Feb 11 18:41:26 2014 +0000
+++ b/CircularArray.cpp	Wed Feb 12 15:51:51 2014 +0000
@@ -1,51 +1,71 @@
 #include "CircularArray.h"
-#include <cstring>
+#include <string.h>
 
-template <typename T> CircularArray<T>::CircularArray(unsigned int capacity)
+CircularArray::CircularArray(unsigned int capacity)
 {
-    data = new T[capacity];
-    m_capacity=capacity
+    data = new uint8_t[capacity];
+    m_capacity=capacity;
     m_full=false;
     m_empty=true;
     w=0;
     r=0;
+    sizeRB=0;
+    sizeWB=0;
+    readerBuffer=0;
+    writerBuffer=0;
 }
-template <typename T>CircularArray<T>::~CircularArray()
+CircularArray::~CircularArray()
 {
-    delete data[];
+    delete[] data;
 }  
-template <typename T> T * CircularArray<T>::getWritePointer()
+
+
+void volatile_memcpy(volatile uint8_t * dest, volatile uint8_t * src, uint16_t num)
 {
-    return data+w;
+    for(int i =0; i<num;i++)
+    {
+        dest[i]=src[i];
+    }
 }
 
-template <typename T> T * CircularArray<T>::getReadPointer(T * buffer,unsigned int num)
+uint8_t * CircularArray::getWritePointer()
 {
-    if(buffer==0 || num <=readable())
+    return (uint8_t *)data+w;
+}
+
+
+uint8_t * CircularArray::getReadPointer(uint16_t num)
+{
+    if(num <=readable())
     {
-        return data+r;
+        return (uint8_t *)data+r;
     }
     else
     {
-        memcpy(buffer,data+r,readable());
-        memcpy(buffer+readable(),data,num-readable());
-        return buffer;
+        if(sizeRB<num)
+        {
+            if(readerBuffer!=0) delete[] readerBuffer;
+            readerBuffer = new uint8_t[num];
+        }
+        volatile_memcpy(readerBuffer,(uint8_t *)(data+r),readable());
+        volatile_memcpy(readerBuffer+readable(),data,num-readable());
+        return (uint8_t *)readerBuffer;
     }
 }
 
 
-template <typename T> void CircularArray<T>::writeElements(unsigned int num)
+void CircularArray::writeElements(unsigned int num)
 {
     w=(w+num)%m_capacity;
     m_full=r==w;
 }
-template <typename T> void CircularArray<T>::readElements(unsigned int num)
+void CircularArray::readElements(unsigned int num)
 {
     r=(r+num)%m_capacity;
     m_empty=r==w;
     
 }
-template <typename T> unsigned int CircularArray<T>::fillCapacity()
+unsigned int CircularArray::fillCapacity()
 {
     if(w>r || m_empty)
     {
@@ -56,7 +76,7 @@
         return r-w;
     }
 }
-template <typename T> unsigned int CircularArray<T>::freeSpace()
+unsigned int CircularArray::freeSpace()
 {
     if(w>r || m_empty)
     {
@@ -68,7 +88,7 @@
     }
     
 }
-template <typename T> unsigned int CircularArray<T>::readable()
+unsigned int CircularArray::readable()
 {
     if(w<r || m_full)
     {
@@ -80,7 +100,7 @@
     }
     
 }
-template <typename T> unsigned int CircularArray<T>::size()
+unsigned int CircularArray::size()
 {
     if(w<r || m_full)
     {
@@ -92,12 +112,12 @@
     }
     
 }
-template <typename T> bool CircularArray<T>::full()
+bool CircularArray::full()
 {
     return m_full;
     
 }
-template <typename T> bool CircularArray<T>::empty()
+bool CircularArray::empty()
 {
     return m_empty;
 }
\ No newline at end of file
diff -r 28766f5a758e -r 5036a532fe62 CircularArray.h
--- a/CircularArray.h	Tue Feb 11 18:41:26 2014 +0000
+++ b/CircularArray.h	Wed Feb 12 15:51:51 2014 +0000
@@ -1,8 +1,9 @@
 
 #ifndef _CIRCULAR_ARRAY_H_
 #define _CIRCULAR_ARRAY_H_
+#include <stdint.h>
 
-template<typename T> class CircularArray
+class CircularArray
 {
     private:
     unsigned int m_capacity;
@@ -10,13 +11,17 @@
     volatile unsigned int r;
     volatile bool m_full;
     volatile bool m_empty;
-    volatile T * data;
+    volatile uint8_t * data;
+    volatile uint8_t * readerBuffer;
+    uint16_t sizeRB;
+    volatile uint8_t * writerBuffer;
+    uint16_t sizeWB;
     
     public:
         CircularArray(unsigned int capacity);
         ~CircularArray();    
-        T * getWritePointer();
-        T * getReadPointer(T * buffer=0,unsigned int num=0);
+        uint8_t * getWritePointer();
+        uint8_t * getReadPointer(uint16_t num);
         void writeElements(unsigned int num);
         void readElements(unsigned int num);
         unsigned int fillCapacity();