diff mbox

[i-g-t,2/6] igt_aux: Add some list helpers from wayland

Message ID 1480084874-12943-1-git-send-email-abdiel.janulgue@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Abdiel Janulgue Nov. 25, 2016, 2:41 p.m. UTC
From: Lyude <lyude@redhat.com>

Since we're going to be using lists for keeping track of EDIDs we've
allocated on the chamelium, add some generic list helpers from the
wayland project.

Signed-off-by: Lyude <lyude@redhat.com>

Changes since v1:
- Rename list helpers to be more like those from the Linux kernel
---
 lib/igt_aux.c | 27 +++++++++++++++++++++++++++
 lib/igt_aux.h | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
diff mbox

Patch

diff --git a/lib/igt_aux.c b/lib/igt_aux.c
index b5ae854..56bddd6 100644
--- a/lib/igt_aux.c
+++ b/lib/igt_aux.c
@@ -1265,3 +1265,30 @@  double igt_stop_siglatency(struct igt_mean *result)
 
 	return mean;
 }
+
+void igt_list_init(struct igt_list *igt_list)
+{
+	igt_list->prev = igt_list;
+	igt_list->next = igt_list;
+}
+
+void igt_list_add(struct igt_list *igt_list, struct igt_list *elm)
+{
+	elm->prev = igt_list;
+	elm->next = igt_list->next;
+	igt_list->next = elm;
+	elm->next->prev = elm;
+}
+
+void igt_list_del(struct igt_list *elm)
+{
+	elm->prev->next = elm->next;
+	elm->next->prev = elm->prev;
+	elm->next = NULL;
+	elm->prev = NULL;
+}
+
+bool igt_list_empty(const struct igt_list *igt_list)
+{
+	return igt_list->next == igt_list;
+}
diff --git a/lib/igt_aux.h b/lib/igt_aux.h
index d4da499..9d1c44a 100644
--- a/lib/igt_aux.h
+++ b/lib/igt_aux.h
@@ -274,4 +274,42 @@  double igt_stop_siglatency(struct igt_mean *result);
 void igt_set_module_param(const char *name, const char *val);
 void igt_set_module_param_int(const char *name, int val);
 
+/*
+ * This list data structure is a verbatim copy from wayland-util.h from the
+ * Wayland project; except that wl_ prefix has been removed.
+ */
+
+struct igt_list {
+	struct igt_list *prev;
+	struct igt_list *next;
+};
+
+void igt_list_init(struct igt_list *list);
+void igt_list_add(struct igt_list *list, struct igt_list *elm);
+void igt_list_del(struct igt_list *elm);
+bool igt_list_empty(const struct igt_list *list);
+
+#ifdef __GNUC__
+#define container_of(ptr, sample, member)				\
+	(__typeof__(sample))((char *)(ptr)	-			\
+		 ((char *)&(sample)->member - (char *)(sample)))
+#else
+#define container_of(ptr, sample, member)				\
+	(void *)((char *)(ptr)	-				        \
+		 ((char *)&(sample)->member - (char *)(sample)))
+#endif
+
+#define igt_list_for_each(pos, head, member)				\
+	for (pos = 0, pos = container_of((head)->next, pos, member);	\
+	     &pos->member != (head);					\
+	     pos = container_of(pos->member.next, pos, member))
+
+#define igt_list_for_each_safe(pos, tmp, head, member)			\
+	for (pos = 0, tmp = 0, 						\
+	     pos = container_of((head)->next, pos, member),		\
+	     tmp = container_of((pos)->member.next, tmp, member);	\
+	     &pos->member != (head);					\
+	     pos = tmp,							\
+	     tmp = container_of(pos->member.next, tmp, member))
+
 #endif /* IGT_AUX_H */