Browse Source Download (without any required ccan dependencies)

Module:

tlist

Summary:

typesafe double linked list routines

Author:

Rusty Russell <rusty@rustcorp.com.au>

Dependencies:

Description:

The list header contains routines for manipulating double linked lists; this extends it so you can create list head types which only accomodate a specific entry type.

Example:

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <ccan/tlist/tlist.h>

// We could use TLIST_TYPE(children, struct child) to define this.
struct tlist_children {
        struct list_head raw;
        TCON(struct child *canary);
};
struct parent {
        const char *name;
        unsigned int num_children;
        struct tlist_children children;
};

struct child {
        const char *name;
        struct list_node list;
};

int main(int argc, char *argv[])
{
        struct parent p;
        struct child *c;
        unsigned int i;

        if (argc < 2)
                errx(1, "Usage: %s parent children...", argv[0]);

        p.name = argv[1];
        tlist_init(&p.children);
        for (i = 2; i < argc; i++) {
                c = malloc(sizeof(*c));
                c->name = argv[i];
                tlist_add(&p.children, c, list);
                p.num_children++;
        }

        printf("%s has %u children:", p.name, p.num_children);
        tlist_for_each(&p.children, c, list)
                printf("%s ", c->name);
        printf("\n");
        return 0;
}

License:

LGPL