/*
- * 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 <stdbool.h>
#include <stddef.h>
#include "util.h"
+#include "openvswitch/list.h"
-/* Doubly linked list head or element. */
-struct ovs_list {
- struct ovs_list *prev; /* Previous list element. */
- struct ovs_list *next; /* Next list element. */
-};
-
-#define LIST_INITIALIZER(LIST) { LIST, LIST }
+/* "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 *);
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. */
&(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) \
? 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. */
static inline void
list_poison(struct ovs_list *list)
{
- memset(list, 0xcc, sizeof *list);
+ *list = OVS_LIST_POISON;
}
/* Inserts 'elem' just before 'before'. */
}
/* 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
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.