The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Functions
ring_buffer.c File Reference

Simple ring buffers for packet contents. More...

#include <freeradius-devel/io/ring_buffer.h>
#include <freeradius-devel/util/strerror.h>
#include <freeradius-devel/util/debug.h>
#include <string.h>
+ Include dependency graph for ring_buffer.c:

Go to the source code of this file.

Data Structures

struct  fr_ring_buffer_s
 

Functions

uint8_tfr_ring_buffer_alloc (fr_ring_buffer_t *rb, size_t size)
 Mark data as allocated. More...
 
int fr_ring_buffer_close (fr_ring_buffer_t *rb)
 Close a ring buffer so that no further allocations can take place. More...
 
fr_ring_buffer_tfr_ring_buffer_create (TALLOC_CTX *ctx, size_t size)
 Create a ring buffer. More...
 
void fr_ring_buffer_debug (fr_ring_buffer_t *rb, FILE *fp)
 Print debug information about the ring buffer. More...
 
int fr_ring_buffer_free (fr_ring_buffer_t *rb, size_t size_to_free)
 Mark data as free,. More...
 
uint8_tfr_ring_buffer_reserve (fr_ring_buffer_t *rb, size_t size)
 Reserve room in the ring buffer. More...
 
size_t fr_ring_buffer_size (fr_ring_buffer_t *rb)
 Get the size of the ring buffer. More...
 
int fr_ring_buffer_start (fr_ring_buffer_t *rb, uint8_t **p_start, size_t *p_size)
 Get a pointer to the data at the start of the ring buffer. More...
 
size_t fr_ring_buffer_used (fr_ring_buffer_t *rb)
 Get the amount of data used in a ring buffer. More...
 

Detailed Description

Simple ring buffers for packet contents.

Id
3589c946da23712a0b0d8a251f91b9b6ba610e80

Definition in file ring_buffer.c.


Data Structure Documentation

◆ fr_ring_buffer_s

struct fr_ring_buffer_s

Definition at line 35 of file ring_buffer.c.

Data Fields
uint8_t * buffer actual start of the ring buffer
bool closed whether allocations are closed
size_t data_end end of used portion of the buffer
size_t data_start start of used portion of the buffer
size_t reserved amount of reserved data at write_offset
size_t size Size of this ring buffer.
size_t write_offset where writes are done

Function Documentation

◆ fr_ring_buffer_alloc()

uint8_t* fr_ring_buffer_alloc ( fr_ring_buffer_t rb,
size_t  size 
)

Mark data as allocated.

The size does not need to be a power of two. The application is responsible for doing cache-line alignment, if required.

The application does NOT need to call fr_ring_buffer_reserve() before calling this function.

If the allocation fails, the application should create a new ring buffer, and start allocating data there.

Parameters
[in]rba ring buffer
[in]sizeto mark as "used" at the tail end of the buffer.
Returns
  • NULL on error. Which can only be "ring buffer is full".
  • pointer to data on success

Definition at line 196 of file ring_buffer.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_ring_buffer_close()

int fr_ring_buffer_close ( fr_ring_buffer_t rb)

Close a ring buffer so that no further allocations can take place.

Once the ring buffer is empty, it will be automatically freed. It's called "close" and not "delete", because the ring buffer will still be active until all data has been removed.

If you don't care about the data in the ring buffer, you can just call talloc_free() on it.

Parameters
[in]rba ring buffer
Returns
  • <0 on error.
  • 0 on success

Definition at line 407 of file ring_buffer.c.

◆ fr_ring_buffer_create()

fr_ring_buffer_t* fr_ring_buffer_create ( TALLOC_CTX *  ctx,
size_t  size 
)

Create a ring buffer.

The size provided will be rounded up to the next highest power of 2, if it's not already a power of 2.

The ring buffer manages how much room is reserved (i.e. available to write to), and used. The application is responsible for tracking the start of the reservation, and it's write offset within that reservation.

Parameters
[in]ctxa talloc context
[in]sizeof the raw ring buffer array to allocate.
Returns
  • A new ring buffer on success.
  • NULL on failure.

Definition at line 64 of file ring_buffer.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_ring_buffer_debug()

void fr_ring_buffer_debug ( fr_ring_buffer_t rb,
FILE *  fp 
)

Print debug information about the ring buffer.

Parameters
[in]rbthe ring buffer
[in]fpthe FILE where the messages are printed.

Definition at line 485 of file ring_buffer.c.

◆ fr_ring_buffer_free()

int fr_ring_buffer_free ( fr_ring_buffer_t rb,
size_t  size_to_free 
)

Mark data as free,.

The size does not need to be a power of two. The application is responsible for doing cache alignment, if required. The application is responsible for tracking sizes of packets in the ring buffer.

If "unused" bytes are more than what's in the buffer, the used bytes are reset to zero.

Parameters
[in]rba ring buffer
[in]size_to_freebytes to mark as "unused" in the buffer.
Returns
  • <0 on error. Which can only be "ring buffer is full".
  • 0 on success

Definition at line 304 of file ring_buffer.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_ring_buffer_reserve()

uint8_t* fr_ring_buffer_reserve ( fr_ring_buffer_t rb,
size_t  size 
)

Reserve room in the ring buffer.

The size does not need to be a power of two. The application is responsible for doing cache alignment, if required.

If the reservation fails, the application should create a new ring buffer, and start reserving data there.

Parameters
[in]rba ring buffer
[in]sizeto see if we can reserve
Returns
  • NULL on error. Which can only be "ring buffer is full".
  • pointer to data on success

Definition at line 119 of file ring_buffer.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_ring_buffer_size()

size_t fr_ring_buffer_size ( fr_ring_buffer_t rb)

Get the size of the ring buffer.

Parameters
[in]rba ring buffer
Returns
size of the ring buffer.
  • <0 on error.
  • 0 on success

Definition at line 423 of file ring_buffer.c.

+ Here is the caller graph for this function:

◆ fr_ring_buffer_start()

int fr_ring_buffer_start ( fr_ring_buffer_t rb,
uint8_t **  p_start,
size_t p_size 
)

Get a pointer to the data at the start of the ring buffer.

Parameters
[in]rba ring buffer
[out]p_startpointer to data at the start of the ring buffer
[in]p_sizesize of the allocated block at the start of the ring buffer.
Returns
size of the used data in the ring buffer.
  • <0 on error.
  • 0 on success

Definition at line 464 of file ring_buffer.c.

+ Here is the caller graph for this function:

◆ fr_ring_buffer_used()

size_t fr_ring_buffer_used ( fr_ring_buffer_t rb)

Get the amount of data used in a ring buffer.

Parameters
[in]rba ring buffer
Returns
size of the used data in the ring buffer.
  • <0 on error.
  • 0 on success

Definition at line 437 of file ring_buffer.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function: