Morse Micro IoT SDK  2.6.4-esp32
mmbuf.h
1/*
2 * Copyright 2024 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
19#pragma once
20
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdint.h>
24
25#include "mmosal.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31struct mmbuf_ops;
32
52struct mmbuf
53{
55 uint8_t *buf;
57 uint32_t buf_len;
59 uint32_t start_offset;
61 uint32_t data_len;
63 const struct mmbuf_ops *ops;
65 struct mmbuf *volatile next;
66};
67
70{
72 void (*free_mmbuf)(void *mmbuf);
73};
74
84static inline void mmbuf_init(struct mmbuf *mmbuf, uint8_t *buf, uint32_t buf_len,
85 uint32_t data_start_offset, const struct mmbuf_ops *ops)
86{
87 memset(mmbuf, 0, sizeof(*mmbuf));
88 mmbuf->buf = buf;
89 mmbuf->buf_len = buf_len;
90 mmbuf->start_offset = data_start_offset;
91 mmbuf->ops = ops;
92}
93
105struct mmbuf *mmbuf_alloc_on_heap(uint32_t space_at_start, uint32_t space_at_end);
106
116struct mmbuf *mmbuf_make_copy_on_heap(struct mmbuf *original);
117
118
126
134static inline uint8_t *mmbuf_get_data_start(struct mmbuf *mmbuf)
135{
136 return mmbuf->buf + mmbuf->start_offset;
137}
138
146static inline uint8_t *mmbuf_get_data_end(struct mmbuf *mmbuf)
147{
149}
150
159static inline uint32_t mmbuf_get_data_length(struct mmbuf *mmbuf)
160{
161 return mmbuf->data_len;
162}
163
171static inline uint32_t mmbuf_available_space_at_start(struct mmbuf *mmbuf)
172{
173 return mmbuf->start_offset;
174}
175
183static inline uint32_t mmbuf_available_space_at_end(struct mmbuf *mmbuf)
184{
186}
187
201static inline uint8_t *mmbuf_prepend(struct mmbuf *mmbuf, uint32_t len)
202{
204 mmbuf->start_offset -= len;
205 mmbuf->data_len += len;
206 return mmbuf->buf + mmbuf->start_offset;
207}
208
220static inline void mmbuf_prepend_data(struct mmbuf *mmbuf, const uint8_t * data, uint32_t len)
221{
222 uint8_t *dest = mmbuf_prepend(mmbuf, len);
223 memcpy(dest, data, len);
224}
225
239static inline uint8_t *mmbuf_append(struct mmbuf *mmbuf, uint32_t len)
240{
241 uint8_t *ret = mmbuf_get_data_end(mmbuf);
243 mmbuf->data_len += len;
244 return ret;
245}
246
256static inline void mmbuf_append_data(struct mmbuf *mmbuf, const uint8_t * data, uint32_t len)
257{
258 uint8_t *dest = mmbuf_append(mmbuf, len);
259 memcpy(dest, data, len);
260}
261
270static inline uint8_t *mmbuf_remove_from_start(struct mmbuf *mmbuf, uint32_t len)
271{
272 uint8_t *ret;
273
274 if (mmbuf_get_data_length(mmbuf) < len)
275 {
276 return NULL;
277 }
278
280
281 mmbuf->start_offset += len;
282 mmbuf->data_len -= len;
283
284 return ret;
285}
286
295static inline uint8_t *mmbuf_remove_from_end(struct mmbuf *mmbuf, uint32_t len)
296{
297 uint8_t *ret;
298
299 if (mmbuf_get_data_length(mmbuf) < len)
300 {
301 return NULL;
302 }
303
304 ret = mmbuf_get_data_end(mmbuf) - len;
305
306 mmbuf->data_len -= len;
307
308 return ret;
309}
310
318static inline void mmbuf_truncate(struct mmbuf *mmbuf, uint32_t len)
319{
320 MMOSAL_ASSERT(len <= mmbuf->data_len);
321 mmbuf->data_len = len;
322}
323
324
325/* --------------------------------------------------------------------------------------------- */
326
329{
331 struct mmbuf *volatile head;
333 struct mmbuf *volatile tail;
335 volatile uint32_t len;
336};
337
339#define MMBUF_LIST_INIT { NULL, NULL, 0 }
340
347static inline void mmbuf_list_init(struct mmbuf_list *list)
348{
349 list->head = NULL;
350 list->tail = NULL;
351 list->len = 0;
352}
353
360void mmbuf_list_prepend(struct mmbuf_list *list, struct mmbuf *mmbuf);
361
368void mmbuf_list_append(struct mmbuf_list *list, struct mmbuf *mmbuf);
369
378bool mmbuf_list_remove(struct mmbuf_list *list, struct mmbuf *mmbuf);
379
387struct mmbuf *mmbuf_list_dequeue(struct mmbuf_list *list);
388
397
405static inline struct mmbuf *mmbuf_list_dequeue_all(struct mmbuf_list *list)
406{
407 struct mmbuf *head = list->head;
408 list->head = NULL;
409 list->tail = NULL;
410 list->len = 0;
411 return head;
412}
413
421static inline bool mmbuf_list_is_empty(struct mmbuf_list *list)
422{
423 return (list->head == NULL);
424}
425
433static inline struct mmbuf *mmbuf_list_peek(struct mmbuf_list *list)
434{
435 return list->head;
436}
437
445static inline struct mmbuf *mmbuf_list_peek_tail(struct mmbuf_list *list)
446{
447 return list->tail;
448}
449
455void mmbuf_list_clear(struct mmbuf_list *list);
456
457#ifdef __cplusplus
458}
459#endif
460
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 ...
Definition: mmbuf.h:239
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.
Definition: mmbuf.h:183
static uint32_t mmbuf_get_data_length(struct mmbuf *mmbuf)
Gets the length of the data currently in the mmbuf.
Definition: mmbuf.h:159
static struct mmbuf * mmbuf_list_dequeue_all(struct mmbuf_list *list)
Remove all mmbufs from the list and return as a linked list.
Definition: mmbuf.h:405
static uint8_t * mmbuf_get_data_end(struct mmbuf *mmbuf)
Gets a pointer to the end of the data in the mmbuf.
Definition: mmbuf.h:146
void mmbuf_list_prepend(struct mmbuf_list *list, struct mmbuf *mmbuf)
Add an mmbuf to the start of an mmbuf list.
struct mmbuf * mmbuf_list_dequeue_tail(struct mmbuf_list *list)
Remove the mmbuf at the tail of the list and return it.
static uint8_t * mmbuf_remove_from_end(struct mmbuf *mmbuf, uint32_t len)
Remove data from the end of the mmbuf.
Definition: mmbuf.h:295
void mmbuf_list_clear(struct mmbuf_list *list)
Free all the packets in the given list and reset the list to empty state.
static bool mmbuf_list_is_empty(struct mmbuf_list *list)
Checks whether the given mmbuf list is empty.
Definition: mmbuf.h:421
static uint8_t * mmbuf_remove_from_start(struct mmbuf *mmbuf, uint32_t len)
Remove data from the start of the mmbuf.
Definition: mmbuf.h:270
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...
Definition: mmbuf.h:201
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()).
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.
Definition: mmbuf.h:171
bool mmbuf_list_remove(struct mmbuf_list *list, struct mmbuf *mmbuf)
Remove an mmbuf from an mmbuf list.
void mmbuf_release(struct mmbuf *mmbuf)
Release a reference to the given mmbuf.
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.
Definition: mmbuf.h:256
static struct mmbuf * mmbuf_list_peek_tail(struct mmbuf_list *list)
Returns the tail of the mmbuf list.
Definition: mmbuf.h:445
static void mmbuf_list_init(struct mmbuf_list *list)
Initialization function for mmbuf_list, for cases where MMBUF_LIST_INIT cannot be used.
Definition: mmbuf.h:347
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.
Definition: mmbuf.h:220
static void mmbuf_truncate(struct mmbuf *mmbuf, uint32_t len)
Truncate the mmbuf data to the given length.
Definition: mmbuf.h:318
struct mmbuf * mmbuf_list_dequeue(struct mmbuf_list *list)
Remove the mmbuf at the head of the list and return it.
static uint8_t * mmbuf_get_data_start(struct mmbuf *mmbuf)
Gets a pointer to the start of the data in the mmbuf.
Definition: mmbuf.h:134
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.
Definition: mmbuf.h:84
void mmbuf_list_append(struct mmbuf_list *list, struct mmbuf *mmbuf)
Add an mmbuf to the end of an mmbuf list.
static struct mmbuf * mmbuf_list_peek(struct mmbuf_list *list)
Returns the head of the mmbuf list.
Definition: mmbuf.h:433
struct mmbuf * mmbuf_make_copy_on_heap(struct mmbuf *original)
Make a copy of the given mmbuf.
#define MMOSAL_ASSERT(expr)
Assert that the given expression evaluates to true and abort execution if not.
Definition: mmosal.h:967
Structure that can be used as the head of a linked list of mmbufs that counts its length.
Definition: mmbuf.h:329
volatile uint32_t len
Length of the list.
Definition: mmbuf.h:335
struct mmbuf *volatile head
First mmbuf in the list.
Definition: mmbuf.h:331
struct mmbuf *volatile tail
Last mmbuf in the list.
Definition: mmbuf.h:333
Operations data structure for mmbuf.
Definition: mmbuf.h:70
void(* free_mmbuf)(void *mmbuf)
Free the given mmbuf.
Definition: mmbuf.h:72
Core mmbuf data structure.
Definition: mmbuf.h:53
struct mmbuf *volatile next
Pointer that can be used to construct linked lists.
Definition: mmbuf.h:65
uint32_t start_offset
Offset where actual data starts in the buffer.
Definition: mmbuf.h:59
const struct mmbuf_ops * ops
Reference to operations data structure for this mmbuf.
Definition: mmbuf.h:63
uint32_t buf_len
Length of the buffer.
Definition: mmbuf.h:57
uint8_t * buf
The buffer where data is stored.
Definition: mmbuf.h:55
uint32_t data_len
Length of actual data in the buffer.
Definition: mmbuf.h:61