[Jack-Devel] Alignment (again)

PrevNext  Index
DateWed, 03 Aug 2011 23:24:19 -0500
From Gabriel Beddingfield <[hidden] at gmail dot com>
To[hidden] at lists dot jackaudio dot org
Follow-UpClemens Ladisch Re: [Jack-Devel] Alignment (again)
Follow-UpSampo Savolainen Re: [Jack-Devel] Alignment (again)
I ran into a strange bug where the conclusion was that:

    float *buf = malloc(512*sizeof(float));

Is invalid/undefined code.  I don't have a copy of the C spec, but the 
C++ spec says, that since a `float` must be 4-byte aligned... so must a 
`float*`.[1]  However, malloc(3) has no alignment requirements.

This is a problem because, for instance, this is how Jack's ringbuffer 
allocates the buffer.  And then we turn around and use it for an array 
of float's.

In practice, this bug only rears its head when compiling with -O3 and 
some implementations libc.[2]

Therefore I propose that JACK switch from malloc(3) to something like 
posix_memalign(3) for most (or all) of its allocations.

-gabriel


[1] C++ Spec, ISO/IEC 14882:2003(E) 3.9.2, "Pointers to cv-qualified
     and cv-unqualified version (3.9.3) of layout-compatible types
     shall have the same value representation and alignment
     requirements (3.9).
[2] glibc is known to always provide an 8-byte aligned pointer
     with malloc(3).  However, I've found that eglibc returns an
     arbitrary alignment.
PrevNext  Index

1312431885.24333_0.ltw:2,a <4E3A1EF3.90004 at gmail dot com>