Azinix

circbuf.h File Reference

Circular buffer data structures. More...

#include "util.h"
#include "array.h"

Go to the source code of this file.

Data Structures

struct  Circbuf_t
 Structure for keeping a generic circular buffer. More...

Defines

#define Circbuf_Init(type, N)   Circbuf_DoAlloc( sizeof( type ), ( (N) + 1) )
 Initialize the buffer.
#define Circbuf_Insert(type, cb, datum)
 Insert an entry into the buffer; return 1 on succ completion, 0 otherwise.
#define Circbuf_Delete(type, cb)
 Code to delete an entry from a circular buffer; value returned is deleted item.

Typedefs

typedef Circbuf_t Circbuf_t

Enumerations

enum  CircbufErrorType_t { CircbufDeleteEmpty_c, CircbufInsertFull_c }
 Types of errors when manipulating a Circbuf. More...

Functions

Circbuf_tCircbuf_DoAlloc (int, int)
 Allocate a circular buffer with entries of size entrySize.
int Circbuf_Num (Circbuf_t *)
 Number of entries.


Detailed Description

Circular buffer data structures.

Definition in file circbuf.h.


Define Documentation

#define Circbuf_Delete ( type,
cb   ) 

Value:

( !Circbuf_IsEmpty( cb  ) ) ? \
        ( ( cb->numEntries-- ) , \
    (  cb->head = ( Circbuf_Mod( ( cb->head + 1 ) , cb->N  ) ) ), \
        ( array_fetch( type, cb->entries, Circbuf_Mod( ( cb->head - 1 ) , cb->N )  ) ) )  \
    : \
    ( ( fprintf( stderr, "CircbufDeleteEmpty_c\n" ), assert(0), ( array_fetch( type, cb->entries, cb->N + 1 ) ) ) )
Code to delete an entry from a circular buffer; value returned is deleted item.

For some reason we cannot just use fail (maybe because it's a macro?) so we print the error, assert 0, and do an array fetch (which will also fail in case we used -O4, removed the assert, and the type of the comma operator works out ok (fetching type)

Definition at line 156 of file circbuf.h.

#define Circbuf_Init ( type,
 )     Circbuf_DoAlloc( sizeof( type ), ( (N) + 1) )

Initialize the buffer.

Definition at line 90 of file circbuf.h.

#define Circbuf_Insert ( type,
cb,
datum   ) 

Value:

( assert( !Circbuf_IsFull( cb ) ), \
      ( array_insert( type, cb->entries, cb->tail, datum ) ), \
      ( cb->numEntries++ ), \
      ( cb->tail = Circbuf_Mod( ( cb->tail + 1 ), cb->N ) ) )
Insert an entry into the buffer; return 1 on succ completion, 0 otherwise.

Circbuf_IsFull( cb ) ? \ ( fail( "CircbufInsertFull_c" ) ) : \ ( ( array_insert( type, cb->entries, cb->tail, datum ) ), \ ( cb->numEntries++ ), \ ( cb->tail = Circbuf_Mod( ( cb->tail + 1 ), cb->N ) ) )

Definition at line 138 of file circbuf.h.


Typedef Documentation

typedef struct Circbuf_t Circbuf_t

Definition at line 31 of file circbuf.h.


Enumeration Type Documentation

enum CircbufErrorType_t

Types of errors when manipulating a Circbuf.

Enumerator:
CircbufDeleteEmpty_c 
CircbufInsertFull_c 

Definition at line 39 of file circbuf.h.


Function Documentation

Circbuf_t* Circbuf_DoAlloc ( int  ,
int   
)

Allocate a circular buffer with entries of size entrySize.

Definition at line 28 of file circbuf.c.

int Circbuf_Num ( Circbuf_t  ) 

Number of entries.

Definition at line 46 of file circbuf.c.