Enforcing packing of variables in GCC/ARM compiler

ADL, A&D memory, Flash objects, multi-tasking, file system, DOTA.

Enforcing packing of variables in GCC/ARM compiler

Postby FAQ_Poster » Mon Jan 21, 2013 1:22 pm

GCC and ARM compilers do not recognize the #pragma pack directive.

Directives to use for packing and alignment using ARM compiler

1. To align all the fields of structure/union on single byte boundary (no padding), use __packed qualifier. For example,

typedef __packed struct
{
u8 first;
u16 second;
u16 third;
s8 fourth;
} test;


This qualifier indicates that:
- There is no padding inserted to align the packed objects.
- Objects of packed type are read or written using unaligned accesses.

Limitations:
- __packed qualifier cannot be used for floating-point types.
- It cannot be used for structures or unions with floating-point fields.
- It cannot be used for structures that were previously declared without __packet.
- The __packed qualifier applies to all the members of a union or structure when it is declared using __packed. There is no padding between members, or at the end of the structure. All the substructures of a packed structure must be declared using __packed.

Integral subfields of an unpacked structure can be packed individually. However, access to packed structure considerably reduces the execution speed. This qualifier is equivalent to #pragma pack(1).


2. To align objects on boundaries which fall on power of 2 (like 2, 4, 8 etc), the __align(n) class modifier can be used. For example,

__align(4) struct test
{
s8 first;
u8 second;
}test1;


But this modifier can only be used for top level objects. For example, if there are definitions like:

u16 integer;
__align(4) struct test
{
s8 first;
u8 second;
}test1;


Then the structure test1 will start at a 4 byte aligned boundary. This alignment will be relative to the previous variable (i.e. u16 integer).
If integer starts at memory address 17169148 then the structure test will start at memory address 17169852. This modifier cannot be used to align the members of the structure/union.
To have the elements of a structure aligned at specified boundaries, the padding should be done manually.

For example,

__align(4) struct test

{
u8 first_field;
u8 pad1;
u8 pad2;
u8 pad3;
u8 second_field;
}test1;

In the above structure, the first_field will always start at four byte aligned boundary, but as __align modifier will not provide alignment for the members of the structure, the padding is provided manually.


Directives to use for alignment and packing in GCC compiler:

1. To align all the fields of a structure/union on a single byte boundary (no padding), use the keyword __attribute__ with packed option.

For instance,

typedef struct
{
u8 first;
u16 second;
u16 third;
float fourth;
s8 fifth;
} __attribute__ ((packed)) test;


The packed option specifies that each member of the structure/union should be placed to minimize the memory requirement. This is equivalent to #pragma pack (1).


2. To align members of structure/union on boundaries which fall on the power of 2 (like 2, 4, 8, 16) use the option aligned(n) with __attribute__ keyword.

For instance:

typedef struct
{
u8 first __attribute__ ((aligned(4)));
u16 second __attribute__ ((aligned(4)));
u16 third __attribute__ ((aligned(4)));
float fourth __attribute__ ((aligned(4)));
s8 fifth __attribute__ ((aligned(4)));
} test;

In the above definition of structure test, each member will be aligned on a 4 byte boundary.
Please note that it is up to the linker if it can provide the facility of aligning the objects on higher alignment values (like 16,32,64).

If this options is provided on top level object definition like:

typedef struct
{
u8 first;
u16 second;
u16 third;
float fourth;
s8 fifth;
} __attribute__ ((aligned (4))) test;

then the structure (test) itself will be aligned at 4 byte boundary. The elements which comprise the structure will occupy the storage according to the default storage option.
User avatar
FAQ_Poster
 
Posts: 254
Joined: Thu Oct 25, 2012 7:10 am
Has thanked: 0 time
Have thanks: 5 times

Topic Tags

GCC, compilation, compiler, variables, packing, ARM

Return to Operating System FAQ

Who is online

Users browsing this forum: No registered users and 1 guest