src/chain.c
changeset 35 3715247e8f5a
child 69 6f298b6e0d5f
equal deleted inserted replaced
34:763f65f9df0c 35:3715247e8f5a
       
     1 #include "chain.h"
       
     2 
       
     3 #include <stdlib.h>
       
     4 
       
     5 err_t chain_add (struct chain_list *list, const void *chain, void *arg)
       
     6 {
       
     7     struct chain_head *item;
       
     8 
       
     9     // allocate the chain item
       
    10     if ((item = calloc(1, sizeof(*item))) == NULL)
       
    11         return ERR_CALLOC;
       
    12 
       
    13     // store
       
    14     item->chain = chain;
       
    15     item->arg = arg;
       
    16 
       
    17     // append
       
    18     STAILQ_INSERT_TAIL(list, item, node);
       
    19 
       
    20     // ok
       
    21     return SUCCESS;
       
    22 }
       
    23 
       
    24 void chain_free (struct chain_list *list)
       
    25 {
       
    26     // start from the first item
       
    27     struct chain_head *next = STAILQ_FIRST(list);
       
    28 
       
    29     // clean up any handler chains
       
    30     while (next) {
       
    31         struct chain_head *node = next;
       
    32 
       
    33         // update next
       
    34         next = STAILQ_NEXT(node, node);
       
    35 
       
    36         // free
       
    37         free(node);
       
    38     }
       
    39 }