1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
//
// Fifo class definition
//
#include "fifo.h"
#include<iostream>
using namespace std;
fifo::fifo(long length)
{
// allocate for new vector
capacity = length;
data = new MY_TYPE[length];
// set to be empty
clear();
}
fifo::~fifo()
{
// clean up data
delete [] data;
}
void fifo::clear()
{
// set to be empty
startInd = 0;
endInd = 0;
isFull = 0;
used = 0;
}
long fifo::append(MY_TYPE *newdata, long length, int overwrite)
{
// append data to the fifo, returning amount appended
long toappend = length;
long appended = 0;
long space = 0;
// append until done, full overwriting, until done
while (appended < length && !(isFull && !overwrite))
{
// see how much space we have
if (!overwrite && startInd > endInd)
{
// can only write up to startInd
space = startInd - endInd;
}
else
{
// can write up to the end of buffer
space = capacity - endInd;
}
// see if the amount to append is less than the space
if (toappend > space)
{
// can only append space amount
toappend = space;
}
// append that amount
memcpy(&data[endInd],&newdata[appended],toappend*sizeof(MY_TYPE));
// See if crossed the start
if ((startInd > endInd) && (endInd+toappend >= startInd))
{
// we are full
isFull = 1;
}
// update values
appended += toappend;
endInd += toappend;
toappend = length-appended;
// See if we must wrap around
if (endInd == capacity)
{
// we are at the end of the buffer, so wrap
endInd = 0;
}
// see if did not cross, but are still full
if (startInd == endInd)
{
// we filled the buffer
isFull = 1;
}
// if we are full, then start and end must be equal
if (isFull)
{
// set start to be end
startInd = endInd;
}
}
// done, return the amount appended
used+=appended;
if (used > capacity)
{
used = capacity;
}
return appended;
}
long fifo::consume(MY_TYPE *newdata, long length)
{
// fill newdata with values from the fifo
long consumed = 0;
long toconsume = length;
long space = 0;
// consume while there is data and we have not filled the newdata
while ((consumed < length) && !(!isFull && startInd==endInd))
{
// see how much we can consume
if (endInd > startInd)
{
// can consume up to the endInd
space = endInd - startInd;
}
else
{
// can consume up to end of buffer
space = capacity - startInd;
}
// see if have less space than needed
if (space < toconsume)
{
// Only consume amount needed
toconsume = space;
}
// consume it
memcpy(&newdata[consumed],&data[startInd],toconsume*sizeof(MY_TYPE));
// see if no longer full
if (toconsume > 0 && isFull)
{
// no longer full
isFull = 0;
}
// update values
consumed += toconsume;
startInd += toconsume;
toconsume = length - consumed;
// see if at end and must loop
if (startInd == capacity)
{
// loop to beginning
startInd = 0;
}
}
// done, return the amount consumed
used-=consumed;
return consumed;
}
long fifo::getUsed()
{
return used;
}
|