A template for ring buffer implementation

Dependents:   AwsomeStation LoRaBaseStation LoRaTerminal

Committer:
rba90
Date:
Thu Sep 01 04:01:40 2016 +0000
Revision:
3:21ee07b29eb7
Parent:
1:5f641cf190da
Child:
4:e996c0644148
use size_t as indices of ringbuffer class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rba90 1:5f641cf190da 1 #include "RingBuffer.h"
rba90 3:21ee07b29eb7 2 #include <stdio.h>
rba90 3:21ee07b29eb7 3 #include <stdlib.h>
rba90 0:36b372831d9e 4
rba90 0:36b372831d9e 5 template <typename T>
rba90 3:21ee07b29eb7 6 CircularBuffer<T>::CircularBuffer(const size_t size)
rba90 0:36b372831d9e 7 :buffer_size(size)
rba90 0:36b372831d9e 8 {
rba90 0:36b372831d9e 9 read_ptr = 0;
rba90 0:36b372831d9e 10 write_ptr = 0;
rba90 0:36b372831d9e 11 count = 0;
rba90 0:36b372831d9e 12
rba90 0:36b372831d9e 13 // mutex lock
rba90 0:36b372831d9e 14 mux = false;
rba90 0:36b372831d9e 15
rba90 0:36b372831d9e 16 // overflow
rba90 0:36b372831d9e 17 is_over_flow = false;
rba90 0:36b372831d9e 18
rba90 0:36b372831d9e 19 // container
rba90 3:21ee07b29eb7 20 // data = new T[buffer_size];
rba90 3:21ee07b29eb7 21
rba90 3:21ee07b29eb7 22 // use c styled memory allocation instead
rba90 3:21ee07b29eb7 23 data = (T *)malloc(buffer_size * sizeof(T));
rba90 3:21ee07b29eb7 24
rba90 3:21ee07b29eb7 25 printf("data: %p\r\n", data);
rba90 0:36b372831d9e 26 }
rba90 0:36b372831d9e 27
rba90 0:36b372831d9e 28 template <typename T>
rba90 0:36b372831d9e 29 CircularBuffer<T>::~CircularBuffer()
rba90 0:36b372831d9e 30 {
rba90 3:21ee07b29eb7 31 // delete[] data;
rba90 3:21ee07b29eb7 32 free(data);
rba90 0:36b372831d9e 33 }
rba90 0:36b372831d9e 34
rba90 0:36b372831d9e 35 template <typename T>
rba90 0:36b372831d9e 36 bool CircularBuffer<T>::isLocked()
rba90 0:36b372831d9e 37 {
rba90 0:36b372831d9e 38 return mux;
rba90 0:36b372831d9e 39 }
rba90 0:36b372831d9e 40
rba90 0:36b372831d9e 41 template <typename T>
rba90 0:36b372831d9e 42 void CircularBuffer<T>::lock()
rba90 0:36b372831d9e 43 {
rba90 0:36b372831d9e 44 mux = true;
rba90 0:36b372831d9e 45 }
rba90 0:36b372831d9e 46
rba90 0:36b372831d9e 47 template <typename T>
rba90 0:36b372831d9e 48 void CircularBuffer<T>::unlock()
rba90 0:36b372831d9e 49 {
rba90 0:36b372831d9e 50 mux = false;
rba90 0:36b372831d9e 51 }
rba90 0:36b372831d9e 52
rba90 0:36b372831d9e 53 template <typename T>
rba90 0:36b372831d9e 54 void CircularBuffer<T>::enqueue(T in)
rba90 0:36b372831d9e 55 {
rba90 0:36b372831d9e 56 data[write_ptr++] = in;
rba90 0:36b372831d9e 57 write_ptr %= buffer_size;
rba90 0:36b372831d9e 58
rba90 0:36b372831d9e 59 count++;
rba90 0:36b372831d9e 60 }
rba90 0:36b372831d9e 61
rba90 0:36b372831d9e 62 template <typename T>
rba90 0:36b372831d9e 63 T CircularBuffer<T>::dequeue()
rba90 0:36b372831d9e 64 {
rba90 0:36b372831d9e 65 T temp = data[read_ptr++];
rba90 0:36b372831d9e 66 read_ptr %= buffer_size;
rba90 0:36b372831d9e 67
rba90 0:36b372831d9e 68 count--;
rba90 0:36b372831d9e 69 return temp;
rba90 0:36b372831d9e 70 }
rba90 0:36b372831d9e 71
rba90 0:36b372831d9e 72 template <typename T>
rba90 3:21ee07b29eb7 73 size_t CircularBuffer<T>::getReadPtr()
rba90 0:36b372831d9e 74 {
rba90 0:36b372831d9e 75 return read_ptr;
rba90 0:36b372831d9e 76 }
rba90 0:36b372831d9e 77
rba90 0:36b372831d9e 78 template <typename T>
rba90 3:21ee07b29eb7 79 size_t CircularBuffer<T>::getWritePtr()
rba90 0:36b372831d9e 80 {
rba90 0:36b372831d9e 81 return write_ptr;
rba90 0:36b372831d9e 82 }
rba90 0:36b372831d9e 83
rba90 0:36b372831d9e 84 template <typename T>
rba90 3:21ee07b29eb7 85 size_t CircularBuffer<T>::getCounter()
rba90 0:36b372831d9e 86 {
rba90 0:36b372831d9e 87 return count;
rba90 0:36b372831d9e 88 }
rba90 0:36b372831d9e 89
rba90 0:36b372831d9e 90 template <typename T>
rba90 0:36b372831d9e 91 bool CircularBuffer<T>::getOverFlow()
rba90 0:36b372831d9e 92 {
rba90 0:36b372831d9e 93 return is_over_flow;
rba90 0:36b372831d9e 94 }
rba90 0:36b372831d9e 95
rba90 0:36b372831d9e 96 template <typename T>
rba90 0:36b372831d9e 97 void CircularBuffer<T>::clearOverFlow()
rba90 0:36b372831d9e 98 {
rba90 0:36b372831d9e 99 is_over_flow = false;
rba90 0:36b372831d9e 100 }
rba90 0:36b372831d9e 101
rba90 0:36b372831d9e 102 template <typename T>
rba90 0:36b372831d9e 103 T CircularBuffer<T>::first()
rba90 0:36b372831d9e 104 {
rba90 0:36b372831d9e 105 if (read_ptr > 0)
rba90 0:36b372831d9e 106 {
rba90 0:36b372831d9e 107 return data[read_ptr - 1];
rba90 0:36b372831d9e 108 }
rba90 0:36b372831d9e 109 else
rba90 0:36b372831d9e 110 {
rba90 0:36b372831d9e 111 return data[read_ptr];
rba90 0:36b372831d9e 112 }
rba90 0:36b372831d9e 113 }
rba90 0:36b372831d9e 114
rba90 0:36b372831d9e 115 template <typename T>
rba90 0:36b372831d9e 116 T CircularBuffer<T>::last()
rba90 0:36b372831d9e 117 {
rba90 0:36b372831d9e 118 if (write_ptr > 0)
rba90 0:36b372831d9e 119 {
rba90 0:36b372831d9e 120 return data[write_ptr - 1];
rba90 0:36b372831d9e 121 }
rba90 0:36b372831d9e 122 else
rba90 0:36b372831d9e 123 {
rba90 0:36b372831d9e 124 return data[write_ptr];
rba90 0:36b372831d9e 125 }
rba90 0:36b372831d9e 126 }
rba90 0:36b372831d9e 127
rba90 0:36b372831d9e 128 template <typename T>
rba90 3:21ee07b29eb7 129 T CircularBuffer<T>::operator[](size_t idx)
rba90 0:36b372831d9e 130 {
rba90 0:36b372831d9e 131 return data[idx];
rba90 0:36b372831d9e 132 }
rba90 0:36b372831d9e 133
rba90 0:36b372831d9e 134 // force compiler to create code for template
rba90 3:21ee07b29eb7 135 template class CircularBuffer<uint8_t>;