diff mbox series

[v4,1/4] common: Move a few more standalone macros from xen/lib.h to xen/macros.h

Message ID 10ce452be68b4f83213f5199a3b84459a479bcda.1689710519.git.sanastasio@raptorengineering.com (mailing list archive)
State Superseded
Headers show
Series Early serial on Power | expand

Commit Message

Shawn Anastasio July 18, 2023, 8:20 p.m. UTC
Move a few more macros which have no dependencies on other headers from
xen/lib.h to xen/macros.h. Notably, this includes BUILD_BUG_ON* and
ARRAY_SIZE.
---
 xen/include/xen/lib.h    | 28 ----------------------------
 xen/include/xen/macros.h | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 28 deletions(-)

Comments

Jan Beulich July 19, 2023, 8:08 a.m. UTC | #1
On 18.07.2023 22:20, Shawn Anastasio wrote:
> Move a few more macros which have no dependencies on other headers from
> xen/lib.h to xen/macros.h. Notably, this includes BUILD_BUG_ON* and
> ARRAY_SIZE.
> ---

I would have wanted to put this directly on top of my patch, but
the submission lack S-o-b.

> --- a/xen/include/xen/macros.h
> +++ b/xen/include/xen/macros.h
> @@ -22,6 +22,38 @@
>  #define __STR(...) #__VA_ARGS__
>  #define STR(...) __STR(__VA_ARGS__)
>  
> +#ifndef __ASSEMBLY__
> +
> +/* All clang versions supported by Xen have _Static_assert. */
> +#if defined(__clang__) || \
> +    (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
> +/* Force a compilation error if condition is true */
> +#define BUILD_BUG_ON(cond) ({ _Static_assert(!(cond), "!(" #cond ")"); })
> +
> +/* Force a compilation error if condition is true, but also produce a
> +   result (of value 0 and type size_t), so the expression can be used
> +   e.g. in a structure initializer (or where-ever else comma expressions
> +   aren't permitted). */

While moving this, please correct comment style.

> +#define BUILD_BUG_ON_ZERO(cond) \
> +    (sizeof(struct { char c; _Static_assert(!(cond), "!(" #cond ")"); }) & 0)
> +#else
> +#define BUILD_BUG_ON_ZERO(cond) \
> +    (sizeof(struct { unsigned u : !(cond); }) & 0)
> +#define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond))
> +#endif
> +
> +#define ABS(_x) ({                              \
> +    typeof(_x) __x = (_x);                      \
> +    (__x < 0) ? -__x : __x;                     \
> +})
> +
> +#define SWAP(_a, _b) \
> +   do { typeof(_a) _t = (_a); (_a) = (_b); (_b) = _t; } while ( 0 )

While I won't insist here, it would be nice if we could also get rid of
the bogus leading underscores as these are moved. Arguments here don't
need any, while the local variables want to use trailing underscores,
like we do elsewhere.

With at least the earlier two items taken care of, feel free to also add
Reviewed-by: Jan Beulich <jbeulich@suse.com>

Jan
diff mbox series

Patch

diff --git a/xen/include/xen/lib.h b/xen/include/xen/lib.h
index dae2a1f085..359cfdc784 100644
--- a/xen/include/xen/lib.h
+++ b/xen/include/xen/lib.h
@@ -21,24 +21,6 @@ 
     unlikely(ret_warn_on_);             \
 })
 
-/* All clang versions supported by Xen have _Static_assert. */
-#if defined(__clang__) || \
-    (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
-/* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(cond) ({ _Static_assert(!(cond), "!(" #cond ")"); })
-
-/* Force a compilation error if condition is true, but also produce a
-   result (of value 0 and type size_t), so the expression can be used
-   e.g. in a structure initializer (or where-ever else comma expressions
-   aren't permitted). */
-#define BUILD_BUG_ON_ZERO(cond) \
-    (sizeof(struct { char c; _Static_assert(!(cond), "!(" #cond ")"); }) & 0)
-#else
-#define BUILD_BUG_ON_ZERO(cond) \
-    (sizeof(struct { unsigned u : !(cond); }) & 0)
-#define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond))
-#endif
-
 #ifndef NDEBUG
 #define ASSERT(p) \
     do { if ( unlikely(!(p)) ) assert_failed(#p); } while (0)
@@ -48,16 +30,6 @@ 
 #define ASSERT_UNREACHABLE() do { } while (0)
 #endif
 
-#define ABS(_x) ({                              \
-    typeof(_x) __x = (_x);                      \
-    (__x < 0) ? -__x : __x;                     \
-})
-
-#define SWAP(_a, _b) \
-   do { typeof(_a) _t = (_a); (_a) = (_b); (_b) = _t; } while ( 0 )
-
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
-
 #define __ACCESS_ONCE(x) ({                             \
             (void)(typeof(x))0; /* Scalar typecheck. */ \
             (volatile typeof(x) *)&(x); })
diff --git a/xen/include/xen/macros.h b/xen/include/xen/macros.h
index 7b92d34504..e2832b0e8a 100644
--- a/xen/include/xen/macros.h
+++ b/xen/include/xen/macros.h
@@ -22,6 +22,38 @@ 
 #define __STR(...) #__VA_ARGS__
 #define STR(...) __STR(__VA_ARGS__)
 
+#ifndef __ASSEMBLY__
+
+/* All clang versions supported by Xen have _Static_assert. */
+#if defined(__clang__) || \
+    (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
+/* Force a compilation error if condition is true */
+#define BUILD_BUG_ON(cond) ({ _Static_assert(!(cond), "!(" #cond ")"); })
+
+/* Force a compilation error if condition is true, but also produce a
+   result (of value 0 and type size_t), so the expression can be used
+   e.g. in a structure initializer (or where-ever else comma expressions
+   aren't permitted). */
+#define BUILD_BUG_ON_ZERO(cond) \
+    (sizeof(struct { char c; _Static_assert(!(cond), "!(" #cond ")"); }) & 0)
+#else
+#define BUILD_BUG_ON_ZERO(cond) \
+    (sizeof(struct { unsigned u : !(cond); }) & 0)
+#define BUILD_BUG_ON(cond) ((void)BUILD_BUG_ON_ZERO(cond))
+#endif
+
+#define ABS(_x) ({                              \
+    typeof(_x) __x = (_x);                      \
+    (__x < 0) ? -__x : __x;                     \
+})
+
+#define SWAP(_a, _b) \
+   do { typeof(_a) _t = (_a); (_a) = (_b); (_b) = _t; } while ( 0 )
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
+
+#endif /* __ASSEMBLY__ */
+
 #endif /* __MACROS_H__ */
 
 /*