CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

[SOLVED] Force static variables to start byte-aligned?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
allenhuffman



Joined: 17 Jun 2019
Posts: 537
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

[SOLVED] Force static variables to start byte-aligned?
PostPosted: Fri Aug 30, 2019 10:23 am     Reply with quote

Is there a pragma/attribute that will force static variables in a function/file to start on an even byte alignment? I know you can specify individual variables, but if I had something like:

Code:
void function ()
{
   static int8 s_value1;
   static int8 s_value2;


...but since the compiler will place int8s on odd bytes if they are available, that first static variable can end up mixed in with other variables in global/static storage.

I'm looking for something that would prevent that -- effectively letting me encapsulate static variables on a per-function basis and keep them all together. This is to work around some "challenges" with int8s being corrupted by the shared byte being modified in an ISR.

Thanks.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?


Last edited by allenhuffman on Wed Sep 04, 2019 8:43 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Fri Aug 30, 2019 11:00 am     Reply with quote

As a comment, I have to ask why you have so many static variables?.
Variables that are declared locally to functions, will always start with the
first variable word aligned to help prevent the sort of issue you are talking
about. By declaring variables globally, you are overriding this behaviour,
making them all available in multiple places.
General rule is always to never declare variables as global, unless they
require multiple location access. Sounds as if you could improve things
a lot by following this rule.
allenhuffman



Joined: 17 Jun 2019
Posts: 537
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Fri Aug 30, 2019 1:55 pm     Reply with quote

Ttelmah wrote:
As a comment, I have to ask why you have so many static variables?.
Variables that are declared locally to functions, will always start with the
first variable word aligned to help prevent the sort of issue you are talking
about. By declaring variables globally, you are overriding this behaviour,
making them all available in multiple places.
General rule is always to never declare variables as global, unless they
require multiple location access. Sounds as if you could improve things
a lot by following this rule.


I think the phrase "preaching to the choir" would be good here.

I am hoping to spearhead a massive code cleanup/refactoring project which will solve some of these 'challenges.'

I like the answers I get here much better than, say, Stack Exchange or whatever. There, the response is "don't write it like that" which is great if I am writing something. But if I am starting a job working on 100,000+ lines of existing code, well, I don't quite have that option.

Of course, the #1 response over there is … can you guess?

"Why would you want to do that?"

I registered this domain specifically to post as a response:


http://www.whywouldyouwanttodothat.com/
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
allenhuffman



Joined: 17 Jun 2019
Posts: 537
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

SOLVED
PostPosted: Wed Sep 04, 2019 8:43 am     Reply with quote

Here is the attribute that will force a variable to align on an even byte:

Code:
__attribute__((aligned(0x2)))


That can be added after a variable declaration:

Code:
int8 alignMe __attribute__((aligned(0x2)));


For my testing, I made a #define for it, and then put that define after variables. I can then switch it on or off by changing the define to an empty one:

Code:
//#define ALIGN __attribute__((aligned(0x2)))
#define ALIGN

int8 value1 ALIGN;
int8 value2 ALIGN;
int8 value3 ALIGN;


The compiler will still place other int8s in the other half of that word if they are not also using this attribute. It won't help library code such as i2c (which has its own internal int8), but at least it's something.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group