tools: use xen-tools/libs.h for common definitions
v4 cover tools: use xen-tools/libs.h for common definitions
There are some macros defined multiple times in tools. Use only a single header file for defining those macros and drop the copies, or use stddef.h for offsetof().
V2:
- add patch 1 (Andrew Cooper)
V3:
- address comments
V4:
- patch 1 of V3 already applied
- new patches 3-5
Juergen Gross (5):
tools: add container_of() macro to xen-tools/common-macros.h
tools: get rid of additional min() and max() definitions tools/hvmloader: remove private offsetof() definition tools/libfsimage: remove private offsetof() definition tools/libs/vchan: remove private offsetof() definition
tools/firmware/hvmloader/util.h | 11 ++--------- tools/include/xen-tools/common-macros.h | 4 ++++ tools/libfsimage/xfs/fsys_xfs.c | 4 +--- tools/libs/vchan/init.c | 8 ++------ tools/tests/vpci/Makefile | 2 +- tools/tests/vpci/emul.h | 22 +--------------------- tools/tests/x86_emulator/x86-emulate.h | 5 ----- tools/xenstore/list.h | 6 ++---- 8 files changed, 13 insertions(+), 49 deletions(-)
Signature · 1 line
2.35.3
PATCH v4 1/5 tools: add container_of() macro to xen-tools/common-macros.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/common-macros.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/common-macros.h | 4 ++++ tools/tests/vpci/emul.h | 6 +----- tools/tests/x86_emulator/x86-emulate.h | 5 ----- tools/xenstore/list.h | 6 ++---- 4 files changed, 7 insertions(+), 14 deletions(-)
tools/include/xen-tools/common-macros.h +4 -0
@@ -76,4 +76,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_COMMON_MACROS__ */
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/common-macros.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/common-macros.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
Re: [PATCH v4 1/5] tools: add container_of() macro to xen-tools/common-macros.h
tools/include/xen-tools/common-macros.h line 81
Quoted 9 lines · Juergen Gross
tools/include/xen-tools/common-macros.h +3
-0@@ -76,4 +76,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) );})
Can the variant used here please be closer to ...
tools/tests/x86_emulator/x86-emulate.h line 62
Quoted 10 lines
tools/tests/x86_emulator/x86-emulate.h +0
-4@@ -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)); \ -})
... this rather than ...
Quoted 12 lines
tools/xenstore/list.h +2
-0@@ -3,6 +3,8 @@/* Taken from Linux kernel code, but de-kernelized for userspace. */ #include <stddef.h> +#include <xen-tools/common-macros.h> +#undef LIST_HEAD_INIT#undef LIST_HEAD#undef INIT_LIST_HEAD
tools/xenstore/list.h line 20
Quoted 6 lines
tools/xenstore/list.h +0
-3@@ -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) );})
... this, both formatting-wise (excess blanks) and local-variable- naming-wise (trailing underscores instead of leading ones)? (If I was the one to commit this, I'd be happy to make the adjustment at that time.) Then
Jan
Re: [PATCH v4 1/5] tools: add container_of() macro to xen-tools/common-macros.h
Quoted 41 lines · Jan Beulich
tools/include/xen-tools/common-macros.h line 81
Quoted 9 lines · Juergen Gross
tools/include/xen-tools/common-macros.h +3
-0@@ -76,4 +76,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) );})Can the variant used here please be closer to ...
tools/tests/x86_emulator/x86-emulate.h line 62
Quoted 10 lines
tools/tests/x86_emulator/x86-emulate.h +0
-4@@ -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)); \ -})... this rather than ...
Quoted 15 lines
tools/xenstore/list.h +2
-0@@ -3,6 +3,8 @@/* Taken from Linux kernel code, but de-kernelized for userspace. */ #include <stddef.h> +#include <xen-tools/common-macros.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)
Quoted 8 lines
tools/xenstore/list.h line 20
Quoted 3 lines
tools/xenstore/list.h +0
-3-#define container_of (ptr, type, member) ({ \ - typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );})... this, both formatting-wise (excess blanks) and local-variable- naming-wise (trailing underscores instead of leading ones)? (If I was the one to commit this, I'd be happy to make the adjustment at that time.) Then
Yes, absolutely fine with me.
Quoted 1 line
Acked-by Jan Beulich <[email protected]>
Thanks,
Juergen
PATCH v4 2/5 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/common-macros.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/vpci/Makefile | 2 +- tools/tests/vpci/emul.h | 16 ---------------- 4 files changed, 4 insertions(+), 25 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/common-macros.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/common-macros.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/vpci/Makefile +1 -1
@@ -11,7 +11,7 @@ run: $(TARGET) ./$(TARGET) $(TARGET): vpci.c vpci.h list.h main.c emul.h - $(HOSTCC) -g -o $@ vpci.c main.c + $(HOSTCC) $(CFLAGS_xeninclude) -g -o $@ vpci.c main.c .PHONY: clean clean:
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
Re: [PATCH v4 2/5] tools: get rid of additional min() and max() definitions
Quoted 11 lines · Juergen Gross
tools/tests/vpci/emul.h +0
-5@@ -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); \
tools/tests/vpci/emul.h typedef union { · line 123
Quoted 10 lines
tools/tests/vpci/emul.h +0
-10- 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; \ -})
The new include is added to this file by the first patch. How do things build warning-free before these macros (functionally compatible but different in the specific tokens used) are removed, i.e. before this patch is (also) applied?
Jan
Re: [PATCH v4 2/5] tools: get rid of additional min() and max() definitions
Quoted 19 lines · Jan Beulich
Quoted 18 lines · Juergen Gross
tools/tests/vpci/emul.h +0
-12@@ -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); \ - \
Quoted 8 lines
tools/tests/vpci/emul.h typedef union { · line 123
Quoted 3 lines
tools/tests/vpci/emul.h +0
-3- (void ) (&tx == &ty); \ - tx > ty ? tx : ty; \ -})The new include is added to this file by the first patch. How do things build warning-free before these macros (functionally compatible but different in the specific tokens used) are removed, i.e. before this patch is (also) applied?
Oh, I seem to have failed to make a test build of the tools/tests directory after applying only the first patch.
It would be probably be the best to switch patches 1 and 2 while keeping the "#include <xen-tools/common-macros.h>" in tools/tests/vpci/emul.h in the first patch.
Juergen
PATCH v4 3/5 tools/hvmloader: remove private offsetof() definition
In reply to Juergen Grossutil.h contains a definition of offsetof(), which isn't needed.
Remove it.
---
V4:
- new patch
--- tools/firmware/hvmloader/util.h | 3 --- 1 file changed, 3 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)
Signature · 1 line
2.35.3
Re: [PATCH v4 3/5] tools/hvmloader: remove private offsetof() definition
Quoted 1 line · Juergen Gross
util.hcontains a definition ofoffsetof(), which isn't needed.
While true, this is also ambiguous in the context of this series: It isn't "not needed" because common-macros.h has another definition, but it is actually unused in hvmloader code. So perhaps s/needed/used/?
Quoted 3 lines
Remove it.
Signed-off-by Juergen Gross <[email protected]>
Re: [PATCH v4 3/5] tools/hvmloader: remove private offsetof() definition
Quoted 6 lines · Jan Beulich
Quoted 1 line · Juergen Gross
util.hcontains a definition ofoffsetof(), which isn't needed.While true, this is also ambiguous in the context of this series: It isn't "not needed" because
common-macros.hhas another definition, but it is actually unused in hvmloader code. So perhaps s/needed/used/?
Fine with me.
Quoted 7 lines
Quoted 4 lines
Remove it.
Signed-off-by Juergen Gross <[email protected]>Acked-by Jan Beulich <[email protected]>
Thanks,
Juergen
PATCH v4 4/5 tools/libfsimage: remove private offsetof() definition
In reply to Juergen Grossxfs/fsys_xfs.c is defining offsetof privately. Remove that definition and just use stddef.h instead.
---
V4:
- new patch
--- tools/libfsimage/xfs/fsys_xfs.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
tools/libfsimage/xfs/fsys_xfs.c +1 -3
@@ -17,6 +17,7 @@* along with this program; If not, see <http://www.gnu.org/licenses/>. */ +#include <stddef.h> #include <xenfsimage_grub.h> #include "xfs.h"
@@ -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) {
Signature · 1 line
2.35.3
PATCH v4 5/5 tools/libs/vchan: remove private offsetof() definition
In reply to Juergen Grossvchan/init.c is defining offsetof privately. Remove that definition and just use stddef.h instead.
---
V4:
- new patch
--- tools/libs/vchan/init.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
tools/libs/vchan/init.c +1 -4
@@ -32,6 +32,7 @@#include <sys/mman.h> #include <sys/ioctl.h> #include <sys/user.h> +#include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <stdint.h>
@@ -69,10 +70,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 ;
Signature · 1 line
2.35.3