Blame SOURCES/0026-Add-a-generic-list-implementation.patch

2eae47
From 31a5eee990d64a98a071ce74cd4d53190c0fe94b Mon Sep 17 00:00:00 2001
2eae47
From: Eugene Syromyatnikov <evgsyr@gmail.com>
2eae47
Date: Sun, 11 Sep 2016 12:11:45 +0300
2eae47
Subject: [PATCH 26/27] Add a generic list implementation
2eae47
2eae47
Similar to the one used in the Linux kernel.
2eae47
2eae47
* macros.h (cast_ptr, containerof): New macros.
2eae47
* list.h: New file.
2eae47
* Makefile.am (strace_SOURCES): Add it.
2eae47
---
2eae47
 Makefile.am |   1 +
2eae47
 list.h      | 300 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2eae47
 macros.h    |  19 ++++
2eae47
 3 files changed, 320 insertions(+)
2eae47
 create mode 100644 list.h
2eae47
2eae47
diff --git a/Makefile.am b/Makefile.am
2eae47
index f6679a3..f252bda 100644
2eae47
--- a/Makefile.am
2eae47
+++ b/Makefile.am
2eae47
@@ -165,6 +165,7 @@ strace_SOURCES =	\
2eae47
 	linux/asm_stat.h \
2eae47
 	linux/x32/asm_stat.h \
2eae47
 	linux/x86_64/asm_stat.h \
2eae47
+	list.h		\
2eae47
 	listen.c	\
2eae47
 	lookup_dcookie.c \
2eae47
 	loop.c		\
2eae47
diff --git a/list.h b/list.h
2eae47
new file mode 100644
2eae47
index 0000000..cf68fa7
2eae47
--- /dev/null
2eae47
+++ b/list.h
2eae47
@@ -0,0 +1,300 @@
2eae47
+/*
2eae47
+ * Some simple implementation of lists similar to the one used in the kernel.
2eae47
+ *
2eae47
+ * Copyright (c) 2016-2019 The strace developers.
2eae47
+ * All rights reserved.
2eae47
+ *
2eae47
+ * SPDX-License-Identifier: LGPL-2.1-or-later
2eae47
+ */
2eae47
+
2eae47
+#ifndef STRACE_LIST_H
2eae47
+#define STRACE_LIST_H
2eae47
+
2eae47
+/*
2eae47
+ * struct list_item and related macros and functions provide an interface
2eae47
+ * for manipulating and iterating linked lists.  In order to have list
2eae47
+ * associated with its payload, struct list_item has to be embedded into
2eae47
+ * a structure type representing payload, and (optionally) an additional
2eae47
+ * struct list_item should be added somewhere as a starting point for list
2eae47
+ * iteration (creating a list with a designated head). A situation where
2eae47
+ * no designated head exists, and each embedded struct list_head is considered
2eae47
+ * a head (i.e. starting point for list iteration), is also possible.
2eae47
+ *
2eae47
+ * List head has to be initialised with list_init() call. Statically allocated
2eae47
+ * list heads can also be defined with an EMPTY_LIST() macro.
2eae47
+ *
2eae47
+ * In order to get a pointer to list item from a struct list_item, list_elem
2eae47
+ * macro is used.
2eae47
+ *
2eae47
+ * When a designated head is used, list_head() and list_tail() can be used
2eae47
+ * for getting pointer to the first and the last list item, respectively.
2eae47
+ *
2eae47
+ * list_next() and list_prev() macros can be used for obtaining pointers
2eae47
+ * to the next and the previous items in the list, respectively.  Note that
2eae47
+ * they do not perform additional checks for the validity of these pointers,
2eae47
+ * so they have to be guarded with respective list_head/list_tail checks in case
2eae47
+ * of lists with designated heads (where the list's head is not embedded withing
2eae47
+ * a list item.
2eae47
+ *
2eae47
+ * list_{insert,append,remove,remove_tail,remove_head,replace} provide some
2eae47
+ * basic means of list manipulation.
2eae47
+ *
2eae47
+ * list_foreach() and list_foreach_safe() are wrapper macros for simplifying
2eae47
+ * iteration over a list, with the latter having an additional argument
2eae47
+ * for storing temporary pointer, thus allowing list manipulations during
2eae47
+ * its iteration.
2eae47
+ *
2eae47
+ * A simple example:
2eae47
+ *
2eae47
+ *     struct my_struct {
2eae47
+ *             int a;
2eae47
+ *             struct list_item l1;
2eae47
+ *             struct list_item l2;
2eae47
+ *     };
2eae47
+ *
2eae47
+ *     EMPTY_LIST(list_1);              <--- Defining a designated head for list
2eae47
+ *
2eae47
+ *     struct my_struct *item =
2eae47
+ *             calloc(1, sizeof(*item));
2eae47
+ *     list_init(&item->l2);            <--- Initialising structure field that
2eae47
+ *                                           is used for lists without designated
2eae47
+ *                                           head.
2eae47
+ *     list_insert(&list_1, &item->l1); <--- Inserting an item into the list
2eae47
+ *
2eae47
+ *     item = calloc(1, sizeof(*item));
2eae47
+ *     list_init(&item->l2);
2eae47
+ *
2eae47
+ *     list_append(&(list_head(list_1, struct my_struct, l1)->l2), &item->l2);
2eae47
+ *
2eae47
+ *     struct my_struct *cur = item;    <--- Iteration over a headless list
2eae47
+ *     do {
2eae47
+ *             printf("%d\n", cur->a);
2eae47
+ *     } while ((cur = list_next(&cur, l2)) != item);
2eae47
+ *
2eae47
+ *     struct my_struct *i;
2eae47
+ *     list_foreach(i, list_1, l1) {    <--- Iteration over list_1 without list
2eae47
+ *             printf("%d\n", i->a);         modification
2eae47
+ *     }
2eae47
+ *
2eae47
+ *     struct my_struct *tmp;           <--- Iteration with modification
2eae47
+ *     list_foreach_safe(i, list_1, l1, tmp) {
2eae47
+ *             list_remove(&i->l1);
2eae47
+ *             free(i);
2eae47
+ *     }
2eae47
+ *
2eae47
+ * See also:
2eae47
+ *     "Linux kernel design patterns - part 2", section "Linked Lists"
2eae47
+ *     https://lwn.net/Articles/336255/
2eae47
+ */
2eae47
+
2eae47
+#include "macros.h"
2eae47
+
2eae47
+struct list_item {
2eae47
+	struct list_item *prev;
2eae47
+	struct list_item *next;
2eae47
+};
2eae47
+
2eae47
+/**
2eae47
+ * Define an empty list head.
2eae47
+ *
2eae47
+ * @param l_ List head variable name.
2eae47
+ */
2eae47
+#define EMPTY_LIST(l_) struct list_item l_ = { &l_, &l_ }
2eae47
+
2eae47
+/** Initialise an empty list. */
2eae47
+static inline void
2eae47
+list_init(struct list_item *l)
2eae47
+{
2eae47
+	l->prev = l;
2eae47
+	l->next = l;
2eae47
+}
2eae47
+
2eae47
+/** Check whether list is empty. */
2eae47
+static inline bool
2eae47
+list_is_empty(const struct list_item *l)
2eae47
+{
2eae47
+	return ((l->next == l) && (l->prev == l))
2eae47
+		/*
2eae47
+		 * XXX This could be the case when struct list_item hasn't been
2eae47
+		 *     initialised at all; we should probably also call some
2eae47
+		 *     errror_func_msg() in that case, as it looks like sloppy
2eae47
+		 *     programming.
2eae47
+		 */
2eae47
+		|| (!l->next && !l->prev);
2eae47
+}
2eae47
+
2eae47
+/**
2eae47
+ * Convert a pointer to a struct list_item to a pointer to a list item.
2eae47
+ *
2eae47
+ * @param var   Pointer to struct list_item.
2eae47
+ * @param type  Type of the list's item.
2eae47
+ * @param field Name of the field that holds the respective struct list_item.
2eae47
+ */
2eae47
+#define list_elem(var, type, field) containerof((var), type, field)
2eae47
+
2eae47
+/**
2eae47
+ * Get the first element in a list.
2eae47
+ *
2eae47
+ * @param head  Pointer to the list's head.
2eae47
+ * @param type  Type of the list's item.
2eae47
+ * @param field Name of the field that holds the respective struct list_item.
2eae47
+ */
2eae47
+#define list_head(head, type, field) \
2eae47
+	(list_is_empty(head) ? NULL : list_elem((head)->next, type, field))
2eae47
+/**
2eae47
+ * Get the last element in a list.
2eae47
+ *
2eae47
+ * @param head  Pointer to the list's head.
2eae47
+ * @param type  Type of the list's item.
2eae47
+ * @param field Name of the field that holds the respective struct list_item.
2eae47
+ */
2eae47
+#define list_tail(head, type, field) \
2eae47
+	(list_is_empty(head) ? NULL : list_elem((head)->prev, type, field))
2eae47
+
2eae47
+/**
2eae47
+ * Get the next element in a list.
2eae47
+ *
2eae47
+ * @param var   Pointer to a list item.
2eae47
+ * @param field Name of the field that holds the respective struct list_item.
2eae47
+ */
2eae47
+#define list_next(var, field) \
2eae47
+	list_elem((var)->field.next, typeof(*(var)), field)
2eae47
+/**
2eae47
+ * Get the previous element in a list.
2eae47
+ *
2eae47
+ * @param var   Pointer to a list item.
2eae47
+ * @param field Name of the field that holds the respective struct list_item.
2eae47
+ */
2eae47
+#define list_prev(var, field) \
2eae47
+	list_elem((var)->field.prev, typeof(*(var)), field)
2eae47
+
2eae47
+/**
2eae47
+ * Insert an item into a list. The item is placed as the next list item
2eae47
+ * to the head.
2eae47
+ */
2eae47
+static inline void
2eae47
+list_insert(struct list_item *head, struct list_item *item)
2eae47
+{
2eae47
+	item->next = head->next;
2eae47
+	item->prev = head;
2eae47
+	head->next->prev = item;
2eae47
+	head->next = item;
2eae47
+}
2eae47
+
2eae47
+/**
2eae47
+ * Insert an item into a list. The item is placed as the previous list item
2eae47
+ * to the head.
2eae47
+ */
2eae47
+static inline void
2eae47
+list_append(struct list_item *head, struct list_item *item)
2eae47
+{
2eae47
+	item->next = head;
2eae47
+	item->prev = head->prev;
2eae47
+	head->prev->next = item;
2eae47
+	head->prev = item;
2eae47
+}
2eae47
+
2eae47
+/**
2eae47
+ * Remove an item from a list.
2eae47
+ *
2eae47
+ * @param item Pointer to struct list_item of the item to be removed.
2eae47
+ * @return     Whether the action has been performed.
2eae47
+ */
2eae47
+static inline bool
2eae47
+list_remove(struct list_item *item)
2eae47
+{
2eae47
+	if (!item->next || !item->prev || list_is_empty(item))
2eae47
+		return false;
2eae47
+
2eae47
+	item->prev->next = item->next;
2eae47
+	item->next->prev = item->prev;
2eae47
+	item->next = item->prev = item;
2eae47
+
2eae47
+	return true;
2eae47
+}
2eae47
+
2eae47
+/**
2eae47
+ * Remove the last element of a list.
2eae47
+ *
2eae47
+ * @param head Pointer to the list's head.
2eae47
+ * @return     Pointer to struct list_item removed from the list;
2eae47
+ *             or NULL, if the list is empty.
2eae47
+ */
2eae47
+static inline struct list_item *
2eae47
+list_remove_tail(struct list_item *head)
2eae47
+{
2eae47
+	struct list_item *t = list_is_empty(head) ? NULL : head->prev;
2eae47
+
2eae47
+	if (t)
2eae47
+		list_remove(t);
2eae47
+
2eae47
+	return t;
2eae47
+}
2eae47
+
2eae47
+/**
2eae47
+ * Remove the first element of a list.
2eae47
+ *
2eae47
+ * @param head Pointer to the list's head.
2eae47
+ * @return     Pointer to struct list_item removed from the list;
2eae47
+ *             or NULL, if the list is empty.
2eae47
+ */
2eae47
+static inline struct list_item *
2eae47
+list_remove_head(struct list_item *head)
2eae47
+{
2eae47
+	struct list_item *h = list_is_empty(head) ? NULL : head->next;
2eae47
+
2eae47
+	if (h)
2eae47
+		list_remove(h);
2eae47
+
2eae47
+	return h;
2eae47
+}
2eae47
+
2eae47
+/**
2eae47
+ * Replace an old struct list_item in a list with the new one.
2eae47
+ *
2eae47
+ * @param old Pointer to struct list_item of the item to be replaced.
2eae47
+ * @param new Pointer to struct list_item of the item to be replaced with.
2eae47
+ * @return    Whether the replacement has been performed.
2eae47
+ */
2eae47
+static inline bool
2eae47
+list_replace(struct list_item *old, struct list_item *new)
2eae47
+{
2eae47
+	if (!old->next || !old->prev || list_is_empty(old))
2eae47
+		return false;
2eae47
+
2eae47
+	new->next = old->next;
2eae47
+	new->prev = old->prev;
2eae47
+	old->prev->next = new;
2eae47
+	old->next->prev = new;
2eae47
+	old->next = old->prev = old;
2eae47
+
2eae47
+	return true;
2eae47
+}
2eae47
+
2eae47
+/**
2eae47
+ * List iteration wrapper for non-destructive operations.
2eae47
+ *
2eae47
+ * @param var_   Variable holding pointer to a current list item.
2eae47
+ * @param head_  Pointer to the list's head.
2eae47
+ * @param field_ Name of the field containing the respective struct list_item
2eae47
+ *               inside list items.
2eae47
+ */
2eae47
+#define list_foreach(var_, head_, field_) \
2eae47
+	for (var_ = list_elem((head_)->next, typeof(*var_), field_); \
2eae47
+	    &(var_->field_) != (head_); var_ = list_next(var_, field_))
2eae47
+
2eae47
+/**
2eae47
+ * List iteration wrapper for destructive operations.
2eae47
+ *
2eae47
+ * @param var_   Variable holding pointer to a current list item.
2eae47
+ * @param head_  Pointer to the list's head.
2eae47
+ * @param field_ Name of the field containing the respective struct list_item
2eae47
+ *               inside list items.
2eae47
+ * @param _tmp   Temporary variable for storing pointer to the next item.
2eae47
+ */
2eae47
+#define list_foreach_safe(var_, head_, field_, _tmp) \
2eae47
+	for (var_ = list_elem((head_)->next, typeof(*var_), field_), \
2eae47
+	    _tmp = list_elem((var_)->field_.next, typeof(*var_), field_); \
2eae47
+	    &var_->field_ != head_; var_ = _tmp, _tmp = list_next(_tmp, field_))
2eae47
+
2eae47
+#endif /* !STRACE_LIST_H */
2eae47
diff --git a/macros.h b/macros.h
2eae47
index 9346d90..6951928 100644
2eae47
--- a/macros.h
2eae47
+++ b/macros.h
2eae47
@@ -33,6 +33,25 @@
2eae47
 	(offsetof(type_, member_) + sizeof(((type_ *)0)->member_))
2eae47
 #endif
2eae47
 
2eae47
+#ifndef cast_ptr
2eae47
+# define cast_ptr(type_, var_)	\
2eae47
+	((type_) (uintptr_t) (const volatile void *) (var_))
2eae47
+#endif
2eae47
+
2eae47
+#ifndef containerof
2eae47
+/**
2eae47
+ * Return a pointer to a structure that contains the provided variable.
2eae47
+ *
2eae47
+ * @param ptr_    Pointer to data that is a field of the container structure.
2eae47
+ * @param struct_ Type of the container structure.
2eae47
+ * @param member_ Name of the member field.
2eae47
+ * @return  Pointer to the container structure.
2eae47
+ */
2eae47
+# define containerof(ptr_, struct_, member_)	\
2eae47
+	cast_ptr(struct_ *,			\
2eae47
+		 (const volatile char *) (ptr_) - offsetof(struct_, member_))
2eae47
+#endif
2eae47
+
2eae47
 static inline bool
2eae47
 is_filled(const char *ptr, char fill, size_t size)
2eae47
 {
2eae47
-- 
2eae47
2.1.4
2eae47