SIMPLEQ队列图例:
-
/*
-
* Simple queue definitions.
-
*/
-
#define SIMPLEQ_HEAD(name, type) \
-
struct name { \
-
struct type *sqh_first; /* first element */ \
-
struct type **sqh_last; /* addr of last next element */ \
-
}
展开SIMPLEQ_HEAD(event_list,event)
struct event_list{
struct event *sqh_first;
struct event **sqh_last;
}
这样就声明了一个SIMPLEQ的一个头节点event_list,包括一个指向event的指针和一个指向event指针的指针(存放的是指向event指针的地址)
sqh_first指向SIMPLEQ的第一个元素,而sqh_last指向SIMPLEQ的最后一个元素
-
#define SIMPLEQ_HEAD_INITIALIZER(head) \ //这里的head是一个链表头节点,而不是指向头节点的指针
-
{ NULL, &(head).sqh_first }
展开SIMPLEQ_HEAD_INITIALIZER(&pEventBase->eventqueue) { NULL, &(pEventBase->eventqueue).sqh_first }
把头节点eventqueue的sqh_first置为NULL,让sqh_last指向sqh_first
-
#define SIMPLEQ_ENTRY(type) \
-
struct { \
-
struct type *sqe_next; /* next element */ \
-
}
展开SIMPLEQ_ENTRY(event)
struct {
struct event *sqe_next;
}
该结构具有一个指向下一个event结构的指针,可以把该结构放入一个struct中,利用ENTRY把各个struct连接起来
-
/*
-
* Simple queue access methods.
-
*/
-
#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
展开SIMPLE_FIRST(&pEventBase->eventqueue) ((&pEventBase->eventqueue)->sqh_first) //访问SIMPLEQ的第一个元素
-
#define SIMPLEQ_END(head) NULL
返回SIMPLEQ的末尾,总是为NULL
-
#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
展开SIMPLE_EMPTY(&pEventBase->eventqueue) (
((&pEventBase->eventqueue)->sqh_first) == NULL )
-
#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
展开SIMPLEQ_NEXT(pEvent, event_next) ((pEvent)->event_next.sqe_next)
访问下一个event节点
-
#define SIMPLEQ_FOREACH(var, head, field) \
-
for((var) = SIMPLEQ_FIRST(head); \
-
(var) != SIMPLEQ_END(head); \
-
(var) = SIMPLEQ_NEXT(var, field))
展开SIMPLEQ_FOREACH(var, head, field)
for((var) = SIMPLEQ_FIRST(head); \
(var) != SIMPLEQ_END(head);
(var) = SIMPLEQ_NEXT(var, field))
SIMPLEQ_FOREACH(pTmp,&pEventBase->eventquue,event_next)
for((pTmp) = SIMPLEQ_FIRST(&pEventBase->eventqueue);
(pTmp) != NULL;
(pTmp) = SIMPLE_NEXT(pTmp,event_next))
继续展开
for((pTmp) = ((&pEventBase->eventqueue)->sqh_first);
(pTmp) != NULL;
(pTmp) = ((pTmp)->event_next.sqe_next) )
-
/*
-
* Simple queue functions.
-
*/
-
#define SIMPLEQ_INIT(head) do { \
-
(head)->sqh_first = NULL; \
-
(head)->sqh_last = &(head)->sqh_first; \
-
} while (0)
展开SIMPLEQ_INIT(&pEventBase->eventqueue)
do{
(&pEventBase->eventqueue)->sqh_first = NULL; //让头节点的first指针指向NULL
(&pEventBase->eventqueue)->sqh_last = &(&pEventBase->eventqueue)->sqh_first; //让头节点的last指针指向头节点的first指针
}
-
#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \ //在head后插入一个元素
-
if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ //head的first指针赋给ele的next指针
-
(head)->sqh_last = &(elm)->field.sqe_next; \ //
-
(head)->sqh_first = (elm); \
-
} while (0)
初始状态:
执行后:
图中的1,2,3分别对应:
1. ((elm)->field.sqe_next = (head)->sqh_first) // 让elm的next指针指向head的first指针所指元素
2. (head)->sqh_last = &(elm)->field.sqe_next; // 让head的last指针指向elm的next指针
3. (head)->sqh_first = (elm); // 让head的fist指针指向elm所指元素
-
#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \在SIMPLEQ尾部插入一个元素
-
(elm)->field.sqe_next = NULL; \
-
*(head)->sqh_last = (elm); \
-
(head)->sqh_last = &(elm)->field.sqe_next; \
-
} while (0)
初始状态:
执行后:
图中的1,2,3分别对应:
-
(elm)->field.sqe_next = NULL; //让elm的next指针指向NULL
-
*(head)->sqh_last = (elm); //让最后一个元素的next指针指向elm所指元素, *(head)->sqh_last可以访问到最后一个元素的next指针,(head)->sqh_last可以得到最后一个元素的next指针的地址,* //(head)->sqh_last则获得最后一个元素的next指针
-
(head)->sqh_last = &(elm)->field.sqe_next; //让头节点的last指针指向elm元素的next指针
-
#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ //在listelm后插入elm
-
if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
-
(head)->sqh_last = &(elm)->field.sqe_next; \
-
(listelm)->field.sqe_next = (elm); \
-
} while (0)
初始状态:
执行后:
图中的1,2,3分别对应:
1. ((elm)->field.sqe_next = (listelm)->field.sqe_next) //让elm的next指针指向listelm的next指针
2. (head)->sqh_last = &(elm)->field.sqe_next; //让head的last指针指向elm的next指针
3. (listelm)->field.sqe_next = (elm); //让listelm的next指针指向elm所指元素
-
#define SIMPLEQ_REMOVE_HEAD(head, elm, field) do { \ //移除head后的第一个元素
-
if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) \
-
(head)->sqh_last = &(head)->sqh_first; \
-
} while (0)
初始状态:
执行后:
1. ((head)->sqh_first = (elm)->field.sqe_next) //让head的first指针指向elm的next指针
2. (head)->sqh_last = &(head)->sqh_first; //让head指针的last指针指向head的next指针
阅读(1754) | 评论(0) | 转发(0) |