[Spice-devel] [PATCH v4 1/3] Avoid clang warnings on casts with stricter alignment requirements
Christophe Fergeau
cfergeau at redhat.com
Wed May 31 09:36:44 UTC 2017
On Tue, May 30, 2017 at 02:51:49PM +0200, Christophe de Dinechin wrote:
> From: Christophe de Dinechin <dinechin at redhat.com>
>
> For example, something like this:
> uint8_t *p8;
> uint32_t *p32 = (uint32_t *) p8;
>
> generates a warning like this:
> spice-channel.c:1350:10: error: cast from 'uint8_t *' (aka 'unsigned char *') to
> 'uint32_t *' (aka 'unsigned int *') increases required alignment from 1 to
> 4 [-Werror,-Wcast-align]
>
> The warning indicates that we end up with a pointer to data that
> should be 4-byte aligned, but its value may be misaligned. On x86,
> this does not make much of a difference, except a relatively minor
> performance penalty. However, on platforms such as ARM, misaligned
> accesses are emulated by the kernel, and support for them is optional.
> So we may end up with a fault.
Nit: such as *older* ARM, since ARMv6, unaligned access are supported in
hardware (no idea about actual performance of such accesses though), see
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html
> The intent of the fix here is to make it easy to identify and rework
> places where actual mis-alignment occurs. Wherever casts raise the warning,
> they are replaced with a macro:
>
> - SPICE_ALIGNED_CAST(type, value) casts value to type, and indicates that
> we believe the resulting pointer is aligned. If it is not, a warning will
"a runtime warning will be issued. This check is disabled if NDEBUG is
defined at compile-time" (in other words, this is not an env var but a
preprocessor define)
> be issued. This runtime check is disabled if NDEBUG is defined
> (much like assert() as defined in <assert.h>)
>
> - SPICE_UNALIGNED_CAST(type, value) casts value to type, and indicates that
> we believe the resulting pointer is not always aligned.
>
> Any code using SPICE_UNALIGNED_CAST may need to be revisited in order
> to improve performance, e.g. by using memcpy.
>
> There are normally no warnings for SPICE_UNALIGNED_CAST, but it is possible
> to emit debug messages for mis-alignment in SPICE_UNALIGNED_CAST
> by configuring with CFLAGS=-DSPICE_DEBUG_ALIGNMENT.
Do we want both -DSPICE_DEBUG_ALIGNMENT and -DNDEBUG to enable/disable
slightly different things at compile time?
Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <https://lists.freedesktop.org/archives/spice-devel/attachments/20170531/3fa70dc8/attachment.sig>
More information about the Spice-devel
mailing list