Blame SOURCES/strace-rhbz1610774-0002-Add-a-generic-list-implementation.patch

d4bfbf
From a5855e84f17181167754caa26280161cd786a386 Mon Sep 17 00:00:00 2001
d4bfbf
From: Eugene Syromyatnikov <evgsyr@gmail.com>
d4bfbf
Date: Sun, 11 Sep 2016 12:11:45 +0300
d4bfbf
Subject: [PATCH 2/3] Add a generic list implementation
d4bfbf
d4bfbf
Similar to one used in the Linux kernel.
d4bfbf
d4bfbf
* macros.h (cast_ptr, containerof): New macros.
d4bfbf
* list.h: New file.
d4bfbf
* Makefile.am (strace_SOURCES): Add it.
d4bfbf
---
d4bfbf
 Makefile.am |   1 +
d4bfbf
 list.h      | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
d4bfbf
 macros.h    |   9 ++++
d4bfbf
 3 files changed, 147 insertions(+)
d4bfbf
 create mode 100644 list.h
d4bfbf
d4bfbf
Index: strace-4.24/Makefile.am
d4bfbf
===================================================================
d4bfbf
--- strace-4.24.orig/Makefile.am	2018-09-13 00:07:30.798555060 +0200
d4bfbf
+++ strace-4.24/Makefile.am	2018-09-13 00:42:10.058675213 +0200
d4bfbf
@@ -185,6 +185,7 @@
d4bfbf
 	linux/asm_stat.h \
d4bfbf
 	linux/x32/asm_stat.h \
d4bfbf
 	linux/x86_64/asm_stat.h \
d4bfbf
+	list.h		\
d4bfbf
 	listen.c	\
d4bfbf
 	lookup_dcookie.c \
d4bfbf
 	loop.c		\
d4bfbf
Index: strace-4.24/list.h
d4bfbf
===================================================================
d4bfbf
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
d4bfbf
+++ strace-4.24/list.h	2018-09-13 00:42:10.059675201 +0200
d4bfbf
@@ -0,0 +1,137 @@
d4bfbf
+/*
d4bfbf
+ * Some simple implementation of a list similar to one used in the kernel.
d4bfbf
+ *
d4bfbf
+ * Copyright (c) 2016-2018 The strace developers.
d4bfbf
+ * All rights reserved.
d4bfbf
+ *
d4bfbf
+ * Redistribution and use in source and binary forms, with or without
d4bfbf
+ * modification, are permitted provided that the following conditions
d4bfbf
+ * are met:
d4bfbf
+ * 1. Redistributions of source code must retain the above copyright
d4bfbf
+ *    notice, this list of conditions and the following disclaimer.
d4bfbf
+ * 2. Redistributions in binary form must reproduce the above copyright
d4bfbf
+ *    notice, this list of conditions and the following disclaimer in the
d4bfbf
+ *    documentation and/or other materials provided with the distribution.
d4bfbf
+ * 3. The name of the author may not be used to endorse or promote products
d4bfbf
+ *    derived from this software without specific prior written permission.
d4bfbf
+ *
d4bfbf
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
d4bfbf
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
d4bfbf
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
d4bfbf
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
d4bfbf
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
d4bfbf
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
d4bfbf
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
d4bfbf
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
d4bfbf
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
d4bfbf
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d4bfbf
+ */
d4bfbf
+
d4bfbf
+#ifndef STRACE_LIST_H
d4bfbf
+#define STRACE_LIST_H
d4bfbf
+
d4bfbf
+#include "macros.h"
d4bfbf
+
d4bfbf
+struct list_item {
d4bfbf
+	struct list_item *prev;
d4bfbf
+	struct list_item *next;
d4bfbf
+};
d4bfbf
+
d4bfbf
+#define EMPTY_LIST(l_) struct list_item l_ = { &l_, &l_ }
d4bfbf
+
d4bfbf
+static inline void
d4bfbf
+list_init(struct list_item *l)
d4bfbf
+{
d4bfbf
+	l->prev = l;
d4bfbf
+	l->next = l;
d4bfbf
+}
d4bfbf
+
d4bfbf
+static inline bool
d4bfbf
+list_is_empty(struct list_item *l)
d4bfbf
+{
d4bfbf
+	return (l->next == l) && (l->prev == l);
d4bfbf
+}
d4bfbf
+
d4bfbf
+#define list_elem(var, type, field) containerof((var), type, field)
d4bfbf
+
d4bfbf
+#define list_head(head, type, field) \
d4bfbf
+	(list_is_empty(head) ? NULL : list_elem((head)->next, type, field))
d4bfbf
+#define list_tail(head, type, field) \
d4bfbf
+	(list_is_empty(head) ? NULL : list_elem((head)->prev, type, field))
d4bfbf
+
d4bfbf
+#define list_next(val, field) \
d4bfbf
+	list_elem((val)->field.next, typeof(*(val)), field)
d4bfbf
+#define list_prev(val, field) \
d4bfbf
+	list_elem((val)->field.prev, typeof(*(val)), field)
d4bfbf
+
d4bfbf
+static inline void
d4bfbf
+list_insert(struct list_item *head, struct list_item *item)
d4bfbf
+{
d4bfbf
+	item->next = head->next;
d4bfbf
+	item->prev = head;
d4bfbf
+	head->next->prev = item;
d4bfbf
+	head->next = item;
d4bfbf
+}
d4bfbf
+
d4bfbf
+static inline void
d4bfbf
+list_append(struct list_item *head, struct list_item *item)
d4bfbf
+{
d4bfbf
+	item->next = head;
d4bfbf
+	item->prev = head->prev;
d4bfbf
+	head->prev->next = item;
d4bfbf
+	head->prev = item;
d4bfbf
+}
d4bfbf
+
d4bfbf
+static inline void
d4bfbf
+list_remove(struct list_item *item)
d4bfbf
+{
d4bfbf
+	if (!item->next || !item->prev)
d4bfbf
+		return;
d4bfbf
+
d4bfbf
+	item->prev->next = item->next;
d4bfbf
+	item->next->prev = item->prev;
d4bfbf
+	item->next = item->prev = NULL;
d4bfbf
+}
d4bfbf
+
d4bfbf
+static inline struct list_item *
d4bfbf
+list_remove_tail(struct list_item *head)
d4bfbf
+{
d4bfbf
+	struct list_item *t = list_is_empty(head) ? NULL : head->prev;
d4bfbf
+
d4bfbf
+	if (t)
d4bfbf
+		list_remove(t);
d4bfbf
+
d4bfbf
+	return t;
d4bfbf
+}
d4bfbf
+
d4bfbf
+static inline struct list_item *
d4bfbf
+list_remove_head(struct list_item *head)
d4bfbf
+{
d4bfbf
+	struct list_item *h = list_is_empty(head) ? NULL : head->next;
d4bfbf
+
d4bfbf
+	if (h)
d4bfbf
+		list_remove(h);
d4bfbf
+
d4bfbf
+	return h;
d4bfbf
+}
d4bfbf
+
d4bfbf
+static inline void
d4bfbf
+list_replace(struct list_item *old, struct list_item *new)
d4bfbf
+{
d4bfbf
+	new->next = old->next;
d4bfbf
+	new->prev = old->prev;
d4bfbf
+	old->prev->next = new;
d4bfbf
+	old->next->prev = new;
d4bfbf
+	old->next = old->prev = NULL;
d4bfbf
+}
d4bfbf
+
d4bfbf
+#define list_foreach(var_, head_, field_) \
d4bfbf
+	for (var_ = list_elem((head_)->next, typeof(*var_), field_); \
d4bfbf
+	    &(var_->field_) != (head_); var_ = list_next(var_, field_))
d4bfbf
+
d4bfbf
+#define list_foreach_safe(var_, head_, field_, _tmp) \
d4bfbf
+	for (var_ = list_elem((head_)->next, typeof(*var_), field_), \
d4bfbf
+	    _tmp = list_elem((var_)->field_.next, typeof(*var_), field_); \
d4bfbf
+	    &var_->field_ != head_; var_ = _tmp, _tmp = list_next(_tmp, field_))
d4bfbf
+
d4bfbf
+#endif /* !STRACE_LIST_H */
d4bfbf
Index: strace-4.24/macros.h
d4bfbf
===================================================================
d4bfbf
--- strace-4.24.orig/macros.h	2018-09-13 00:07:30.798555060 +0200
d4bfbf
+++ strace-4.24/macros.h	2018-09-13 00:42:10.059675201 +0200
d4bfbf
@@ -53,6 +53,15 @@
d4bfbf
 	(offsetof(type_, member_) + sizeof(((type_ *)0)->member_))
d4bfbf
 #endif
d4bfbf
 
d4bfbf
+#ifndef cast_ptr
d4bfbf
+# define cast_ptr(type, var) ((type) (uintptr_t) (const volatile void *) (var))
d4bfbf
+#endif
d4bfbf
+
d4bfbf
+#ifndef containerof
d4bfbf
+# define containerof(x, s, m)	\
d4bfbf
+	cast_ptr(s *, (const volatile char *) (x) - offsetof(s, m))
d4bfbf
+#endif
d4bfbf
+
d4bfbf
 static inline bool
d4bfbf
 is_filled(const char *ptr, char fill, size_t size)
d4bfbf
 {
d4bfbf
Index: strace-4.24/Makefile.in
d4bfbf
===================================================================
d4bfbf
--- strace-4.24.orig/Makefile.in	2018-08-14 02:44:37.000000000 +0200
d4bfbf
+++ strace-4.24/Makefile.in	2018-09-13 00:45:21.266213460 +0200
d4bfbf
@@ -340,7 +340,7 @@
d4bfbf
 	ipc_sem.c ipc_shm.c ipc_shmctl.c kcmp.c kernel_types.h kexec.c \
d4bfbf
 	keyctl.c keyctl_kdf_params.h kvm.c largefile_wrappers.h ldt.c \
d4bfbf
 	link.c linux/asm_stat.h linux/x32/asm_stat.h \
d4bfbf
-	linux/x86_64/asm_stat.h listen.c lookup_dcookie.c loop.c \
d4bfbf
+	linux/x86_64/asm_stat.h list.h listen.c lookup_dcookie.c loop.c \
d4bfbf
 	lseek.c macros.h mem.c membarrier.c memfd_create.c mknod.c \
d4bfbf
 	mmap_notify.c mmap_notify.h mmsghdr.c mount.c mpers_type.h \
d4bfbf
 	mq.c msghdr.c msghdr.h mtd.c native_defs.h negated_errno.h \
d4bfbf
@@ -1357,7 +1357,7 @@
d4bfbf
 	ipc_shmctl.c kcmp.c kernel_types.h kexec.c keyctl.c \
d4bfbf
 	keyctl_kdf_params.h kvm.c largefile_wrappers.h ldt.c link.c \
d4bfbf
 	linux/asm_stat.h linux/x32/asm_stat.h linux/x86_64/asm_stat.h \
d4bfbf
-	listen.c lookup_dcookie.c loop.c lseek.c macros.h mem.c \
d4bfbf
+	list.h listen.c lookup_dcookie.c loop.c lseek.c macros.h mem.c \
d4bfbf
 	membarrier.c memfd_create.c mknod.c mmap_notify.c \
d4bfbf
 	mmap_notify.h mmsghdr.c mount.c mpers_type.h mq.c msghdr.c \
d4bfbf
 	msghdr.h mtd.c native_defs.h negated_errno.h net.c netlink.c \