reconnect.at: Run tests for Python 2 and 3.
[cascardo/ovs.git] / lib / list.h
index 15be0f8..f9c9d85 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2013 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2015 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "util.h"
 #include "openvswitch/list.h"
 
+/* "struct ovs_list" with pointers that will (probably) cause segfaults if
+ * dereferenced and, better yet, show up clearly in a debugger. */
+#define OVS_LIST_POISON \
+(struct ovs_list) { (struct ovs_list *) (uintptr_t) 0xccccccccccccccccULL, \
+                    (struct ovs_list *) (uintptr_t) 0xccccccccccccccccULL }
+
 static inline void list_init(struct ovs_list *);
 static inline void list_poison(struct ovs_list *);
 
@@ -33,7 +39,7 @@ static inline void list_splice(struct ovs_list *before, struct ovs_list *first,
 static inline void list_push_front(struct ovs_list *, struct ovs_list *);
 static inline void list_push_back(struct ovs_list *, struct ovs_list *);
 static inline void list_replace(struct ovs_list *, const struct ovs_list *);
-static inline void list_moved(struct ovs_list *);
+static inline void list_moved(struct ovs_list *, const struct ovs_list *orig);
 static inline void list_move(struct ovs_list *dst, struct ovs_list *src);
 
 /* List removal. */
@@ -56,7 +62,7 @@ static inline bool list_is_short(const struct ovs_list *);
          &(ITER)->MEMBER != (LIST);                                     \
          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER))
 #define LIST_FOR_EACH_CONTINUE(ITER, MEMBER, LIST)                      \
-    for (INIT_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER);             \
+    for (ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER);             \
          &(ITER)->MEMBER != (LIST);                                     \
          ASSIGN_CONTAINER(ITER, (ITER)->MEMBER.next, MEMBER))
 #define LIST_FOR_EACH_REVERSE(ITER, MEMBER, LIST)                       \
@@ -73,6 +79,9 @@ static inline bool list_is_short(const struct ovs_list *);
           ? INIT_CONTAINER(NEXT, (ITER)->MEMBER.next, MEMBER), 1   \
           : 0);                                                    \
          (ITER) = (NEXT))
+#define LIST_FOR_EACH_POP(ITER, MEMBER, LIST)                      \
+    while (!list_is_empty(LIST)                                    \
+           && (INIT_CONTAINER(ITER, list_pop_front(LIST), MEMBER), 1))
 \f
 /* Inline implementations. */
 
@@ -88,7 +97,7 @@ list_init(struct ovs_list *list)
 static inline void
 list_poison(struct ovs_list *list)
 {
-    memset(list, 0xcc, sizeof *list);
+    *list = OVS_LIST_POISON;
 }
 
 /* Inserts 'elem' just before 'before'. */
@@ -150,15 +159,21 @@ list_replace(struct ovs_list *element, const struct ovs_list *position)
 }
 
 /* Adjusts pointers around 'list' to compensate for 'list' having been moved
- * around in memory (e.g. as a consequence of realloc()).
+ * around in memory (e.g. as a consequence of realloc()), with original
+ * location 'orig'.
  *
- * This always works if 'list' is a member of a list, or if 'list' is the head
- * of a non-empty list.  It fails badly, however, if 'list' is the head of an
- * empty list; just use list_init() in that case. */
+ * ('orig' likely points to freed memory, but this function does not
+ * dereference 'orig', it only compares it to 'list'.  In a very pedantic
+ * language lawyer sense, this still yields undefined behavior, but it works
+ * with actual compilers.) */
 static inline void
-list_moved(struct ovs_list *list)
+list_moved(struct ovs_list *list, const struct ovs_list *orig)
 {
-    list->prev->next = list->next->prev = list;
+    if (list->next == orig) {
+        list_init(list);
+    } else {
+        list->prev->next = list->next->prev = list;
+    }
 }
 
 /* Initializes 'dst' with the contents of 'src', compensating for moving it
@@ -167,12 +182,8 @@ list_moved(struct ovs_list *list)
 static inline void
 list_move(struct ovs_list *dst, struct ovs_list *src)
 {
-    if (!list_is_empty(src)) {
-        *dst = *src;
-        list_moved(dst);
-    } else {
-        list_init(dst);
-    }
+    *dst = *src;
+    list_moved(dst, src);
 }
 
 /* Removes 'elem' from its list and returns the element that followed it.