A template for ring buffer implementation

Dependents:   AwsomeStation LoRaBaseStation LoRaTerminal

Revision:
3:21ee07b29eb7
Parent:
1:5f641cf190da
Child:
4:e996c0644148
--- a/RingBuffer.cpp	Sat Jul 23 07:50:53 2016 +0000
+++ b/RingBuffer.cpp	Thu Sep 01 04:01:40 2016 +0000
@@ -1,7 +1,9 @@
 #include "RingBuffer.h"
+#include <stdio.h>
+#include <stdlib.h>
 
 template <typename T>
-CircularBuffer<T>::CircularBuffer(const uint32_t size)
+CircularBuffer<T>::CircularBuffer(const size_t size)
     :buffer_size(size)
 {
     read_ptr = 0;
@@ -15,13 +17,19 @@
     is_over_flow = false;
     
     // container
-    data = new T[buffer_size];
+    // data = new T[buffer_size];
+    
+    // use c styled memory allocation instead
+    data = (T *)malloc(buffer_size * sizeof(T));
+    
+    printf("data: %p\r\n", data);
 }
 
 template <typename T>
 CircularBuffer<T>::~CircularBuffer()
 {
-    delete[] data;
+    // delete[] data;
+    free(data);
 }
 
 template <typename T>
@@ -62,19 +70,19 @@
 }
 
 template <typename T>
-uint32_t CircularBuffer<T>::getReadPtr()
+size_t CircularBuffer<T>::getReadPtr()
 {
     return read_ptr;
 }
 
 template <typename T>
-uint32_t CircularBuffer<T>::getWritePtr()
+size_t CircularBuffer<T>::getWritePtr()
 {
     return write_ptr;
 }
 
 template <typename T>
-uint32_t CircularBuffer<T>::getCounter()
+size_t CircularBuffer<T>::getCounter()
 {
     return count;
 }
@@ -118,13 +126,10 @@
 }
 
 template <typename T>
-T CircularBuffer<T>::operator[](uint32_t idx)
+T CircularBuffer<T>::operator[](size_t idx)
 {
     return data[idx];
 }
 
 // force compiler to create code for template
-template class CircularBuffer<int>;
-template class CircularBuffer<uint8_t>;
-template class CircularBuffer<uint16_t>;
-template class CircularBuffer<uint32_t>;
\ No newline at end of file
+template class CircularBuffer<uint8_t>;
\ No newline at end of file