Apart from speed purposes, when is it necessary to align variables in PS2 development? I've been wondering if some of my problems I've been having could be from structures taking memory allocated to others because of some way in which the PS2 handles memory blocks, or just not being able to access the data as expected (I first noticed something like this when I had a 14 byte BMP header being reported as 16 bytes by sizeof).
And I know I've been asking a lot of questions lately, but that's what happens when working on a project, I guess.
__attribute__ ((aligned()))
Depends on what you are doing with the data. If it's to be dma-transferred, for example, or if it's to be accessed in asm using lq/sq, it has to be aligned on the corresponding alignment...
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
Well, also structures I think will be aligned, at a minimum, on word boundries. That would explain why your sizeof() operator returns 16 instead of 14. That normally shouldn't make a difference, so long as you aren't expecting something else to be in the next two bytes. Also, depending on any data you are reading, don't forget the role of endianness and byte swapping.
One more thing, the 16 bytes, and word alignment, can be caused by a two byte gap WITHIN the structure, if you have a structure like so:
struct blah
{
u32 foo;
u32 bar;
u16 oog;
// u16 sized gap here
u32 barf;
};
In memory, between oof and barf, there will be a two byte gap, because barf must start on a word boundry.
One more thing, the 16 bytes, and word alignment, can be caused by a two byte gap WITHIN the structure, if you have a structure like so:
struct blah
{
u32 foo;
u32 bar;
u16 oog;
// u16 sized gap here
u32 barf;
};
In memory, between oof and barf, there will be a two byte gap, because barf must start on a word boundry.
Problem is, gcc may fail producing "unaligned" fetching code in the latter example, thus could lead to a loading exception.
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
You need to apply the "packed" attribute to each structure member individually. Then GCC will generate the proper unaligned load/store instructions.pixel wrote:Problem is, gcc may fail producing "unaligned" fetching code in the latter example, thus could lead to a loading exception.
"He was warned..."