Skip to contentSkip to conversation

tools: use xen-tools/libs.h for common definitions

PATCH Superseded Juergen Gross 4 participants 13 messages Mar 6, 2023 – Mar 23, 2023 Arm/actions semi architecture

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 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.

V2:
- add patch 1 (Andrew Cooper)

V3:
- address comments

Juergen Gross (4):
tools: rename xen-tools/libs.h file to common-macros.h
tools: add container_of() macro to xen-tools/common-macros.h
tools: get rid of additional min() and max() definitions
tools: add offsetof() to xen-tools/common-macros.h

 tools/console/daemon/io.c                     |  2 +-
 tools/firmware/hvmloader/util.h               | 11 ++-------
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c     |  2 +-
 .../xen-tools/{libs.h => common-macros.h}     | 23 ++++++++++++++++---
 tools/libfsimage/Rules.mk                     |  2 ++
 tools/libfsimage/xfs/fsys_xfs.c               |  4 +---
 tools/libs/call/buffer.c                      |  2 +-
 tools/libs/ctrl/xc_pm.c                       |  2 +-
 tools/libs/ctrl/xc_private.h                  |  2 +-
 tools/libs/foreignmemory/linux.c              |  2 +-
 tools/libs/gnttab/freebsd.c                   |  2 +-
 tools/libs/gnttab/linux.c                     |  2 +-
 tools/libs/guest/xg_core_arm.c                |  2 +-
 tools/libs/guest/xg_cpuid_x86.c               |  2 +-
 tools/libs/guest/xg_dom_arm.c                 |  2 +-
 tools/libs/guest/xg_dom_bzimageloader.c       |  2 +-
 tools/libs/guest/xg_dom_x86.c                 |  2 +-
 tools/libs/guest/xg_sr_common.c               |  2 +-
 tools/libs/light/libxl_internal.h             |  2 +-
 tools/libs/light/libxl_psr.c                  |  2 +-
 tools/libs/stat/xenstat_linux.c               |  2 +-
 tools/libs/vchan/init.c                       |  7 +-----
 tools/misc/xen-access.c                       |  2 +-
 tools/misc/xen-cpuid.c                        |  2 +-
 tools/misc/xen-diag.c                         |  2 +-
 tools/misc/xen-hptool.c                       |  2 +-
 tools/misc/xen-livepatch.c                    |  2 +-
 tools/misc/xen-mfndump.c                      |  2 +-
 tools/misc/xenpm.c                            |  2 +-
 tools/ocaml/libs/mmap/xenmmap_stubs.c         |  2 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c           |  2 +-
 tools/python/xen/lowlevel/xc/xc.c             |  2 +-
 tools/tests/cpu-policy/test-cpu-policy.c      |  2 +-
 .../paging-mempool/test-paging-mempool.c      |  2 +-
 tools/tests/resource/test-resource.c          |  2 +-
 tools/tests/tsx/test-tsx.c                    |  2 +-
 tools/tests/vhpet/emul.h                      | 16 +------------
 tools/tests/vpci/emul.h                       | 22 +-----------------
 tools/tests/x86_emulator/x86-emulate.h        |  7 +-----
 tools/tests/xenstore/test-xenstore.c          |  2 +-
 tools/xenstore/list.h                         |  6 ++---
 tools/xenstore/utils.h                        |  2 +-
 tools/xentrace/analyze.h                      |  2 +-
 tools/xl/xl_cmdtable.c                        |  2 +-
 xen/lib/x86/private.h                         |  2 +-
 45 files changed, 67 insertions(+), 103 deletions(-)
 rename tools/include/xen-tools/{libs.h => common-macros.h} (74%)
Signature · 1 line
2.35.3
#1 lore raw

PATCH v3 1/4 tools: rename xen-tools/libs.h file to common-macros.h

In order to better reflect the contents of the header and to make it more appropriate to use it for different runtime environments like programs, libraries, and firmware, rename the libs.h include file to common-macros.h. Additionally add a comment pointing out the need to be self-contained.

---
V2:
- new patch
V3:
- small rephrase of warning comment (Jan Beulich)

---
 tools/console/daemon/io.c                         |  2 +-
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c         |  2 +-
 .../include/xen-tools/{libs.h => common-macros.h} | 15 ++++++++++++---
 tools/libs/call/buffer.c                          |  2 +-
 tools/libs/ctrl/xc_pm.c                           |  2 +-
 tools/libs/ctrl/xc_private.h                      |  2 +-
 tools/libs/foreignmemory/linux.c                  |  2 +-
 tools/libs/gnttab/freebsd.c                       |  2 +-
 tools/libs/gnttab/linux.c                         |  2 +-
 tools/libs/guest/xg_core_arm.c                    |  2 +-
 tools/libs/guest/xg_cpuid_x86.c                   |  2 +-
 tools/libs/guest/xg_dom_arm.c                     |  2 +-
 tools/libs/guest/xg_dom_bzimageloader.c           |  2 +-
 tools/libs/guest/xg_dom_x86.c                     |  2 +-
 tools/libs/guest/xg_sr_common.c                   |  2 +-
 tools/libs/light/libxl_internal.h                 |  2 +-
 tools/libs/light/libxl_psr.c                      |  2 +-
 tools/libs/stat/xenstat_linux.c                   |  2 +-
 tools/misc/xen-access.c                           |  2 +-
 tools/misc/xen-cpuid.c                            |  2 +-
 tools/misc/xen-diag.c                             |  2 +-
 tools/misc/xen-hptool.c                           |  2 +-
 tools/misc/xen-livepatch.c                        |  2 +-
 tools/misc/xen-mfndump.c                          |  2 +-
 tools/misc/xenpm.c                                |  2 +-
 tools/ocaml/libs/mmap/xenmmap_stubs.c             |  2 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c               |  2 +-
 tools/python/xen/lowlevel/xc/xc.c                 |  2 +-
 tools/tests/cpu-policy/test-cpu-policy.c          |  2 +-
 tools/tests/paging-mempool/test-paging-mempool.c  |  2 +-
 tools/tests/resource/test-resource.c              |  2 +-
 tools/tests/tsx/test-tsx.c                        |  2 +-
 tools/tests/vhpet/emul.h                          |  2 +-
 tools/tests/x86_emulator/x86-emulate.h            |  2 +-
 tools/tests/xenstore/test-xenstore.c              |  2 +-
 tools/xenstore/utils.h                            |  2 +-
 tools/xentrace/analyze.h                          |  2 +-
 tools/xl/xl_cmdtable.c                            |  2 +-
 xen/lib/x86/private.h                             |  2 +-
 39 files changed, 50 insertions(+), 41 deletions(-)
 rename tools/include/xen-tools/{libs.h => common-macros.h} (82%)
tools/console/daemon/io.c +1 -1
index 682c1f4008..6bfe96715b 100644
@@ -50,7 +50,7 @@
 #include <sys/ioctl.h>
 #include <libutil.h>
 #endif
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 /* Each 10 bits takes ~ 3 digits, plus one, plus one for nul terminator. */
 #define MAX_STRLEN(x) ((sizeof(x) * CHAR_BIT + CHAR_BIT-1) / 10 * 3 + 2)
tools/fuzz/cpu-policy/afl-policy-fuzzer.c +1 -1
index 79a849a044..7d0f274c6c 100644
@@ -8,7 +8,7 @@
 
 #include <getopt.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 #include <xen/lib/x86/cpuid.h>
 #include <xen/lib/x86/msr.h>
 #include <xen/domctl.h>
tools/include/xen-tools/common-macros.h renamed from tools/include/xen-tools/libs.h +12 -3
similarity index 82%
rename from tools/include/xen-tools/libs.h
rename to tools/include/xen-tools/common-macros.h
index bafc90e2f6..a372b9ecf2 100644
@@ -1,5 +1,14 @@
-#ifndef __XEN_TOOLS_LIBS__
-#define __XEN_TOOLS_LIBS__
+#ifndef __XEN_TOOLS_COMMON_MACROS__
+#define __XEN_TOOLS_COMMON_MACROS__
+
+/*
+ * Caution:
+ *
+ * This header must be completely self-contained. There are no external
+ * references to variables or functions allowed, as the file might be included
+ * for different runtime environments, such as firmware or target and build
+ * host programs.
+ */
 
 #ifndef BUILD_BUG_ON
 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
@@ -67,4 +76,4 @@
 #define __must_check __attribute__((__warn_unused_result__))
 #endif
 
-#endif	/* __XEN_TOOLS_LIBS__ */
+#endif	/* __XEN_TOOLS_COMMON_MACROS__ */
tools/libs/call/buffer.c +1 -1
index 085674d882..2579b8c719 100644
@@ -16,7 +16,7 @@
 #include <errno.h>
 #include <string.h>
 #include <pthread.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "private.h"
 
tools/libs/ctrl/xc_pm.c +1 -1
index 76d7eb7f26..c3a9864bf7 100644
@@ -21,7 +21,7 @@
 #include <stdbool.h>
 #include "xc_private.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 /*
  * Get PM statistic info
tools/libs/ctrl/xc_private.h +1 -1
index ed960c6f30..80dc464c93 100644
@@ -39,7 +39,7 @@
 
 #include <xen/sys/privcmd.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #if defined(HAVE_VALGRIND_MEMCHECK_H) && !defined(NDEBUG) && !defined(__MINIOS__)
 /* Compile in Valgrind client requests? */
tools/libs/foreignmemory/linux.c +1 -1
index 9062117407..12f959765a 100644
@@ -25,7 +25,7 @@
 
 #include <sys/mman.h>
 #include <sys/ioctl.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "private.h"
 
tools/libs/gnttab/freebsd.c +1 -1
index e42ac3fbf3..d69d928a16 100644
@@ -31,7 +31,7 @@
 #include <xen/sys/gntdev.h>
 
 #include <xenctrl.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "private.h"
 
tools/libs/gnttab/linux.c +1 -1
index 5628fd5719..7286c1d4fe 100644
@@ -33,7 +33,7 @@
 #include <xen/sys/gntalloc.h>
 
 #include <xenctrl.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "private.h"
 
tools/libs/guest/xg_core_arm.c +1 -1
index aaabd07558..de30cf0c31 100644
@@ -19,7 +19,7 @@
 #include "xc_private.h"
 #include "xg_core.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 int
 xc_core_arch_gpfn_may_present(struct xc_core_arch_context *arch_ctxt,
tools/libs/guest/xg_cpuid_x86.c +1 -1
index 57f81eb0a0..4542878bbe 100644
@@ -24,7 +24,7 @@
 #include <limits.h>
 #include "xg_private.h"
 #include <xen/hvm/params.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 enum {
 #define XEN_CPUFEATURE(name, value) X86_FEATURE_##name = value,
tools/libs/guest/xg_dom_arm.c +1 -1
index 5e3b76355e..d616dccbd6 100644
@@ -21,7 +21,7 @@
 
 #include <xen/xen.h>
 #include <xen/io/protocols.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xg_private.h"
 
tools/libs/guest/xg_dom_bzimageloader.c +1 -1
index 764ddbb43e..c6ee6d83e7 100644
@@ -34,7 +34,7 @@
 #include "xg_private.h"
 #include "xg_dom_decompress.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #ifndef __MINIOS__
 
tools/libs/guest/xg_dom_x86.c +1 -1
index 9328fbf804..811d176488 100644
@@ -35,7 +35,7 @@
 #include <xen/arch-x86/hvm/start_info.h>
 #include <xen/io/protocols.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xg_private.h"
 #include "xenctrl.h"
tools/libs/guest/xg_sr_common.c +1 -1
index 17567ab133..7ccdc3b1f6 100644
@@ -2,7 +2,7 @@
 
 #include "xg_sr_common.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static const char *const dhdr_types[] =
 {
tools/libs/light/libxl_internal.h +1 -1
index ad982d691a..5244fde623 100644
@@ -58,7 +58,7 @@
 #include <xenguest.h>
 #include <xenhypfs.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xentoollog.h"
 
tools/libs/light/libxl_psr.c +1 -1
index 9ced7d1715..712a55a73d 100644
@@ -16,7 +16,7 @@
 #include "libxl_osdeps.h" /* must come before any other headers */
 #include "libxl_internal.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define IA32_QM_CTR_ERROR_MASK         (0x3ul << 62)
 
tools/libs/stat/xenstat_linux.c +1 -1
index ebcd65f24a..05d0b07553 100644
@@ -30,7 +30,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <regex.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xenstat_priv.h"
 
tools/misc/xen-access.c +1 -1
index 0731c20b83..4097eebe6f 100644
@@ -43,7 +43,7 @@
 #include <xenevtchn.h>
 #include <xen/vm_event.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #if defined(__arm__) || defined(__aarch64__)
 #include <xen/arch-arm.h>
tools/misc/xen-cpuid.c +1 -1
index 3ab820571d..8cf6a5ecfe 100644
@@ -10,7 +10,7 @@
 #include <xenctrl.h>
 #include <xenguest.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static uint32_t nr_features;
 
tools/misc/xen-diag.c +1 -1
index 4778914820..cd0c2c1fc6 100644
@@ -11,7 +11,7 @@
 #include <xenctrl.h>
 
 #include <xen/errno.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static xc_interface *xch;
 
tools/misc/xen-hptool.c +1 -1
index 7f17f24942..6d6aec8bad 100644
@@ -5,7 +5,7 @@
 #include <xenctrl.h>
 #include <xenguest.h>
 #include <xenstore.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static xc_interface *xch;
 
tools/misc/xen-livepatch.c +1 -1
index 16a4a47ab7..5bf9d9a32b 100644
@@ -15,7 +15,7 @@
 #include <xenstore.h>
 
 #include <xen/errno.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static xc_interface *xch;
 
tools/misc/xen-mfndump.c +1 -1
index 62121bd241..b32c95e262 100644
@@ -8,7 +8,7 @@
 #include <xenctrl.h>
 #include <xenguest.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define M2P_SIZE(_m)    ROUNDUP(((_m) * sizeof(xen_pfn_t)), 21)
 #define is_mapped(pfn_type) (!((pfn_type) & 0x80000000UL))
tools/misc/xenpm.c +1 -1
index 4f8cde690a..1bb6187e56 100644
@@ -28,7 +28,7 @@
 #include <inttypes.h>
 #include <sys/time.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define MAX_PKG_RESIDENCIES 12
 #define MAX_CORE_RESIDENCIES 8
tools/ocaml/libs/mmap/xenmmap_stubs.c +1 -1
index d623ad390e..c85b1fcce7 100644
@@ -21,7 +21,7 @@
 #include <errno.h>
 #include "mmap_stubs.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include <caml/mlvalues.h>
 #include <caml/memory.h>
tools/ocaml/libs/xc/xenctrl_stubs.c +1 -1
index ed1cbafdb4..6ec9ed6d1e 100644
@@ -33,7 +33,7 @@
 #define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 #include <xenguest.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "mmap_stubs.h"
 
tools/python/xen/lowlevel/xc/xc.c +1 -1
index cfb2734a99..224671bc02 100644
@@ -22,7 +22,7 @@
 #include <xen/hvm/hvm_info_table.h>
 #include <xen/hvm/params.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 /* Needed for Python versions earlier than 2.3. */
 #ifndef PyMODINIT_FUNC
tools/tests/cpu-policy/test-cpu-policy.c +1 -1
index ed450a0997..d3f24fd6d2 100644
@@ -7,7 +7,7 @@
 #include <string.h>
 #include <err.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 #include <xen/asm/x86-vendors.h>
 #include <xen/lib/x86/cpu-policy.h>
 #include <xen/domctl.h>
tools/tests/paging-mempool/test-paging-mempool.c +1 -1
index b2dfe9b074..1ebc13455a 100644
@@ -8,7 +8,7 @@
 #include <xenctrl.h>
 #include <xenforeignmemory.h>
 #include <xengnttab.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static unsigned int nr_failures;
 #define fail(fmt, ...)                          \
tools/tests/resource/test-resource.c +1 -1
index 0a950072f9..7ae88ea348 100644
@@ -7,7 +7,7 @@
 #include <xenctrl.h>
 #include <xenforeignmemory.h>
 #include <xengnttab.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static unsigned int nr_failures;
 #define fail(fmt, ...)                          \
tools/tests/tsx/test-tsx.c +1 -1
index f11e8c54e0..d6d98c299b 100644
@@ -28,7 +28,7 @@
 
 #include <xenctrl.h>
 #include <xenguest.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xg_private.h"
 
tools/tests/vhpet/emul.h +1 -1
index b022cc0eab..dfeb10f74e 100644
@@ -24,7 +24,7 @@
 
 #include "hpet.h"
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define NR_CPUS 8
 
tools/tests/x86_emulator/x86-emulate.h +1 -1
index 18ae40d017..46d4e43cea 100644
@@ -39,7 +39,7 @@
 #include <xen/asm/x86-defns.h>
 #include <xen/asm/x86-vendors.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define BUG() abort()
 #define ASSERT assert
tools/tests/xenstore/test-xenstore.c +1 -1
index d3574b3fa2..d491dac53b 100644
@@ -31,7 +31,7 @@
 #include <time.h>
 #include <xenstore.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define TEST_PATH "xenstore-test"
 #define WRITE_BUFFERS_N    10
tools/xenstore/utils.h +1 -1
index 9d012b97c1..028ecb9d7a 100644
@@ -5,7 +5,7 @@
 #include <stdint.h>
 #include <stdio.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xenstore_lib.h"
 
tools/xentrace/analyze.h +1 -1
index f9d98d8da4..6fefa19639 100644
@@ -3,7 +3,7 @@
 
 #include <stdint.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #define TRC_GEN_MAIN     0
 #define TRC_SCHED_MAIN   1
tools/xl/xl_cmdtable.c +1 -1
index 35182ca196..ccf4d83584 100644
@@ -15,7 +15,7 @@
 #include <string.h>
 
 #include <libxl.h>
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 #include "xl.h"
 
xen/lib/x86/private.h +1 -1
index 2d53bd3ced..60bb82a400 100644
@@ -26,7 +26,7 @@
 #include <xen/asm/msr-index.h>
 #include <xen/asm/x86-vendors.h>
 
-#include <xen-tools/libs.h>
+#include <xen-tools/common-macros.h>
 
 static inline bool test_bit(unsigned int bit, const void *vaddr)
 {
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH v3 1/4] tools: rename xen-tools/libs.h file to common-macros.h

Quoted 9 lines · Juergen Gross

In order to better reflect the contents of the header and to make it more appropriate to use it for different runtime environments like programs, libraries, and firmware, rename the libs.h include file to common-macros.h. Additionally add a comment pointing out the need to be self-contained.

Just as a general remark: I think it would be nice to strive to keep such scope restriction identifiers short as possible without becoming ambiguous. With this ...

Quoted 34 lines
---
 tools/console/daemon/io.c                         |  2 +-
 tools/fuzz/cpu-policy/afl-policy-fuzzer.c         |  2 +-
 .../include/xen-tools/{libs.h => common-macros.h} | 15 ++++++++++++---
 tools/libs/call/buffer.c                          |  2 +-
 tools/libs/ctrl/xc_pm.c                           |  2 +-
 tools/libs/ctrl/xc_private.h                      |  2 +-
 tools/libs/foreignmemory/linux.c                  |  2 +-
 tools/libs/gnttab/freebsd.c                       |  2 +-
 tools/libs/gnttab/linux.c                         |  2 +-
 tools/libs/guest/xg_core_arm.c                    |  2 +-
 tools/libs/guest/xg_cpuid_x86.c                   |  2 +-
 tools/libs/guest/xg_dom_arm.c                     |  2 +-
 tools/libs/guest/xg_dom_bzimageloader.c           |  2 +-
 tools/libs/guest/xg_dom_x86.c                     |  2 +-
 tools/libs/guest/xg_sr_common.c                   |  2 +-
 tools/libs/light/libxl_internal.h                 |  2 +-
 tools/libs/light/libxl_psr.c                      |  2 +-
 tools/libs/stat/xenstat_linux.c                   |  2 +-
 tools/misc/xen-access.c                           |  2 +-
 tools/misc/xen-cpuid.c                            |  2 +-
 tools/misc/xen-diag.c                             |  2 +-
 tools/misc/xen-hptool.c                           |  2 +-
 tools/misc/xen-livepatch.c                        |  2 +-
 tools/misc/xen-mfndump.c                          |  2 +-
 tools/misc/xenpm.c                                |  2 +-
 tools/ocaml/libs/mmap/xenmmap_stubs.c             |  2 +-
 tools/ocaml/libs/xc/xenctrl_stubs.c               |  2 +-
 tools/python/xen/lowlevel/xc/xc.c                 |  2 +-
 tools/tests/cpu-policy/test-cpu-policy.c          |  2 +-
 tools/tests/paging-mempool/test-paging-mempool.c  |  2 +-
 tools/tests/resource/test-resource.c              |  2 +-
 tools/tests/tsx/test-tsx.c                        |  2 +-
 tools/tests/vhpet/emul.h                          |  2 +-
Quoted 8 lines
 tools/tests/x86_emulator/x86-emulate.h            |  2 +-
 tools/tests/xenstore/test-xenstore.c              |  2 +-
 tools/xenstore/utils.h                            |  2 +-
 tools/xentrace/analyze.h                          |  2 +-
 tools/xl/xl_cmdtable.c                            |  2 +-
 xen/lib/x86/private.h                             |  2 +-
 39 files changed, 50 insertions(+), 41 deletions(-)
 rename tools/include/xen-tools/{libs.h => common-macros.h} (82%)

... diffstat, tools/python/ would be sufficient here imo. (As noted earlier, I consider the marker unnecessary here, just like Christian's isn't scope restricted to tools/ocaml/. While I accept that views may differ here, I think it would be helpful if within a patch optional scope restrictions were all present or all absent; ones truly restricting scope are different of course.)

Jan

#3 lore raw

Re: [PATCH v3 1/4] tools: rename xen-tools/libs.h file to common-macros.h

Quoted 58 lines · Jan Beulich
Quoted 9 lines · Juergen Gross

In order to better reflect the contents of the header and to make it more appropriate to use it for different runtime environments like programs, libraries, and firmware, rename the libs.h include file to common-macros.h. Additionally add a comment pointing out the need to be self-contained.

Just as a general remark: I think it would be nice to strive to keep such scope restriction identifiers short as possible without becoming ambiguous. With this ...

Quoted 41 lines
---
  tools/console/daemon/io.c                         |  2 +-
  tools/fuzz/cpu-policy/afl-policy-fuzzer.c         |  2 +-
  .../include/xen-tools/{libs.h => common-macros.h} | 15 ++++++++++++---
  tools/libs/call/buffer.c                          |  2 +-
  tools/libs/ctrl/xc_pm.c                           |  2 +-
  tools/libs/ctrl/xc_private.h                      |  2 +-
  tools/libs/foreignmemory/linux.c                  |  2 +-
  tools/libs/gnttab/freebsd.c                       |  2 +-
  tools/libs/gnttab/linux.c                         |  2 +-
  tools/libs/guest/xg_core_arm.c                    |  2 +-
  tools/libs/guest/xg_cpuid_x86.c                   |  2 +-
  tools/libs/guest/xg_dom_arm.c                     |  2 +-
  tools/libs/guest/xg_dom_bzimageloader.c           |  2 +-
  tools/libs/guest/xg_dom_x86.c                     |  2 +-
  tools/libs/guest/xg_sr_common.c                   |  2 +-
  tools/libs/light/libxl_internal.h                 |  2 +-
  tools/libs/light/libxl_psr.c                      |  2 +-
  tools/libs/stat/xenstat_linux.c                   |  2 +-
  tools/misc/xen-access.c                           |  2 +-
  tools/misc/xen-cpuid.c                            |  2 +-
  tools/misc/xen-diag.c                             |  2 +-
  tools/misc/xen-hptool.c                           |  2 +-
  tools/misc/xen-livepatch.c                        |  2 +-
  tools/misc/xen-mfndump.c                          |  2 +-
  tools/misc/xenpm.c                                |  2 +-
  tools/ocaml/libs/mmap/xenmmap_stubs.c             |  2 +-
  tools/ocaml/libs/xc/xenctrl_stubs.c               |  2 +-
  tools/python/xen/lowlevel/xc/xc.c                 |  2 +-
  tools/tests/cpu-policy/test-cpu-policy.c          |  2 +-
  tools/tests/paging-mempool/test-paging-mempool.c  |  2 +-
  tools/tests/resource/test-resource.c              |  2 +-
  tools/tests/tsx/test-tsx.c                        |  2 +-
  tools/tests/vhpet/emul.h                          |  2 +-
  tools/tests/x86_emulator/x86-emulate.h            |  2 +-
  tools/tests/xenstore/test-xenstore.c              |  2 +-
  tools/xenstore/utils.h                            |  2 +-
  tools/xentrace/analyze.h                          |  2 +-
  tools/xl/xl_cmdtable.c                            |  2 +-
  xen/lib/x86/private.h                             |  2 +-
  39 files changed, 50 insertions(+), 41 deletions(-)
Quoted 1 line

rename tools/include/xen-tools/{libs.h => common-macros.h} (82%)

Quoted 6 lines

... diffstat, tools/python/ would be sufficient here imo. (As noted earlier, I consider the marker unnecessary here, just like Christian's isn't scope restricted to tools/ocaml/. While I accept that views may differ here, I think it would be helpful if within a patch optional scope restrictions were all present or all absent; ones truly restricting scope are different of course.)

Hmm, right. In case I need to resend I'll drop the restriction.

Juergen

Acked-by on v3 1/4
#4 lore raw

Re: [PATCH v3 1/4] tools: rename xen-tools/libs.h file to common-macros.h

In reply to Juergen Gross
Quoted 10 lines · Juergen Gross

In order to better reflect the contents of the header and to make it more appropriate to use it for different runtime environments like programs, libraries, and firmware, rename the libs.h include file to common-macros.h. Additionally add a comment pointing out the need to be self-contained.

Thanks,

Signature · 1 line
Anthony PERARD
#5 lore raw

PATCH v3 2/4 tools: add container_of() macro to xen-tools/common-macros.h

In reply to Juergen Gross

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/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/common-macros.h +4 -0
index a372b9ecf2..b046ab48bf 100644
@@ -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/vhpet/emul.h +0 -3
index dfeb10f74e..610641ab0c 100644
@@ -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
index f03e3a56d1..7169a2ea02 100644
@@ -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
index 46d4e43cea..1af986f78d 100644
@@ -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
index b17d13e0ec..a464a38b61 100644
@@ -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
#6 lore raw

Re: [PATCH v3 2/4] tools: add container_of() macro to xen-tools/common-macros.h

Quoted 1 line · Juergen Gross

Instead of having 4 identical copies of the definition of a

3 now ;-), as tests/vhpet has been removed.

Quoted 6 lines

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.

Quoted 14 lines

---
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, ...).

I would like to keep the functionality where we can use a type or a var, as this is more convenient. Even Linux developer wants this extra functionality as I've seen a couple of use of container_of() with "typeof(*var)" in the 2nd parameter (in linux source code).

I don't know if having our macro "container_of()" been different than the one from Linux or QEMU is going to be an issue, if that's not likely to be an issue we could add the functionality, but if it's likely to be an issue we could instead replace "container_of()" by "CONTAINER_OF()".

The issue that I could see with adding "*var" option to container_of() is if someone copies code from Xen into other projects, and not realizing that container_of() is different. While if we spell it "CONTAINER_OF()" instead, it would be less of an issue as the macro would just be missing. (But maybe the first case just mean the compiler will complain)

So I'm in favor of having only one macro, with the functionality of the existing "CONTAINER_OF()" macro, and I guess spell it "CONTAINER_OF()" instead of "container_of()". Unless you think the lower case spelling isn't likely to be an issue.

Quoted 14 lines
---
 tools/include/xen-tools/common-macros.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/common-macros.h +0 -0
index a372b9ecf2..b046ab48bf 100644

tools/include/xen-tools/common-macros.h line 80

Quoted 5 lines
tools/include/xen-tools/common-macros.h +2 -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);        \

I think identifier starting with two '_' are supposed to be reserved. Would you be ok to have just one? (So "_mptr")

Quoted 8 lines
tools/include/xen-tools/common-macros.h +2 -0
+    (type *)( (char *)__mptr - offsetof(type,member) );})
+
 #endif	/* __XEN_TOOLS_COMMON_MACROS__ */
tools/tests/vpci/emul.h +0 -0
index f03e3a56d1..7169a2ea02 100644

tools/tests/vpci/emul.h line 30

Quoted 9 lines
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>

This doesn't build, so some change are needed in the Makefile.

I wondered why the gitlab ci was green while this failed to build on my machine, and it turns out that the "default" target been used is often "install", which does nothing for vpci. But I guess this should be fixed by https://lore.kernel.org/r/[email protected]

Cheers,

Signature · 1 line
Anthony PERARD
#7 lore raw

Re: [PATCH v3 2/4] tools: add container_of() macro to xen-tools/common-macros.h

Quoted 7 lines · Anthony PERARD
Quoted 6 lines · Juergen Gross
tools/include/xen-tools/common-macros.h +0 -0
@@ -76,4 +76,8 @@
 #define __must_check __attribute__((__warn_unused_result__))
 #endif
 
Quoted 5 lines

tools/include/xen-tools/common-macros.h line 80

Quoted 2 lines
tools/include/xen-tools/common-macros.h +2 -0
+#define container_of(ptr, type, member) ({                \
+    typeof( ((type *)0)->member ) *__mptr = (ptr);        \

I think identifier starting with two '_' are supposed to be reserved. Would you be ok to have just one? (So "_mptr")

Except that single-underscore prefixed names are also kind of reserved (for file-scope symbols). Hence why I'm generally suggesting / asking for trailing underscores to be used.

Jan

#8 lore raw

PATCH v3 3/4 tools: get rid of additional min() and max() definitions

In reply to Juergen Gross

Defining 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/vhpet/emul.h        | 11 +----------
 tools/tests/vpci/emul.h         | 16 ----------------
 4 files changed, 4 insertions(+), 34 deletions(-)
tools/firmware/hvmloader/util.h +2 -6
index 8d95eab28a..e04990ee97 100644
@@ -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
index 9195bd3b98..021e1f29e1 100644
@@ -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/vhpet/emul.h +1 -10
index 610641ab0c..6af880cd43 100644
@@ -114,16 +114,7 @@ enum
     TASKLET_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
index 7169a2ea02..8c5bcadd5f 100644
@@ -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
#9 lore raw

PATCH v3 4/4 tools: add offsetof() to xen-tools/common-macros.h

In reply to Juergen Gross

Instead of having multiple files defining offsetof(), add the definition to tools/include/xen-tools/common-macros.h.

---
V3:
- don't modify tools/firmware/include/stddef.h (Jan Beulich)

---
 tools/firmware/hvmloader/util.h         | 3 ---
 tools/include/xen-tools/common-macros.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 --
 6 files changed, 7 insertions(+), 12 deletions(-)
tools/firmware/hvmloader/util.h +0 -3
index e04990ee97..7249773eeb 100644
@@ -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/include/xen-tools/common-macros.h +4 -0
index b046ab48bf..b149be8365 100644
@@ -80,4 +80,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_COMMON_MACROS__ */
tools/libfsimage/Rules.mk +2 -0
index cf37d6cb0d..85674f2379 100644
@@ -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
index d735a88e55..f562daaef0 100644
@@ -19,6 +19,7 @@
 
 #include <xenfsimage_grub.h>
 #include "xfs.h"
+#include <xen-tools/common-macros.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
index 021e1f29e1..ad4cc66d2c 100644
@@ -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
index 6af880cd43..7b3ee88fb5 100644
@@ -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
#10 lore raw

Re: [PATCH v3 4/4] tools: add offsetof() to xen-tools/common-macros.h

tools/include/xen-tools/common-macros.h line 85

Quoted 9 lines · Juergen Gross
tools/include/xen-tools/common-macros.h +3 -0
@@ -80,4 +80,8 @@
     typeof( ((type *)0)->member ) *__mptr = (ptr);        \
     (type *)( (char *)__mptr - offsetof(type,member) );})
 
+#ifndef offsetof
+#define offsetof(a, b) __builtin_offsetof(a, b)
+#endif

How's this going to work reliably with parties perhaps also including stddef.h, which also is expected to define this macro, and which may do so with a different sequence of tokens (even if that ends up having the same effect)? There shouldn't be a need to define this macro for any "normal" environments, e.g. ...

Quoted 11 lines
tools/libfsimage/xfs/fsys_xfs.c +1 -0
@@ -19,6 +19,7 @@
 
 #include <xenfsimage_grub.h>
 #include "xfs.h"
+#include <xen-tools/common-macros.h>
 
 #define MAX_LINK_COUNT	8
 

tools/libfsimage/xfs/fsys_xfs.c fsb2daddr (xfs_fsblock_t fsbno) · line 188

Quoted 9 lines
tools/libfsimage/xfs/fsys_xfs.c +0 -3
@@ -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)
 {

... this library or ...

tools/libs/vchan/init.c line 74

Quoted 13 lines
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;

... this tool or ...

tools/tests/vhpet/emul.h enum · line 120

Quoted 11 lines
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

... this test binary (which is an ordinary host one as well). They all should include stddef.h and be done.

Jan

#11 lore raw

Re: [PATCH v3 4/4] tools: add offsetof() to xen-tools/common-macros.h

Quoted 62 lines · Jan Beulich

tools/include/xen-tools/common-macros.h line 85

Quoted 9 lines · Juergen Gross
tools/include/xen-tools/common-macros.h +3 -0
@@ -80,4 +80,8 @@
      typeof( ((type *)0)->member ) *__mptr = (ptr);        \
      (type *)( (char *)__mptr - offsetof(type,member) );})
 
+#ifndef offsetof
+#define offsetof(a, b) __builtin_offsetof(a, b)
+#endif

How's this going to work reliably with parties perhaps also including stddef.h, which also is expected to define this macro, and which may do so with a different sequence of tokens (even if that ends up having the same effect)? There shouldn't be a need to define this macro for any "normal" environments, e.g. ...

Quoted 11 lines
tools/libfsimage/xfs/fsys_xfs.c +1 -0
@@ -19,6 +19,7 @@
 
  #include <xenfsimage_grub.h>
  #include "xfs.h"
+#include <xen-tools/common-macros.h>
 
  #define MAX_LINK_COUNT	8
 

tools/libfsimage/xfs/fsys_xfs.c fsb2daddr (xfs_fsblock_t fsbno) · line 188

Quoted 9 lines
tools/libfsimage/xfs/fsys_xfs.c +0 -3
@@ -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)
  {

... this library or ...

tools/libs/vchan/init.c line 74

Quoted 13 lines
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;

... this tool or ...

Quoted 6 lines
tools/tests/vhpet/emul.h +0 -0
@@ -115,8 +115,6 @@ enum
      NR_COMMON_SOFTIRQS
  };
 
Quoted 8 lines

tools/tests/vhpet/emul.h enum · line 120

Quoted 5 lines
tools/tests/vhpet/emul.h +0 -2
-#define offsetof(t, m) ((unsigned long )&((t *)0)->m)
-
  struct domain;
 
  struct vcpu

... this test binary (which is an ordinary host one as well). They all should include stddef.h and be done.

Yes, this is probably a better way to handle this.

So I think this patch should be dropped.

I'll send out another small series removing the private offsetof() definitions and using stddef.h instead. This should include hvmloader, which can use the tools/firmware/include/stddef.h variant.

Juergen

Acked-by on the v3 series
#12 lore raw
In reply to Juergen Gross
Quoted 10 lines · Juergen Gross

There are some macros defined multiple times in tools. Use only a single header file for defining those macros and drop the copies.

V2:
- add patch 1 (Andrew Cooper)

Quoted 8 lines

V3:
- address comments

Juergen Gross (4):
tools: rename xen-tools/libs.h file to common-macros.h
tools: add container_of() macro to xen-tools/common-macros.h
tools: get rid of additional min() and max() definitions
tools: add offsetof() to xen-tools/common-macros.h