[i-g-t,2/6] igt_aux: Add some list helpers from the kernel
diff mbox

Message ID 1480074032-26818-1-git-send-email-abdiel.janulgue@linux.intel.com
State New
Headers show

Commit Message

Abdiel Janulgue Nov. 25, 2016, 11:40 a.m. UTC
Since we're going to be using lists for keeping track of spinners
add some generic list helpers from the kernel.

Signed-off-by: Abdiel Janulgue <abdiel.janulgue@linux.intel.com>
---
 lib/igt_aux.c | 39 +++++++++++++++++++++++++++++++++++++++
 lib/igt_aux.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

Comments

Chris Wilson Nov. 25, 2016, 11:52 a.m. UTC | #1
On Fri, Nov 25, 2016 at 01:40:31PM +0200, Abdiel Janulgue wrote:
> Since we're going to be using lists for keeping track of spinners
> add some generic list helpers from the kernel.
> 
> Signed-off-by: Abdiel Janulgue <abdiel.janulgue@linux.intel.com>
> ---
>  lib/igt_aux.c | 39 +++++++++++++++++++++++++++++++++++++++
>  lib/igt_aux.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 85 insertions(+)
> 
> diff --git a/lib/igt_aux.c b/lib/igt_aux.c
> index b5ae854..28af1d4 100644
> --- a/lib/igt_aux.c
> +++ b/lib/igt_aux.c
> @@ -1265,3 +1265,42 @@ double igt_stop_siglatency(struct igt_mean *result)
>  
>  	return mean;
>  }
> +
> +static void __list_add(struct list_head *new,
> +			      struct list_head *prev,
> +			      struct list_head *next)
> +{
> +	next->prev = new;
> +	new->next = next;
> +	new->prev = prev;
> +	prev->next = new;
> +}
> +
> +static void __list_del(struct list_head * prev, struct list_head * next)
> +{
> +	next->prev = prev;
> +        prev->next = next;
> +}
> +
> +void INIT_LIST_HEAD(struct list_head *list)
> +{
> +	list->next = list;
> +	list->prev = list;
> +}
> +
> +void list_add(struct list_head *new, struct list_head *head)
> +{
> +	__list_add(new, head, head->next);
> +}
> +
> +void list_del(struct list_head *entry)
> +{
> +	__list_del(entry->prev, entry->next);
> +	entry->next = NULL;
> +	entry->prev = NULL;

list_del in the absence of list_del_init() should reinit the list.

> +}
> +
> +bool list_empty(const struct list_head *head)
> +{
> +	return head->next == head;
> +}
> diff --git a/lib/igt_aux.h b/lib/igt_aux.h
> index d4da499..73836d0 100644
> --- a/lib/igt_aux.h
> +++ b/lib/igt_aux.h
> @@ -274,4 +274,50 @@ 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 derived from the Linux kerne's list.h
> + */
> +
> +struct list_head {
> +	struct list_head *next, *prev;
> +};
> +
> +#define LIST_HEAD_INIT(name) { &(name), &(name) }
> +
> +#define LIST_HEAD(name) \
> +	struct list_head name = LIST_HEAD_INIT(name)
> +
> +void INIT_LIST_HEAD(struct list_head *list);
> +
> +void list_add(struct list_head *new, struct list_head *head);
> +
> +void list_del(struct list_head *entry);
> +
> +bool list_empty(const struct list_head *head);
> +
> +#undef offsetof

#ifndef offsetof

> +#ifdef __compiler_offsetof
> +#define offsetof(TYPE, MEMBER)	__compiler_offsetof(TYPE, MEMBER)
> +#else
> +#define offsetof(TYPE, MEMBER)	((size_t)&((TYPE *)0)->MEMBER)
> +#endif
> +
> +#define container_of(ptr, type, member) ({			\
> +	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
> +	(type *)( (char *)__mptr - offsetof(type,member) );})
> +
> +#define list_entry(ptr, type, member) \
> +	container_of(ptr, type, member)
> +
> +#define list_next_entry(pos, member)				\
> +	list_entry((pos)->member.next, typeof(*(pos)), member)
> +
> +#define list_first_entry(ptr, type, member)	\
> +	list_entry((ptr)->next, type, member)
> +
> +#define list_for_each_entry(pos, head, member)				\
> +	for (pos = list_first_entry(head, typeof(*pos), member);	\
> +	     &pos->member != (head);					\
> +	     pos = list_next_entry(pos, member))

Since we can use c99, there is an argument for using mesa's util/list.h
instead (nicer loop iteration). Plus we don't have to wrangle over
copyright clauses as much.
-Chris
Chris Wilson Nov. 25, 2016, 11:54 a.m. UTC | #2
On Fri, Nov 25, 2016 at 01:40:31PM +0200, Abdiel Janulgue wrote:
> Since we're going to be using lists for keeping track of spinners
> add some generic list helpers from the kernel.

I should also say look at the chamelium patches so that we only have to
import a circular list once (i.e. make sure the interface is complete
enough for other patches in flight).
-Chris

Patch
diff mbox

diff --git a/lib/igt_aux.c b/lib/igt_aux.c
index b5ae854..28af1d4 100644
--- a/lib/igt_aux.c
+++ b/lib/igt_aux.c
@@ -1265,3 +1265,42 @@  double igt_stop_siglatency(struct igt_mean *result)
 
 	return mean;
 }
+
+static void __list_add(struct list_head *new,
+			      struct list_head *prev,
+			      struct list_head *next)
+{
+	next->prev = new;
+	new->next = next;
+	new->prev = prev;
+	prev->next = new;
+}
+
+static void __list_del(struct list_head * prev, struct list_head * next)
+{
+	next->prev = prev;
+        prev->next = next;
+}
+
+void INIT_LIST_HEAD(struct list_head *list)
+{
+	list->next = list;
+	list->prev = list;
+}
+
+void list_add(struct list_head *new, struct list_head *head)
+{
+	__list_add(new, head, head->next);
+}
+
+void list_del(struct list_head *entry)
+{
+	__list_del(entry->prev, entry->next);
+	entry->next = NULL;
+	entry->prev = NULL;
+}
+
+bool list_empty(const struct list_head *head)
+{
+	return head->next == head;
+}
diff --git a/lib/igt_aux.h b/lib/igt_aux.h
index d4da499..73836d0 100644
--- a/lib/igt_aux.h
+++ b/lib/igt_aux.h
@@ -274,4 +274,50 @@  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 derived from the Linux kerne's list.h
+ */
+
+struct list_head {
+	struct list_head *next, *prev;
+};
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define LIST_HEAD(name) \
+	struct list_head name = LIST_HEAD_INIT(name)
+
+void INIT_LIST_HEAD(struct list_head *list);
+
+void list_add(struct list_head *new, struct list_head *head);
+
+void list_del(struct list_head *entry);
+
+bool list_empty(const struct list_head *head);
+
+#undef offsetof
+#ifdef __compiler_offsetof
+#define offsetof(TYPE, MEMBER)	__compiler_offsetof(TYPE, MEMBER)
+#else
+#define offsetof(TYPE, MEMBER)	((size_t)&((TYPE *)0)->MEMBER)
+#endif
+
+#define container_of(ptr, type, member) ({			\
+	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
+	(type *)( (char *)__mptr - offsetof(type,member) );})
+
+#define list_entry(ptr, type, member) \
+	container_of(ptr, type, member)
+
+#define list_next_entry(pos, member)				\
+	list_entry((pos)->member.next, typeof(*(pos)), member)
+
+#define list_first_entry(ptr, type, member)	\
+	list_entry((ptr)->next, type, member)
+
+#define list_for_each_entry(pos, head, member)				\
+	for (pos = list_first_entry(head, typeof(*pos), member);	\
+	     &pos->member != (head);					\
+	     pos = list_next_entry(pos, member))
+
 #endif /* IGT_AUX_H */