Table of Contents

NAME

QccFifo - data structure for bitstream fifo

SYNOPSIS

#include "libQccPack.h"

int QccFifoInitialize(QccFifo *fifo);
int QccFifoStart(QccFifo *fifo);
int QccFifoEnd(QccFifo *fifo);
int QccFifoFlush(QccFifo *fifo);
int QccFifoRestart(QccFifo *fifo);

DESCRIPTION

QccPack provides the QccFifo data structure to provide first-in, first-out operation on bitstreams. That is, a bitstream can be written to fifo and then read from fifo with the bits read out in the same order they were written in.

DATA STRUCTURE

The QccFifo data structure is defined as:

typedef struct
{
QccBitBuffer output_buffer;
QccBitBuffer input_buffer;
} QccFifo;

The fields of QccFifo are as follows:

output_buffer
The QccBitBuffer(3) structure to which the bitstream is written.
input_buffer
The QccBitBuffer(3) structure from which the bitstream is read.

ROUTINES

QccFifoInitialize() should be called before any use of a QccFifo structure. QccFifoInitialize() initializes the input_buffer and output_buffer fields via calls to QccBitBufferInitialize(3) .

QccFifoStart() starts both input_buffer and output_buffer via calls to QccBitBufferStart(3) .

QccFifoEnd() should be called after all accesses (read or write) to fifo are complete. QccFifoEnd() calls QccBitBufferEnd(3) to stop both buffers.

QccFifoFlush() should be called at the end of writing a bitstream after all bits have been written but before QccFifoEnd() is called. QccFifoFlush() ensures that the last byte being packed with bits, which may not be full, is output to the fifo by calling QccBitBufferFlush(3) on both buffers.

Once QccFifoStart(3) has been called, any of the usual bit-buffer routines for reading (e.g., QccBitBufferReadChar(3) , QccBitBufferReadInt(3) ) or writing (e.g., QccBitBufferWriteChar(3) , QccBitBufferWriteInt(3) ) can be called on the input_buffer and output_buffer fields of the fifo. The bits are read out of input_buffer in the same order they were written into output_buffer, so one or more calls to write to output_buffer should precede the first call to read from input_buffer.

QccFifoRestart() restarts fifo by calling QccFifoEnd() and QccFifoStart() in succession. Any data currently held in fifo is lost; that is, the fifo is emptied.

IMPLEMENTATION

The fifo operation within the QccFifo structure is implemented with a pipe created by a call to pipe(2) during the call to QccFifoStart(). Specifically, fileptr of output_buffer is set to the write end of the pipe, while fileptr of input_buffer is set to the read end. Consequently, input_buffer is joined to output_buffer through this pipe. QccFileDescriptorOpen(3) is called to create file streams for the file descriptors returned by pipe(2) , and fcntl(2) is used to set both ends of the pipe to non-blocking.

As a consequence of the use of pipe(2) to implement the fifo, there is a limit to amount of bits that may be held in the fifo at any given time. Unfortunately, this limit is system-dependent. On POSIX-compliant systems, the pipe is required to be capable of holding at least PIPE_BUF bytes, and PIPE_BUF must be at least 512 bytes. This means that, on a POSIX-compliant system, at least 4096 bits can be written to a QccFifo structure before bits must be read out.

Attempts to write to a full fifo (one already containing the system-dependent maximum number of bits) will return in error, as will attempts to read from an empty fifo. It is the responsibility of the calling application to avoid fifo overflow and underflow, and the calling application should do so without trying to determine the maximum size of the fifo, as there does not seem to be a reliable system-independent way of doing so.

RETURN VALUE

Each of these routines return 0 upon successful completion, 1 on error.

SEE ALSO

QccBitBuffer(3) , QccFileDescriptorOpen(3) , QccPack(3) , pipe(2) , fcntl(2)

AUTHOR

Copyright (C) 1997-2021 James E. Fowler


Table of Contents



Get QccPack at SourceForge.net. Fast, secure and Free Open Source software downloads