added LIST_FOR_EACH_ENTRY_SAFE macro
[reactos.git] / reactos / include / wine / list.h
index 92b84d8..ce4c968 100644 (file)
-/*\r
- * Linked lists support\r
- *\r
- * Copyright (C) 2002 Alexandre Julliard\r
- *\r
- * This library is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU Lesser General Public\r
- * License as published by the Free Software Foundation; either\r
- * version 2.1 of the License, or (at your option) any later version.\r
- *\r
- * This library is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
- * Lesser General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Lesser General Public\r
- * License along with this library; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- */\r
-\r
-#ifndef __WINE_SERVER_LIST_H\r
-#define __WINE_SERVER_LIST_H\r
-\r
-struct list\r
-{\r
-    struct list *next;\r
-    struct list *prev;\r
-};\r
-\r
-/* Define a list like so:\r
- *\r
- *   struct gadget\r
- *   {\r
- *       struct list  entry;   <-- doesn't have to be the first item in the struct\r
- *       int          a, b;\r
- *   };\r
- *\r
- *   static struct list global_gadgets = LIST_INIT( global_gadgets );\r
- *\r
- * or\r
- *\r
- *   struct some_global_thing\r
- *   {\r
- *       struct list gadgets;\r
- *   };\r
- *\r
- *   list_init( &some_global_thing->gadgets );\r
- *\r
- * Manipulate it like this:\r
- *\r
- *   list_add_head( &global_gadgets, &new_gadget->entry );\r
- *   list_remove( &new_gadget->entry );\r
- *   list_add_after( &some_random_gadget->entry, &new_gadget->entry );\r
- *\r
- * And to iterate over it:\r
- *\r
- *   struct list *cursor;\r
- *   LIST_FOR_EACH( cursor, &global_gadgets )\r
- *   {\r
- *       struct gadget *gadget = LIST_ENTRY( cursor, struct gadget, entry );\r
- *   }\r
- *\r
- */\r
-\r
-/* add an element after the specified one */\r
-inline static void list_add_after( struct list *elem, struct list *to_add )\r
-{\r
-    to_add->next = elem->next;\r
-    to_add->prev = elem;\r
-    elem->next->prev = to_add;\r
-    elem->next = to_add;\r
-}\r
-\r
-/* add an element before the specified one */\r
-inline static void list_add_before( struct list *elem, struct list *to_add )\r
-{\r
-    to_add->next = elem;\r
-    to_add->prev = elem->prev;\r
-    elem->prev->next = to_add;\r
-    elem->prev = to_add;\r
-}\r
-\r
-/* add element at the head of the list */\r
-inline static void list_add_head( struct list *list, struct list *elem )\r
-{\r
-    list_add_after( list, elem );\r
-}\r
-\r
-/* add element at the tail of the list */\r
-inline static void list_add_tail( struct list *list, struct list *elem )\r
-{\r
-    list_add_before( list, elem );\r
-}\r
-\r
-/* remove an element from its list */\r
-inline static void list_remove( struct list *elem )\r
-{\r
-    elem->next->prev = elem->prev;\r
-    elem->prev->next = elem->next;\r
-}\r
-\r
-/* get the next element */\r
-inline static struct list *list_next( struct list *list, struct list *elem )\r
-{\r
-    struct list *ret = elem->next;\r
-    if (elem->next == list) ret = NULL;\r
-    return ret;\r
-}\r
-\r
-/* get the previous element */\r
-inline static struct list *list_prev( struct list *list, struct list *elem )\r
-{\r
-    struct list *ret = elem->prev;\r
-    if (elem->prev == list) ret = NULL;\r
-    return ret;\r
-}\r
-\r
-/* get the first element */\r
-inline static struct list *list_head( struct list *list )\r
-{\r
-    return list_next( list, list );\r
-}\r
-\r
-/* get the last element */\r
-inline static struct list *list_tail( struct list *list )\r
-{\r
-    return list_prev( list, list );\r
-}\r
-\r
-/* check if a list is empty */\r
-inline static int list_empty( struct list *list )\r
-{\r
-    return list->next == list;\r
-}\r
-\r
-/* initialize a list */\r
-inline static void list_init( struct list *list )\r
-{\r
-    list->next = list->prev = list;\r
-}\r
-\r
-/* iterate through the list */\r
-#define LIST_FOR_EACH(cursor,list) \\r
-    for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)\r
-\r
-/* macros for statically initialized lists */\r
-#define LIST_INIT(list)  { &(list), &(list) }\r
-\r
-/* get pointer to object containing list element */\r
-#define LIST_ENTRY(elem, type, field) \\r
-    ((type *)((char *)(elem) - (unsigned int)(&((type *)0)->field)))\r
-\r
-#endif  /* __WINE_SERVER_LIST_H */\r
+/*
+ * Linked lists support
+ *
+ * Copyright (C) 2002 Alexandre Julliard
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __WINE_SERVER_LIST_H
+#define __WINE_SERVER_LIST_H
+
+struct list
+{
+    struct list *next;
+    struct list *prev;
+};
+
+/* Define a list like so:
+ *
+ *   struct gadget
+ *   {
+ *       struct list  entry;   <-- doesn't have to be the first item in the struct
+ *       int          a, b;
+ *   };
+ *
+ *   static struct list global_gadgets = LIST_INIT( global_gadgets );
+ *
+ * or
+ *
+ *   struct some_global_thing
+ *   {
+ *       struct list gadgets;
+ *   };
+ *
+ *   list_init( &some_global_thing->gadgets );
+ *
+ * Manipulate it like this:
+ *
+ *   list_add_head( &global_gadgets, &new_gadget->entry );
+ *   list_remove( &new_gadget->entry );
+ *   list_add_after( &some_random_gadget->entry, &new_gadget->entry );
+ *
+ * And to iterate over it:
+ *
+ *   struct list *cursor;
+ *   LIST_FOR_EACH( cursor, &global_gadgets )
+ *   {
+ *       struct gadget *gadget = LIST_ENTRY( cursor, struct gadget, entry );
+ *   }
+ *
+ */
+
+/* add an element after the specified one */
+inline static void list_add_after( struct list *elem, struct list *to_add )
+{
+    to_add->next = elem->next;
+    to_add->prev = elem;
+    elem->next->prev = to_add;
+    elem->next = to_add;
+}
+
+/* add an element before the specified one */
+inline static void list_add_before( struct list *elem, struct list *to_add )
+{
+    to_add->next = elem;
+    to_add->prev = elem->prev;
+    elem->prev->next = to_add;
+    elem->prev = to_add;
+}
+
+/* add element at the head of the list */
+inline static void list_add_head( struct list *list, struct list *elem )
+{
+    list_add_after( list, elem );
+}
+
+/* add element at the tail of the list */
+inline static void list_add_tail( struct list *list, struct list *elem )
+{
+    list_add_before( list, elem );
+}
+
+/* remove an element from its list */
+inline static void list_remove( struct list *elem )
+{
+    elem->next->prev = elem->prev;
+    elem->prev->next = elem->next;
+}
+
+/* get the next element */
+inline static struct list *list_next( struct list *list, struct list *elem )
+{
+    struct list *ret = elem->next;
+    if (elem->next == list) ret = NULL;
+    return ret;
+}
+
+/* get the previous element */
+inline static struct list *list_prev( struct list *list, struct list *elem )
+{
+    struct list *ret = elem->prev;
+    if (elem->prev == list) ret = NULL;
+    return ret;
+}
+
+/* get the first element */
+inline static struct list *list_head( struct list *list )
+{
+    return list_next( list, list );
+}
+
+/* get the last element */
+inline static struct list *list_tail( struct list *list )
+{
+    return list_prev( list, list );
+}
+
+/* check if a list is empty */
+inline static int list_empty( struct list *list )
+{
+    return list->next == list;
+}
+
+/* initialize a list */
+inline static void list_init( struct list *list )
+{
+    list->next = list->prev = list;
+}
+
+/* iterate through the list */
+#define LIST_FOR_EACH(cursor,list) \
+    for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)
+
+/* iterate through the list, with safety against removal */
+#define LIST_FOR_EACH_SAFE(cursor, cursor2, list) \
+    for ((cursor) = (list)->next, (cursor2) = (cursor)->next; \
+         (cursor) != (list); \
+         (cursor) = (cursor2), (cursor2) = (cursor)->next)
+
+/* iterate through the list using a list entry */
+#define LIST_FOR_EACH_ENTRY(elem, list, type, field) \
+    for ((elem) = LIST_ENTRY((list)->next, type, field); \
+         &(elem)->field != (list); \
+         (elem) = LIST_ENTRY((elem)->field.next, type, field))
+
+#define LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, list, type, field) \
+    for ((cursor) = LIST_ENTRY((list)->next, type, field), \
+         (cursor2) = LIST_ENTRY((cursor)->field.next, type, field); \
+         &(cursor)->field != (list); \
+         (cursor) = (cursor2), \
+         (cursor2) = LIST_ENTRY((cursor)->field.next, type, field))
+
+/* macros for statically initialized lists */
+#define LIST_INIT(list)  { &(list), &(list) }
+
+/* get pointer to object containing list element */
+#define LIST_ENTRY(elem, type, field) \
+    ((type *)((char *)(elem) - (unsigned int)(&((type *)0)->field)))
+
+#endif  /* __WINE_SERVER_LIST_H */