From 5e05f47d61ebad8aee6c3831912b21ad5dcc36e3 Mon Sep 17 00:00:00 2001 From: "B. Bogart" Date: Sat, 13 Aug 2005 01:16:59 +0000 Subject: Initial commit of readanysf~ 0.13.1 for August svn path=/trunk/externals/august/readanysf~/; revision=3426 --- src/Fifo.cpp | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/Fifo.cpp (limited to 'src/Fifo.cpp') diff --git a/src/Fifo.cpp b/src/Fifo.cpp new file mode 100644 index 0000000..c5f0853 --- /dev/null +++ b/src/Fifo.cpp @@ -0,0 +1,152 @@ +#include +#include "Fifo.h" + +Fifo::Fifo () +{ + astate = 0; // not allocated + buffer = NULL; + totsize = 0; + datasize = 0; + start = 0; + pthread_mutex_init (&mut, 0); +} + +Fifo::Fifo (unsigned int size) +{ + buffer = new char[size]; + if (buffer != NULL) + { + astate = 1; + totsize = size; + datasize = 0; + start = 0; + } + else + { + astate = 0; + totsize = 0; + datasize = 0; + start = 0; + } + pthread_mutex_init (&mut, 0); +} + +Fifo::~Fifo () +{ + pthread_mutex_lock (&mut); + if (astate) + delete buffer; + pthread_mutex_unlock (&mut); +} + +void +Fifo::Flush () +{ + pthread_mutex_lock (&mut); + astate = 1; + //totsize = size; + datasize = 0; + start = 0; + pthread_mutex_unlock (&mut); + //for (int x =0; x < sizeof(buffer); x++) { + // buffer[x] = 0; + //} +} + +int +Fifo::ReAlloc (unsigned int size) +{ + pthread_mutex_lock (&mut); + if (astate) + delete buffer; + buffer = new char[size]; + if (buffer != NULL) + { + astate = 1; + totsize = size; + datasize = 0; + start = 0; + pthread_mutex_unlock (&mut); + } + else + { + astate = 0; + totsize = 0; + datasize = 0; + start = 0; + pthread_mutex_unlock (&mut); + return -1; + } + return 0; +} + +void * +Fifo::Read (void *buf, unsigned int &len) +{ + pthread_mutex_lock (&mut); + if (len > datasize) + len = datasize; + unsigned int rest; + if (len > (totsize - start)) + rest = len - (totsize - start); + else + rest = 0; + unsigned int first = len - rest; + memcpy (buf, buffer + start, first); + memcpy ((char *) buf + first, buffer, rest); + datasize -= len; + start += len; + if (start >= totsize) + start = rest; + //if (datasize == 0) printf("in fifo READ, data is zero\n"); + pthread_mutex_unlock (&mut); + return buf; +} + +int +Fifo::Write (void *buf, unsigned int len) +{ + pthread_mutex_lock (&mut); + unsigned int end; + end = start + datasize; + if (end > totsize) + end = end - totsize; + if (len > (totsize - datasize)) + { + pthread_mutex_unlock (&mut); + return -1; + } + unsigned int rest; + if ((len + end) > totsize) + rest = (len + end) - totsize; + else + rest = 0; + unsigned int first = len - rest; + memcpy (buffer + end, buf, first); + memcpy (buffer, (char *) buf + first, rest); + + datasize += len; + //if (datasize == 0) printf("in fifo WRITE, data is zero\n"); + pthread_mutex_unlock (&mut); + return len; +} + +unsigned int +Fifo::FreeSpace (void) +{ // do we need locks here? + int x; + pthread_mutex_lock (&mut); + x = totsize - datasize; + pthread_mutex_unlock (&mut); + return x; +} + +unsigned int +Fifo::UsedSpace (void) +{ + int x; + pthread_mutex_lock (&mut); + x = datasize; + pthread_mutex_unlock (&mut); + return x; +} -- cgit v1.2.1