summary refs log tree commit diff stats
path: root/include/qemu/queue.h
diff options
context:
space:
mode:
authorStefan Hajnoczi <stefanha@redhat.com>2020-02-14 17:17:10 +0000
committerStefan Hajnoczi <stefanha@redhat.com>2020-02-22 08:26:47 +0000
commit195ed8cb365edeb0d0a70a2ffdeb7a073f9a8117 (patch)
tree5178e83f699e90927825a009012c9ff17047c12c /include/qemu/queue.h
parentca8c6b22754b0f17818b1d1910d31f0aa1a49cc7 (diff)
downloadfocaccia-qemu-195ed8cb365edeb0d0a70a2ffdeb7a073f9a8117.tar.gz
focaccia-qemu-195ed8cb365edeb0d0a70a2ffdeb7a073f9a8117.zip
qemu/queue.h: add QLIST_SAFE_REMOVE()
QLIST_REMOVE() assumes the element is in a list.  It also leaves the
element's linked list pointers dangling.

Introduce a safe version of QLIST_REMOVE() and convert open-coded
instances of this pattern.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Sergio Lopez <slp@redhat.com>
Message-id: 20200214171712.541358-4-stefanha@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Diffstat (limited to 'include/qemu/queue.h')
-rw-r--r--include/qemu/queue.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/include/qemu/queue.h b/include/qemu/queue.h
index fcecb70228..60e794a4e3 100644
--- a/include/qemu/queue.h
+++ b/include/qemu/queue.h
@@ -144,6 +144,20 @@ struct {                                                                \
         *(elm)->field.le_prev = (elm)->field.le_next;                   \
 } while (/*CONSTCOND*/0)
 
+/*
+ * Like QLIST_REMOVE() but safe to call when elm is not in a list
+ */
+#define QLIST_SAFE_REMOVE(elm, field) do {                              \
+        if ((elm)->field.le_prev != NULL) {                             \
+                if ((elm)->field.le_next != NULL)                       \
+                        (elm)->field.le_next->field.le_prev =           \
+                            (elm)->field.le_prev;                       \
+                *(elm)->field.le_prev = (elm)->field.le_next;           \
+                (elm)->field.le_next = NULL;                            \
+                (elm)->field.le_prev = NULL;                            \
+        }                                                               \
+} while (/*CONSTCOND*/0)
+
 #define QLIST_FOREACH(var, head, field)                                 \
         for ((var) = ((head)->lh_first);                                \
                 (var);                                                  \