Morse Micro IoT SDK  2.9.7
Morse Micro Buffer (mmbuf) API

Detailed Description

This API provides support for buffers tailored towards packet-like data that has headers and trailers that are applied at subsequent layers.

It is designed to support various backends for memory allocation. The default is allocation on the heap, but other methods could be used due to the flexible API.

Data Structures

struct  mmbuf
 Core mmbuf data structure. More...
 
struct  mmbuf_ops
 Operations data structure for mmbuf. More...
 
struct  mmbuf_list
 Structure that can be used as the head of a linked list of mmbufs that counts its length. More...
 

Macros

#define MMBUF_LIST_INIT   { NULL, NULL, 0 }
 Static initializer for mmbuf_list. More...
 

Functions

static void mmbuf_init (struct mmbuf *mmbuf, uint8_t *buf, uint32_t buf_len, uint32_t data_start_offset, const struct mmbuf_ops *ops)
 Initialize an mmbuf header with the given values. More...
 
struct mmbufmmbuf_alloc_on_heap (uint32_t space_at_start, uint32_t space_at_end)
 Allocate a new mmbuf on the heap (using mmosal_malloc()). More...
 
struct mmbufmmbuf_make_copy_on_heap (struct mmbuf *original)
 Make a copy of the given mmbuf. More...
 
void mmbuf_release (struct mmbuf *mmbuf)
 Release a reference to the given mmbuf. More...
 
static uint8_t * mmbuf_get_data_start (struct mmbuf *mmbuf)
 Gets a pointer to the start of the data in the mmbuf. More...
 
static uint8_t * mmbuf_get_data_end (struct mmbuf *mmbuf)
 Gets a pointer to the end of the data in the mmbuf. More...
 
static uint32_t mmbuf_get_data_length (struct mmbuf *mmbuf)
 Gets the length of the data currently in the mmbuf. More...
 
static uint32_t mmbuf_available_space_at_start (struct mmbuf *mmbuf)
 Returns the amount of space available for prepending to the data in the buffer. More...
 
static uint32_t mmbuf_available_space_at_end (struct mmbuf *mmbuf)
 Returns the amount of space available for appending to the data in the buffer. More...
 
static uint8_t * mmbuf_prepend (struct mmbuf *mmbuf, uint32_t len)
 Reserves space immediately before the data currently in the given mmbuf and returns a pointer to this space. More...
 
static void mmbuf_prepend_data (struct mmbuf *mmbuf, const uint8_t *data, uint32_t len)
 Prepends the given data to the data already in the mmbuf. More...
 
static uint8_t * mmbuf_append (struct mmbuf *mmbuf, uint32_t len)
 Reserves space immediately after the data currently in the given mmbuf and returns a pointer to this space. More...
 
static void mmbuf_append_data (struct mmbuf *mmbuf, const uint8_t *data, uint32_t len)
 Appends the given data to the data already in the mmbuf. More...
 
static uint8_t * mmbuf_remove_from_start (struct mmbuf *mmbuf, uint32_t len)
 Remove data from the start of the mmbuf. More...
 
static uint8_t * mmbuf_remove_from_end (struct mmbuf *mmbuf, uint32_t len)
 Remove data from the end of the mmbuf. More...
 
static void mmbuf_truncate (struct mmbuf *mmbuf, uint32_t len)
 Truncate the mmbuf data to the given length. More...
 
static void mmbuf_list_init (struct mmbuf_list *list)
 Initialization function for mmbuf_list, for cases where MMBUF_LIST_INIT cannot be used. More...
 
void mmbuf_list_prepend (struct mmbuf_list *list, struct mmbuf *mmbuf)
 Add an mmbuf to the start of an mmbuf list. More...
 
void mmbuf_list_append (struct mmbuf_list *list, struct mmbuf *mmbuf)
 Add an mmbuf to the end of an mmbuf list. More...
 
bool mmbuf_list_remove (struct mmbuf_list *list, struct mmbuf *mmbuf)
 Remove an mmbuf from an mmbuf list. More...
 
struct mmbufmmbuf_list_dequeue (struct mmbuf_list *list)
 Remove the mmbuf at the head of the list and return it. More...
 
struct mmbufmmbuf_list_dequeue_tail (struct mmbuf_list *list)
 Remove the mmbuf at the tail of the list and return it. More...
 
static struct mmbufmmbuf_list_dequeue_all (struct mmbuf_list *list)
 Remove all mmbufs from the list and return as a linked list. More...
 
static bool mmbuf_list_is_empty (struct mmbuf_list *list)
 Checks whether the given mmbuf list is empty. More...
 
static struct mmbufmmbuf_list_peek (struct mmbuf_list *list)
 Returns the head of the mmbuf list. More...
 
static struct mmbufmmbuf_list_peek_tail (struct mmbuf_list *list)
 Returns the tail of the mmbuf list. More...
 
void mmbuf_list_clear (struct mmbuf_list *list)
 Free all the packets in the given list and reset the list to empty state. More...
 

Macro Definition Documentation

◆ MMBUF_LIST_INIT

#define MMBUF_LIST_INIT   { NULL, NULL, 0 }

Static initializer for mmbuf_list.

Definition at line 337 of file mmbuf.h.

Function Documentation

◆ mmbuf_alloc_on_heap()

struct mmbuf * mmbuf_alloc_on_heap ( uint32_t  space_at_start,
uint32_t  space_at_end 
)

Allocate a new mmbuf on the heap (using mmosal_malloc()).

Parameters
space_at_startAmount of space to reserve at start of buffer.
space_at_endAmount of space to reserve at end of buffer.
Note
start_offset will be set to space_at_start, and buf_len will be the sum of space_at_start and space_at_end (rounded up to a multiple of 4).
Returns
newly allocated mmbuf on success or NULL on failure.

◆ mmbuf_append()

static uint8_t * mmbuf_append ( struct mmbuf mmbuf,
uint32_t  len 
)
inlinestatic

Reserves space immediately after the data currently in the given mmbuf and returns a pointer to this space.

For a function that also copies data in, see mmbuf_append_data().

Warning
len must be less than or equal to mmbuf_available_space_at_end().
Parameters
mmbufThe mmbuf to operate on.
lenLength of data to be append.
Returns
a pointer to the place in the buffer where the data should be put.

Definition at line 238 of file mmbuf.h.

◆ mmbuf_append_data()

static void mmbuf_append_data ( struct mmbuf mmbuf,
const uint8_t *  data,
uint32_t  len 
)
inlinestatic

Appends the given data to the data already in the mmbuf.

Warning
len must be less than or equal to mmbuf_available_space_at_start().
Parameters
mmbufThe mmbuf to operate on.
dataThe data to be prepended.
lenLength of data to be prepended.

Definition at line 255 of file mmbuf.h.

◆ mmbuf_available_space_at_end()

static uint32_t mmbuf_available_space_at_end ( struct mmbuf mmbuf)
inlinestatic

Returns the amount of space available for appending to the data in the buffer.

Parameters
mmbufThe mmbuf to operate on.
Returns
the available space in bytes.

Definition at line 182 of file mmbuf.h.

◆ mmbuf_available_space_at_start()

static uint32_t mmbuf_available_space_at_start ( struct mmbuf mmbuf)
inlinestatic

Returns the amount of space available for prepending to the data in the buffer.

Parameters
mmbufThe mmbuf to operate on.
Returns
the available space in bytes.

Definition at line 170 of file mmbuf.h.

◆ mmbuf_get_data_end()

static uint8_t * mmbuf_get_data_end ( struct mmbuf mmbuf)
inlinestatic

Gets a pointer to the end of the data in the mmbuf.

Parameters
mmbufThe mmbuf to operate on.
Returns
a pointer to the end of the data in the mmbuf.

Definition at line 145 of file mmbuf.h.

◆ mmbuf_get_data_length()

static uint32_t mmbuf_get_data_length ( struct mmbuf mmbuf)
inlinestatic

Gets the length of the data currently in the mmbuf.

Parameters
mmbufThe mmbuf to operate on.
Returns
the length of the data currently in the mmbuf (note that this is different from the length of the available buffer space).

Definition at line 158 of file mmbuf.h.

◆ mmbuf_get_data_start()

static uint8_t * mmbuf_get_data_start ( struct mmbuf mmbuf)
inlinestatic

Gets a pointer to the start of the data in the mmbuf.

Parameters
mmbufThe mmbuf to operate on.
Returns
a pointer to the start of the data in the mmbuf.

Definition at line 133 of file mmbuf.h.

◆ mmbuf_init()

static void mmbuf_init ( struct mmbuf mmbuf,
uint8_t *  buf,
uint32_t  buf_len,
uint32_t  data_start_offset,
const struct mmbuf_ops ops 
)
inlinestatic

Initialize an mmbuf header with the given values.

Parameters
mmbufmmbuf to initialize.
bufPointer to buffer.
buf_lenLength of buf.
data_start_offsetInitial value for start_offset.
opsOperations data structure.

Definition at line 84 of file mmbuf.h.

◆ mmbuf_list_append()

void mmbuf_list_append ( struct mmbuf_list list,
struct mmbuf mmbuf 
)

Add an mmbuf to the end of an mmbuf list.

Parameters
listThe list to append to.
mmbufThe mmbuf to append.

◆ mmbuf_list_clear()

void mmbuf_list_clear ( struct mmbuf_list list)

Free all the packets in the given list and reset the list to empty state.

Parameters
listThe list to clear.

◆ mmbuf_list_dequeue()

struct mmbuf * mmbuf_list_dequeue ( struct mmbuf_list list)

Remove the mmbuf at the head of the list and return it.

Parameters
listThe list to dequeue from.
Returns
the dequeued mmbuf, or NULL if the list is empty.

◆ mmbuf_list_dequeue_all()

static struct mmbuf * mmbuf_list_dequeue_all ( struct mmbuf_list list)
inlinestatic

Remove all mmbufs from the list and return as a linked list.

Parameters
listThe list to dequeue from.
Returns
the dequeued mmbufs, or NULL if the list is empty.

Definition at line 403 of file mmbuf.h.

◆ mmbuf_list_dequeue_tail()

struct mmbuf * mmbuf_list_dequeue_tail ( struct mmbuf_list list)

Remove the mmbuf at the tail of the list and return it.

Parameters
listThe list to dequeue from.
Returns
the dequeued mmbuf, or NULL if the list is empty.

◆ mmbuf_list_init()

static void mmbuf_list_init ( struct mmbuf_list list)
inlinestatic

Initialization function for mmbuf_list, for cases where MMBUF_LIST_INIT cannot be used.

Parameters
listThe mmbuf_list to init.

Definition at line 345 of file mmbuf.h.

◆ mmbuf_list_is_empty()

static bool mmbuf_list_is_empty ( struct mmbuf_list list)
inlinestatic

Checks whether the given mmbuf list is empty.

Parameters
listThe list to check.
Returns
true if the list is empty, else false.

Definition at line 419 of file mmbuf.h.

◆ mmbuf_list_peek()

static struct mmbuf * mmbuf_list_peek ( struct mmbuf_list list)
inlinestatic

Returns the head of the mmbuf list.

Parameters
listThe list to peek into.
Returns
the mmbuf at the head of the list.

Definition at line 431 of file mmbuf.h.

◆ mmbuf_list_peek_tail()

static struct mmbuf * mmbuf_list_peek_tail ( struct mmbuf_list list)
inlinestatic

Returns the tail of the mmbuf list.

Parameters
listThe list to peek into.
Returns
the mmbuf at the tail of the list.

Definition at line 443 of file mmbuf.h.

◆ mmbuf_list_prepend()

void mmbuf_list_prepend ( struct mmbuf_list list,
struct mmbuf mmbuf 
)

Add an mmbuf to the start of an mmbuf list.

Parameters
listThe list to prepend to.
mmbufThe mmbuf to prepend.

◆ mmbuf_list_remove()

bool mmbuf_list_remove ( struct mmbuf_list list,
struct mmbuf mmbuf 
)

Remove an mmbuf from an mmbuf list.

Parameters
listThe list to remove from.
mmbufThe mmbuf to remove.
Returns
true if the given mmbuf was present in list, else fase.

◆ mmbuf_make_copy_on_heap()

struct mmbuf * mmbuf_make_copy_on_heap ( struct mmbuf original)

Make a copy of the given mmbuf.

Note that regardless of the backend that allocated original, the newly allocated mmbuf will be allocated on the heap using mmbuf_alloc_on_heap().

Parameters
originalmmbuf to copy.
Returns
newly allocated mmbuf on success or NULL on failure.

◆ mmbuf_prepend()

static uint8_t * mmbuf_prepend ( struct mmbuf mmbuf,
uint32_t  len 
)
inlinestatic

Reserves space immediately before the data currently in the given mmbuf and returns a pointer to this space.

For a function that also copies data in, see mmbuf_prepend_data().

Warning
len must be less than or equal to mmbuf_available_space_at_start().
Parameters
mmbufThe mmbuf to operate on.
lenLength of data to be prepended.
Returns
a pointer to the place in the buffer where the data should be put.

Definition at line 200 of file mmbuf.h.

◆ mmbuf_prepend_data()

static void mmbuf_prepend_data ( struct mmbuf mmbuf,
const uint8_t *  data,
uint32_t  len 
)
inlinestatic

Prepends the given data to the data already in the mmbuf.

Warning
len must be less than or equal to mmbuf_available_space_at_start().
The memory area pointed to by data must not overlap with the mmbuf data.
Parameters
mmbufThe mmbuf to operate on.
dataThe data to be prepended.
lenLength of data to be prepended.

Definition at line 219 of file mmbuf.h.

◆ mmbuf_release()

void mmbuf_release ( struct mmbuf mmbuf)

Release a reference to the given mmbuf.

If this was the last reference (addition_ref_cnt was 0) then the mmbuf will be freed using the appropriate op callback.

Parameters
mmbufThe mmbuf to release reference to. May be NULL.

◆ mmbuf_remove_from_end()

static uint8_t * mmbuf_remove_from_end ( struct mmbuf mmbuf,
uint32_t  len 
)
inlinestatic

Remove data from the end of the mmbuf.

Parameters
mmbufmmbuf to operate on.
lenLength of data to remove.
Returns
a pointer to the removed data or NULL if the mmbuf data length was less than len.

Definition at line 294 of file mmbuf.h.

◆ mmbuf_remove_from_start()

static uint8_t * mmbuf_remove_from_start ( struct mmbuf mmbuf,
uint32_t  len 
)
inlinestatic

Remove data from the start of the mmbuf.

Parameters
mmbufmmbuf to operate on.
lenLength of data to remove.
Returns
a pointer to the removed data or NULL if the mmbuf data length was less than len.

Definition at line 269 of file mmbuf.h.

◆ mmbuf_truncate()

static void mmbuf_truncate ( struct mmbuf mmbuf,
uint32_t  len 
)
inlinestatic

Truncate the mmbuf data to the given length.

Parameters
mmbufmmbuf to operate on.
lenNew data length. (Must be less than or equal to the data length of the mmbuf).

Definition at line 317 of file mmbuf.h.