tools: use xen-tools/libs.h for common definitions
cover tools: use xen-tools/libs.h for common definitions
There are some macros defined multiple times in tools. Use only xen-tools/libs.h for defining those macros and drop the copies.
Juergen Gross (3):
tools: add container_of() macro to xen-tools/libs.h
tools: get rid of additional min() and max() definitions
tools: add offsetof() to xen-tools/libs.h
tools/firmware/hvmloader/util.h | 11 ++--------- tools/firmware/include/stddef.h | 4 ++-- tools/include/xen-tools/libs.h | 8 ++++++++ tools/libfsimage/Rules.mk | 2 ++ tools/libfsimage/xfs/fsys_xfs.c | 4 +--- tools/libs/vchan/init.c | 7 +------ tools/tests/vhpet/emul.h | 14 -------------- tools/tests/vpci/emul.h | 22 +--------------------- tools/tests/x86_emulator/x86-emulate.h | 5 ----- tools/xenstore/list.h | 6 ++---- 10 files changed, 19 insertions(+), 64 deletions(-)
Signature · 1 line
2.35.3
PATCH 1/3 tools: add container_of() macro to xen-tools/libs.h
Instead of having 4 identical copies of the definition of a container_of() macro in different tools header files, add that macro to xen-tools/libs.h and use that instead.
Delete the other copies of that macro.
---
There is a similar macro CONTAINER_OF() defined in tools/include/xentoolcore_internal.h, which allows to not only use a type for the 2nd parameter, but a variable, too.
I'd like to get rid of that macro as well, but there are lots of use cases especially in libxl. Any thoughts regarding that macro? I could either:
- don't touch it at all
- enhance container_of() like CONTAINER_OF() and replace all use cases of CONTAINER_OF() with container_of()
- replace the few CONTAINER_OF() users outside libxl with container_of() and define CONTAINER_OF() in e.g. libxl_internal.h
- replace all CONTAINER_OF() use cases with container_of(), including the change from (.., var, ..) to (.., type, ...).
--- tools/include/xen-tools/libs.h | 4 ++++ tools/tests/vhpet/emul.h | 3 --- tools/tests/vpci/emul.h | 6 +----- tools/tests/x86_emulator/x86-emulate.h | 5 ----- tools/xenstore/list.h | 6 ++---- 5 files changed, 7 insertions(+), 17 deletions(-)
tools/include/xen-tools/libs.h +4 -0
@@ -67,4 +67,8 @@#define __must_check __attribute__((__warn_unused_result__))#endif +#define container_of (ptr, type, member) ({ \ + typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) +#endif /* __XEN_TOOLS_LIBS__ */
tools/tests/vhpet/emul.h +0 -3
@@ -125,9 +125,6 @@ enum#define max_t (type, x, y) \ ({ type __x = (x); type __y = (y); __x > __y ? __x : __y; })#define offsetof (t, m) ((unsigned long )&((t *)0)->m) -#define container_of (ptr, type, member) ({ \ - typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) ); })struct domain ;
tools/tests/vpci/emul.h +1 -5
@@ -27,11 +27,7 @@#include <stdio.h> #include <stdlib.h> -#define container_of (ptr, type, member) ({ \ - typeof(((type *)0)->member) *mptr = (ptr); \ - \ - (type *)((char *)mptr - offsetof(type, member)); \ -}) +#include <xen-tools/libs.h> #define smp_wmb ()#define prefetch (x) __builtin_prefetch(x)
tools/tests/x86_emulator/x86-emulate.h +0 -5
@@ -56,11 +56,6 @@#define cf_check/* No Control Flow Integriy checking */ -#define container_of (ptr, type, member) ({ \ - typeof(((type *)0)->member) *mptr__ = (ptr); \ - (type *)((char *)mptr__ - offsetof(type, member)); \ -}) -#define AC_ (n,t) (n##t)#define _AC (n,t) AC_(n,t)
tools/xenstore/list.h +2 -4
@@ -3,6 +3,8 @@/* Taken from Linux kernel code, but de-kernelized for userspace. */ #include <stddef.h> +#include <xen-tools/libs.h> +#undef LIST_HEAD_INIT#undef LIST_HEAD#undef INIT_LIST_HEAD
@@ -15,10 +17,6 @@#define LIST_POISON1 ((void *) 0x00100100)#define LIST_POISON2 ((void *) 0x00200200) -#define container_of (ptr, type, member) ({ \ - typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) -/* * Simple doubly linked list implementation. *
Signature · 1 line
2.35.3
PATCH 2/3 tools: get rid of additional min() and max() definitions
In reply to Juergen GrossDefining min(), min_t(), max() and max_t() at other places than xen-tools/libs.h isn't needed, as the definitions in said header can be used instead.
Same applies to BUILD_BUG_ON() in hvmloader.
--- tools/firmware/hvmloader/util.h | 8 ++------ tools/libs/vchan/init.c | 3 +-- tools/tests/vhpet/emul.h | 11 +---------- tools/tests/vpci/emul.h | 16 ---------------- 4 files changed, 4 insertions(+), 34 deletions(-)
tools/firmware/hvmloader/util.h +2 -6
@@ -9,6 +9,8 @@#include <xen/hvm/hvm_info_table.h> #include "e820.h" +#include <xen-tools/libs.h> +/* Request un-prefixed values from errno.h. */ #define XEN_ERRNO (name, value) name = value,enum {
@@ -41,12 +43,6 @@ void __assert_failed(const char *assertion, const char *file, int line)void __bug (const char *file,int line) __attribute__((noreturn));#define BUG () __bug(__FILE__, __LINE__)#define BUG_ON (p) do { if (p) BUG(); } while (0) -#define BUILD_BUG_ON (p) ((void)sizeof(char[1 - 2 * !!(p)])) - -#define min_t (type,x,y) \ - ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; }) -#define max_t (type,x,y) \ - ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })#define MB (mb) (mb##ULL << 20)#define GB (gb) (gb##ULL << 30)
tools/libs/vchan/init.c +1 -2
@@ -45,6 +45,7 @@#include <xen/sys/gntalloc.h> #include <xen/sys/gntdev.h> #include <libxenvchan.h> +#include <xen-tools/libs.h> #include "vchan.h"
@@ -72,8 +73,6 @@#define offsetof (TYPE ,MEMBER ) ((size_t) &((TYPE *)0)->MEMBER)#endif -#define max (a,b) ((a > b) ? a : b) -static int init_gnt_srv (struct libxenvchan *ctrl,int domain) {int pages_left = ctrl->read.order >=PAGE_SHIFT ?1 << (ctrl->read.order -PAGE_SHIFT ) :0 ;
tools/tests/vhpet/emul.h +1 -10
@@ -114,16 +114,7 @@ enumTASKLET_SOFTIRQ , NR_COMMON_SOFTIRQS }; -/* -* ..and if you can't take the strict -* types, you can specify one yourself. -* -* Or not use min/max at all, of course. -*/ -#define min_t (type, x, y) \ - ({ type __x = (x); type __y = (y); __x < __y ? __x : __y; }) -#define max_t (type, x, y) \ - ({ type __x = (x); type __y = (y); __x > __y ? __x : __y; }) +#define offsetof (t, m) ((unsigned long )&((t *)0)->m)struct domain ;
tools/tests/vpci/emul.h +0 -16
@@ -106,22 +106,6 @@ typedef union {#define BUG () assert(0)#define ASSERT_UNREACHABLE () assert(0) -#define min (x, y) ({ \ - const typeof(x) tx = (x); \ - const typeof(y) ty = (y); \ - \ - (void) (&tx == &ty); \ - tx < ty ? tx : ty; \ -}) - -#define max (x, y) ({ \ - const typeof(x) tx = (x); \ - const typeof(y) ty = (y); \ - \ - (void) (&tx == &ty); \ - tx > ty ? tx : ty; \ -}) -#endif /*
Signature · 1 line
2.35.3
PATCH 3/3 tools: add offsetof() to xen-tools/libs.h
In reply to Juergen GrossInstead of having multiple files defining offsetof(), add the definition to tools/include/xen-tools/libs.h.
--- tools/firmware/hvmloader/util.h | 3 --- tools/firmware/include/stddef.h | 4 ++-- tools/include/xen-tools/libs.h | 4 ++++ tools/libfsimage/Rules.mk | 2 ++ tools/libfsimage/xfs/fsys_xfs.c | 4 +--- tools/libs/vchan/init.c | 4 ---- tools/tests/vhpet/emul.h | 2 -- 7 files changed, 9 insertions(+), 14 deletions(-)
tools/firmware/hvmloader/util.h +0 -3
@@ -30,9 +30,6 @@ enum {#define SEL_DATA32 0x0020#define SEL_CODE64 0x0028 -#undef offsetof -#define offsetof (t, m) ((unsigned long)&((t *)0)->m) -#undef NULL#define NULL ((void*)0)
tools/firmware/include/stddef.h +2 -2
@@ -1,10 +1,10 @@#ifndef _STDDEF_H_#define _STDDEF_H_ +#include <xen-tools/libs.h> +typedef __SIZE_TYPE__ size_t ;#define NULL ((void*)0) -#define offsetof (t, m) __builtin_offsetof(t, m) -#endif
tools/include/xen-tools/libs.h +4 -0
@@ -71,4 +71,8 @@typeof ( ((type *)0 )->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) +#ifndef offsetof +#define offsetof (a, b) __builtin_offsetof(a, b) +#endif +#endif /* __XEN_TOOLS_LIBS__ */
tools/libfsimage/Rules.mk +2 -0
@@ -3,6 +3,8 @@ include $(XEN_ROOT)/tools/libfsimage/common.mk FSLIB = fsimage.so TARGETS += $(FSLIB) +CFLAGS += $(CFLAGS_xeninclude) + .PHONY: all all: $(TARGETS)
tools/libfsimage/xfs/fsys_xfs.c +1 -3
@@ -19,6 +19,7 @@#include <xenfsimage_grub.h> #include "xfs.h" +#include <xen-tools/libs.h> #define MAX_LINK_COUNT 8
@@ -182,9 +183,6 @@ fsb2daddr (xfs_fsblock_t fsbno) (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog))); } -#undef offsetof -#define offsetof (t,m) ((size_t)&(((t *)0)->m)) -static inline int btroot_maxrecs (fsi_file_t *ffi) {
tools/libs/vchan/init.c +0 -4
@@ -69,10 +69,6 @@#define MAX_RING_SHIFT 20#define MAX_RING_SIZE (1 << MAX_RING_SHIFT) -#ifndef offsetof -#define offsetof (TYPE ,MEMBER ) ((size_t) &((TYPE *)0)->MEMBER) -#endif -static int init_gnt_srv (struct libxenvchan *ctrl,int domain) {int pages_left = ctrl->read.order >=PAGE_SHIFT ?1 << (ctrl->read.order -PAGE_SHIFT ) :0 ;
tools/tests/vhpet/emul.h +0 -2
@@ -115,8 +115,6 @@ enum NR_COMMON_SOFTIRQS }; -#define offsetof (t, m) ((unsigned long )&((t *)0)->m) -struct domain ;struct vcpu
Signature · 1 line
2.35.3
Re: [PATCH 3/3] tools: add offsetof() to xen-tools/libs.h
Quoted 17 lines · Juergen Gross
tools/firmware/hvmloader/util.h +0
-3@@ -30,9 +30,6 @@ enum {#define SEL_DATA32 0x0020#define SEL_CODE64 0x0028 -#undef offsetof -#define offsetof (t, m) ((unsigned long)&((t *)0)->m) -#undef NULL#define NULL ((void*)0)tools/firmware/include/stddef.h +0
-0
tools/firmware/include/stddef.h line 4
Quoted 4 lines
tools/firmware/include/stddef.h +1
-0@@ -1,10 +1,10 @@#ifndef _STDDEF_H_#define _STDDEF_H_ +#include <xen-tools/libs.h>
I'm not convinced firmware/ files can validly include this header.
Jan
Re: [PATCH 3/3] tools: add offsetof() to xen-tools/libs.h
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
tools/firmware/hvmloader/util.h +0
-3@@ -30,9 +30,6 @@ enum {#define SEL_DATA32 0x0020#define SEL_CODE64 0x0028 -#undef offsetof -#define offsetof (t, m) ((unsigned long)&((t *)0)->m) -#undef NULL#define NULL ((void*)0)tools/firmware/include/stddef.h +0
-0@@ -1,10 +1,10 @@#ifndef _STDDEF_H_#define _STDDEF_H_
Quoted 3 lines
tools/firmware/include/stddef.h line 4
Quoted 1 line
tools/firmware/include/stddef.h +1
-0+#include <xen-tools/libs.h> I'm not convinced
firmware/files can validly include this header.
This file only contains macros which don't call any external functions.
Would you be fine with me adding a related comment to it?
Juergen
Re: [PATCH 3/3] tools: add offsetof() to xen-tools/libs.h
Quoted 22 lines · Juergen Gross
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
tools/firmware/hvmloader/util.h +0
-3@@ -30,9 +30,6 @@ enum {#define SEL_DATA32 0x0020#define SEL_CODE64 0x0028 -#undef offsetof -#define offsetof (t, m) ((unsigned long)&((t *)0)->m) -#undef NULL#define NULL ((void*)0)tools/firmware/include/stddef.h +0
-0@@ -1,10 +1,10 @@#ifndef _STDDEF_H_#define _STDDEF_H_
Quoted 7 lines
Quoted 3 lines
tools/firmware/include/stddef.h line 4
Quoted 1 line
tools/firmware/include/stddef.h +1
-0+#include <xen-tools/libs.h> I'm not convinced
firmware/files can validly include this header.This file only contains macros which don't call any external functions.
Would you be fine with me adding a related comment to it?
If it was to be usable like this, that would be a prereq, but personally I don't view this as sufficient. The environment code runs in that lives under firmware/ is simply different (hvmloader, for example, is 32-bit no matter that the toolstack would normally be 64-bit), so to me it feels like setting up a trap for ourselves. If Andrew or Roger are fully convinced this is a good move, I won't be standing in the way ...
Jan
Re: [PATCH 3/3] tools: add offsetof() to xen-tools/libs.h
Quoted 27 lines · Jan Beulich
Quoted 26 lines · Juergen Gross
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
tools/firmware/hvmloader/util.h +0
-3@@ -30,9 +30,6 @@ enum {#define SEL_DATA32 0x0020#define SEL_CODE64 0x0028 -#undef offsetof -#define offsetof (t, m) ((unsigned long)&((t *)0)->m) -#undef NULL#define NULL ((void*)0)tools/firmware/include/stddef.h +0
-0@@ -1,10 +1,10 @@#ifndef _STDDEF_H_#define _STDDEF_H_Quoted 2 lines
tools/firmware/include/stddef.h line 4
Quoted 1 line
tools/firmware/include/stddef.h +1
-0+#include <xen-tools/libs.h> I'm not convinced
firmware/files can validly include this header.This file only contains macros which don't call any external functions.
Quoted 7 lines
Quoted 1 line
Would you be fine with me adding a related comment to it?
If it was to be usable like this, that would be a prereq, but personally I don't view this as sufficient. The environment code runs in that lives under
firmware/is simply different (hvmloader, for example, is 32-bit no matter that the toolstack would normally be 64-bit), so to me it feels like setting up a trap for ourselves. If Andrew or Roger are fully convinced this is a good move, I won't be standing in the way ...
We still support 32bit builds of the toolstack on multiple architectures, so I don't see bitness as an argument against.
I am slightly uneasy adding a header like this, but it really is only common macros and I don't see how it could possibly change from that given the existing uses. OTOH, removing things like the problematic definition of offsetof() is an improvement.
I'd probably tentatively ack this patch, seeing as it is a minor improvlement, but I think there's a middle option too. Rename libs.h to macros.h or common-macros.h? IMO that would be something that's far more obviously safe to include into firmware/, and something rather more descriptive at all of its include sites.
Thoughts?
~Andrew
Re: [PATCH 3/3] tools: add offsetof() to xen-tools/libs.h
Quoted 44 lines · Andrew Cooper
Quoted 27 lines · Jan Beulich
Quoted 26 lines · Juergen Gross
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
tools/firmware/hvmloader/util.h +0
-3@@ -30,9 +30,6 @@ enum {#define SEL_DATA32 0x0020#define SEL_CODE64 0x0028 -#undef offsetof -#define offsetof (t, m) ((unsigned long)&((t *)0)->m) -#undef NULL#define NULL ((void*)0)tools/firmware/include/stddef.h +0
-0@@ -1,10 +1,10 @@#ifndef _STDDEF_H_#define _STDDEF_H_Quoted 2 lines
tools/firmware/include/stddef.h line 4
Quoted 1 line
tools/firmware/include/stddef.h +1
-0+#include <xen-tools/libs.h> I'm not convinced
firmware/files can validly include this header.This file only contains macros which don't call any external functions.
Quoted 7 lines
Quoted 1 line
Would you be fine with me adding a related comment to it?
If it was to be usable like this, that would be a prereq, but personally I don't view this as sufficient. The environment code runs in that lives under
firmware/is simply different (hvmloader, for example, is 32-bit no matter that the toolstack would normally be 64-bit), so to me it feels like setting up a trap for ourselves. If Andrew or Roger are fully convinced this is a good move, I won't be standing in the way ...We still support 32bit builds of the toolstack on multiple architectures, so I don't see bitness as an argument against.
I am slightly uneasy adding a header like this, but it really is only common macros and I don't see how it could possibly change from that given the existing uses. OTOH, removing things like the problematic definition of
offsetof()is an improvement.
Quoted 7 lines
I'd probably tentatively ack this patch, seeing as it is a minor improvlement, but I think there's a middle option too. Rename
libs.htomacros.hor common-macros.h? IMO that would be something that's far more obviously safe to include intofirmware/, and something rather more descriptive at all of its include sites.Thoughts?
I'm fine with that.
My preference would be "common-macros.h".
Juergen
Re: [PATCH 3/3] tools: add offsetof() to xen-tools/libs.h
In reply to Andrew CooperQuoted 36 lines · Andrew Cooper
Quoted 27 lines · Jan Beulich
Quoted 26 lines · Juergen Gross
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
tools/firmware/hvmloader/util.h +0
-3@@ -30,9 +30,6 @@ enum {#define SEL_DATA32 0x0020#define SEL_CODE64 0x0028 -#undef offsetof -#define offsetof (t, m) ((unsigned long)&((t *)0)->m) -#undef NULL#define NULL ((void*)0)tools/firmware/include/stddef.h +0
-0@@ -1,10 +1,10 @@#ifndef _STDDEF_H_#define _STDDEF_H_Quoted 2 lines
tools/firmware/include/stddef.h line 4
Quoted 1 line
tools/firmware/include/stddef.h +1
-0+#include <xen-tools/libs.h> I'm not convinced
firmware/files can validly include this header.This file only contains macros which don't call any external functions.
Quoted 7 lines
Quoted 1 line
Would you be fine with me adding a related comment to it?
If it was to be usable like this, that would be a prereq, but personally I don't view this as sufficient. The environment code runs in that lives under
firmware/is simply different (hvmloader, for example, is 32-bit no matter that the toolstack would normally be 64-bit), so to me it feels like setting up a trap for ourselves. If Andrew or Roger are fully convinced this is a good move, I won't be standing in the way ...
Quoted 2 lines
We still support 32bit builds of the toolstack on multiple architectures, so I don't see bitness as an argument against.
Bitness by itself wasn't the argument. Mixed bitness is what concerns me.
Quoted 5 lines
I am slightly uneasy adding a header like this, but it really is only common macros and I don't see how it could possibly change from that given the existing uses. OTOH, removing things like the problematic definition of
offsetof()is an improvement.
Quoted 7 lines
I'd probably tentatively ack this patch, seeing as it is a minor improvlement, but I think there's a middle option too. Rename
libs.htomacros.hor common-macros.h? IMO that would be something that's far more obviously safe to include intofirmware/, and something rather more descriptive at all of its include sites.Thoughts?
Maybe. One fundamental requirement would need to be made prominently visible in such a header: It has to be entirely self-contained, i.e. it may in particular not gain any dependencies on configure's output.
Jan
Re: [PATCH 3/3] tools: add offsetof() to xen-tools/libs.h
Quoted 36 lines · Jan Beulich
Quoted 35 lines · Andrew Cooper
Quoted 34 lines · Jan Beulich
Quoted 22 lines · Juergen Gross
Quoted 21 lines · Jan Beulich
Quoted 20 lines · Juergen Gross
tools/firmware/hvmloader/util.h +0
-3@@ -30,9 +30,6 @@ enum {#define SEL_DATA32 0x0020#define SEL_CODE64 0x0028 -#undef offsetof -#define offsetof (t, m) ((unsigned long)&((t *)0)->m) -#undef NULL#define NULL ((void*)0)tools/firmware/include/stddef.h +0
-0@@ -1,10 +1,10 @@#ifndef _STDDEF_H_#define _STDDEF_H_Quoted 5 lines
Quoted 2 lines
tools/firmware/include/stddef.h line 4
Quoted 1 line
tools/firmware/include/stddef.h +1
-0+#include <xen-tools/libs.h> I'm not convinced
firmware/files can validly include this header.This file only contains macros which don't call any external functions.
Would you be fine with me adding a related comment to it?
If it was to be usable like this, that would be a prereq, but personally I don't view this as sufficient. The environment code runs in that lives under
firmware/is simply different (hvmloader, for example, is 32-bit no matter that the toolstack would normally be 64-bit), so to me it feels like setting up a trap for ourselves. If Andrew or Roger are fully convinced this is a good move, I won't be standing in the way ...
Quoted 4 lines
Quoted 2 lines
We still support 32bit builds of the toolstack on multiple architectures, so I don't see bitness as an argument against.
Bitness by itself wasn't the argument. Mixed bitness is what concerns me.
I still don't see how that would matter. The bitness would always be consistent inside a single translation.
Quoted 12 lines
Quoted 12 lines
I am slightly uneasy adding a header like this, but it really is only common macros and I don't see how it could possibly change from that given the existing uses. OTOH, removing things like the problematic definition of
offsetof()is an improvement.I'd probably tentatively ack this patch, seeing as it is a minor improvlement, but I think there's a middle option too. Rename
libs.htomacros.hor common-macros.h? IMO that would be something that's far more obviously safe to include intofirmware/, and something rather more descriptive at all of its include sites.
Quoted 4 lines
Quoted 1 line
Thoughts?
Maybe. One fundamental requirement would need to be made prominently visible in such a header: It has to be entirely self-contained, i.e. it may in particular not gain any dependencies on configure's output.
That's a reasonable restriction IMO. Again, I don't see how we'd ever come to violate that in the future, given the current use here.
~Andrew