Morse Micro IoT SDK  2.11.2
mmpkt_list.h
1/*
2 * Copyright 2022-2024 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
14#pragma once
15
16#include <stddef.h>
17
18#include "mmpkt.h"
19
20#ifdef __cplusplus
21extern "C"
22{
23#endif
24
27{
29 struct mmpkt *volatile head;
31 struct mmpkt *volatile tail;
33 volatile uint32_t len;
34};
35
37#define MMPKT_LIST_INIT { NULL, NULL, 0 }
38
45static inline void mmpkt_list_init(struct mmpkt_list *list)
46{
47 list->head = NULL;
48 list->tail = NULL;
49 list->len = 0;
50}
51
59static inline uint32_t mmpkt_list_length(struct mmpkt_list *list)
60{
61 return list->len;
62}
63
70void mmpkt_list_prepend(struct mmpkt_list *list, struct mmpkt *mmpkt);
71
78void mmpkt_list_append(struct mmpkt_list *list, struct mmpkt *mmpkt);
79
87void mmpkt_list_insert_after(struct mmpkt_list *list, struct mmpkt *ref, struct mmpkt *mmpkt);
88
95void mmpkt_list_remove(struct mmpkt_list *list, struct mmpkt *mmpkt);
96
104struct mmpkt *mmpkt_list_dequeue(struct mmpkt_list *list);
105
119 struct mmpkt_list *dst,
120 uint32_t count);
121
130
138static inline struct mmpkt *mmpkt_list_dequeue_all(struct mmpkt_list *list)
139{
140 struct mmpkt *head = list->head;
141 list->head = NULL;
142 list->tail = NULL;
143 list->len = 0;
144 return head;
145}
146
154static inline bool mmpkt_list_is_empty(struct mmpkt_list *list)
155{
156 return (list->head == NULL);
157}
158
166static inline struct mmpkt *mmpkt_list_peek(struct mmpkt_list *list)
167{
168 return list->head;
169}
170
178static inline struct mmpkt *mmpkt_list_peek_tail(struct mmpkt_list *list)
179{
180 return list->tail;
181}
182
190uint32_t mmpkt_list_clear(struct mmpkt_list *list);
191
200void mmpkt_list_append_list(struct mmpkt_list *list, struct mmpkt_list *other);
201
210#define MMPKT_LIST_WALK(_lst, _wlk, _nxt) \
211 if ((_lst)->head != NULL) /* NOLINT(readability/braces) */ \
212 for (_wlk = (_lst)->head, _nxt = mmpkt_get_next(_wlk); _wlk != NULL; \
213 _wlk = _nxt, _nxt = _wlk ? mmpkt_get_next(_wlk) : NULL)
214
215#ifdef __cplusplus
216}
217#endif
218
struct mmpkt * mmpkt_list_dequeue_tail(struct mmpkt_list *list)
Remove the mmpkt at the tail of the list and return it.
void mmpkt_list_insert_after(struct mmpkt_list *list, struct mmpkt *ref, struct mmpkt *mmpkt)
Insert an mmpkt into an mmpkt list after a specified list entry.
static struct mmpkt * mmpkt_list_dequeue_all(struct mmpkt_list *list)
Remove all mmpkts from the list and return as a linked list.
Definition: mmpkt_list.h:138
static uint32_t mmpkt_list_length(struct mmpkt_list *list)
Gets the current length of the given mmpkt list.
Definition: mmpkt_list.h:59
uint32_t mmpkt_list_dequeue_multiple(struct mmpkt_list *src, struct mmpkt_list *dst, uint32_t count)
Remove up to count mmpkts from the head of src and append them to dst.
void mmpkt_list_prepend(struct mmpkt_list *list, struct mmpkt *mmpkt)
Add an mmpkt to the start of an mmpkt list.
void mmpkt_list_remove(struct mmpkt_list *list, struct mmpkt *mmpkt)
Remove an mmpkt from an mmpkt list.
static struct mmpkt * mmpkt_list_peek(struct mmpkt_list *list)
Returns the head of the mmpkt list.
Definition: mmpkt_list.h:166
static bool mmpkt_list_is_empty(struct mmpkt_list *list)
Checks whether the given mmpkt list is empty.
Definition: mmpkt_list.h:154
static struct mmpkt * mmpkt_list_peek_tail(struct mmpkt_list *list)
Returns the tail of the mmpkt list.
Definition: mmpkt_list.h:178
static void mmpkt_list_init(struct mmpkt_list *list)
Initialization function for mmpkt_list, for cases where MMPKT_LIST_INIT cannot be used.
Definition: mmpkt_list.h:45
struct mmpkt * mmpkt_list_dequeue(struct mmpkt_list *list)
Remove the mmpkt at the head of the list and return it.
uint32_t mmpkt_list_clear(struct mmpkt_list *list)
Free all the packets in the given list and reset the list to empty state.
void mmpkt_list_append(struct mmpkt_list *list, struct mmpkt *mmpkt)
Add an mmpkt to the end of an mmpkt list.
void mmpkt_list_append_list(struct mmpkt_list *list, struct mmpkt_list *other)
Insert mmpkts to the end of an mmpkt list, from another mmpkt list.
Structure that can be used as the head of a linked list of mmpkts that counts its length.
Definition: mmpkt_list.h:27
struct mmpkt *volatile tail
Last mmpkt in the list.
Definition: mmpkt_list.h:31
volatile uint32_t len
Length of the list.
Definition: mmpkt_list.h:33
struct mmpkt *volatile head
First mmpkt in the list.
Definition: mmpkt_list.h:29
Core mmpkt data structure.
Definition: mmpkt.h:88