+
+/*!
+ * Pop a character from the fifo buffer.
+ *
+ * \note Calling \c fifo_push() on a full buffer is undefined.
+ * The caller must make sure the buffer has at least
+ * one free slot before calling this function.
+ *
+ * \note It is safe to call fifo_pop() and fifo_push() from
+ * concurrent contexts.
+ */
+INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
+{
+ /* Write at tail position */
+ *(fb->tail) = c;
+
+ if (fb->tail == fb->end)
+ /* wrap tail around */
+ fb->tail = fb->begin;
+ else
+ /* Move tail forward */
+ fb->tail++;
+}
+
+
+/*!
+ * Pop a character from the fifo buffer.
+ *
+ * \note Calling \c fifo_pop() on an empty buffer is undefined.
+ * The caller must make sure the buffer contains at least
+ * one character before calling this function.
+ *
+ * \note It is safe to call fifo_pop() and fifo_push() from
+ * concurrent contexts.
+ */
+INLINE unsigned char fifo_pop(FIFOBuffer *fb)
+{
+ if (fb->head == fb->end)
+ {
+ /* wrap head around */
+ fb->head = fb->begin;
+ return *(fb->end);
+ }
+ else
+ /* move head forward */
+ return *(fb->head++);
+}
+
+
+/*!
+ * Make the fifo empty, discarding all its current contents.
+ */
+INLINE void fifo_flush(FIFOBuffer *fb)
+{
+ fb->head = fb->tail;
+}
+
+
+/*!
+ * FIFO Initialization.
+ */
+INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
+{
+ fb->head = fb->tail = fb->begin = buf;
+ fb->end = buf + size - 1;
+}
+
+
+
+#if 0
+
+/*
+ * UNTESTED: if uncommented, to be moved in fifobuf.c
+ */
+void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
+{
+ size_t freelen;
+
+ /* Se c'e' spazio da tail alla fine del buffer */
+ if (fb->tail >= fb->head)
+ {
+ freelen = fb->end - fb->tail + 1;
+
+ /* C'e' abbastanza spazio per scrivere tutto il blocco? */
+ if (freelen < len)
+ {
+ /* Scrivi quello che entra fino alla fine del buffer */
+ memcpy(fb->tail, block, freelen);
+ block += freelen;
+ len -= freelen;
+ fb->tail = fb->begin;
+ }
+ else
+ {
+ /* Scrivi tutto il blocco */
+ memcpy(fb->tail, block, len);
+ fb->tail += len;
+ return;
+ }
+ }
+
+ for(;;)
+ {
+ while (!(freelen = fb->head - fb->tail - 1))
+ Delay(FIFO_POLLDELAY);
+
+ /* C'e' abbastanza spazio per scrivere tutto il blocco? */
+ if (freelen < len)
+ {
+ /* Scrivi quello che entra fino alla fine del buffer */
+ memcpy(fb->tail, block, freelen);
+ block += freelen;
+ len -= freelen;
+ fb->tail += freelen;
+ }
+ else
+ {
+ /* Scrivi tutto il blocco */
+ memcpy(fb->tail, block, len);
+ fb->tail += len;
+ return;
+ }
+ }
+}
+#endif
+
+#endif /* MWARE_FIFO_H */