4.1.11. __packed

The __packed qualifier sets the alignment of any valid type to 1. This means that:

The __packed qualifier applies to all members of a structure or union when it is declared using __packed. There is no padding between members, or at the end of the structure. All substructures of a packed structure must be declared using __packed. Integral subfields of an unpacked structure can be packed individually.

Usage

The __packed qualifier is useful to map a structure to an external data structure, or for accessing unaligned data, but it is generally not useful to save data size because of the relatively high cost of access. Only packing fields in a structure that requires packing can reduce the number of unaligned accesses.

Note

On ARM processors that do not support unaligned access in hardware, for example, pre-ARMv6, access to unaligned data can be costly in terms of code size and execution speed. Data accesses through packed structures must be minimized to avoid increase in code size and performance loss.

Restrictions

The following restrictions apply to the use of __packed:

  • The __packed qualifier cannot be used on structures that were previously declared without __packed.

  • Unlike other type qualifiers you cannot have both a __packed and non-__packed version of the same structure type.

  • The __packed qualifier does not affect local variables of integral type.

  • A packed structure or union is not assignment-compatible with the corresponding unpacked structure. Because the structures have a different memory layout, the only way to assign a packed structure to an unpacked structure is by a field-by-field copy.

  • The effect of casting away __packed is undefined. The effect of casting a non packed structure to a packed structure is undefined. A pointer to an integral type can be legally cast, explicitly or implicitly, to a pointer to a packed integral type. You can also cast away the __packed on char types.

  • There are no packed array types. A packed array is an array of objects of packed type. There is no padding in the array.

Example

Example 4.4 shows that a pointer can point to a packed type.

Example 4.4. Pointer to packed

typedef __packed int* PpI;          /* pointer to a __packed int */
__packed int *p;                    /* pointer to a __packed int */
PpI p2;                             /* 'p2' has the same type as 'p' */
                                    /* __packed is a qualifier  */
                                    /* just like 'const' or 'volatile' */

typedef int *PI;                    /* pointer to int */
__packed PI p3;                     /* a __packed pointer to a normal int */
                                    /*  -- not the same type as 'p' and 'p2' */

int *__packed p4;                   /* 'p4' has the same type as 'p3' */

Example 4.5 shows that when a packed object is accessed using a pointer, the compiler generates code that works and that is independent of the pointer alignment.

Example 4.5. Packed structure

typedef __packed struct
{
    char x;                   // all fields inherit the __packed qualifier	
    int y;
} X;                          // 5 byte structure, natural alignment = 1	

int f(X *p)
{
    return p->y;              // does an unaligned read
}

typedef struct
{
    short x;
    char y;
    __packed int z;           // only pack this field
    char a;
} Y;                          // 8 byte structure, natural alignment = 2

int g(Y *p)
{
    return p->z + p->x;       // only unaligned read for z
}

See also

Copyright © 2007, 2010 ARM Limited. All rights reserved.ARM DUI 0348A
Non-Confidential