Skip to contentSkip to conversation

tools/libxc: Introduce typesafe and side-effect safe min()/max() macros

PATCH In review Andrew Cooper 2 participants 5 messages Jul 8, 2014 – Jul 10, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libxc: Introduce typesafe and side-effect safe min()/max() macros

Replace the current users, and remove scattered re-definitions.

CC Ian Campbell <[email protected]>
CC Ian Jackson <[email protected]>
---
 tools/libxc/xc_core_x86.c                 |    4 ----
 tools/libxc/xc_dom_decompress_unsafe_xz.c |    6 ------
 tools/libxc/xc_domain_restore.c           |    2 +-
 tools/libxc/xc_private.h                  |   12 ++++++++++++
 tools/libxc/xg_private.h                  |    2 +-
 tools/libxc/xg_save_restore.h             |    7 -------
 6 files changed, 14 insertions(+), 19 deletions(-)
tools/libxc/xc_core_x86.c +0 -4
index e328dcf..f05060a 100644
@@ -24,10 +24,6 @@
 
 #define GET_FIELD(_p, _f) ((dinfo->guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f))
 
-#ifndef MAX
-#define MAX(_a, _b) ((_a) >= (_b) ? (_a) : (_b))
-#endif
-
 int
 xc_core_arch_gpfn_may_present(struct xc_core_arch_context *arch_ctxt,
                               unsigned long pfn)
tools/libxc/xc_dom_decompress_unsafe_xz.c +0 -6
index 2a32d40..d4b8ac8 100644
@@ -34,12 +34,6 @@ static inline u32 le32_to_cpup(const u32 *p)
 	return cpu_to_le32(*p);
 }
 
-#define min(x,y) ({ \
-        const typeof(x) _x = (x);       \
-        const typeof(y) _y = (y);       \
-        (void) (&_x == &_y);            \
-        _x < _y ? _x : _y; })
-
 #define min_t(type,x,y) \
         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
 #define max_t(type,x,y) \
tools/libxc/xc_domain_restore.c +1 -1
index 071ab6a..1a99370 100644
@@ -339,7 +339,7 @@ static xen_pfn_t *load_p2m_frame_list(
             /* Any remaining bytes of this chunk: read and discard. */
             while ( chunk_bytes )
             {
-                unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t));
+                unsigned long sz = min((size_t)chunk_bytes, sizeof(xen_pfn_t));
                 if ( RDEXACT(io_fd, &p2m_fl_zero, sz) )
                 {
                     PERROR("read-and-discard extended-info chunk bytes failed");
tools/libxc/xc_private.h +12 -0
index 6cc0f2b..d7afc95 100644
@@ -367,4 +367,16 @@ int xc_mem_event_memop(xc_interface *xch, domid_t domain_id,
 void *xc_mem_event_enable(xc_interface *xch, domid_t domain_id, int param,
                           uint32_t *port);
 
+#define min(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \
+            (void) (&_x == &_y);                 \
+            (_x < _y) ? _x : _y; })
+
+#define max(X, Y) ({                             \
+            const typeof (X) _x = (X);           \
+            const typeof (Y) _y = (Y);           \
+            (void) (&_x == &_y);                 \
+            (_x > _y) ? _x : _y; })
+
 #endif /* __XC_PRIVATE_H__ */
tools/libxc/xg_private.h +1 -1
index e593364..36651ca 100644
@@ -159,7 +159,7 @@ static inline xen_pfn_t xc_pfn_to_mfn(xen_pfn_t pfn, xen_pfn_t *p2m,
 /* Size in bytes of the pfn_to_mfn_frame_list     */
 #define P2M_GUEST_FL_SIZE ((P2M_FL_ENTRIES) * (dinfo->guest_width))
 #define P2M_TOOLS_FL_SIZE ((P2M_FL_ENTRIES) *                           \
-                           MAX((sizeof (xen_pfn_t)), dinfo->guest_width))
+                           max(sizeof(xen_pfn_t), (size_t)dinfo->guest_width))
 
 /* Masks for PTE<->PFN conversions */
 #define MADDR_BITS_X86  ((dinfo->guest_width == 8) ? 52 : 44)
tools/libxc/xg_save_restore.h +0 -7
index aa93c13..bdd9009 100644
@@ -392,10 +392,3 @@ static inline int get_platform_info(xc_interface *xch, uint32_t dom,
     else                                                           \
         memset(&(_p)->x32._f[0], (_v), sizeof((_p)->x32._f));      \
 } while (0)
-
-#ifndef MAX
-#define MAX(_a, _b) ((_a) >= (_b) ? (_a) : (_b))
-#endif
-#ifndef MIN
-#define MIN(_a, _b) ((_a) <= (_b) ? (_a) : (_b))
-#endif
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#1 lore raw
Quoted 3 lines · Andrew Cooper

Replace the current users, and remove scattered re-definitions.

tools/libxc/xc_domain_restore.c static xen_pfn_t *load_p2m_frame_list( · line 342

Quoted 2 lines
tools/libxc/xc_domain_restore.c +1 -1
-                unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t));
+                unsigned long sz = min((size_t)chunk_bytes, sizeof(xen_pfn_t));

FWIW on the hyperevisor side we have max_t and min_t to handle these cases where a cast is needed.

I think it's a little bit safer too since rather than casting it assigns the arguments to temporary variables of the named types (so any type promotion etc mismatches are flagged rather than hidden by the cast).

Ian.

#2 lore raw
Quoted 5 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

Replace the current users, and remove scattered re-definitions.

Quoted 7 lines

tools/libxc/xc_domain_restore.c static xen_pfn_t *load_p2m_frame_list( · line 342

Quoted 2 lines
tools/libxc/xc_domain_restore.c +1 -1
-                unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t));
+                unsigned long sz = min((size_t)chunk_bytes, sizeof(xen_pfn_t));

FWIW on the hyperevisor side we have max_t and min_t to handle these cases where a cast is needed.

Which I've just noticed/remembered I promoted to xc_private.h in my ARM p2m superpages series!

Quoted 11 lines

I think it's a little bit safer too since rather than casting it assigns the arguments to temporary variables of the named types (so any type promotion etc mismatches are flagged rather than hidden by the cast).

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw
Quoted 5 lines · Ian Campbell
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

Replace the current users, and remove scattered re-definitions.

Quoted 8 lines
Quoted 6 lines

tools/libxc/xc_domain_restore.c static xen_pfn_t *load_p2m_frame_list( · line 342

Quoted 2 lines
tools/libxc/xc_domain_restore.c +1 -1
-                unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t));
+                unsigned long sz = min((size_t)chunk_bytes, sizeof(xen_pfn_t));

FWIW on the hyperevisor side we have max_t and min_t to handle these cases where a cast is needed.

Which I've just noticed/remembered I promoted to xc_private.h in my ARM p2m superpages series!

I noticed that. I guess it depends which patch gets committed first.

This patch is a prerequisite of the writev() patch, which is a prerequisite for my migration v2.

If you wish, I can do a v2 which moves min_t/max_t at the same time and uses them in preference.

~Andrew

#4 lore raw
Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Ian Campbell
Quoted 6 lines · Ian Campbell
Quoted 3 lines · Andrew Cooper

Replace the current users, and remove scattered re-definitions.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

tools/libxc/xc_domain_restore.c static xen_pfn_t *load_p2m_frame_list( · line 342

Quoted 2 lines
tools/libxc/xc_domain_restore.c +1 -1
-                unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t));
+                unsigned long sz = min((size_t)chunk_bytes, sizeof(xen_pfn_t));

FWIW on the hyperevisor side we have max_t and min_t to handle these cases where a cast is needed.

Which I've just noticed/remembered I promoted to xc_private.h in my ARM p2m superpages series!

I noticed that. I guess it depends which patch gets committed first.

I've just committed a batch of patches which includes my min_t/max_t one.

Quoted 5 lines

This patch is a prerequisite of the writev() patch, which is a prerequisite for my migration v2.

If you wish, I can do a v2 which moves min_t/max_t at the same time and uses them in preference.

If you rebase onto staging should get min_t and max_t provided already, then a v2 which uses them as appropriate would be appreciated, thanks.

Ian.