Structure packing

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
RuPi_283656
Level 4
Level 4
10 sign-ins First solution authored 25 replies posted

Does Creator 2.2 have any control of structure packing of data?  For example I create a structure like:

   

typedef

uint8 LoadedFlag;

uint16 Version;

struct {

uint16 OutRef;

} ConfigData;

   

When I look in memory I see two bytes used for LoadedFlag, rather than one.  Is there any way to force the structure to be packed?  In MS VC++ it would be "pragma pack (1)", but I cannot find any reference to packing control in the docs...

   

Thanks, Russ

0 Likes
7 Replies
RuPi_283656
Level 4
Level 4
10 sign-ins First solution authored 25 replies posted

Boy the web page scrambled my structure example.  Let me try again, if it still does not work I guess you can figure out what I mean.

   
typedef struct {     uint8  LoadedFlag;     uint16 Version;     uint16 OutRef; } ConfigData;  Maybe that will work better...  Russ
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Very curious about this, I did not realize compilers did this.

   

I found this for Keil, but does not apply to PSOC as GCC

   

used for ARM, PSOC 4/5.

   

 

   

      www.keil.com/support/man/docs/armcc/armcc_CJADHEDH.htm

   

 

   

I did find this - axelio.wordpress.com/2007/07/24/be-careful-with-packed-structures/

   

 

   

On page 309/310 in Creator, GCC doc, there is a discussion of alignment and packling.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 standard GCC: __attribute__ (__packed__) will do it BUT ...

   

 

   

care needs to be taken. packed structure will slow down memory fetches or possibly cause alignment exceptions on some processors. standard practice is to only use it for interprocessor communication or possible external storage. in memory execution is usually best if left unpacked.

   

 

   

-Ed

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The ARM core works best when values are stored on even addresses same rules for byte-values. Probably some optimization settings are hindering a ram-space optimization. The question is: can you spend that byte..?

   

 

   

Bob

0 Likes
RuPi_283656
Level 4
Level 4
10 sign-ins First solution authored 25 replies posted

Yes, I can spend the byte, this time.  It is a packet of data being sent over the serial interface, and it did not match what the receiving program was expecting.  THIS TIME I was able to modify the receiving program, but what if I could not?  Then it would be necessary to pack the structure.

   

Unpacked structures are organized so that each item starts on an even address boundary that matches the native integer size of the processor.  This makes data fetches and stores much more efficient, thus increasing speed.  But with the fast processors of today I do not think it makes all that much difference.  I guess in a real time process maybe it could be useful...

   

Well I now have the answer in case I need to pack a struct in the future.

   

Thanks, Russ

0 Likes
Anonymous
Not applicable

 that's one of the issue when needs check when needs to port project from one cpu to another. such as from PSoC3 to/from PSoC5.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Memory access times don't care about the CPU speed, but about memory access times. And the latter ones haven't improved that much in the last 20 years...

   

Essentially the problem is when a data element larger than 1 byte is places around a word (or double- or quad-word, depending on the architecture) boundary. Then every operation on the element requires now 2 memory accesses, which will slow it down tremendously. It gets even more problematic when the operation is supposed to be atomic.

   

See also Wikipedia: http://en.wikipedia.org/wiki/Data_structure_alignment

0 Likes