Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® Software > Structure packing

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Structure packing
Moderator:
JFMD

Post Reply
Follow this topic



Structure packing

RussK2txb posted on 09 May 2013 3:32 PM PST
Member
3 Forum Posts

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

 




Re: Structure packing

RussK2txb posted on 09 May 2013 03:36 PM PST
Member
3 Forum Posts

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


Re: Structure packing

danaaknight posted on 09 May 2013 03:54 PM PST
Top Contributor
1773 Forum Posts

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.



Re: Structure packing

psoc-ed posted on 09 May 2013 04:07 PM PST
Senior Member
15 Forum Posts

 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



Re: Structure packing

Bob Marlowe posted on 10 May 2013 03:10 AM PST
Top Contributor
1768 Forum Posts

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



Re: Structure packing

RussK2txb posted on 10 May 2013 04:21 AM PST
Member
3 Forum Posts

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

 



Re: Structure packing

H L posted on 11 May 2013 10:10 PM PST
Top Contributor
679 Forum Posts

 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.



Re: Structure packing

hli posted on 11 May 2013 01:00 PM PST
Top Contributor
675 Forum Posts

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

 






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20