Morse Micro IoT SDK  2.9.7
mmpkt_list.h
1/*
2 * Copyright 2022-2024 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
13#pragma once
14
15#include <stddef.h>
16
17#include "mmpkt.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
25{
27 struct mmpkt *volatile head;
29 struct mmpkt *volatile tail;
31 volatile uint32_t len;
32};
33
35#define MMPKT_LIST_INIT { NULL, NULL, 0 }
36
43static inline void mmpkt_list_init(struct mmpkt_list *list)
44{
45 list->head = NULL;
46 list->tail = NULL;
47 list->len = 0;
48}
49
57static inline uint32_t mmpkt_list_length(struct mmpkt_list *list)
58{
59 return list->len;
60}
61
68void mmpkt_list_prepend(struct mmpkt_list *list, struct mmpkt *mmpkt);
69
76void mmpkt_list_append(struct mmpkt_list *list, struct mmpkt *mmpkt);
77
85void mmpkt_list_insert_after(struct mmpkt_list *list, struct mmpkt *ref, struct mmpkt *mmpkt);
86
93void mmpkt_list_remove(struct mmpkt_list *list, struct mmpkt *mmpkt);
94
102struct mmpkt *mmpkt_list_dequeue(struct mmpkt_list *list);
103
111struct mmpkt *mmpkt_list_dequeue_tail(struct mmpkt_list *list);
112
120static inline struct mmpkt *mmpkt_list_dequeue_all(struct mmpkt_list *list)
121{
122 struct mmpkt *head = list->head;
123 list->head = NULL;
124 list->tail = NULL;
125 list->len = 0;
126 return head;
127}
128
136static inline bool mmpkt_list_is_empty(struct mmpkt_list *list)
137{
138 return (list->head == NULL);
139}
140
148static inline struct mmpkt *mmpkt_list_peek(struct mmpkt_list *list)
149{
150 return list->head;
151}
152
160static inline struct mmpkt *mmpkt_list_peek_tail(struct mmpkt_list *list)
161{
162 return list->tail;
163}
164
170void mmpkt_list_clear(struct mmpkt_list *list);
171
180#define MMPKT_LIST_WALK(_lst, _wlk, _nxt) \
181 if ((_lst)->head != NULL) /* NOLINT(readability/braces) */ \
182 for (_wlk = (_lst)->head, _nxt = mmpkt_get_next(_wlk); \
183 _wlk != NULL; \
184 _wlk = _nxt, _nxt = _wlk ? mmpkt_get_next(_wlk) : NULL)
185
186#ifdef __cplusplus
187}
188#endif
189
Structure that can be used as the head of a linked list of mmpkts that counts its length.
Definition: mmpkt_list.h:25
struct mmpkt *volatile tail
Last mmpkt in the list.
Definition: mmpkt_list.h:29
volatile uint32_t len
Length of the list.
Definition: mmpkt_list.h:31
struct mmpkt *volatile head
First mmpkt in the list.
Definition: mmpkt_list.h:27
Core mmpkt data structure.
Definition: mmpkt.h:79