Alignment of Data

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Post Reply
russose
Posts: 0
Joined: Mon Apr 19, 2004 5:01 pm

Alignment of Data

Post by russose » Thu Sep 01, 2005 9:44 am

Hallo Guys,

For a couple of weeks, I am trying to solve a "Data misalignment exeption" on my pocketpc Blender Port... this error occur in the malloc lib, in the following ligne:

"memh= (MemHead *)malloc(len+sizeof(MemHead)+sizeof(MemTail));"

The part of the code of malloc is :

typedef struct MemHead {
int tag1;
int len;
struct MemHead *next,*prev;
char * name;
char * nextname;
/* int level; */ /* historical, can be removed, but check alignment issues - zr */
int tag2;
} MemHead;

typedef struct MemTail {
int tag3, pad;
} MemTail;



void *MEM_mallocN(unsigned int len, char *str)
{
MemHead *memh;
MemTail *memt;

...

len = (len + 3 ) & ~3; /* allocate in units of 4 */

memh= (MemHead *)malloc(len+sizeof(MemHead)+sizeof(MemTail));

blabla
}

I tried lot of changes... to change de default alignment... to add some #pragma pack(1, 2, 4, 8 or 16!)... to add some __unaligned macro... no changes :=(
I generated the DNA.c from my pocketpc and it worked fine... the simple memory test provided worked also fine...
I read again an again the Ton note on SDNA... I can read at the end:

"If you can't manage this, just instert 'pad' variables in structures.
..... Do not use 'pad' variables in Blender!!!"
I can not understand this last sentence "Do not use 'pad' variables in Blender"... we use pad variable everywhere to align our structure...
Or it is meant that the compiler should be configured to not add pad variable (Pack structure with alignment = 1... I don't think so).

For ARM architecture, alignment of data is very sensitive, I have to be carefull to have all data 4 bytes aligned... but Blender take care of this alignment if I understood good the purpose of SNDA...

Do someone have an advise to put me in the right road :=) ? I tried all my ideas in the last weeks...

Thanks for your help,
Salvatore

simonharvey
Posts: 0
Joined: Tue May 18, 2004 8:11 am

Re: Alignment of Data

Post by simonharvey » Thu Sep 01, 2005 8:50 pm

russose wrote: "If you can't manage this, just instert 'pad' variables in structures.
..... Do not use 'pad' variables in Blender!!!"
What he ment was that if you have to add padding variables in your structures to get them aligned properly then do so. However after you have put them in there do not use the padding bytes as locations to hold useful data. This is true for your structures and also for anybody elses structures as well.

Kind Regards
Simon Harvey

eric.m.forsythe
Posts: 0
Joined: Thu Jun 23, 2005 3:26 pm

Post by eric.m.forsythe » Thu Sep 01, 2005 11:09 pm

I think that you may be looking in the wrong place...

I don't see how the 'malloc' line is causing the exception. 'malloc' should always return a properly aligned pointer. This usually happens if you "unalign" the returned pointer or have a structure with data members that are not aligned.

What does the compiler evaluate sizeof(MemHead) and sizeof(MemTail) to?

russose
Posts: 0
Joined: Mon Apr 19, 2004 5:01 pm

Post by russose » Wed Sep 07, 2005 12:28 am

Hello Guys,

Thanks for your help and sorry for my late answer (I had no connection these last days).
You were right the problem was not in malloc but in one of my others allocations...
Now, I have a version of Blender in my Pocketpc but it need lot of work to implement opengl instructions that I implement as dummy functions in a first time... so the UI is not good :=(
I will work on that :=)

Thanks again,
Salvatore

LetterRip
Posts: 0
Joined: Thu Mar 25, 2004 7:03 am

Post by LetterRip » Wed Sep 07, 2005 5:07 am

you might consider opengl ES which has a subset of OpenGL defined for mobile platforms, and which has an implementation for pocket pc.

http://www.khronos.org/opengles/tutorials_code/
http://www.khronos.org/devu/opengles_challenge/


LetterRip[/img]

russose
Posts: 0
Joined: Mon Apr 19, 2004 5:01 pm

Post by russose » Fri Sep 09, 2005 10:24 pm

Yes I do it... that's what I did... but opengl Es is a subset of opengl... lot of functions are missing... and I writte empty functions for all missing functions needed by Blender... but of course, it is not sufficient to have something good. Indispensible functions (maybe all) will have to be implemented and now I am looking for the best approach...

Daniel, who made the previous port of Blender 2.01 proposed to make its opengl implementation availlable a long time ago. I recently write to Bf-committers to know if I could receive these source. I will see.

I had a look in the source of Vincent, Klimt, Mesa and SGI opengl implementation... but if I really have to developp that alone... that will be an other story :=)

Let see...

Salvatore

Post Reply