All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>, qemu-devel@nongnu.org
Cc: patches@linaro.org, Brad Smith <brad@comstyle.com>,
	Kamil Rytarowski <n54@gmx.com>
Subject: Re: [Qemu-devel] [PATCH for-2.11?] osdep.h: Make TIME_MAX handle different time_t types
Date: Thu, 23 Nov 2017 17:00:52 +0100	[thread overview]
Message-ID: <bdbc6042-bbae-b057-c9ee-769c6d7e8345@redhat.com> (raw)
In-Reply-To: <1511452598-6077-1-git-send-email-peter.maydell@linaro.org>

On 23/11/2017 16:56, Peter Maydell wrote:
> In our various supported host OSes, the time_t type may be either 32
> or 64 bit, and could in theory also be either signed or unsigned.
> Notably, in OpenBSD time_t is a 64 bit type even if 'long' is 32
> bits, so using LONG_MAX for TIME_MAX is incorrect.
> 
> Use an approach suggested by Paolo Bonzini which calculates
> the maximum value of the type rather than hardcoding it;
> to do this we use the TYPE_MAXIMUM macro from Gnulib.
> 
> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  include/qemu/osdep.h | 29 ++++++++++++++++++++++++++++-
>  1 file changed, 28 insertions(+), 1 deletion(-)
> 
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index 281782d..e8568a0 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -147,8 +147,35 @@ extern int daemon(int, int);
>  #if !defined(ESHUTDOWN)
>  #define ESHUTDOWN 4099
>  #endif
> +
> +/* time_t may be either 32 or 64 bits depending on the host OS, and
> + * can be either signed or unsigned, so we can't just hardcode a
> + * specific maximum value. This is not a C preprocessor constant,
> + * so you can't use TIME_MAX in an #ifdef, but for our purposes
> + * this isn't a problem.
> + */
> +
> +/* The macros TYPE_SIGNED, TYPE_WIDTH, and TYPE_MAXIMUM are from
> + * Gnulib, and are under the LGPL v2.1 or (at your option) any
> + * later version.
> + */
> +
> +/* True if the real type T is signed.  */
> +#define TYPE_SIGNED(t) (!((t)0 < (t)-1))
> +
> +/* The width in bits of the integer type or expression T.
> + * Padding bits are not supported.
> + */
> +#define TYPE_WIDTH(t) (sizeof(t) * CHAR_BIT)
> +
> +/* The maximum and minimum values for the integer type T.  */
> +#define TYPE_MAXIMUM(t)                                                \
> +  ((t) (!TYPE_SIGNED(t)                                                \
> +        ? (t)-1                                                        \
> +        : ((((t)1 << (TYPE_WIDTH(t) - 2)) - 1) * 2 + 1)))
> +
>  #ifndef TIME_MAX
> -#define TIME_MAX LONG_MAX
> +#define TIME_MAX TYPE_MAXIMUM(time_t)
>  #endif
>  
>  /* HOST_LONG_BITS is the size of a native pointer in bits. */
> 

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>

  parent reply	other threads:[~2017-11-23 16:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-23 15:56 [Qemu-devel] [PATCH for-2.11?] osdep.h: Make TIME_MAX handle different time_t types Peter Maydell
2017-11-23 15:59 ` no-reply
2017-11-23 16:00 ` Paolo Bonzini [this message]
2017-11-24 15:13   ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bdbc6042-bbae-b057-c9ee-769c6d7e8345@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=brad@comstyle.com \
    --cc=n54@gmx.com \
    --cc=patches@linaro.org \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.