Hi guys,
I’ve asked this question some time ago, and saw that packing has been
fixed for 64-bit, but I still think it should be fixed for the 32-bit
case. Even better, remove #pragma pack(push,xx) / pragma pack(pop) at
all, this way letting the default behavior rule, which hopefully would
make different compiler more happy to play with each other.
/* Force structure packing at 4 byte alignment.
This is necessary if the header is included in code which has structure
packing set to an alternate value, say for loading structures from disk.
The packing is reset to the previous value in close_code.h
/
#if defined(_MSC_VER) || defined(MWERKS) || defined(BORLANDC)
#ifdef _MSC_VER
#pragma warning(disable: 4103)
#endif
#ifdef BORLANDC
#pragma nopackwarning
#endif
#ifdef _M_X64
/ Use 8-byte alignment on 64-bit architectures, so pointers are aligned /
#pragma pack(push,8)
#else
#pragma pack(push,4)
#endif
#endif / Compiler needs structure packing set */
More specifically the fact that pragma packing is done only for
_MSC_VER, MWERKS, BORLANDC, but not for mingw/cygwin or others.
The problem here is that one cannot mix an sdl.dll produced by one
compiler with another.
The bigger problem, is that it’s hard for FFI bindings to any language
to communicate directly the packings, as the FFI binding don’t know what
DLL is going to be loaded.
For 64-bit we are fine now, as pack(push,8) is the default for MSVC
compilers.
but for 32-bit, the default is again 8 /Zp8 - e.g. pack(push,8) not 4
http://msdn.microsoft.com/en-us/library/2e70t5y1(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/2e70t5y1(v=VS.90).aspx
http://msdn.microsoft.com/en-us/library/2e70t5y1(v=VS.100).aspx
I still don’t see the need for such packing, there is one case (by just
looking for the double type, where some minor degration might be
happening). The “rate_incr” would get aligned to the 16-byte in the
non-MSVC compiler, and 12-byte with MSVC. There is also the len_ratio,
but haven’t checked what it’s going to be.
typedef struct SDL_AudioCVT
{
int needed; /< Set to 1 if conversion possible */
SDL_AudioFormat src_format; /< Source audio format */
SDL_AudioFormat dst_format; /< Target audio format */
double rate_incr; /< Rate conversion increment */
Uint8 *buf; /< Buffer to hold entire audio data */
int len; /< Length of original audio buffer /
int len_cvt; /< Length of converted audio buffer */
int len_mult; /< buffer must be lenlen_mult big /
double len_ratio; /**< Given len, final size is
lenlen_ratio */
SDL_AudioFilter filters[10]; /< Filter list */
int filter_index; /< Current audio conversion function */
} SDL_AudioCVT;
Alternatively if these pragma packs need to be kept, then maybe the
SDL_AudioCVT structure might have to be padded, so that it’s binary
compatible with different SDL.dll’s compiled from different compilers.
I have a luajit FFI library bindings -
https://github.com/malkia/ufo/blob/master/ffi/sdl.lua and so far I’ve
been manually patching begin_code.h/end_code.h to not have the pragma
packs, but this means I can’t reuse my code with different SDL.dll (or
someone else).
Thanks,
Dimiter ‘malkia’ Stanev.