MPD
Functions
fifo_buffer.h File Reference

This is a general purpose FIFO buffer library. More...

#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Functions

struct fifo_buffer * fifo_buffer_new (size_t size)
 Creates a new #fifo_buffer object.
 
struct fifo_buffer * fifo_buffer_realloc (struct fifo_buffer *buffer, size_t new_size)
 Change the capacity of the #fifo_buffer, while preserving existing data.
 
void fifo_buffer_free (struct fifo_buffer *buffer)
 Frees the resources consumed by this #fifo_buffer object.
 
size_t fifo_buffer_capacity (const struct fifo_buffer *buffer)
 Return the capacity of the buffer, i.e.
 
size_t fifo_buffer_available (const struct fifo_buffer *buffer)
 Return the number of bytes currently stored in the buffer.
 
void fifo_buffer_clear (struct fifo_buffer *buffer)
 Clears all data currently in this #fifo_buffer object.
 
const void * fifo_buffer_read (const struct fifo_buffer *buffer, size_t *length_r)
 Reads from the beginning of the buffer.
 
void fifo_buffer_consume (struct fifo_buffer *buffer, size_t length)
 Marks data at the beginning of the buffer as "consumed".
 
void * fifo_buffer_write (struct fifo_buffer *buffer, size_t *max_length_r)
 Prepares writing to the buffer.
 
void fifo_buffer_append (struct fifo_buffer *buffer, size_t length)
 Commits the write operation initiated by fifo_buffer_write().
 
bool fifo_buffer_is_empty (struct fifo_buffer *buffer)
 Checks if the buffer is empty.
 
bool fifo_buffer_is_full (struct fifo_buffer *buffer)
 Checks if the buffer is full.
 

Detailed Description

This is a general purpose FIFO buffer library.

You may append data at the end, while another instance reads data from the beginning. It is optimized for zero-copy usage: you get pointers to the real buffer, where you may operate on.

This library is not thread safe.

Definition in file fifo_buffer.h.

Function Documentation

void fifo_buffer_append ( struct fifo_buffer *  buffer,
size_t  length 
)

Commits the write operation initiated by fifo_buffer_write().

Parameters
bufferthe #fifo_buffer object
lengththe number of bytes which were written
size_t fifo_buffer_available ( const struct fifo_buffer *  buffer)

Return the number of bytes currently stored in the buffer.

size_t fifo_buffer_capacity ( const struct fifo_buffer *  buffer)

Return the capacity of the buffer, i.e.

the size that was passed to fifo_buffer_new().

void fifo_buffer_clear ( struct fifo_buffer *  buffer)

Clears all data currently in this #fifo_buffer object.

This does not overwrite the actuall buffer; it just resets the internal pointers.

void fifo_buffer_consume ( struct fifo_buffer *  buffer,
size_t  length 
)

Marks data at the beginning of the buffer as "consumed".

Parameters
bufferthe #fifo_buffer object
lengththe number of bytes which were consumed
void fifo_buffer_free ( struct fifo_buffer *  buffer)

Frees the resources consumed by this #fifo_buffer object.

bool fifo_buffer_is_empty ( struct fifo_buffer *  buffer)

Checks if the buffer is empty.

bool fifo_buffer_is_full ( struct fifo_buffer *  buffer)

Checks if the buffer is full.

struct fifo_buffer* fifo_buffer_new ( size_t  size)
read

Creates a new #fifo_buffer object.

Free this object with fifo_buffer_free().

Parameters
sizethe size of the buffer in bytes
Returns
the new #fifo_buffer object
const void* fifo_buffer_read ( const struct fifo_buffer *  buffer,
size_t *  length_r 
)

Reads from the beginning of the buffer.

To remove consumed data from the buffer, call fifo_buffer_consume().

Parameters
bufferthe #fifo_buffer object
length_rthe maximum amount to read is returned here
Returns
a pointer to the beginning of the buffer, or NULL if the buffer is empty
struct fifo_buffer* fifo_buffer_realloc ( struct fifo_buffer *  buffer,
size_t  new_size 
)
read

Change the capacity of the #fifo_buffer, while preserving existing data.

Parameters
bufferthe old buffer, may be NULL
new_sizethe requested new size of the #fifo_buffer; must not be smaller than the data which is stored in the old buffer
Returns
the new buffer, may be NULL if the requested new size is 0
void* fifo_buffer_write ( struct fifo_buffer *  buffer,
size_t *  max_length_r 
)

Prepares writing to the buffer.

This returns a buffer which you can write to. To commit the write operation, call fifo_buffer_append().

Parameters
bufferthe #fifo_buffer object
max_length_rthe maximum amount to write is returned here
Returns
a pointer to the end of the buffer, or NULL if the buffer is already full