[PATCH 1/4] Add single linked lists wl_slist similar to wl_list

alexl at redhat.com alexl at redhat.com
Thu May 23 10:31:21 PDT 2013


From: Alexander Larsson <alexl at redhat.com>

---
 src/wayland-util.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/wayland-util.h | 24 ++++++++++++++++++++
 2 files changed, 90 insertions(+)

diff --git a/src/wayland-util.c b/src/wayland-util.c
index 598ab42..c774a7e 100644
--- a/src/wayland-util.c
+++ b/src/wayland-util.c
@@ -89,6 +89,72 @@ wl_list_insert_list(struct wl_list *list, struct wl_list *other)
 }
 
 WL_EXPORT void
+wl_slist_init(struct wl_slist *list)
+{
+	list->next = list;
+}
+
+WL_EXPORT void
+wl_slist_insert(struct wl_slist *list, struct wl_slist *elm)
+{
+	elm->next = list->next;
+	list->next = elm;
+}
+
+WL_EXPORT void
+wl_slist_remove(struct wl_slist *list, struct wl_slist *elm)
+{
+	struct wl_slist *prev;
+
+	prev = list;
+	while (prev->next != elm && prev->next != list)
+		prev = prev->next;
+
+	if (prev->next == elm) {
+		prev->next = elm->next;
+		elm->next = NULL;
+	}
+}
+
+WL_EXPORT int
+wl_slist_length(const struct wl_slist *list)
+{
+	struct wl_slist *e;
+	int count;
+
+	count = 0;
+	e = list->next;
+	while (e != list) {
+		e = e->next;
+		count++;
+	}
+
+	return count;
+}
+
+WL_EXPORT int
+wl_slist_empty(const struct wl_slist *list)
+{
+	return list->next == list;
+}
+
+WL_EXPORT void
+wl_slist_insert_list(struct wl_slist *list, struct wl_slist *other)
+{
+	struct wl_slist *end;
+
+	if (wl_slist_empty(other))
+		return;
+
+	end = other;
+	while (end->next != other)
+		end = end->next;
+
+	end->next = list->next;
+	list->next = other->next;
+}
+
+WL_EXPORT void
 wl_array_init(struct wl_array *array)
 {
 	memset(array, 0, sizeof *array);
diff --git a/src/wayland-util.h b/src/wayland-util.h
index dbe05a5..40f2509 100644
--- a/src/wayland-util.h
+++ b/src/wayland-util.h
@@ -145,6 +145,30 @@ void wl_list_insert_list(struct wl_list *list, struct wl_list *other);
 	     pos = tmp,							\
 	     tmp = wl_container_of(pos->member.prev, tmp, member))
 
+struct wl_slist {
+	struct wl_slist *next;
+};
+
+void wl_slist_init(struct wl_slist *list);
+void wl_slist_insert(struct wl_slist *list, struct wl_slist *elm);
+void wl_slist_remove(struct wl_slist *list, struct wl_slist *elm);
+int wl_slist_length(const struct wl_slist *list);
+int wl_slist_empty(const struct wl_slist *list);
+void wl_slist_insert_list(struct wl_slist *list, struct wl_slist *other);
+
+#define wl_slist_for_each(pos, head, member)				\
+	for (pos = 0, pos = wl_container_of((head)->next, pos, member);	\
+	     &pos->member != (head);					\
+	     pos = wl_container_of(pos->member.next, pos, member))
+
+#define wl_slist_for_each_safe(pos, tmp, head, member)			\
+	for (pos = 0, tmp = 0, 						\
+	     pos = wl_container_of((head)->next, pos, member),		\
+	     tmp = wl_container_of((pos)->member.next, tmp, member);	\
+	     &pos->member != (head);					\
+	     pos = tmp,							\
+	     tmp = wl_container_of(pos->member.next, tmp, member))
+
 struct wl_array {
 	size_t size;
 	size_t alloc;
-- 
1.8.1.4



More information about the wayland-devel mailing list