参考代码
This commit is contained in:
285
components/system/include/list/dlist.h
Executable file
285
components/system/include/list/dlist.h
Executable file
@@ -0,0 +1,285 @@
|
||||
/**
|
||||
* @file dlist.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __DUPLEXLIST_H__
|
||||
#define __DUPLEXLIST_H__
|
||||
|
||||
#include "sys_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef LIST_NODE_TYPE
|
||||
#define LIST_NODE_TYPE 0 /* 0 -- 使用指针连结(推荐);1 -- 使用偏移量连结,内存的地址允许被迁移并继续管理(推荐) */
|
||||
#endif
|
||||
|
||||
#if LIST_NODE_TYPE == 1
|
||||
typedef int list_node_t;
|
||||
#else
|
||||
typedef void * list_node_t;
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
list_node_t next;
|
||||
list_node_t prev;
|
||||
} dlist_node_t;
|
||||
typedef struct
|
||||
{
|
||||
list_node_t head;
|
||||
} dlist_t;
|
||||
|
||||
__static_inline void dlist_init_list (dlist_t *list);
|
||||
__static_inline void dlist_init_node (dlist_node_t *node);
|
||||
|
||||
__static_inline int dlist_insert_font (dlist_t *list, dlist_node_t *node);
|
||||
__static_inline int dlist_insert_tail (dlist_t *list, dlist_node_t *node);
|
||||
__static_inline int dlist_remove (dlist_t *list, dlist_node_t *node);
|
||||
|
||||
__static_inline int dlist_insert_next (dlist_t *list, dlist_node_t *tar_node, dlist_node_t *new_node);
|
||||
__static_inline int dlist_insert_prev (dlist_t *list, dlist_node_t *tar_node, dlist_node_t *new_node);
|
||||
|
||||
__static_inline void dlist_set_next (dlist_t *list);
|
||||
|
||||
__static_inline dlist_node_t *dlist_take_head (dlist_t *list);
|
||||
__static_inline dlist_node_t *dlist_take_tail (dlist_t *list);
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_head (dlist_t *list);
|
||||
__static_inline dlist_node_t *dlist_peek_tail (dlist_t *list);
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_next (dlist_t *list, dlist_node_t *node);
|
||||
__static_inline dlist_node_t *dlist_peek_prev (dlist_t *list, dlist_node_t *node);
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_next_loop (dlist_t *list, dlist_node_t *node);
|
||||
__static_inline dlist_node_t *dlist_peek_prev_loop (dlist_t *list, dlist_node_t *node);
|
||||
|
||||
__static_inline int dlist_is_pending (dlist_node_t *node);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if LIST_NODE_TYPE == 1
|
||||
#define DLIST_SET(Value1, Value2) (Value1 = Value2 == 0 ? 0 : ((int)Value2 - (int)&Value1) | 1)
|
||||
#define DLIST_GET(Value) ((dlist_node_t*)(Value == 0 ? 0 : ((int)&Value + Value) & ~1))
|
||||
#else
|
||||
#define DLIST_SET(Value1, Value2) (Value1 = (list_node_t)Value2)
|
||||
#define DLIST_GET(Value) ((dlist_node_t*)(Value))
|
||||
#endif
|
||||
|
||||
__static_inline void dlist_init_list(dlist_t *list)
|
||||
{
|
||||
DLIST_SET(list->head, 0);
|
||||
}
|
||||
|
||||
__static_inline void dlist_init_node(dlist_node_t *node)
|
||||
{
|
||||
DLIST_SET(node->next, 0);
|
||||
DLIST_SET(node->prev, 0);
|
||||
}
|
||||
|
||||
__static_inline int dlist_insert_font(dlist_t *list, dlist_node_t *node)
|
||||
{
|
||||
if (dlist_insert_tail(list, node) == 0)
|
||||
{
|
||||
DLIST_SET(list->head, node);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline int dlist_insert_tail(dlist_t *list, dlist_node_t *node)
|
||||
{
|
||||
if (DLIST_GET(node->next) != NULL || DLIST_GET(node->prev) != NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
dlist_node_t *first_node = DLIST_GET(list->head);
|
||||
if (first_node == NULL)
|
||||
{
|
||||
/* 直接设置链头 */
|
||||
DLIST_SET(node->next, node);
|
||||
DLIST_SET(node->prev, node);
|
||||
DLIST_SET(list->head, node);
|
||||
}
|
||||
else
|
||||
{
|
||||
dlist_node_t *first_node_prev = DLIST_GET(first_node->prev);
|
||||
|
||||
DLIST_SET(node->next, first_node);
|
||||
DLIST_SET(node->prev, first_node_prev);
|
||||
DLIST_SET(first_node_prev->next, node);
|
||||
DLIST_SET(first_node->prev, node);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline int dlist_remove(dlist_t *list, dlist_node_t *node)
|
||||
{
|
||||
if (node == NULL || list == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
dlist_node_t *first_node = DLIST_GET(list->head);
|
||||
dlist_node_t *node_next = DLIST_GET(node->next);
|
||||
if (node_next == NULL || first_node == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (node_next == node) // 是最后一节点
|
||||
{
|
||||
DLIST_SET(list->head, 0);
|
||||
}
|
||||
else // 不是最后一节点
|
||||
{
|
||||
dlist_node_t *node_prev = DLIST_GET(node->prev);
|
||||
if (first_node == node)
|
||||
{
|
||||
DLIST_SET(list->head, node_next); // 链头指向下一节点
|
||||
}
|
||||
DLIST_SET(node_prev->next, node_next);
|
||||
DLIST_SET(node_next->prev, node_prev);
|
||||
}
|
||||
|
||||
DLIST_SET(node->next, 0);
|
||||
DLIST_SET(node->prev, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline int dlist_insert_next(dlist_t *list, dlist_node_t *tar_node, dlist_node_t *new_node)
|
||||
{
|
||||
dlist_node_t *tar_node_next = DLIST_GET(tar_node->next);
|
||||
if (DLIST_GET(list->head) == NULL || tar_node_next == NULL || DLIST_GET(new_node->next) != NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
DLIST_SET(new_node->next, tar_node_next);
|
||||
DLIST_SET(new_node->prev, tar_node);
|
||||
DLIST_SET(tar_node_next->prev, new_node);
|
||||
DLIST_SET(tar_node->next, new_node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline int dlist_insert_prev(dlist_t *list, dlist_node_t *tar_node, dlist_node_t *new_node)
|
||||
{
|
||||
dlist_node_t *list_head = DLIST_GET(list->head);
|
||||
if (list_head == NULL || DLIST_GET(tar_node->next) == NULL || DLIST_GET(new_node->next) != NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
DLIST_SET(new_node->next, tar_node);
|
||||
DLIST_SET(new_node->prev, DLIST_GET(tar_node->prev));
|
||||
DLIST_SET(DLIST_GET(tar_node->prev)->next, new_node);
|
||||
DLIST_SET(tar_node->prev, new_node);
|
||||
if (tar_node == list_head) // tar_node 是首个节点
|
||||
{
|
||||
DLIST_SET(list->head, new_node);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline void dlist_set_next(dlist_t *list)
|
||||
{
|
||||
if (list != NULL)
|
||||
{
|
||||
dlist_node_t *node = DLIST_GET(list->head);
|
||||
if (node != NULL)
|
||||
{
|
||||
DLIST_SET(list->head, DLIST_GET(node->next)); // 链头指向下一节点
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline dlist_node_t *dlist_take_head(dlist_t *list)
|
||||
{
|
||||
dlist_node_t *ret = DLIST_GET(list->head);
|
||||
dlist_remove(list, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
__static_inline dlist_node_t *dlist_take_tail(dlist_t *list)
|
||||
{
|
||||
dlist_node_t *ret = dlist_peek_tail(list);
|
||||
dlist_remove(list, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_head(dlist_t *list)
|
||||
{
|
||||
return DLIST_GET(list->head);
|
||||
}
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_tail(dlist_t *list)
|
||||
{
|
||||
dlist_node_t *list_head = DLIST_GET(list->head);
|
||||
if (list_head == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return DLIST_GET(list_head->prev);
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_next(dlist_t *list, dlist_node_t *node)
|
||||
{
|
||||
dlist_node_t *ret = DLIST_GET(node->next);
|
||||
if (dlist_peek_head(list) == ret)
|
||||
{
|
||||
ret = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_prev(dlist_t *list, dlist_node_t *node)
|
||||
{
|
||||
dlist_node_t *ret = DLIST_GET(node->prev);
|
||||
if (dlist_peek_tail(list) == ret)
|
||||
{
|
||||
ret = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_next_loop(dlist_t *list, dlist_node_t *node)
|
||||
{
|
||||
(void)list;
|
||||
return DLIST_GET(node->next);
|
||||
}
|
||||
|
||||
__static_inline dlist_node_t *dlist_peek_prev_loop(dlist_t *list, dlist_node_t *node)
|
||||
{
|
||||
(void)list;
|
||||
return DLIST_GET(node->prev);
|
||||
}
|
||||
|
||||
__static_inline int dlist_is_pending(dlist_node_t *node)
|
||||
{
|
||||
return (DLIST_GET(node->next) != NULL);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
373
components/system/include/list/flist.h
Executable file
373
components/system/include/list/flist.h
Executable file
@@ -0,0 +1,373 @@
|
||||
/**
|
||||
* @file flist.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __FULLLIST_H__
|
||||
#define __FULLLIST_H__
|
||||
|
||||
#include "sys_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef LIST_NODE_TYPE
|
||||
#define LIST_NODE_TYPE 0 /* 0 -- 使用指针连结(推荐);1 -- 使用偏移量连结,内存的地址允许被迁移并继续管理(推荐) */
|
||||
#endif
|
||||
|
||||
#if LIST_NODE_TYPE == 1
|
||||
typedef int list_node_t;
|
||||
#else
|
||||
typedef void * list_node_t;
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
list_node_t next;
|
||||
list_node_t prev;
|
||||
list_node_t list;
|
||||
} flist_node_t;
|
||||
typedef struct
|
||||
{
|
||||
list_node_t head;
|
||||
} flist_t;
|
||||
|
||||
__static_inline void flist_init_list (flist_t *list);
|
||||
__static_inline void flist_init_node (flist_node_t *node);
|
||||
|
||||
__static_inline int flist_insert_font (flist_t *list, flist_node_t *node);
|
||||
__static_inline int flist_insert_tail (flist_t *list, flist_node_t *node);
|
||||
__static_inline int flist_remove (flist_t *list, flist_node_t *node);
|
||||
|
||||
__static_inline int flist_insert_next (flist_node_t *tar_node, flist_node_t *new_node);
|
||||
__static_inline int flist_insert_prev (flist_node_t *tar_node, flist_node_t *new_node);
|
||||
__static_inline int flist_node_free (flist_node_t *node);
|
||||
|
||||
__static_inline void flist_set_next (flist_t *list);
|
||||
|
||||
__static_inline flist_node_t *flist_take_head (flist_t *list);
|
||||
__static_inline flist_node_t *flist_take_tail (flist_t *list);
|
||||
|
||||
__static_inline flist_node_t *flist_peek_head (flist_t *list);
|
||||
__static_inline flist_node_t *flist_peek_tail (flist_t *list);
|
||||
|
||||
__static_inline flist_t *flist_peek_list (flist_node_t *node);
|
||||
__static_inline flist_node_t *flist_peek_first (flist_node_t *node);
|
||||
__static_inline flist_node_t *flist_peek_next (flist_node_t *node);
|
||||
__static_inline flist_node_t *flist_peek_prev (flist_node_t *node);
|
||||
|
||||
__static_inline flist_node_t *flist_peek_next_loop (flist_node_t *node);
|
||||
__static_inline flist_node_t *flist_peek_prev_loop (flist_node_t *node);
|
||||
|
||||
__static_inline int flist_is_pending (flist_node_t *node);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if LIST_NODE_TYPE == 1
|
||||
#define FLIST_SET(Value1, Value2) (Value1 = Value2 == 0 ? 0 : ((int)Value2 - (int)&Value1) | 1)
|
||||
#define FLIST_GET(Value) ((flist_node_t*)(Value == 0 ? 0 : ((int)&Value + Value) & ~1))
|
||||
#else
|
||||
#define FLIST_SET(Value1, Value2) (Value1 = (list_node_t)Value2)
|
||||
#define FLIST_GET(Value) ((flist_node_t*)(Value))
|
||||
#endif
|
||||
|
||||
__static_inline void flist_init_list(flist_t *list)
|
||||
{
|
||||
FLIST_SET(list->head, 0);
|
||||
}
|
||||
|
||||
__static_inline void flist_init_node(flist_node_t *node)
|
||||
{
|
||||
FLIST_SET(node->list, 0);
|
||||
FLIST_SET(node->next, 0);
|
||||
FLIST_SET(node->prev, 0);
|
||||
}
|
||||
|
||||
__static_inline int flist_insert_font(flist_t *list, flist_node_t *node)
|
||||
{
|
||||
flist_insert_tail(list, node);
|
||||
FLIST_SET(list->head, node);
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline int flist_insert_tail(flist_t *list, flist_node_t *node)
|
||||
{
|
||||
if (FLIST_GET(node->list) != NULL)
|
||||
{
|
||||
flist_node_free(node);
|
||||
}
|
||||
|
||||
FLIST_SET(node->list, list);
|
||||
|
||||
flist_node_t *first_node = FLIST_GET(list->head);
|
||||
if (first_node == NULL)
|
||||
{
|
||||
/* 直接设置链头 */
|
||||
FLIST_SET(node->next, node);
|
||||
FLIST_SET(node->prev, node);
|
||||
FLIST_SET(list->head, node);
|
||||
}
|
||||
else
|
||||
{
|
||||
flist_node_t *first_node_prev = FLIST_GET(first_node->prev);
|
||||
FLIST_SET(node->next, first_node);
|
||||
FLIST_SET(node->prev, first_node_prev);
|
||||
FLIST_SET(first_node_prev->next, node);
|
||||
FLIST_SET(first_node->prev, node);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline int flist_remove(flist_t *list, flist_node_t *node)
|
||||
{
|
||||
if (node == NULL || list == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
flist_node_t *first_node = FLIST_GET(list->head);
|
||||
flist_node_t *node_next = FLIST_GET(node->next);
|
||||
if (node_next == NULL || first_node == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (node_next == node) // 是最后一节点
|
||||
{
|
||||
FLIST_SET(list->head, 0);
|
||||
}
|
||||
else // 不是最后一节点
|
||||
{
|
||||
if (first_node == node)
|
||||
{
|
||||
FLIST_SET(list->head, node_next); // 链头指向下一节点
|
||||
}
|
||||
flist_node_t *node_prev = FLIST_GET(node->prev);
|
||||
FLIST_SET(node_prev->next, node_next);
|
||||
FLIST_SET(node_next->prev, node_prev);
|
||||
}
|
||||
|
||||
FLIST_SET(node->list, 0);
|
||||
FLIST_SET(node->next, 0);
|
||||
FLIST_SET(node->prev, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline int flist_insert_next(flist_node_t *tar_node, flist_node_t *new_node)
|
||||
{
|
||||
flist_node_t *tar_node_list = FLIST_GET(tar_node->list);
|
||||
if (tar_node_list == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tar_node == new_node)
|
||||
{
|
||||
flist_node_t *src_node_next = FLIST_GET(new_node->next);
|
||||
if (src_node_next != new_node) // 不是最后一节点
|
||||
{
|
||||
flist_t *list = (flist_t *)FLIST_GET(new_node->list);
|
||||
flist_node_t *first_node = FLIST_GET(list->head);
|
||||
if (first_node == new_node)
|
||||
{
|
||||
FLIST_SET(list->head, src_node_next); // 链头指向下一节点
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (FLIST_GET(new_node->list) != NULL)
|
||||
{
|
||||
flist_node_free(new_node);
|
||||
}
|
||||
flist_node_t *tar_node_next = FLIST_GET(tar_node->next);
|
||||
FLIST_SET(new_node->list, tar_node_list);
|
||||
FLIST_SET(new_node->next, tar_node_next);
|
||||
FLIST_SET(new_node->prev, tar_node);
|
||||
FLIST_SET(tar_node_next->prev, new_node);
|
||||
FLIST_SET(tar_node->next, new_node);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline int flist_insert_prev(flist_node_t *tar_node, flist_node_t *new_node)
|
||||
{
|
||||
flist_node_t *tar_node_list = FLIST_GET(tar_node->list);
|
||||
if (tar_node_list == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tar_node == new_node)
|
||||
{
|
||||
flist_node_t *src_node_next = FLIST_GET(new_node->next);
|
||||
if (src_node_next != new_node) // 不是最后一节点
|
||||
{
|
||||
flist_t *list = (flist_t *)FLIST_GET(new_node->list);
|
||||
flist_node_t *first_node = (flist_node_t *)FLIST_GET(list->head);
|
||||
if (first_node == new_node)
|
||||
{
|
||||
FLIST_SET(list->head, src_node_next); // 链头指向下一节点
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (FLIST_GET(new_node->list) != NULL)
|
||||
{
|
||||
flist_node_free(new_node);
|
||||
}
|
||||
flist_node_t *tar_node_prev = FLIST_GET(tar_node->prev);
|
||||
FLIST_SET(new_node->list, tar_node_list);
|
||||
FLIST_SET(new_node->next, tar_node);
|
||||
FLIST_SET(new_node->prev, tar_node_prev);
|
||||
FLIST_SET(tar_node_prev->next, new_node);
|
||||
FLIST_SET(tar_node->prev, new_node);
|
||||
flist_t *list = (flist_t *)tar_node_list;
|
||||
if (tar_node == FLIST_GET(list->head)) // tar_node 是首个节点
|
||||
{
|
||||
FLIST_SET(list->head, new_node);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline int flist_node_free(flist_node_t *node)
|
||||
{
|
||||
if (node == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return flist_remove((flist_t *)FLIST_GET(node->list), node);
|
||||
}
|
||||
|
||||
__static_inline void flist_set_next(flist_t *list)
|
||||
{
|
||||
if (list != NULL)
|
||||
{
|
||||
flist_node_t *node = FLIST_GET(list->head);
|
||||
if (node != NULL)
|
||||
{
|
||||
FLIST_SET(list->head, FLIST_GET(node->next)); // 链头指向下一节点
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_take_head(flist_t *list)
|
||||
{
|
||||
flist_node_t *ret = FLIST_GET(list->head);
|
||||
flist_remove(list, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_take_tail(flist_t *list)
|
||||
{
|
||||
flist_node_t *ret = flist_peek_tail(list);
|
||||
flist_remove(list, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_peek_head(flist_t *list)
|
||||
{
|
||||
return FLIST_GET(list->head);
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_peek_tail(flist_t *list)
|
||||
{
|
||||
flist_node_t *list_head = FLIST_GET(list->head);
|
||||
if (list_head == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FLIST_GET(list_head->prev);
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline flist_t *flist_peek_list(flist_node_t *node)
|
||||
{
|
||||
return (flist_t *)FLIST_GET(node->list);
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_peek_first(flist_node_t *node)
|
||||
{
|
||||
flist_node_t *node_list = FLIST_GET(node->list);
|
||||
if (node_list == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FLIST_GET(((flist_t *)node_list)->head);
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_peek_next(flist_node_t *node)
|
||||
{
|
||||
flist_t *pFlist = (flist_t *)FLIST_GET(node->list);
|
||||
if (pFlist == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
flist_node_t *ret = FLIST_GET(node->next);
|
||||
if (flist_peek_head(pFlist) == ret)
|
||||
{
|
||||
ret = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_peek_prev(flist_node_t *node)
|
||||
{
|
||||
flist_t *pFlist = (flist_t *)FLIST_GET(node->list);
|
||||
if (pFlist == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
flist_node_t *ret = FLIST_GET(node->prev);
|
||||
if (flist_peek_tail(pFlist) == ret)
|
||||
{
|
||||
ret = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_peek_next_loop(flist_node_t *node)
|
||||
{
|
||||
return FLIST_GET(node->next);
|
||||
}
|
||||
|
||||
__static_inline flist_node_t *flist_peek_prev_loop(flist_node_t *node)
|
||||
{
|
||||
return FLIST_GET(node->prev);
|
||||
}
|
||||
|
||||
__static_inline int flist_is_pending(flist_node_t *node)
|
||||
{
|
||||
return (FLIST_GET(node->next) != NULL);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
248
components/system/include/list/pslist.h
Executable file
248
components/system/include/list/pslist.h
Executable file
@@ -0,0 +1,248 @@
|
||||
/**
|
||||
* @file pslist.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __PSLIST_H__
|
||||
#define __PSLIST_H__
|
||||
|
||||
#include "sys_types.h"
|
||||
|
||||
typedef struct _psnode sys_psnode_t;
|
||||
|
||||
typedef struct _pslist sys_pslist_t;
|
||||
|
||||
struct _psnode
|
||||
{
|
||||
sys_psnode_t *const *next;
|
||||
};
|
||||
|
||||
struct _pslist
|
||||
{
|
||||
sys_psnode_t *const *head;
|
||||
sys_psnode_t *const *tail;
|
||||
};
|
||||
|
||||
#define _CONTAINER_OF(PTR, TYPE, MEMBER) ((TYPE *)&((uint8_t *)PTR)[-(int)&((TYPE *)0)->MEMBER])
|
||||
|
||||
#define SYS_PSLIST_FOR_EACH_NODE(__sl, __sn) \
|
||||
for (__sn = sys_pslist_peek_head(__sl); __sn; \
|
||||
__sn = sys_pslist_peek_next(__sn))
|
||||
|
||||
#define SYS_PSLIST_ITERATE_FROM_NODE(__sl, __sn) \
|
||||
for (__sn = __sn ? sys_pslist_peek_next_no_check(__sn) \
|
||||
: sys_pslist_peek_head(__sl); \
|
||||
__sn; \
|
||||
__sn = sys_pslist_peek_next(__sn))
|
||||
|
||||
#define SYS_PSLIST_FOR_EACH_NODE_SAFE(__sl, __sn, __sns) \
|
||||
for (__sn = sys_pslist_peek_head(__sl), \
|
||||
__sns = sys_pslist_peek_next(__sn); \
|
||||
__sn; \
|
||||
__sn = __sns, __sns = sys_pslist_peek_next(__sn))
|
||||
|
||||
#define SYS_PSLIST_CONTAINER(__ln, __cn, __n) \
|
||||
((__ln) ? _CONTAINER_OF((__ln), __typeof__(*(__cn)), __n) : NULL)
|
||||
|
||||
#define SYS_PSLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n) \
|
||||
SYS_PSLIST_CONTAINER(sys_pslist_peek_head(__sl), __cn, __n)
|
||||
|
||||
#define SYS_PSLIST_PEEK_TAIL_CONTAINER(__sl, __cn, __n) \
|
||||
SYS_PSLIST_CONTAINER(sys_pslist_peek_tail(__sl), __cn, __n)
|
||||
|
||||
#define SYS_PSLIST_PEEK_NEXT_CONTAINER(__cn, __n) \
|
||||
((__cn) ? SYS_PSLIST_CONTAINER(sys_pslist_peek_next(&((__cn)->__n)), \
|
||||
__cn, __n) \
|
||||
: NULL)
|
||||
|
||||
#define SYS_PSLIST_FOR_EACH_CONTAINER(__sl, __cn, __n) \
|
||||
for (__cn = SYS_PSLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n); \
|
||||
__cn; \
|
||||
__cn = SYS_PSLIST_PEEK_NEXT_CONTAINER(__cn, __n))
|
||||
|
||||
#define SYS_PSLIST_FOR_EACH_CONTAINER_SAFE(__sl, __cn, __cns, __n) \
|
||||
for (__cn = SYS_PSLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n), \
|
||||
__cns = SYS_PSLIST_PEEK_NEXT_CONTAINER(__cn, __n); \
|
||||
__cn; \
|
||||
__cn = __cns, __cns = SYS_PSLIST_PEEK_NEXT_CONTAINER(__cn, __n))
|
||||
|
||||
__static_inline void sys_pslist_init(sys_pslist_t *list)
|
||||
{
|
||||
list->head = NULL;
|
||||
list->tail = NULL;
|
||||
}
|
||||
|
||||
#define SYS_PSLIST_STATIC_INIT(ptr_to_list) \
|
||||
{ \
|
||||
NULL, NULL \
|
||||
}
|
||||
|
||||
__static_inline bool sys_pslist_is_empty(sys_pslist_t *list)
|
||||
{
|
||||
return (!list->head);
|
||||
}
|
||||
|
||||
__static_inline sys_psnode_t *const *sys_pslist_peek_head(sys_pslist_t *list)
|
||||
{
|
||||
return list->head;
|
||||
}
|
||||
|
||||
__static_inline sys_psnode_t *const *sys_pslist_peek_tail(sys_pslist_t *list)
|
||||
{
|
||||
return list->tail;
|
||||
}
|
||||
|
||||
__static_inline sys_psnode_t *const *sys_pslist_peek_next_no_check(sys_psnode_t *const *node)
|
||||
{
|
||||
return (*node)->next;
|
||||
}
|
||||
|
||||
__static_inline sys_psnode_t *const *sys_pslist_peek_next(sys_psnode_t *const *node)
|
||||
{
|
||||
return node ? sys_pslist_peek_next_no_check(node) : NULL;
|
||||
}
|
||||
|
||||
__static_inline void sys_pslist_prepend(sys_pslist_t *list,
|
||||
sys_psnode_t *const *node)
|
||||
{
|
||||
(*node)->next = list->head;
|
||||
list->head = node;
|
||||
|
||||
if (!list->tail)
|
||||
{
|
||||
list->tail = list->head;
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline void sys_pslist_append(sys_pslist_t *list,
|
||||
sys_psnode_t *const *node)
|
||||
{
|
||||
(*node)->next = NULL;
|
||||
|
||||
if (!list->tail)
|
||||
{
|
||||
list->tail = node;
|
||||
list->head = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*(list->tail))->next = node;
|
||||
list->tail = node;
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline void sys_pslist_append_list(sys_pslist_t *list,
|
||||
sys_psnode_t *const *head, sys_psnode_t *const *tail)
|
||||
{
|
||||
if (!list->tail)
|
||||
{
|
||||
list->head = head;
|
||||
list->tail = tail;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*(list->tail))->next = head;
|
||||
list->tail = tail;
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline void sys_pslist_merge_pslist(sys_pslist_t *list,
|
||||
sys_pslist_t *list_to_append)
|
||||
{
|
||||
sys_pslist_append_list(list, list_to_append->head,
|
||||
list_to_append->tail);
|
||||
sys_pslist_init(list_to_append);
|
||||
}
|
||||
|
||||
__static_inline void sys_pslist_insert(sys_pslist_t *list,
|
||||
sys_psnode_t *const *prev,
|
||||
sys_psnode_t *const *node)
|
||||
{
|
||||
if (!prev)
|
||||
{
|
||||
sys_pslist_prepend(list, node);
|
||||
}
|
||||
else if (!(*prev)->next)
|
||||
{
|
||||
sys_pslist_append(list, node);
|
||||
}
|
||||
else
|
||||
{
|
||||
(*node)->next = (*prev)->next;
|
||||
(*prev)->next = node;
|
||||
}
|
||||
}
|
||||
|
||||
__static_inline sys_psnode_t *const *sys_pslist_get_not_empty(sys_pslist_t *list)
|
||||
{
|
||||
sys_psnode_t *const *node = list->head;
|
||||
|
||||
list->head = (*node)->next;
|
||||
if (list->tail == node)
|
||||
{
|
||||
list->tail = list->head;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
__static_inline sys_psnode_t *const *sys_pslist_get(sys_pslist_t *list)
|
||||
{
|
||||
return sys_pslist_is_empty(list) ? NULL : sys_pslist_get_not_empty(list);
|
||||
}
|
||||
|
||||
__static_inline void sys_pslist_remove(sys_pslist_t *list,
|
||||
sys_psnode_t *const *prev_node,
|
||||
sys_psnode_t *const *node)
|
||||
{
|
||||
if (!prev_node)
|
||||
{
|
||||
list->head = (*node)->next;
|
||||
|
||||
/* Was node also the tail? */
|
||||
if (list->tail == node)
|
||||
{
|
||||
list->tail = list->head;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
(*prev_node)->next = (*node)->next;
|
||||
|
||||
/* Was node the tail? */
|
||||
if (list->tail == node)
|
||||
{
|
||||
list->tail = prev_node;
|
||||
}
|
||||
}
|
||||
|
||||
(*node)->next = NULL;
|
||||
}
|
||||
|
||||
__static_inline bool sys_pslist_find_and_remove(sys_pslist_t *list,
|
||||
sys_psnode_t *const *node)
|
||||
{
|
||||
sys_psnode_t *const *prev = NULL;
|
||||
sys_psnode_t *const *test;
|
||||
|
||||
SYS_PSLIST_FOR_EACH_NODE(list, test)
|
||||
{
|
||||
if (test == node)
|
||||
{
|
||||
sys_pslist_remove(list, prev, node);
|
||||
return true;
|
||||
}
|
||||
|
||||
prev = test;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
412
components/system/include/list/slist.h
Executable file
412
components/system/include/list/slist.h
Executable file
@@ -0,0 +1,412 @@
|
||||
/**
|
||||
* @file slist.h
|
||||
* @author LokLiang (lokliang@163.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-05-01
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __SINGLELIST_H__
|
||||
#define __SINGLELIST_H__
|
||||
|
||||
#include "sys_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef LIST_NODE_TYPE
|
||||
#define LIST_NODE_TYPE 0 /* 0 -- 使用指针连结(推荐);1 -- 使用偏移量连结,内存的地址允许被迁移并继续管理(推荐) */
|
||||
#endif
|
||||
|
||||
#if LIST_NODE_TYPE == 1
|
||||
typedef int list_node_t;
|
||||
#else
|
||||
typedef void * list_node_t;
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
list_node_t next;
|
||||
} slist_node_t;
|
||||
typedef struct
|
||||
{
|
||||
list_node_t head;
|
||||
list_node_t tail;
|
||||
} slist_t;
|
||||
|
||||
__static_inline void slist_init_list (slist_t *list);
|
||||
__static_inline void slist_init_node (slist_node_t *node);
|
||||
|
||||
__static_inline int slist_insert_font (slist_t *list, slist_node_t *node);
|
||||
__static_inline int slist_insert_tail (slist_t *list, slist_node_t *node);
|
||||
__static_inline int slist_remove (slist_t *list, slist_node_t *node);
|
||||
__static_inline int slist_remove_next (slist_t *list, slist_node_t *prev_node, slist_node_t *remove_node);
|
||||
|
||||
__static_inline int slist_insert_next (slist_t *list, slist_node_t *tar_node, slist_node_t *new_node);
|
||||
__static_inline int slist_insert_prev (slist_t *list, slist_node_t *tar_node, slist_node_t *new_node);
|
||||
|
||||
__static_inline slist_node_t *slist_take_head (slist_t *list);
|
||||
__static_inline slist_node_t *slist_take_tail (slist_t *list);
|
||||
|
||||
__static_inline slist_node_t *slist_peek_head (slist_t *list);
|
||||
__static_inline slist_node_t *slist_peek_tail (slist_t *list);
|
||||
|
||||
__static_inline slist_node_t *slist_peek_next (slist_node_t *node);
|
||||
__static_inline slist_node_t *slist_peek_prev (slist_t *list, slist_node_t *node);
|
||||
|
||||
__static_inline slist_node_t *slist_peek_next_loop (slist_t *list, slist_node_t *node);
|
||||
__static_inline slist_node_t *slist_peek_prev_loop (slist_t *list, slist_node_t *node);
|
||||
|
||||
__static_inline int slist_is_pending (slist_node_t *node);
|
||||
|
||||
#define SLIST_CONTAINER_OF(PTR, TYPE, MEMBER) ((TYPE *)&((uint8_t *)PTR)[-(int)&((TYPE *)0)->MEMBER])
|
||||
|
||||
#define SLIST_FOR_EACH_NODE(__sl, __sn) \
|
||||
for (__sn = slist_peek_head(__sl); __sn; \
|
||||
__sn = slist_peek_next(__sn))
|
||||
|
||||
#define SLIST_ITERATE_FROM_NODE(__sl, __sn) \
|
||||
for (__sn = __sn ? slist_peek_next_no_check(__sn) \
|
||||
: slist_peek_head(__sl); \
|
||||
__sn; \
|
||||
__sn = slist_peek_next(__sn))
|
||||
|
||||
#define SLIST_FOR_EACH_NODE_SAFE(__sl, __sn, __sns) \
|
||||
for (__sn = slist_peek_head(__sl), \
|
||||
__sns = slist_peek_next(__sn); \
|
||||
__sn; \
|
||||
__sn = __sns, __sns = slist_peek_next(__sn))
|
||||
|
||||
#define SLIST_CONTAINER(__ln, __cn, __n) \
|
||||
((__ln) ? SLIST_CONTAINER_OF((__ln), __typeof__(*(__cn)), __n) : NULL)
|
||||
|
||||
#define SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n) \
|
||||
SLIST_CONTAINER(slist_peek_head(__sl), __cn, __n)
|
||||
|
||||
#define SLIST_PEEK_TAIL_CONTAINER(__sl, __cn, __n) \
|
||||
SLIST_CONTAINER(slist_peek_tail(__sl), __cn, __n)
|
||||
|
||||
#define SLIST_PEEK_NEXT_CONTAINER(__cn, __n) \
|
||||
((__cn) ? SLIST_CONTAINER(slist_peek_next(&((__cn)->__n)), __cn, __n) : NULL)
|
||||
|
||||
#define SLIST_FOR_EACH_CONTAINER(__sl, __cn, __n) \
|
||||
for (__cn = SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n); \
|
||||
__cn; \
|
||||
__cn = SLIST_PEEK_NEXT_CONTAINER(__cn, __n))
|
||||
|
||||
#define SLIST_FOR_EACH_CONTAINER_SAFE(__sl, __cn, __cns, __n) \
|
||||
for (__cn = SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n), \
|
||||
__cns = SLIST_PEEK_NEXT_CONTAINER(__cn, __n); \
|
||||
__cn; \
|
||||
__cn = __cns, __cns = SLIST_PEEK_NEXT_CONTAINER(__cn, __n))
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if LIST_NODE_TYPE == 1
|
||||
#define SLIST_SET(Value1, Value2) (Value1 = Value2 == 0 ? 0 : ((int)Value2 - (int)&Value1) | 1)
|
||||
#define SLIST_GET(Value) ((slist_node_t*)(Value == 0 ? 0 : ((int)&Value + Value) & ~1))
|
||||
#else
|
||||
#define SLIST_SET(Value1, Value2) (Value1 = (list_node_t)Value2)
|
||||
#define SLIST_GET(Value) ((slist_node_t*)(Value))
|
||||
#endif
|
||||
|
||||
__static_inline void slist_init_list(slist_t *list)
|
||||
{
|
||||
SLIST_SET(list->head, 0);
|
||||
SLIST_SET(list->tail, 0);
|
||||
}
|
||||
__static_inline void slist_init_node(slist_node_t *node)
|
||||
{
|
||||
SLIST_SET(node->next, 0);
|
||||
}
|
||||
__static_inline int slist_insert_font(slist_t *list, slist_node_t *node)
|
||||
{
|
||||
if (SLIST_GET(node->next) != NULL)
|
||||
{
|
||||
// SYS_LOG_WRN("Node is pending");
|
||||
return -1;
|
||||
}
|
||||
|
||||
slist_node_t *head_node = SLIST_GET(list->head);
|
||||
if (head_node)
|
||||
{
|
||||
SLIST_SET(node->next, head_node);
|
||||
}
|
||||
else
|
||||
{
|
||||
SLIST_SET(node->next, node);
|
||||
SLIST_SET(list->tail, node);
|
||||
}
|
||||
SLIST_SET(list->head, node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
__static_inline int slist_insert_tail(slist_t *list, slist_node_t *node)
|
||||
{
|
||||
if (SLIST_GET(node->next) != NULL)
|
||||
{
|
||||
// SYS_LOG_WRN("Node is pending");
|
||||
return -1;
|
||||
}
|
||||
|
||||
SLIST_SET(node->next, node);
|
||||
if (SLIST_GET(list->head) == NULL)
|
||||
{
|
||||
SLIST_SET(list->head, node);
|
||||
}
|
||||
else
|
||||
{
|
||||
slist_node_t *tail_node = (slist_node_t *)SLIST_GET(list->tail);
|
||||
|
||||
SLIST_SET(tail_node->next, node);
|
||||
}
|
||||
SLIST_SET(list->tail, node);
|
||||
|
||||
return 0;
|
||||
}
|
||||
__static_inline int slist_remove(slist_t *list, slist_node_t *node)
|
||||
{
|
||||
if (node == NULL || list == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
slist_node_t *list_head = SLIST_GET(list->head);
|
||||
slist_node_t *node_next = SLIST_GET(node->next);
|
||||
if (node_next == NULL || list_head == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (node == list_head) // node 是首个节点
|
||||
{
|
||||
if (node == SLIST_GET(list->tail)) // node 是最后一个节点
|
||||
{
|
||||
SLIST_SET(list->head, 0);
|
||||
SLIST_SET(list->tail, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
SLIST_SET(list->head, node_next);
|
||||
}
|
||||
}
|
||||
else // node 不是首个节点
|
||||
{
|
||||
slist_node_t *prev_node = slist_peek_prev(list, node);
|
||||
if (prev_node == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (node == SLIST_GET(list->tail)) // node 是最后一个节点
|
||||
{
|
||||
SLIST_SET(list->tail, prev_node);
|
||||
SLIST_SET(prev_node->next, prev_node);
|
||||
}
|
||||
else
|
||||
{
|
||||
SLIST_SET(prev_node->next, node_next);
|
||||
}
|
||||
}
|
||||
|
||||
SLIST_SET(node->next, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
__static_inline int slist_remove_next(slist_t *list, slist_node_t *prev_node, slist_node_t *remove_node)
|
||||
{
|
||||
if (remove_node == NULL || list == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
slist_node_t *list_head = SLIST_GET(list->head);
|
||||
slist_node_t *node_next = SLIST_GET(remove_node->next);
|
||||
if (node_next == NULL || list_head == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (remove_node == list_head) // remove_node 是首个节点
|
||||
{
|
||||
if (remove_node == SLIST_GET(list->tail)) // remove_node 是最后一个节点
|
||||
{
|
||||
SLIST_SET(list->head, 0);
|
||||
SLIST_SET(list->tail, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
SLIST_SET(list->head, node_next);
|
||||
}
|
||||
}
|
||||
else // remove_node 不是首个节点
|
||||
{
|
||||
if (prev_node == NULL)
|
||||
{
|
||||
prev_node = slist_peek_prev(list, remove_node);
|
||||
if (prev_node == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (remove_node == SLIST_GET(list->tail)) // remove_node 是最后一个节点
|
||||
{
|
||||
SLIST_SET(list->tail, prev_node);
|
||||
SLIST_SET(prev_node->next, prev_node);
|
||||
}
|
||||
else
|
||||
{
|
||||
SLIST_SET(prev_node->next, node_next);
|
||||
}
|
||||
}
|
||||
|
||||
SLIST_SET(remove_node->next, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
__static_inline int slist_insert_next(slist_t *list, slist_node_t *tar_node, slist_node_t *new_node)
|
||||
{
|
||||
slist_node_t *tar_node_next = SLIST_GET(tar_node->next);
|
||||
if (SLIST_GET(list->head) == NULL || tar_node_next == NULL || SLIST_GET(new_node->next) != NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tar_node == SLIST_GET(list->tail)) // tar_node 是最后一个节点
|
||||
{
|
||||
SLIST_SET(new_node->next, new_node);
|
||||
SLIST_SET(tar_node->next, new_node);
|
||||
SLIST_SET(list->tail, new_node);
|
||||
}
|
||||
else
|
||||
{
|
||||
SLIST_SET(new_node->next, tar_node_next);
|
||||
SLIST_SET(tar_node->next, new_node);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
__static_inline int slist_insert_prev(slist_t *list, slist_node_t *tar_node, slist_node_t *new_node)
|
||||
{
|
||||
slist_node_t *list_head = SLIST_GET(list->head);
|
||||
if (list_head == NULL || SLIST_GET(tar_node->next) == NULL || SLIST_GET(new_node->next) != NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tar_node == list_head) // tar_node 是首个节点
|
||||
{
|
||||
SLIST_SET(new_node->next, list_head);
|
||||
SLIST_SET(list->head, new_node);
|
||||
}
|
||||
else
|
||||
{
|
||||
slist_node_t *prev_node = slist_peek_prev(list, tar_node);
|
||||
if (prev_node == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
SLIST_SET(new_node->next, tar_node);
|
||||
SLIST_SET(prev_node->next, new_node);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__static_inline slist_node_t *slist_take_head(slist_t *list)
|
||||
{
|
||||
slist_node_t *ret = SLIST_GET(list->head);
|
||||
slist_remove(list, ret);
|
||||
return ret;
|
||||
}
|
||||
__static_inline slist_node_t *slist_take_tail(slist_t *list)
|
||||
{
|
||||
slist_node_t *ret = SLIST_GET(list->tail);
|
||||
slist_remove(list, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
__static_inline slist_node_t *slist_peek_head(slist_t *list)
|
||||
{
|
||||
return SLIST_GET(list->head);
|
||||
}
|
||||
__static_inline slist_node_t *slist_peek_tail(slist_t *list)
|
||||
{
|
||||
return SLIST_GET(list->tail);
|
||||
}
|
||||
|
||||
__static_inline slist_node_t *slist_peek_next(slist_node_t *node)
|
||||
{
|
||||
slist_node_t *next_node = SLIST_GET(node->next);
|
||||
if (next_node == node)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return next_node;
|
||||
}
|
||||
}
|
||||
__static_inline slist_node_t *slist_peek_prev(slist_t *list, slist_node_t *node)
|
||||
{
|
||||
slist_node_t *prev_node = NULL;
|
||||
slist_node_t *test_node = SLIST_GET(list->head);
|
||||
while (test_node && test_node != node && prev_node != test_node)
|
||||
{
|
||||
prev_node = test_node;
|
||||
test_node = SLIST_GET(test_node->next);
|
||||
}
|
||||
if (test_node == node)
|
||||
{
|
||||
return prev_node;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
__static_inline slist_node_t *slist_peek_next_loop(slist_t *list, slist_node_t *node)
|
||||
{
|
||||
slist_node_t *next_node = SLIST_GET(node->next);
|
||||
if (next_node == node)
|
||||
{
|
||||
return SLIST_GET(list->head);
|
||||
}
|
||||
else
|
||||
{
|
||||
return next_node;
|
||||
}
|
||||
}
|
||||
__static_inline slist_node_t *slist_peek_prev_loop(slist_t *list, slist_node_t *node)
|
||||
{
|
||||
slist_node_t *prev_node = SLIST_GET(list->tail);
|
||||
slist_node_t *test_node = SLIST_GET(list->head);
|
||||
while (test_node && test_node != node && prev_node != test_node)
|
||||
{
|
||||
prev_node = test_node;
|
||||
test_node = SLIST_GET(test_node->next);
|
||||
}
|
||||
if (test_node == node)
|
||||
{
|
||||
return prev_node;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
__static_inline int slist_is_pending(slist_node_t *node)
|
||||
{
|
||||
return (SLIST_GET(node->next) != NULL);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user