   Structure Packing
  I don't understand structure packing, I need to pack a set of structures tightly#pragma pack(push, packing)#pragma pack(1)#pragma pack(pop, packing)in my MS3D model loader, what does this mean?What is #pragma?How do I pack structures in gcc for Linux? I'm trying to port my model loader to the PS2 and I can't seem to figure it out.Thanks for your help. :)   
Quote:Original post by Leo_E_49How do I pack structures in gcc for Linux? I'm trying to port my model loader to the PS2 and I can't seem to figure it out.Packing the structure tightly almost certainly results in misaligned structure members, and x86s are virtually the only modern processors capable of handling unaligned accesses.You'll have to load it manually member-by-member or translate it into a format better suited for the PS2.The pragmas directive itself is just a standard prefix for non-standard compiler extensions. GCC has it's own method of packing structure (probably through a __declspec) however it apparently supports Microsoft's syntax too.;  Ok thanks. :)Looks like I've got a lot of work ahead of me. Any tips on manually loading these? I know the basic principle.   ;Quote:Original post by Leo_E_49Any tips on manually loading these? I know the basic principle.Not really, I generally just write a bunch of functions for reading specific datatype (bytes/words/longs/floats) and do it manually.If performance is an issue (which seems considering that the PS2 is involved) then I suggest translating the models into a format as close to the internal representation as possible.   ;  Yeah well, I'm doing this for a university coursework and it's due in a week so performance is the least of my worries. :(Thanks again for your help, you've given me hope that I can actually finish this on time. :)   ;  Okay, I think I don't understand what structure packing actually is.What is structure packing and what does #pragma pack actually do?How does a packed structure differ from a normal structure. Is it a memory issue?   ;  Packing is a way of specifying to the compiler how much it's allowed to pad out datamembers of a structure in order to align them better for the platform you're compiling to. Most modern architectures will either take a hit from reading misaligned data (that is not on the boundaries of the bus width of that arhitecture), or even crash from it.Now, on the gcc compiler that is used with the ps2 linux kit (I assume this is what you're using for you coursework), you can use__attribute__((packed))after a structure member to specify that members as packed, that is, it will not use any more space in the structure than the size of its data type.for instance, here's a structure I used for a TGA loader on the PS2 linux kit.(where uint# are previously defined sized integer types)define PACKED __attribute__((packed))struct TGAHeader{uint8  identsizePACKED;   // size of ID field that follows 18 uint8 header (0 usually)uint8  colourmaptypePACKED;   // type of colour map 0=none, 1=has paletteuint8  imagetypePACKED;   // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packeduint16 colourmapstartPACKED;   // first colour map entry in paletteuint16 colourmaplengthPACKED;   // number of colours in paletteuint8  colourmapbitsPACKED;   // number of bits per palette entry 15,16,24,32uint16 xstartPACKED;   // image x originuint16 ystartPACKED;   // image y originuint16 widthPACKED;   // image width in pixelsuint16 heightPACKED;   // image height in pixelsuint8  bitsPACKED;   // image bits per pixel 8,16,24,32uint8  descriptorPACKED;   // image descriptor bits (vh flip bits)};As far as I can remember, the PS2 can handle misaligned data just fine (correct me if I'm wrong). If it couldn't (or can't) though, it would still be up to the compiler to ensure that no such reads happen even when using a packed structure. The compiler would have to generate the necessary code to read in aligned parts of the data members, and shift them around in the registers.. nothing you would need to worry about (unless ofcourse you're passing around pointers to members).   ;Quote:Original post by c2_0As far as I can remember, the PS2 can handle misaligned data just fine (correct me if I'm wrong). If it couldn't (or can't) though, it would still be up to the compiler to ensure that no such reads happen even when using a packed structure. The compiler would have to generate the necessary code to read in aligned parts of the data members, and shift them around in the registers.. nothing you would need to worry about (unless ofcourse you're passing around pointers to members).Doesn't the PS2 use a fairly standard MIPS processor? I'm almost certain they don't support unaligned accesses (at least I've experienced crashes from it on a R4400). But you seem to be right about GCC generating special code to to access unaligned data, I just assumed it was your own responsibility to handle such things.Sorry for causing extra work..   ;  Yes, it uses a MIPS processor, and you might be right, I'm honestly not entirely sure if it handles unalligned access. It shouldn't be a problem in this case anyway.@LeoOn another note, if you're reading a binary file for loading, remember the PS2 is little endian as opposed to big endian as you might expect from most MIPS base architectures.   ;  Well I assume that since I prototyped the model loader on the PC, it should be in a little endian format anyway. Shouldn't it?   