Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

The FifoEmbed Library


October, 2004: The FifoEmbed Library

Listing 2

#if 0 /* REQUIRED DEFINITIONS */
  #define FIFO_CELL_T int /* any type */
  #define FIFO_T myq      /* optional name of queue type */
  #define FIFO_P2_SIZE    /* optional, optimize for size 2^n */
#endif

#include "circ_arr.h"

typedef struct {
  FIFO_CELL_T *base, *end;
  union {
    volatile FIFO_CELL_T * volatile w;
    volatile FIFO_CELL_T * volatile const r;
    volatile FIFO_CELL_T * const l;
  } head, tail;
  int size, mask;
} FIFO_T;


/* Initialization */
#define FIFO_INIT( q, b, sz ) (\
  (q)->base = (b), (q)->end = (b) + (sz), \
  (q)->tail.w = (q)->head.w = (b), \
  (q)->size = (sz), (q)->mask = (sz) - 1 \
)

/* Producer primitives */
static inline BOOL fifo_full (const FIFO_T *q) {
  volatile const FIFO_CELL_T *head = q->head.r;
  #ifdef FIFO_P2_SIZE
  return CIRC_ARR_P2_DS_FULL (head, q->tail.l, q->mask);
  #else
  return CIRC_ARR_DS_FULL (head, q->tail.l, q->size);
  #endif
}
static inline void fifo_ins (FIFO_T *q, FIFO_CELL_T x) {
  *q->tail.l = x;
  q->tail.w = CIRC_ARR_ADV_PTR (q->tail.l, q->base, q->end);
}
#define FIFO_INS_CHK( q, x ) \
  (fifo_full (q) ? FALSE : (fifo_ins (q, x), TRUE))

static inline void fifo_swallow (FIFO_T *q, int delta) {
  q->tail.w =
    CIRC_ARR_WRAP_END (q->tail.l + delta, q->base, q->end);
}

/* Consumer primitives */
static inline FIFO_CELL_T fifo_read (FIFO_T *q) {
  q->head.w = CIRC_ARR_ADV_PTR (q->head.l, q->base, q->end);
  return *q->head.l;
}

#define FIFO_DROP( q, n ) ((q)->head.w = \
  CIRC_ARR_WRAP_FWD ((q)->head.l + n, (q)->end, (q)->size))


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.