Re: [Libguestfs] [PATCH nbdkit v3 2/3] common: Introduce round up, down; and divide round up functions.

Eric Blake
Mon, 17 Sep 2018 21:34:06 -0000

On 9/17/18 4:27 PM, Richard W.M. Jones wrote:
> This refactoring also fixes a bug spotted by Eric Blake:
>
>    nbdkit --filter truncate pattern size=5G round-up=512
>
> results in a 1G virtual disk.
> ---

> +/* Round up i to next multiple of n (n must be a power of 2).
> + */
> +#define ROUND_UP(i, n) ({                          \
> +      assert (is_power_of_2 (n));                  \
> +      ((i) + (n) - 1) & -((typeof (i))n);          \
> +})

If we ever call ROUND_UP(32-bit, 0x100000000ULL), then this silently
truncates to 0 instead of producing a 64-bit value. On the other hand,
we are unlikely to be trying to round to sizes that large in the nbdkit
code base. So I can live with this definition, although it may need a
comment reminding the reader to exercise caution with n larger than 31 bits.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org