[systemd-devel] [PATCH] CODING_STYLE: this also help with unaligned memory accesses

Shawn Landden shawn at churchofgit.com
Thu Mar 26 23:39:00 PDT 2015


On Thu, Mar 26, 2015 at 1:31 AM, Lennart Poettering
<lennart at poettering.net> wrote:
> On Tue, 24.03.15 11:16, Shawn Landden (shawn at churchofgit.com) wrote:
>
>> And those arches don't get much testing too.
>> ---
>>  CODING_STYLE | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/CODING_STYLE b/CODING_STYLE
>> index b687e72..8934954 100644
>> --- a/CODING_STYLE
>> +++ b/CODING_STYLE
>> @@ -14,7 +14,8 @@
>>  - The destructors always unregister the object from the next bigger
>>    object, not the other way around
>>
>> -- To minimize strict aliasing violations, we prefer unions over casting
>> +- To minimize strict aliasing violations and unaligned memory accesses,
>> +  we prefer unions over casting
>
> Unaligned memory accesses are an orthogonal problem really. I don't
> see how the change above really makes sense?
casting often leads to unaligned memory accesses. Without casting the
compiler usually gets it right, but yeah this has little to do with
unions. I was confusing with having a packed and unpacked struct and
converting between the two like thus:

typedef struct {
uint8_t a
uint32_t b
} packed __attribute__((packed));

#if HAVE_FAST_UNALIGNED
#define unpacked packed
#elsif
typedef struct {
uint8_t a
// uint8_t __padding[3]; gcc will do this for you....
uint32_t b
} unpacked;
#endif

foo get_foo(p *void) {
  unpacked e;
  e.a = (*packed)->a;
  e.b = (*packed)->b;
  return e;
}
>
> Lennart
>
> --
> Lennart Poettering, Red Hat
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel



-- 
Shawn Landden


More information about the systemd-devel mailing list