Fix Windows CPU count
[fio.git] / fifo.c
diff --git a/fifo.c b/fifo.c
index 1e55b6344ba1d4cbcb52dac65a6ddbb87d33a81d..ac0d21576dcf9fce5192945472cb7dd04f624bff 100644 (file)
--- a/fifo.c
+++ b/fifo.c
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  */
 
@@ -24,6 +24,7 @@
 #include <string.h>
 
 #include "fifo.h"
+#include "minmax.h"
 
 struct fifo *fifo_alloc(unsigned int size)
 {
@@ -31,11 +32,11 @@ struct fifo *fifo_alloc(unsigned int size)
 
        fifo = malloc(sizeof(struct fifo));
        if (!fifo)
-               return 0;
+               return NULL;
 
        fifo->buffer = malloc(size);
        fifo->size = size;
-       fifo->in = fifo->out = 0xffff0000;
+       fifo->in = fifo->out = 0;
 
        return fifo;
 }
@@ -50,7 +51,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)));
@@ -69,20 +70,29 @@ unsigned int fifo_put(struct fifo *fifo, void *buffer, unsigned int len)
        return len;
 }
 
-unsigned int fifo_get(struct fifo *fifo, void *buffer, unsigned int len)
+unsigned int fifo_get(struct fifo *fifo, void *buf, 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 (buf) {
+               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(buf, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
+
+               /*
+                * then get the rest (if any) from the beginning of the buffer
+                */
+               memcpy(buf + l, fifo->buffer, len - l);
+       }
 
        fifo->out += len;
 
+       if (fifo->in == fifo->out)
+               fifo->in = fifo->out = 0;
+
        return len;
 }