diff mbox

[PATCHv5,1/6] util: introduce MIN_NON_ZERO

Message ID 1414253919-3044-2-git-send-email-pl@kamp.de
State New
Headers show

Commit Message

Peter Lieven Oct. 25, 2014, 4:18 p.m. UTC
at least in block layer we have the case of limits being defined for a
BlockDriverState. However, in this context often zero (0) has the special
meanining of undefined which means no limit. If two of those limits are
combined and the minimum is needed the minimum function should only return
zero if both parameters are zero.

Signed-off-by: Peter Lieven <pl@kamp.de>
---
 include/qemu/osdep.h |    6 ++++++
 1 file changed, 6 insertions(+)

Comments

Max Reitz Oct. 27, 2014, 8:24 a.m. UTC | #1
On 2014-10-25 at 18:18, Peter Lieven wrote:
> at least in block layer we have the case of limits being defined for a
> BlockDriverState. However, in this context often zero (0) has the special
> meanining of undefined which means no limit. If two of those limits are
> combined and the minimum is needed the minimum function should only return
> zero if both parameters are zero.
>
> Signed-off-by: Peter Lieven <pl@kamp.de>
> ---
>   include/qemu/osdep.h |    6 ++++++
>   1 file changed, 6 insertions(+)
>
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index 1565404..bcdf088 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -68,6 +68,12 @@ typedef signed int              int_fast16_t;
>   #define MAX(a, b) (((a) > (b)) ? (a) : (b))
>   #endif
>   
> +/* Minimum function that returns zero only iff both values are zero.
> + * Intended for use with unsigned values only. */
> +#ifndef MIN_NON_ZERO
> +#define MIN_NON_ZERO(a, b) (((a != 0) && (a) < (b)) ? (a) : (b))

*cough* This should be (((a) != 0 && (a) < (b)) ? (a) : (b)), so the 
parentheses need to enclose the "a", not the "a != 0".

I guess the maintainer could fix that up, though.

And with that fixed, of course,

Reviewed-by: Max Reitz <mreitz@redhat.com>

> +#endif
> +
>   #ifndef ROUND_UP
>   #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
>   #endif
diff mbox

Patch

diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 1565404..bcdf088 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -68,6 +68,12 @@  typedef signed int              int_fast16_t;
 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
 #endif
 
+/* Minimum function that returns zero only iff both values are zero.
+ * Intended for use with unsigned values only. */
+#ifndef MIN_NON_ZERO
+#define MIN_NON_ZERO(a, b) (((a != 0) && (a) < (b)) ? (a) : (b))
+#endif
+
 #ifndef ROUND_UP
 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
 #endif