/** ****************************************************************************** * @file * @author 2022-11-17 created by Radkil * @brief ****************************************************************************** * @attention ****************************************************************************** */ #ifndef SLIST_H #define SLIST_H #include #include "common.h"// Assume rd_inline is defined as: #define rd_inline static inline //单向链表 struct rd_slist_node { struct rd_slist_node *next;//指向链表的下一个节点 }; typedef struct rd_slist_node rd_slist_t; #define RD_SLIST_OBJECT_INIT(object) { NULL } /** * @brief initialize a single list * * @param l the single list to be initialized */ rd_inline void rd_slist_init(rd_slist_t *l) { l->next = NULL; } rd_inline void rd_slist_append(rd_slist_t *l, rd_slist_t *n) { struct rd_slist_node *node; node = l; while (node->next) node = node->next; /* append the node to the tail */ node->next = n; n->next = NULL; } rd_inline void rd_slist_insert(rd_slist_t *l, rd_slist_t *n) { n->next = l->next; l->next = n; } rd_inline unsigned int rd_slist_len(const rd_slist_t *l) { unsigned int len = 0; const rd_slist_t *list = l->next; while (list != NULL) { list = list->next; len ++; } return len; } rd_inline rd_slist_t *rd_slist_remove(rd_slist_t *l, rd_slist_t *n) { /* remove slist head */ struct rd_slist_node *node = l; while (node->next && node->next != n) node = node->next; /* remove node */ if (node->next != (rd_slist_t *)0) node->next = node->next->next; return n; } rd_inline rd_slist_t *rd_slist_pop(rd_slist_t *l) { /* pop slist first node */ struct rd_slist_node *node = l->next; l->next = node->next; node->next = NULL; return node; } rd_inline rd_slist_t *rd_slist_first(rd_slist_t *l) { return l->next; } rd_inline rd_slist_t *rd_slist_tail(rd_slist_t *l) { while (l->next) l = l->next; return l; } rd_inline rd_slist_t *rd_slist_next(rd_slist_t *n) { return n->next; } rd_inline int rd_slist_isempty(rd_slist_t *l) { return l->next == NULL; } /** * rd_container_of - return the member address of ptr, if the type of ptr is the * struct type. */ #define rd_container_of(ptr, type, member) \ ((type *)((char *)(ptr) - offsetof(type, member))) /** * @brief get the struct for this single list node * @param node the entry point * @param type the type of structure * @param member the name of list in structure */ #define rd_slist_entry(node, type, member) \ rd_container_of(node, type, member) /** * rd_slist_for_each - iterate over a single list * @pos: the rd_slist_t * to use as a loop cursor. * @head: the head for your single list. */ #define rd_slist_for_each(pos, n, head) \ for (pos = (head)->next, n = (pos ? pos->next : NULL); \ pos; \ pos = n, n = (pos ? pos->next : NULL)) /** * rd_slist_first_entry - get the first element from a slist * @ptr: the slist head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the slist_struct within the struct. * * Note, that slist is expected to be not empty. */ #define rd_slist_first_entry(ptr, type, member) \ rd_slist_entry((ptr)->next, type, member) /** * rd_slist_tail_entry - get the tail element from a slist * @ptr: the slist head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the slist_struct within the struct. * * Note, that slist is expected to be not empty. */ #define rd_slist_tail_entry(ptr, type, member) \ rd_slist_entry(rd_slist_tail(ptr), type, member) #endif /* end of include guard: SLIST_H */