From: Willy Tarreau Date: Sat, 28 Apr 2007 10:42:06 +0000 (+0200) Subject: [MINOR] added support for dual-linked lists X-Git-Tag: v1.3.10~34 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=40cf67da89cf2332804f42edfc0e0fe87dbe8806;p=thirdparty%2Fhaproxy.git [MINOR] added support for dual-linked lists Imported dual-linked lists management macros from the old scheduler project, and fixed a long-standing nasty bug in the DLIST_ADD() macro. --- diff --git a/include/common/mini-clist.h b/include/common/mini-clist.h index cf472ba8ff..fb31ac86e2 100644 --- a/include/common/mini-clist.h +++ b/include/common/mini-clist.h @@ -1,6 +1,6 @@ /* * list.h : list manipulation macros and structures. - * Copyright 2002-2006 Willy Tarreau + * Copyright 2002-2007 Willy Tarreau * */ @@ -31,6 +31,44 @@ struct list { * */ +/* adds an element at the beginning of a dual-linked list ; returns the element */ +#define DLIST_ADD(lh, el) ({ typeof(el) __ret = (el); __ret->n = (void *)(lh); __ret->p = (void *)&(lh); if (__ret->n != NULL) __ret->n->p = __ret; (lh) = (typeof(lh))&__ret->n; __ret; }) + +/* removes an element from a dual-linked list and returns it */ +#define DLIST_DEL(el) ({ typeof(el) __ret = (el); if (__ret->n != NULL) __ret->n->p = __ret->p; __ret->p->n = __ret->n; __ret; }) + +/* + * iterates through a list of items of type "" which are + * linked via a "struct list" member named . The head of the + * list is stored at a location designed by , which should be a + * "struct list *". A variable of type "" will + * be used as temporary end of list pointer. It can be derived from + * since this one is only used before. will be modified except for + * foreach_dlist_item_cst which is slightly slower. + * Major difference between FOREACH_ITEM is that it stops at NULL. + * Example: foreach_dlist_item(cur_node, args, struct node *, list) { ... }; + * foreach_dlist_item_cst(cur_node, &node->args, struct node *, list) { ... }; + */ +#define foreach_dlist_item_cst(iterator, list_head, struct_type, struct_member) \ + for ((iterator) = LIST_ELEM(&(list_head), struct_type, struct_member.n); \ + ((iterator)->struct_member.n != NULL) && \ + (((iterator) = LIST_ELEM((iterator)->struct_member.n, struct_type, struct_member.n)), 1);\ + ) + +#define foreach_dlist_item(iterator, var_list_head, struct_type, struct_member) \ + while ((var_list_head != NULL) && \ + ((var_list_head=((iterator)=LIST_ELEM(var_list_head, struct_type, struct_member.n))->struct_member.n), 1)) + +/* + * Like foreach_dlist_item, except that this one only operates on the head of + * the list. It's to the inner instructions to iterate the list head. If not, + * this will be an endless loop. + */ +#define while_dlist_item(iterator, var_list_head, struct_type, struct_member) \ + while ((var_list_head != NULL) && \ + (((iterator)=LIST_ELEM(var_list_head, struct_type, struct_member.n)),1)) + + /****** circular lists ********/ /* adds an element at the beginning of a list ; returns the element */