summaryrefslogtreecommitdiff
path: root/src/basic/list.h
diff options
context:
space:
mode:
authorAlex Crawford <alex.crawford@coreos.com>2015-08-03 23:11:53 -0700
committerAlex Crawford <alex.crawford@coreos.com>2015-08-04 03:23:43 -0700
commitdbe465c93ea8a87da95919d3f0ee802865802cfa (patch)
tree1cc0d50e2a026e988a85672d531383f97776af7c /src/basic/list.h
parent58a2e9acd8216f55b004e5432f76a851f7b233c0 (diff)
basic: add LIST_INSERT_BEFORE
Similar in function to LIST_INSERT_AFTER, this will insert a new element into the list before the specified position. If the specified position is NULL, the element is added as the tail of the list.
Diffstat (limited to 'src/basic/list.h')
-rw-r--r--src/basic/list.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/basic/list.h b/src/basic/list.h
index 2939216adb..760abcdab3 100644
--- a/src/basic/list.h
+++ b/src/basic/list.h
@@ -123,6 +123,32 @@
} \
} while(false)
+/* Insert an item before another one (a = where, b = what) */
+#define LIST_INSERT_BEFORE(name,head,a,b) \
+ do { \
+ typeof(*(head)) **_head = &(head), *_a = (a), *_b = (b); \
+ assert(_b); \
+ if (!_a) { \
+ if (!*_head) { \
+ _b->name##_next = NULL; \
+ _b->name##_prev = NULL; \
+ *_head = _b; \
+ } else { \
+ typeof(*(head)) *_tail = (head); \
+ while (_tail->name##_next) \
+ _tail = _tail->name##_next; \
+ _b->name##_next = NULL; \
+ _b->name##_prev = _tail; \
+ _tail->name##_next = _b; \
+ } \
+ } else { \
+ if ((_b->name##_prev = _a->name##_prev)) \
+ _b->name##_prev->name##_next = _b; \
+ _b->name##_next = _a; \
+ _a->name##_prev = _b; \
+ } \
+ } while(false)
+
#define LIST_JUST_US(name,item) \
(!(item)->name##_prev && !(item)->name##_next) \