fifo updates
authorJens Axboe <jens.axboe@oracle.com>
Wed, 16 May 2007 09:54:43 +0000 (11:54 +0200)
committerJens Axboe <jens.axboe@oracle.com>
Wed, 16 May 2007 09:54:43 +0000 (11:54 +0200)
fifo.c
fifo.h

diff --git a/fifo.c b/fifo.c
index 1e55b6344ba1d4cbcb52dac65a6ddbb87d33a81d..5406aa572199a9c1f59756f3ecfd397c74a07e50 100644 (file)
--- a/fifo.c
+++ b/fifo.c
@@ -50,7 +50,7 @@ unsigned int fifo_put(struct fifo *fifo, void *buffer, unsigned int len)
 {
        unsigned int l;
 
-       len = min(len, fifo->size - fifo->in + fifo->out);
+       len = min(len, fifo_room(fifo));
 
        /* first put the data starting from fifo->in to buffer end */
        l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
@@ -71,18 +71,27 @@ unsigned int fifo_put(struct fifo *fifo, void *buffer, unsigned int len)
 
 unsigned int fifo_get(struct fifo *fifo, void *buffer, unsigned int len)
 {
-       unsigned int l;
-
        len = min(len, fifo->in - fifo->out);
 
-       /* first get the data from fifo->out until the end of the buffer */
-       l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
-       memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
+       if (buffer) {
+               unsigned int l;
 
-       /* then get the rest (if any) from the beginning of the buffer */
-       memcpy(buffer + l, fifo->buffer, len - l);
+               /*
+                * first get the data from fifo->out until the end of the buffer
+                */
+               l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
+               memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)),l);
+
+               /*
+                * then get the rest (if any) from the beginning of the buffer
+                */
+               memcpy(buffer + l, fifo->buffer, len - l);
+       }
 
        fifo->out += len;
 
+       if (fifo->in == fifo->out)
+               fifo->in = fifo->out = 0;
+
        return len;
 }
diff --git a/fifo.h b/fifo.h
index cfacea996f0309b323b93b67a79f1b89d555dd2b..8e34fb61f3675fa250696e5bf92348dfe531b2bc 100644 (file)
--- a/fifo.h
+++ b/fifo.h
@@ -35,6 +35,11 @@ static inline unsigned int fifo_len(struct fifo *fifo)
        return fifo->in - fifo->out;
 }
 
+static inline unsigned int fifo_room(struct fifo *fifo)
+{
+       return fifo->size - fifo->in + fifo->out;
+}
+
 #define min(x,y) ({ \
        typeof(x) _x = (x);     \
        typeof(y) _y = (y);     \