Packing structures on bit boundaries

OP #3665270
Rate this post
• ▲ useful
▼ not useful
I have a an issue with gcc and I am looking for help.

I am trying to use a library that was designed for a different 
environment on a RAM constrained cortex-M0 processor. The library itself 
makes extensive use of structures like the following (details removed to 
express the problem succinctly):

struct {
   struct {
      int chan1;
      int chan2;
   } dma;
   int state;
} X;

By default, this takes up 12 bytes. But the range of possible values for 
these fields are very small. I would like to change it to the following:

struct __attribute__(( packed )) {
   struct __attribute__(( packed )) {
      uint8_t chan1:2;
      uint8_t chan2:2;
   } dma;
   uint8_t  :1;
   uint8_t  state:3;
} X;

This structure should only take 1 byte. But it seems gcc is always 
padding out the dma structure to a full byte, and then placing the state 
field in a second byte. So my structure winds up with size 2. I have 
read everything on the net and have not yet found anything which would 
allow me to tell gcc not to pad the dma structure, or to align it on a 
nibble boundary.

I am stuck with this structure. To change it would require changing it 
in hundreds of places of C code, and worse, redoing it every single time 
there was a library code update.

This is doubling my RAM usage. If I could find a way to make gcc behave, 
then we could squeeze into a smaller part.

Does anyone have any idea on a way to make the gcc compiler be more 
accommodating to my constraints?

Thank you for any assistance.
Guest #3665290
Rate this post
• ▲ useful
▼ not useful
Charles Zi wrote:
> This structure should only take 1 byte. But it seems gcc is always
> padding out the dma structure to a full byte, and then placing the state
> field in a second byte.

Yes, because it's a separate type. The dma struct is one bit field that 
must have a size that is a multiple of one byte. That doesn't change 
just because it is embedded into another structure with bitfield 
elements.

> I have read everything on the net and have not yet found anything which
> would allow me to tell gcc not to pad the dma structure, or to align it
> on a nibble boundary.

I don't think there a way to put bits of multiple bit fields together 
into one byte.

> I am stuck with this structure. To change it would require changing it
> in hundreds of places of C code,

Then better start now ;-)

> and worse, redoing it every single time there was a library code update.

Why?

Reply

A question of your own? This thread is over 12 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.