X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;ds=sidebyside;f=bertos%2Fstruct%2Flist.h;h=b970b7cf2d3355af09f9706815a854e029e1d4c1;hb=32d1445272120a254d77ce8d1af1f527da7a2c17;hp=87808edf86ac9937a4ff76ab98e87105966d60e3;hpb=e5174304054e26cd8f3cd1f9980871c20c07fc46;p=bertos.git diff --git a/bertos/struct/list.h b/bertos/struct/list.h index 87808edf..b970b7cf 100644 --- a/bertos/struct/list.h +++ b/bertos/struct/list.h @@ -32,12 +32,12 @@ * * \brief General pourpose double-linked lists * - * \version $Id: list.h 1594 2008-08-10 12:20:10Z bernie $ + * \version $Id$ * \author Bernie Innocenti */ -#ifndef MWARE_LIST_H -#define MWARE_LIST_H +#ifndef STRUCT_LIST_H +#define STRUCT_LIST_H #include /* INLINE */ #include /* ASSERT_VALID_PTR() */ @@ -104,7 +104,7 @@ typedef struct _PriNode * ADDHEAD(&foo_list, &foo1); * INSERT_BEFORE(&foo_list, &foo2); * FOREACH_NODE(fp, &foo_list) - * fp->a = 10; + * fp->a = 10; * } * * \endcode @@ -203,17 +203,30 @@ typedef struct _PriNode ASSERT(n == &(l)->tail); \ } while (0) + /// Checks that a node isn't part of a given list + #define LIST_ASSERT_NOT_CONTAINS(list,node) \ + do { \ + Node *ln; \ + ASSERT_VALID_PTR(list); \ + ASSERT_VALID_PTR(node); \ + FOREACH_NODE(ln, list) \ + ASSERT(ln != (Node *)(node)); \ + } while (0) + #define INVALIDATE_NODE(n) ((n)->succ = (n)->pred = NULL) #else #define LIST_ASSERT_VALID(l) do {} while (0) + #define LIST_ASSERT_NOT_CONTAINS(list,node) do {} while (0) #define INVALIDATE_NODE(n) do {} while (0) #endif +/** Tell whether a list is empty. */ +#define LIST_EMPTY(l) ( (void *)((l)->head.succ) == (void *)(&(l)->tail) ) + /** Add node to list head. */ #define ADDHEAD(l,n) \ do { \ - ASSERT_VALID_PTR(l); \ - ASSERT_VALID_PTR(n); \ + LIST_ASSERT_NOT_CONTAINS((l),(n)); \ (n)->succ = (l)->head.succ; \ (n)->pred = (l)->head.succ->pred; \ (n)->succ->pred = (n); \ @@ -223,8 +236,7 @@ typedef struct _PriNode /** Add node to list tail. */ #define ADDTAIL(l,n) \ do { \ - ASSERT_VALID_PTR(l); \ - ASSERT_VALID_PTR(n); \ + LIST_ASSERT_NOT_CONTAINS((l),(n)); \ (n)->succ = &(l)->tail; \ (n)->pred = (l)->tail.pred; \ (n)->pred->succ = (n); \ @@ -261,21 +273,32 @@ typedef struct _PriNode INVALIDATE_NODE(n); \ } while (0) -/** Tell whether a list is empty. */ -#define LIST_EMPTY(l) ( (void *)((l)->head.succ) == (void *)(&(l)->tail) ) +/** + * Insert a priority node in a priority queue. + * + * The new node is inserted immediately before the first node with the same + * priority or appended to the tail if no such node exists. + */ +#define LIST_ENQUEUE_HEAD(list, node) \ + do { \ + PriNode *ln; \ + LIST_ASSERT_NOT_CONTAINS((list),(node)); \ + FOREACH_NODE(ln, (list)) \ + if (ln->pri <= (node)->pri) \ + break; \ + INSERT_BEFORE(&(node)->link, &ln->link); \ + } while (0) /** * Insert a priority node in a priority queue. * - * The new node is inserted immediately before the - * first node with lower priority or appended to - * the tail if no such node exists. + * The new node is inserted immediately before the first node with lower + * priority or appended to the tail if no such node exists. */ #define LIST_ENQUEUE(list, node) \ do { \ PriNode *ln; \ - ASSERT_VALID_PTR(list); \ - ASSERT_VALID_PTR(node); \ + LIST_ASSERT_NOT_CONTAINS((list),(node)); \ FOREACH_NODE(ln, (list)) \ if (ln->pri < (node)->pri) \ break; \ @@ -327,4 +350,4 @@ INLINE Node *list_remTail(List *l) return n; } -#endif /* MWARE_LIST_H */ +#endif /* STRUCT_LIST_H */