pgm_read_int(): New macro.
[bertos.git] / mware / list.h
index c596f1d5b00805a5349cef70c9b8ecf72046a806..8d89b4d61736748f77b4230c6631e1ec30550b3b 100755 (executable)
 
 /*#*
  *#* $Log$
+ *#* Revision 1.19  2006/03/20 17:50:29  bernie
+ *#* Fix typo.
+ *#*
+ *#* Revision 1.18  2006/02/27 22:40:21  bernie
+ *#* Add support for poor pre-C99 compilers.
+ *#*
+ *#* Revision 1.17  2006/02/24 01:18:34  bernie
+ *#* LIST_ENQUEUE(): New macro; Remove obsolete names.
+ *#*
  *#* Revision 1.16  2006/01/23 23:10:38  bernie
  *#* REVERSE_FOREACH_NODE(): New macro; FOREACHNODE(): Rename to FOREACH_NODE.
  *#*
@@ -98,6 +107,15 @@ typedef struct _List
        Node tail;
 } List;
 
+/**
+ * Extended node for priority queues.
+ */
+typedef struct _PriNode
+{
+       Node link;
+       int  pri;
+} PriNode;
+
 
 /*!
  * Template for a naked node in a list of \a T structures.
@@ -122,7 +140,7 @@ typedef struct _List
  *
  *        LIST_INIT(&foo_list);
  *        ADDHEAD(&foo_list, &foo1);
- *        INSERTBEFORE(&foo_list, &foo2);
+ *        INSERT_BEFORE(&foo_list, &foo2);
  *        FOREACH_NODE(fp, &foo_list)
  *             fp->a = 10;
  *    }
@@ -161,6 +179,13 @@ typedef struct _List
  */
 #define LIST_TAIL(l) ((l)->tail.pred)
 
+// TODO: move in compiler.h
+#if COMPILER_TYPEOF
+       #define TYPEOF_OR_VOIDPTR(type) typeof(type)
+#else
+       #define TYPEOF_OR_VOIDPTR(type) void *
+#endif
+
 /*!
  * Iterate over all nodes in a list.
  *
@@ -170,13 +195,11 @@ typedef struct _List
  */
 #define FOREACH_NODE(n, l) \
        for( \
-               (n) = (typeof(n))LIST_HEAD(l); \
+               (n) = (TYPEOF_OR_VOIDPTR(n))LIST_HEAD(l); \
                ((Node *)(n))->succ; \
-               (n) = (typeof(n))(((Node *)(n))->succ) \
+               (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->succ) \
        )
 
-#define FOREACHNODE FOREACH_NODE /* OBSOLETE */
-
 /*!
  * Iterate backwards over all nodes in a list.
  *
@@ -186,18 +209,18 @@ typedef struct _List
  */
 #define REVERSE_FOREACH_NODE(n, l) \
        for( \
-               (n) = (typeof(n))LIST_TAIL(l); \
+               (n) = (TYPEOF_OR_VOIDPTR(n))LIST_TAIL(l); \
                ((Node *)(n))->pred; \
-               (n) = (typeof(n))(((Node *)(n))->pred) \
+               (n) = (TYPEOF_OR_VOIDPTR(n))(((Node *)(n))->pred) \
        )
 
 /*! Initialize a list. */
 #define LIST_INIT(l) \
        do { \
-               (l)->head.succ = (typeof((l)->head.succ)) &(l)->tail; \
+               (l)->head.succ = (TYPEOF_OR_VOIDPTR((l)->head.succ)) &(l)->tail; \
                (l)->head.pred = NULL; \
                (l)->tail.succ = NULL; \
-               (l)->tail.pred = (typeof((l)->tail.pred)) &(l)->head; \
+               (l)->tail.pred = (TYPEOF_OR_VOIDPTR((l)->tail.pred)) &(l)->head; \
        } while (0)
 
 #ifdef _DEBUG
@@ -276,6 +299,23 @@ typedef struct _List
 /*! 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 lower priority or appended to
+ * the tail if no such node exists.
+ */
+#define LIST_ENQUEUE(list, node) \
+       do { \
+               PriNode *ln; \
+               FOREACH_NODE(ln, (list)) \
+                       if (ln->pri < (node)->pri) \
+                               break; \
+               INSERT_BEFORE(&(node)->link, &ln->link); \
+       } while (0)
+
+
 /*!
  * Unlink a node from the head of the list \a l.
  *
@@ -316,11 +356,4 @@ INLINE Node *list_remTail(List *l)
        return n;
 }
 
-/* OBSOLETE names */
-#define REMHEAD list_remHead
-#define REMTAIL list_remTail
-#define INSERTBEFORE INSERT_BEFORE
-#define ISLISTEMPTY LIST_EMPTY
-
-
 #endif /* MWARE_LIST_H */