An implementation of a circular array allowing direct write and read.
CircularArray.cpp@1:5036a532fe62, 2014-02-12 (annotated)
- Committer:
- GLemasson
- Date:
- Wed Feb 12 15:51:51 2014 +0000
- Revision:
- 1:5036a532fe62
- Parent:
- 0:28766f5a758e
change to char version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GLemasson | 0:28766f5a758e | 1 | #include "CircularArray.h" |
GLemasson | 1:5036a532fe62 | 2 | #include <string.h> |
GLemasson | 0:28766f5a758e | 3 | |
GLemasson | 1:5036a532fe62 | 4 | CircularArray::CircularArray(unsigned int capacity) |
GLemasson | 0:28766f5a758e | 5 | { |
GLemasson | 1:5036a532fe62 | 6 | data = new uint8_t[capacity]; |
GLemasson | 1:5036a532fe62 | 7 | m_capacity=capacity; |
GLemasson | 0:28766f5a758e | 8 | m_full=false; |
GLemasson | 0:28766f5a758e | 9 | m_empty=true; |
GLemasson | 0:28766f5a758e | 10 | w=0; |
GLemasson | 0:28766f5a758e | 11 | r=0; |
GLemasson | 1:5036a532fe62 | 12 | sizeRB=0; |
GLemasson | 1:5036a532fe62 | 13 | sizeWB=0; |
GLemasson | 1:5036a532fe62 | 14 | readerBuffer=0; |
GLemasson | 1:5036a532fe62 | 15 | writerBuffer=0; |
GLemasson | 0:28766f5a758e | 16 | } |
GLemasson | 1:5036a532fe62 | 17 | CircularArray::~CircularArray() |
GLemasson | 0:28766f5a758e | 18 | { |
GLemasson | 1:5036a532fe62 | 19 | delete[] data; |
GLemasson | 0:28766f5a758e | 20 | } |
GLemasson | 1:5036a532fe62 | 21 | |
GLemasson | 1:5036a532fe62 | 22 | |
GLemasson | 1:5036a532fe62 | 23 | void volatile_memcpy(volatile uint8_t * dest, volatile uint8_t * src, uint16_t num) |
GLemasson | 0:28766f5a758e | 24 | { |
GLemasson | 1:5036a532fe62 | 25 | for(int i =0; i<num;i++) |
GLemasson | 1:5036a532fe62 | 26 | { |
GLemasson | 1:5036a532fe62 | 27 | dest[i]=src[i]; |
GLemasson | 1:5036a532fe62 | 28 | } |
GLemasson | 0:28766f5a758e | 29 | } |
GLemasson | 0:28766f5a758e | 30 | |
GLemasson | 1:5036a532fe62 | 31 | uint8_t * CircularArray::getWritePointer() |
GLemasson | 0:28766f5a758e | 32 | { |
GLemasson | 1:5036a532fe62 | 33 | return (uint8_t *)data+w; |
GLemasson | 1:5036a532fe62 | 34 | } |
GLemasson | 1:5036a532fe62 | 35 | |
GLemasson | 1:5036a532fe62 | 36 | |
GLemasson | 1:5036a532fe62 | 37 | uint8_t * CircularArray::getReadPointer(uint16_t num) |
GLemasson | 1:5036a532fe62 | 38 | { |
GLemasson | 1:5036a532fe62 | 39 | if(num <=readable()) |
GLemasson | 0:28766f5a758e | 40 | { |
GLemasson | 1:5036a532fe62 | 41 | return (uint8_t *)data+r; |
GLemasson | 0:28766f5a758e | 42 | } |
GLemasson | 0:28766f5a758e | 43 | else |
GLemasson | 0:28766f5a758e | 44 | { |
GLemasson | 1:5036a532fe62 | 45 | if(sizeRB<num) |
GLemasson | 1:5036a532fe62 | 46 | { |
GLemasson | 1:5036a532fe62 | 47 | if(readerBuffer!=0) delete[] readerBuffer; |
GLemasson | 1:5036a532fe62 | 48 | readerBuffer = new uint8_t[num]; |
GLemasson | 1:5036a532fe62 | 49 | } |
GLemasson | 1:5036a532fe62 | 50 | volatile_memcpy(readerBuffer,(uint8_t *)(data+r),readable()); |
GLemasson | 1:5036a532fe62 | 51 | volatile_memcpy(readerBuffer+readable(),data,num-readable()); |
GLemasson | 1:5036a532fe62 | 52 | return (uint8_t *)readerBuffer; |
GLemasson | 0:28766f5a758e | 53 | } |
GLemasson | 0:28766f5a758e | 54 | } |
GLemasson | 0:28766f5a758e | 55 | |
GLemasson | 0:28766f5a758e | 56 | |
GLemasson | 1:5036a532fe62 | 57 | void CircularArray::writeElements(unsigned int num) |
GLemasson | 0:28766f5a758e | 58 | { |
GLemasson | 0:28766f5a758e | 59 | w=(w+num)%m_capacity; |
GLemasson | 0:28766f5a758e | 60 | m_full=r==w; |
GLemasson | 0:28766f5a758e | 61 | } |
GLemasson | 1:5036a532fe62 | 62 | void CircularArray::readElements(unsigned int num) |
GLemasson | 0:28766f5a758e | 63 | { |
GLemasson | 0:28766f5a758e | 64 | r=(r+num)%m_capacity; |
GLemasson | 0:28766f5a758e | 65 | m_empty=r==w; |
GLemasson | 0:28766f5a758e | 66 | |
GLemasson | 0:28766f5a758e | 67 | } |
GLemasson | 1:5036a532fe62 | 68 | unsigned int CircularArray::fillCapacity() |
GLemasson | 0:28766f5a758e | 69 | { |
GLemasson | 0:28766f5a758e | 70 | if(w>r || m_empty) |
GLemasson | 0:28766f5a758e | 71 | { |
GLemasson | 0:28766f5a758e | 72 | return m_capacity-w; |
GLemasson | 0:28766f5a758e | 73 | } |
GLemasson | 0:28766f5a758e | 74 | else |
GLemasson | 0:28766f5a758e | 75 | { |
GLemasson | 0:28766f5a758e | 76 | return r-w; |
GLemasson | 0:28766f5a758e | 77 | } |
GLemasson | 0:28766f5a758e | 78 | } |
GLemasson | 1:5036a532fe62 | 79 | unsigned int CircularArray::freeSpace() |
GLemasson | 0:28766f5a758e | 80 | { |
GLemasson | 0:28766f5a758e | 81 | if(w>r || m_empty) |
GLemasson | 0:28766f5a758e | 82 | { |
GLemasson | 0:28766f5a758e | 83 | return fillCapacity()+r; |
GLemasson | 0:28766f5a758e | 84 | } |
GLemasson | 0:28766f5a758e | 85 | else |
GLemasson | 0:28766f5a758e | 86 | { |
GLemasson | 0:28766f5a758e | 87 | return fillCapacity(); |
GLemasson | 0:28766f5a758e | 88 | } |
GLemasson | 0:28766f5a758e | 89 | |
GLemasson | 0:28766f5a758e | 90 | } |
GLemasson | 1:5036a532fe62 | 91 | unsigned int CircularArray::readable() |
GLemasson | 0:28766f5a758e | 92 | { |
GLemasson | 0:28766f5a758e | 93 | if(w<r || m_full) |
GLemasson | 0:28766f5a758e | 94 | { |
GLemasson | 0:28766f5a758e | 95 | return m_capacity-r; |
GLemasson | 0:28766f5a758e | 96 | } |
GLemasson | 0:28766f5a758e | 97 | else |
GLemasson | 0:28766f5a758e | 98 | { |
GLemasson | 0:28766f5a758e | 99 | return w-r; |
GLemasson | 0:28766f5a758e | 100 | } |
GLemasson | 0:28766f5a758e | 101 | |
GLemasson | 0:28766f5a758e | 102 | } |
GLemasson | 1:5036a532fe62 | 103 | unsigned int CircularArray::size() |
GLemasson | 0:28766f5a758e | 104 | { |
GLemasson | 0:28766f5a758e | 105 | if(w<r || m_full) |
GLemasson | 0:28766f5a758e | 106 | { |
GLemasson | 0:28766f5a758e | 107 | return readable()+w; |
GLemasson | 0:28766f5a758e | 108 | } |
GLemasson | 0:28766f5a758e | 109 | else |
GLemasson | 0:28766f5a758e | 110 | { |
GLemasson | 0:28766f5a758e | 111 | return readable(); |
GLemasson | 0:28766f5a758e | 112 | } |
GLemasson | 0:28766f5a758e | 113 | |
GLemasson | 0:28766f5a758e | 114 | } |
GLemasson | 1:5036a532fe62 | 115 | bool CircularArray::full() |
GLemasson | 0:28766f5a758e | 116 | { |
GLemasson | 0:28766f5a758e | 117 | return m_full; |
GLemasson | 0:28766f5a758e | 118 | |
GLemasson | 0:28766f5a758e | 119 | } |
GLemasson | 1:5036a532fe62 | 120 | bool CircularArray::empty() |
GLemasson | 0:28766f5a758e | 121 | { |
GLemasson | 0:28766f5a758e | 122 | return m_empty; |
GLemasson | 0:28766f5a758e | 123 | } |