[PATCH batadv 2/2] batman-adv: compat: add scoped_guard(spinlock_bh, ...) support

Sven Eckelmann <[email protected]>
Newsgroups org.open-mesh.lists.batman
Message-ID <[email protected]>
Signed-off-by: Sven Eckelmann <[email protected]>
---
 compat-include/linux/cleanup.h  | 48 +++++++++++++++++++++++++++++++++++++++++
 compat-include/linux/compiler.h | 15 +++++++++++++
 compat-include/linux/spinlock.h | 20 +++++++++++++++++
 3 files changed, 83 insertions(+)

diff --git a/compat-include/linux/cleanup.h b/compat-include/linux/cleanup.h
new file mode 100644
index 00000000..9cdb6466
--- /dev/null
+++ b/compat-include/linux/cleanup.h
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _NET_BATMAN_ADV_COMPAT_LINUX_CLEANUP_H_
+#define _NET_BATMAN_ADV_COMPAT_LINUX_CLEANUP_H_
+
+#include <linux/version.h>
+
+#if LINUX_VERSION_IS_LESS(6, 5, 0) && \
+    !(LINUX_VERSION_IS_GEQ(6, 1, 79) && LINUX_VERSION_IS_LESS(6, 2, 0))
+
+#include <linux/compiler.h>
+
+#define CLASS(_name, var)						\
+	class_##_name##_t var __cleanup(class_##_name##_destructor) =	\
+		class_##_name##_constructor
+
+#define scoped_guard(_name, args...)					\
+	for (CLASS(_name, scope)(args),					\
+	     *done = NULL; !done; done = (void *)1)
+
+#define __DEFINE_UNLOCK_GUARD(_name, _type, _unlock, ...)		\
+typedef struct {							\
+	_type *lock;							\
+	__VA_ARGS__;							\
+} class_##_name##_t;							\
+									\
+static inline void class_##_name##_destructor(class_##_name##_t *_T)	\
+{									\
+	if (_T->lock) { _unlock; }					\
+}
+
+#define __DEFINE_LOCK_GUARD_1(_name, _type, _lock)			\
+static inline class_##_name##_t class_##_name##_constructor(_type *l)	\
+{									\
+	class_##_name##_t _t = { .lock = l }, *_T = &_t;		\
+	_lock;								\
+	return _t;							\
+}
+
+#define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...)		\
+__DEFINE_UNLOCK_GUARD(_name, _type, _unlock, __VA_ARGS__)		\
+__DEFINE_LOCK_GUARD_1(_name, _type, _lock)
+
+#else
+#include_next <linux/cleanup.h>
+#endif /* LINUX_VERSION_IS_LESS(6, 5, 0) */
+
+#endif /* _NET_BATMAN_ADV_COMPAT_LINUX_CLEANUP_H_ */
diff --git a/compat-include/linux/compiler.h b/compat-include/linux/compiler.h
new file mode 100644
index 00000000..de7c09c4
--- /dev/null
+++ b/compat-include/linux/compiler.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _NET_BATMAN_ADV_COMPAT_LINUX_COMPILER_H_
+#define _NET_BATMAN_ADV_COMPAT_LINUX_COMPILER_H_
+
+#include <linux/version.h>
+#include_next <linux/compiler.h>
+
+#if LINUX_VERSION_IS_LESS(6, 5, 0)
+
+#define __cleanup(func)			__attribute__((__cleanup__(func)))
+
+#endif /* LINUX_VERSION_IS_LESS(6, 5, 0) */
+
+#endif	/* _NET_BATMAN_ADV_COMPAT_LINUX_COMPILER_H_ */
diff --git a/compat-include/linux/spinlock.h b/compat-include/linux/spinlock.h
new file mode 100644
index 00000000..71a333f8
--- /dev/null
+++ b/compat-include/linux/spinlock.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _NET_BATMAN_ADV_COMPAT_LINUX_SPINLOCK_H_
+#define _NET_BATMAN_ADV_COMPAT_LINUX_SPINLOCK_H_
+
+#include <linux/version.h>
+#include_next <linux/spinlock.h>
+
+#if LINUX_VERSION_IS_LESS(6, 15, 0) && \
+    !(LINUX_VERSION_IS_GEQ(6, 12, 37) && LINUX_VERSION_IS_LESS(6, 13, 0))
+
+#include <linux/cleanup.h>
+
+DEFINE_LOCK_GUARD_1(spinlock_bh, spinlock_t,
+		    spin_lock_bh(_T->lock),
+		    spin_unlock_bh(_T->lock))
+
+#endif /* LINUX_VERSION_IS_LESS(6, 15, 0) */
+
+#endif /* _NET_BATMAN_ADV_COMPAT_LINUX_SPINLOCK_H_ */

-- 
2.47.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.