19 Jul 2012 05:41
packed data structures in jack2
Basil Nutmeg <basil+jack <at> li95-58.members.linode.com>
2012-07-19 03:41:12 GMT
2012-07-19 03:41:12 GMT
Hello, I found Jack2 was crashing on my ARM cpu, and I traced it down to a misuse of __attribute__((packed)). This CPU doesn't support misaligned memory references. packed structs are supported, by having the compiler emit a longer sequence of instructions. However, consider this code from common/JackAtomicArrayState.h: ... CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter) ... fCounter is a member of a class which is packed, and this happens to make it misaligned. Its address is taken and passed off to a different function, where it appears to the compiler to be an ordinary pointer. The compiler justifiably assumes the pointer is aligned, so it emits normal code, and the code crashes at runtime because the pointer isn't actually aligned. Looking around, I was surprised to see that lots of classes in Jack2 are marked packed, for example classes like JackConnectionManager, JackClientControl, and JackEngineControl, and I'm puzzled. Making JackEngineControl packed, for example, shrinks its nominal size from 1040 to 1032 (on this platform). It doesn't look like there are that many JackEngineControl instances floating around at runtime, so this isn't actually saving much memory. And it doesn't appear to be serialized anywhere. And on the down side, packed makes many of JackEngineControl's fields misaligned, making them slower to access. Also, it's possible to shrink JackEngineControl to size 1032 just by(Continue reading)
.
>
> however, as stephane noted: far easier to just change the macros that do
> this stuff so that they are no-ops.
This is easy to do, and it works for my project today. However, I was
thinking about what would be best for Jack in the long term.
I think it would be preferable to have a solution which could support
mixed 64/32 mode even on platforms which don't have misaligned accesses
(64-bit ARM chips are coming...), which would avoid the performance
issues, and which would avoid "breaking the rules" even on platforms
where it works, because clever compiler writers can still theoretically
find ways to take advantage of alignment assumptions there. And it would
undo what is technically an ABI break in the recent addition of "packed"
to <jack/types.h>.
I think it's even worthwhile to make moderately more invasive source
changes to achieve this. And, I'm willing to do some work to help make it
happen. But if the maintainers disagree, I'll move on.
>
> > As another idea would be to have a source file which simply includes all
> > the important headers and is compiled with -Wpadded and maybe even
> > -Werror, to catch any mistakes.
> >
>
> i don't this is portable to non-gnu compilers. i could be wrong.
Perhaps so, or perhaps other compilers have analogous features; I don't
know offhand. I'd be willing to do some research here. It might not be a
complete solution, but it might be a nice sanity check, regardless.
-- Basil

RSS Feed