src/chain.c
author Tero Marttila <terom@fixme.fi>
Fri, 24 Apr 2009 23:01:34 +0300
changeset 153 d35e7cb3a489
parent 69 6f298b6e0d5f
child 171 b54f393c3df0
permissions -rw-r--r--
implement irc_net reconnect, requires testing
#include "chain.h"

#include <stdlib.h>

err_t chain_add (struct chain_list *list, const void *chain, void *arg)
{
    struct chain_head *item;

    // allocate the chain item
    if ((item = calloc(1, sizeof(*item))) == NULL)
        return ERR_CALLOC;

    // store
    item->chain = chain;
    item->arg = arg;

    // append
    STAILQ_INSERT_TAIL(list, item, node);

    // ok
    return SUCCESS;
}

void chain_remove (struct chain_list *list, const void *chain, void *arg)
{
    struct chain_head *item;
    
    // look for it
    CHAIN_FOREACH(list, item) {
        if (item->chain == chain && item->arg == arg) {
            // remove it
            // XXX: use TAILQ instead?
            STAILQ_REMOVE(list, item, chain_head, node);

            // free it
            free(item);

            // ok
            return;
        }
    }

    // not found... ignore
}

void chain_free (struct chain_list *list)
{
    // start from the first item
    struct chain_head *next = STAILQ_FIRST(list);

    // clean up any handler chains
    while (next) {
        struct chain_head *node = next;

        // update next
        next = STAILQ_NEXT(node, node);

        // free
        free(node);
    }
}