PatchworkOS
Loading...
Searching...
No Matches
Ring Buffer

Ring buffer. More...

Data Structures

struct  ring_t
 Ring buffer structure. More...
 

Macros

#define RING_CREATE(bufferPtr, bufferSize)    {.buffer = bufferPtr, .size = bufferSize, .readIndex = 0, .writeIndex = 0, .dataLength = 0}
 Create a ring buffer initializer.
 

Functions

static void ring_init (ring_t *ring, void *buffer, uint64_t size)
 Initialize a ring buffer.
 
static uint64_t ring_data_length (const ring_t *ring)
 Get the length of data currently stored in the ring buffer.
 
static uint64_t ring_free_length (const ring_t *ring)
 Get the length of free space in the ring buffer.
 
static uint64_t ring_write (ring_t *ring, const void *buffer, uint64_t count)
 Write data to the ring buffer.
 
static uint64_t ring_read (ring_t *ring, void *buffer, uint64_t count)
 Read data from the ring buffer.
 
static uint64_t ring_read_at (const ring_t *ring, uint64_t offset, void *buffer, uint64_t count)
 Read data from the ring buffer at a specific offset without modifying the read index.
 
static void ring_move_read_forward (ring_t *ring, uint64_t offset)
 Move the read index forward by a specified offset.
 
static uint64_t ring_get_byte (const ring_t *ring, uint64_t offset, uint8_t *byte)
 Get a byte from the ring buffer at a specific offset without modifying the read index.
 

Detailed Description

Ring buffer.

Macro Definition Documentation

◆ RING_CREATE

#define RING_CREATE (   bufferPtr,
  bufferSize 
)     {.buffer = bufferPtr, .size = bufferSize, .readIndex = 0, .writeIndex = 0, .dataLength = 0}

Create a ring buffer initializer.

Parameters
bufferPtrPointer to the buffer memory.
bufferSizeSize of the buffer memory in bytes.

Definition at line 33 of file ring.h.

Function Documentation

◆ ring_data_length()

static uint64_t ring_data_length ( const ring_t ring)
inlinestatic

Get the length of data currently stored in the ring buffer.

Parameters
ringPointer to the ring buffer.
Returns
Length of data in bytes.

Definition at line 58 of file ring.h.

References ring_t::dataLength, and ring.

Referenced by local_socket_poll(), local_socket_recv(), log_file_flush_to_screen(), pipe_poll(), pipe_read(), ring_move_read_forward(), and ring_read().

◆ ring_free_length()

static uint64_t ring_free_length ( const ring_t ring)
inlinestatic

Get the length of free space in the ring buffer.

Parameters
ringPointer to the ring buffer.
Returns
Length of free space in bytes.

Definition at line 69 of file ring.h.

References ring_t::dataLength, ring, and ring_t::size.

Referenced by local_socket_poll(), local_socket_send(), pipe_poll(), pipe_write(), and ring_write().

◆ ring_get_byte()

static uint64_t ring_get_byte ( const ring_t ring,
uint64_t  offset,
uint8_t byte 
)
inlinestatic

Get a byte from the ring buffer at a specific offset without modifying the read index.

Parameters
ringPointer to the ring buffer.
offsetOffset from the current read index to get the byte.
bytePointer to store the retrieved byte.
Returns
0 on success, or ERR if offset is out of bounds.

Definition at line 210 of file ring.h.

References ring_t::buffer, ring_t::dataLength, ERR, ring_t::readIndex, ring, and ring_t::size.

Referenced by log_file_flush_to_screen().

◆ ring_init()

static void ring_init ( ring_t ring,
void *  buffer,
uint64_t  size 
)
inlinestatic

Initialize a ring buffer.

Parameters
ringPointer to the ring buffer to initialize.
bufferPointer to the buffer memory.
sizeSize of the buffer memory in bytes.

Definition at line 43 of file ring.h.

References ring_t::buffer, buffer, ring_t::dataLength, ring_t::readIndex, ring, ring_t::size, and ring_t::writeIndex.

Referenced by local_conn_new(), pipe_open(), and pipe_open2().

◆ ring_move_read_forward()

static void ring_move_read_forward ( ring_t ring,
uint64_t  offset 
)
static

Move the read index forward by a specified offset.

If the offset exceeds the current data length, no action is taken.

Parameters
ringPointer to the ring buffer.
offsetNumber of bytes to move the read index forward.

Definition at line 191 of file ring.h.

References ring_t::dataLength, ring_t::readIndex, ring, ring_data_length(), and ring_t::size.

Referenced by local_socket_recv().

◆ ring_read()

static uint64_t ring_read ( ring_t ring,
void *  buffer,
uint64_t  count 
)
inlinestatic

Read data from the ring buffer.

Parameters
ringPointer to the ring buffer.
bufferPointer to the buffer to store the read data.
countNumber of bytes to read.
Returns
Number of bytes read, or ERR if not enough data is available.

Definition at line 121 of file ring.h.

References ring_t::buffer, buffer, count, ring_t::dataLength, ERR, memcpy(), ring_t::readIndex, ring, ring_data_length(), and ring_t::size.

Referenced by local_socket_recv(), and pipe_read().

◆ ring_read_at()

static uint64_t ring_read_at ( const ring_t ring,
uint64_t  offset,
void *  buffer,
uint64_t  count 
)
inlinestatic

Read data from the ring buffer at a specific offset without modifying the read index.

Parameters
ringPointer to the ring buffer.
offsetOffset from the current read index to start reading.
bufferPointer to the buffer to store the read data.
countNumber of bytes to read.
Returns
Number of bytes read, or 0 if offset is out of bounds.

Definition at line 156 of file ring.h.

References ring_t::buffer, buffer, count, ring_t::dataLength, memcpy(), ring_t::readIndex, ring, and ring_t::size.

Referenced by local_socket_recv(), log_file_flush_to_screen(), and log_file_op_read().

◆ ring_write()

static uint64_t ring_write ( ring_t ring,
const void *  buffer,
uint64_t  count 
)
inlinestatic

Write data to the ring buffer.

If the data to be written exceeds the free space in the buffer, the oldest data will be overwritten.

Parameters
ringPointer to the ring buffer.
bufferPointer to the data to write.
countNumber of bytes to write.
Returns
Number of bytes written.

Definition at line 85 of file ring.h.

References ring_t::buffer, buffer, count, ring_t::dataLength, memcpy(), ring_t::readIndex, ring, ring_free_length(), ring_t::size, and ring_t::writeIndex.

Referenced by local_socket_send(), log_file_write(), and pipe_write().