本帖最后由 dukedz 于 2018-5-15 14:13 编辑
你這個鏈表不通用,最好寫成 Linux 內核那樣的結構,比較優雅,不同的業務可共用同一份代碼,運行的開銷也比較小,我有單獨提取出適合 MCU 用的 list.c 和 list.h 文件,頭文件 list.h 部分摘錄如下:
- #ifndef __LIST_H__
- #define __LIST_H__
- typedef struct list_node {
- struct list_node *next;
- } list_node_t;
- typedef struct {
- list_node_t *first;
- list_node_t *last;
- uint32_t len;
- } list_head_t;
- list_node_t *list_get(list_head_t *head);
- void list_put(list_head_t *head, list_node_t *node);
- list_node_t *list_get_last(list_head_t *head);
- void list_put_begin(list_head_t *head, list_node_t *node);
- void list_pick(list_head_t *head, list_node_t *pre, list_node_t *node);
- void list_move_begin(list_head_t *head, list_node_t *pre, list_node_t *node);
- #define list_entry(ptr, type) \
- container_of(ptr, type, node)
- #define list_entry_safe(ptr, type) ({ \
- list_node_t *__ptr = (ptr); \
- __ptr ? container_of(__ptr, type, node) : NULL; \
- })
完整文件在:https://github.com/dukelec/cdnet/tree/master/utils
|