+/*
+ * Macro: qe_element
+ * Function:
+ * Convert a queue_entry_t to a queue element pointer.
+ * Get a pointer to the user-defined element containing
+ * a given queue_entry_t
+ * Header:
+ * <type> * qe_element(queue_entry_t qe, <type>, field)
+ * qe - queue entry to convert
+ * <type> - what's in the queue (e.g., struct some_data)
+ * <field> - is the chain field in <type>
+ * Note:
+ * Do not use pointer types for <type>
+ */
+#define qe_element(qe, type, field) \
+ ((type *)((void *)((char *)(qe) - __offsetof(type, field))))
+
+/*
+ * Macro: qe_foreach
+ * Function:
+ * Iterate over each queue_entry_t structure.
+ * Generates a 'for' loop, setting 'qe' to
+ * each queue_entry_t in the queue.
+ * Header:
+ * qe_foreach(queue_entry_t qe, queue_t head)
+ * qe - iteration variable
+ * head - pointer to queue_head_t (head of queue)
+ * Note:
+ * This should only be used with Method 1 queue iteration (linkage chains)
+ */
+#define qe_foreach(qe, head) \
+ for (qe = (head)->next; qe != (head); qe = (qe)->next)
+
+/*
+ * Macro: qe_foreach_safe
+ * Function:
+ * Safely iterate over each queue_entry_t structure.
+ *
+ * Use this iterator macro if you plan to remove the
+ * queue_entry_t, qe, from the queue during the
+ * iteration.
+ * Header:
+ * qe_foreach_safe(queue_entry_t qe, queue_t head)
+ * qe - iteration variable
+ * head - pointer to queue_head_t (head of queue)
+ * Note:
+ * This should only be used with Method 1 queue iteration (linkage chains)
+ */
+#define qe_foreach_safe(qe, head) \
+ for (queue_entry_t _ne = ((head)->next)->next, \
+ __ ## qe ## _unused_shadow __unused = (qe = (head)->next); \
+ qe != (head); \
+ qe = _ne, _ne = (qe)->next)
+
+/*
+ * Macro: qe_foreach_element
+ * Function:
+ * Iterate over each _element_ in a queue
+ * where each queue_entry_t points to another
+ * queue_entry_t, i.e., managed by the [de|en]queue_head/
+ * [de|en]queue_tail / remqueue / etc. function.
+ * Header:
+ * qe_foreach_element(<type> *elt, queue_t head, <field>)
+ * elt - iteration variable
+ * <type> - what's in the queue (e.g., struct some_data)
+ * <field> - is the chain field in <type>
+ * Note:
+ * This should only be used with Method 1 queue iteration (linkage chains)
+ */
+#define qe_foreach_element(elt, head, field) \
+ for (elt = qe_element((head)->next, typeof(*(elt)), field); \
+ &((elt)->field) != (head); \
+ elt = qe_element((elt)->field.next, typeof(*(elt)), field))
+
+/*
+ * Macro: qe_foreach_element_safe
+ * Function:
+ * Safely iterate over each _element_ in a queue
+ * where each queue_entry_t points to another
+ * queue_entry_t, i.e., managed by the [de|en]queue_head/
+ * [de|en]queue_tail / remqueue / etc. function.
+ *
+ * Use this iterator macro if you plan to remove the
+ * element, elt, from the queue during the iteration.
+ * Header:
+ * qe_foreach_element_safe(<type> *elt, queue_t head, <field>)
+ * elt - iteration variable
+ * <type> - what's in the queue (e.g., struct some_data)
+ * <field> - is the chain field in <type>
+ * Note:
+ * This should only be used with Method 1 queue iteration (linkage chains)
+ */
+#define qe_foreach_element_safe(elt, head, field) \
+ for (typeof(*(elt)) *_nelt = qe_element(((head)->next)->next, typeof(*(elt)), field), \
+ *__ ## elt ## _unused_shadow __unused = \
+ (elt = qe_element((head)->next, typeof(*(elt)), field)); \
+ &((elt)->field) != (head); \
+ elt = _nelt, _nelt = qe_element((elt)->field.next, typeof(*(elt)), field)) \