All of lore.kernel.org
 help / color / mirror / Atom feed
* [Xen-devel] [PATCH] xen/x86: Use min macro instead of ternary operator
@ 2020-03-29  7:22 Simran Singhal
  2020-03-29 11:08 ` Julien Grall
  0 siblings, 1 reply; 3+ messages in thread
From: Simran Singhal @ 2020-03-29  7:22 UTC (permalink / raw)
  To: xen-devel
  Cc: Kevin Tian, Jan Beulich, Wei Liu, Andrew Cooper, George Dunlap,
	Jun Nakajima, Roger Pau Monné

Replace ternary operator with macro min as it is shorter and
thus increases code readability. Macro min return the minimum of the
two compared values.

Signed-off-by: Simran Singhal <singhalsimran0@gmail.com>
---
 xen/arch/x86/bzimage.c    | 2 +-
 xen/arch/x86/mm.c         | 2 +-
 xen/arch/x86/mm/p2m-ept.c | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/xen/arch/x86/bzimage.c b/xen/arch/x86/bzimage.c
index ac4fd428be..f396aa3445 100644
--- a/xen/arch/x86/bzimage.c
+++ b/xen/arch/x86/bzimage.c
@@ -136,7 +136,7 @@ int __init bzimage_parse(void *image_base, void **image_start,
         *image_len = output_len;
     }
 
-    return err > 0 ? 0 : err;
+    return min(0, err);
 }
 
 /*
diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
index 2fac67ad57..c7617f80e8 100644
--- a/xen/arch/x86/mm.c
+++ b/xen/arch/x86/mm.c
@@ -1988,7 +1988,7 @@ static int demote_l3_table(struct page_info *page)
         page->partial_flags = partial_flags;
         rc = -ERESTART;
     }
-    return rc > 0 ? 0 : rc;
+    return min(0, rc);
 }
 
 static int demote_l4_table(struct page_info *page)
diff --git a/xen/arch/x86/mm/p2m-ept.c b/xen/arch/x86/mm/p2m-ept.c
index eb0f0edfef..38faa4df52 100644
--- a/xen/arch/x86/mm/p2m-ept.c
+++ b/xen/arch/x86/mm/p2m-ept.c
@@ -1069,7 +1069,7 @@ static int ept_change_entry_type_range(struct p2m_domain *p2m,
     if ( sync )
         ept_sync_domain(p2m);
 
-    return rc < 0 ? rc : 0;
+    return min(rc, 0);
 }
 
 static void ept_memory_type_changed(struct p2m_domain *p2m)
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [Xen-devel] [PATCH] xen/x86: Use min macro instead of ternary operator
  2020-03-29  7:22 [Xen-devel] [PATCH] xen/x86: Use min macro instead of ternary operator Simran Singhal
@ 2020-03-29 11:08 ` Julien Grall
  2020-03-29 13:48   ` Wei Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Julien Grall @ 2020-03-29 11:08 UTC (permalink / raw)
  To: Simran Singhal, xen-devel
  Cc: Kevin Tian, Jan Beulich, Wei Liu, Andrew Cooper, George Dunlap,
	Jun Nakajima, Roger Pau Monné

Hi,

On 29/03/2020 08:22, Simran Singhal wrote:
> Replace ternary operator with macro min as it is shorter and
> thus increases code readability. Macro min return the minimum of the
> two compared values.

While I understand the ternary operator is doing exactly the same as a 
min(), I read the current code as "If there is an error then return the 
error code, otherwise return 0".

This is quite different from the meaning of "min" which is "I want the 
minimum of the two values". Therefore I am not convinced using min() is 
the right thing to do.

> Signed-off-by: Simran Singhal <singhalsimran0@gmail.com>
> ---
>   xen/arch/x86/bzimage.c    | 2 +-
>   xen/arch/x86/mm.c         | 2 +-
>   xen/arch/x86/mm/p2m-ept.c | 2 +-
>   3 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/xen/arch/x86/bzimage.c b/xen/arch/x86/bzimage.c
> index ac4fd428be..f396aa3445 100644
> --- a/xen/arch/x86/bzimage.c
> +++ b/xen/arch/x86/bzimage.c
> @@ -136,7 +136,7 @@ int __init bzimage_parse(void *image_base, void **image_start,
>           *image_len = output_len;
>       }
>   
> -    return err > 0 ? 0 : err;
> +    return min(0, err);
>   }
>   
>   /*
> diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c
> index 2fac67ad57..c7617f80e8 100644
> --- a/xen/arch/x86/mm.c
> +++ b/xen/arch/x86/mm.c
> @@ -1988,7 +1988,7 @@ static int demote_l3_table(struct page_info *page)
>           page->partial_flags = partial_flags;
>           rc = -ERESTART;
>       }
> -    return rc > 0 ? 0 : rc;
> +    return min(0, rc);
>   }
>   
>   static int demote_l4_table(struct page_info *page)
> diff --git a/xen/arch/x86/mm/p2m-ept.c b/xen/arch/x86/mm/p2m-ept.c
> index eb0f0edfef..38faa4df52 100644
> --- a/xen/arch/x86/mm/p2m-ept.c
> +++ b/xen/arch/x86/mm/p2m-ept.c
> @@ -1069,7 +1069,7 @@ static int ept_change_entry_type_range(struct p2m_domain *p2m,
>       if ( sync )
>           ept_sync_domain(p2m);
>   
> -    return rc < 0 ? rc : 0;
> +    return min(rc, 0);
>   }
>   
>   static void ept_memory_type_changed(struct p2m_domain *p2m)
> 

-- 
Julien Grall


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Xen-devel] [PATCH] xen/x86: Use min macro instead of ternary operator
  2020-03-29 11:08 ` Julien Grall
@ 2020-03-29 13:48   ` Wei Liu
  0 siblings, 0 replies; 3+ messages in thread
From: Wei Liu @ 2020-03-29 13:48 UTC (permalink / raw)
  To: Julien Grall
  Cc: Kevin Tian, Jun Nakajima, Wei Liu, Andrew Cooper, George Dunlap,
	Jan Beulich, xen-devel, Simran Singhal, Roger Pau Monné

On Sun, Mar 29, 2020 at 12:08:39PM +0100, Julien Grall wrote:
> Hi,
> 
> On 29/03/2020 08:22, Simran Singhal wrote:
> > Replace ternary operator with macro min as it is shorter and
> > thus increases code readability. Macro min return the minimum of the
> > two compared values.
> 
> While I understand the ternary operator is doing exactly the same as a
> min(), I read the current code as "If there is an error then return the
> error code, otherwise return 0".
> 
> This is quite different from the meaning of "min" which is "I want the
> minimum of the two values". Therefore I am not convinced using min() is the
> right thing to do.

I agree with Julien's assessment.

Wei.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-29 13:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-29  7:22 [Xen-devel] [PATCH] xen/x86: Use min macro instead of ternary operator Simran Singhal
2020-03-29 11:08 ` Julien Grall
2020-03-29 13:48   ` Wei Liu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.