[libvirt] [PATCH 1/3] virutil: Introduce a new macro named CLAMP.

Martin Kletzander mkletzan at redhat.com
Thu May 15 11:53:00 UTC 2014


On Thu, May 15, 2014 at 06:39:49PM +0900, Dongsheng Yang wrote:
>This patch introduce a new macro to return a
>value clamped to a given range.
>
>Signed-off-by: Dongsheng Yang <yangds.fnst at cn.fujitsu.com>
>---
> src/util/virutil.h | 6 ++++++
> 1 file changed, 6 insertions(+)
>
>diff --git a/src/util/virutil.h b/src/util/virutil.h
>index 2bb74e2..e8536d8 100644
>--- a/src/util/virutil.h
>+++ b/src/util/virutil.h
>@@ -37,6 +37,12 @@
> # ifndef MAX
> #  define MAX(a, b) ((a) > (b) ? (a) : (b))
> # endif
>+# ifndef CLAMP
>+#  define CLAMP(v, min, max) ({         \
>+        typeof(v) _v = v;               \
>+        _v = _v < min ? min: _v;        \
>+        _v > max ? max: _v; })
>+# endif
>

It's just my subjective impression, but wouldn't the following be a
bit more readable and less obfuscated?

#define CLAMP(v, min, max) MAX(MIN(v, max), min)

Martin.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20140515/116fd8cc/attachment-0001.sig>


More information about the libvir-list mailing list