开发者

Understanding SLIST - queue.h

I have been looking at how I could use Single linked lists using the macros defined in sys/queue.h and I have a few doubts. Currently I am trying to create a simple list using the same. Please note that the program below is incomplete, I have put in only the part I feel is relevant.

int main() {
    SLIST_HEAD(slisthead, entry) head = SLIST_HEAD_INITIALIZER(head);
    st开发者_如何学JAVAruct slisthead *headp;

    struct entry {
        SLIST_ENTRY(entry) entries;
    }*n1, *n2, *n3, *np;


    /* Upon expanding the macro we would get,
     * struct entry {
     *      struct {
     *          struct entry *sle_first;
     *      } entries;
     *  }*n1, *n2, *np;
     */

I fail to understand why "sle_first" is stored in another struct altogether. Couldnt SLIST_ENTRY expand to something like the following instead?

#define SLIST_ENTRY(type) struct type* sle_first;


sys/queue.h also contain double linked lists and queues, that require more than one pointer in entry.

So I think SLIST_ENTRY expanded this way to be similar to another type entries, like queues.


This is almost 5 years late, but in case someone reads this IMHO this is because a node of an intrusive list can be made to belong to many different lists simultaneously (as many as intrusive node hook members has), so each member of your "entry" struct needs to have a different name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜