ZEDA  1.6.18
Macros
universal ring buffer.

Macros

#define zRingClass(ring_t, cell_t)
 generate ring buffer class. More...
 
#define zRingSize(y)   (y)->size
 
#define zRingBuf(y)   ( (y)->buf )
 
#define zRingElemSize(y)   sizeof(*zRingBuf(y))
 
#define zRingHead(y)   ( &zRingBuf(y)[(y)->head] )
 
#define zRingElem(y, i)   ( &zRingBuf(y)[((y)->head+(i)) % (y)->size] )
 
#define zRingSetElem(a, i, d)   memcpy( zRingElem(a,i), (d), zRingElemSize(a) )
 
#define zRingReset(y)   ( (y)->head = 0 );
 
#define zRingInit(y)
 
#define zRingAlloc(y, type, size)
 allocate an array. More...
 
#define zRingFree(y)
 
#define zRingIncHead(y)
 
#define zRingDecHead(y)
 

Detailed Description

Macro Definition Documentation

◆ zRingClass

#define zRingClass (   ring_t,
  cell_t 
)
Value:
typedef struct{\
int size;\
int head;\
cell_t *buf;\
} ring_t

generate ring buffer class.

A macro zRingClass() can generate a new ring buffer class, which consists of the total number of elements, the current head of the ring buffer, and the pointer to the ring buffer. ring_t is the class name to be defined. cell_t is the class name of the data to be arrayed.

To allocate and free a ring buffer, use zRingAlloc() and zRingFree().

The number of elements is acquired by zRingSize(). The actual array head can be accessed by zRingBuf(). But, the ring buffer conceptually conceal the body array. zRingHead() is preferable to be used, instead. It allows to access to the data at the current head. An elements after some elements the head can be safely accessed by zRingElem() and zRingSetElem().

zRingReset() resets the head position to the actual head of the array.

To advance and get back the head, use zRingIncHead() and zRingDecHead(), respectively.

◆ zRingSize

#define zRingSize (   y)    (y)->size

◆ zRingBuf

#define zRingBuf (   y)    ( (y)->buf )

◆ zRingElemSize

#define zRingElemSize (   y)    sizeof(*zRingBuf(y))

◆ zRingHead

#define zRingHead (   y)    ( &zRingBuf(y)[(y)->head] )

◆ zRingElem

#define zRingElem (   y,
 
)    ( &zRingBuf(y)[((y)->head+(i)) % (y)->size] )

◆ zRingSetElem

#define zRingSetElem (   a,
  i,
 
)    memcpy( zRingElem(a,i), (d), zRingElemSize(a) )

◆ zRingReset

#define zRingReset (   y)    ( (y)->head = 0 );

◆ zRingInit

#define zRingInit (   y)
Value:
do{\
zRingSize(y) = (y)->head = 0;\
zRingBuf(y) = NULL;\
} while(0)

◆ zRingAlloc

#define zRingAlloc (   y,
  type,
  size 
)
Value:
do{\
zRingInit( y );\
if( !( zRingBuf(y) = zAlloc(type,size) ) )\
ZALLOCERROR();\
zRingSize(y) = (size);\
} while(0)
#define zAlloc(t, n)
Definition: zeda_misc.h:81
#define zRingBuf(y)
Definition: zeda_ring.h:66
#define zRingSize(y)
Definition: zeda_ring.h:65

allocate an array.

Parameters
yring buffer class instance to be created.
typethe data type of the array cells.
sthe size of the array.

◆ zRingFree

#define zRingFree (   y)
Value:
do{\
zFree( zRingBuf(y) );\
zRingInit( y );\
} while(0)
#define zRingBuf(y)
Definition: zeda_ring.h:66

◆ zRingIncHead

#define zRingIncHead (   y)
Value:
do{\
if( ++(y)->head >= zRingSize(y) ) (y)->head = 0;\
} while(0)
#define zRingSize(y)
Definition: zeda_ring.h:65

◆ zRingDecHead

#define zRingDecHead (   y)
Value:
do{\
if( --(y)->head < 0 ) (y)->head = zRingSize(y) - 1;\
} while(0)
#define zRingSize(y)
Definition: zeda_ring.h:65