ustream: prevent recursive calls to the read callback
[project/libubox.git] / list.h
diff --git a/list.h b/list.h
index 47ce33b51c45ec61cf90bad1cf182d67734eb2b7..6aa7b2abdc3d6a552b74dfc102070ba9b980ac5f 100644 (file)
--- a/list.h
+++ b/list.h
 #define        prefetch(x)
 
 #ifndef container_of
-#define container_of(ptr, type, member) (           \
-    (type *)( (char *)ptr - offsetof(type,member) ))
+#define container_of(ptr, type, member)                                        \
+       ({                                                              \
+               const __typeof__(((type *) NULL)->member) *__mptr = (ptr);      \
+               (type *) ((char *) __mptr - offsetof(type, member));    \
+       })
+#endif
+
+#ifndef container_of_safe
+#define container_of_safe(ptr, type, member)                                           \
+       ({                                                                              \
+               const __typeof__(((type *) NULL)->member) *__mptr = (ptr);              \
+               __mptr ? (type *)((char *) __mptr - offsetof(type, member)) : NULL;     \
+       })
 #endif
 
 struct list_head {
@@ -109,6 +120,8 @@ list_del_init(struct list_head *entry)
 #define        list_entry(ptr, type, field)    container_of(ptr, type, field)
 #define        list_first_entry(ptr, type, field)      list_entry((ptr)->next, type, field)
 #define        list_last_entry(ptr, type, field)       list_entry((ptr)->prev, type, field)
+#define        list_next_entry(pos, member)            list_entry((pos)->member.next, typeof(*(pos)), member)
+#define        list_entry_is_h(p, h, field)            (&p->field == (h))
 
 #define        list_for_each(p, head)                                          \
        for (p = (head)->next; p != (head); p = p->next)
@@ -117,17 +130,27 @@ list_del_init(struct list_head *entry)
        for (p = (head)->next, n = p->next; p != (head); p = n, n = p->next)
 
 #define list_for_each_entry(p, h, field)                               \
-       for (p = list_first_entry(h, typeof(*p), field); &p->field != (h); \
-           p = list_entry(p->field.next, typeof(*p), field))
+       for (p = list_first_entry(h, __typeof__(*p), field); &p->field != (h); \
+           p = list_entry(p->field.next, __typeof__(*p), field))
+
+#define list_for_each_entry_continue(p, h, field)                      \
+       for (p = list_next_entry(p, field);                             \
+            !list_entry_is_h(p, h, field);                             \
+            p = list_next_entry(p, field))
+
+#define list_for_each_entry_continue_reverse(p, h, field)              \
+       for (p = list_prev_entry(p, field);                             \
+            !list_entry_is_h(p, h, field);                             \
+            p = list_prev_entry(p, field))
 
 #define list_for_each_entry_safe(p, n, h, field)                       \
-       for (p = list_first_entry(h, typeof(*p), field),                \
-           n = list_entry(p->field.next, typeof(*p), field); &p->field != (h);\
-           p = n, n = list_entry(n->field.next, typeof(*n), field))
+       for (p = list_first_entry(h, __typeof__(*p), field),            \
+           n = list_entry(p->field.next, __typeof__(*p), field); &p->field != (h);\
+           p = n, n = list_entry(n->field.next, __typeof__(*n), field))
 
 #define        list_for_each_entry_reverse(p, h, field)                        \
-       for (p = list_last_entry(h, typeof(*p), field); &p->field != (h); \
-           p = list_entry(p->field.prev, typeof(*p), field))
+       for (p = list_last_entry(h, __typeof__(*p), field); &p->field != (h); \
+           p = list_entry(p->field.prev, __typeof__(*p), field))
 
 #define        list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = p->prev)
 #define        list_for_each_prev_safe(p, n, h) for (p = (h)->prev, n = p->prev; p != (h); p = n, n = p->prev)