Skip to content
  • Christophe de Dinechin's avatar
    Avoid clang warnings on casts with stricter alignment requirements · babe5630
    Christophe de Dinechin authored and Christophe Fergeau's avatar Christophe Fergeau committed
    
    
    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 older ARM, misaligned
    accesses are emulated by the kernel, and support for them is optional.
    So we may end up with a fault.
    
    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 runtime
      warning will be issued if you configure with --enable-alignment-warnings.
    
    - SPICE_UNALIGNED_CAST(type, value) casts value to type, and indicates that
      we believe the resulting pointer is not always aligned. A debug message
      is issued if you configure with --enable-alignment-warnings.
    
    Any code using SPICE_UNALIGNED_CAST may need to be revisited in order
    to improve performance, e.g. by using memcpy. A few easy fixes have been
    made in his patch.
    
    Signed-off-by: default avatarChristophe de Dinechin <dinechin@redhat.com>
    babe5630