Keil™, An ARM® Company

RealView Compiler Reference Guide

Technical Support

On-Line Manuals

RealView Compiler Reference Guide

Preface
Introduction
Compiler Command-line Options
Language Extensions
Compiler-specific Features
Keywords and operators
__align
__alignof__
__ALIGNOF__
__asm
__forceinline
__global_reg
__inline
__int64
__INTADDR__
__irq
__packed
__pure
__smc
__softfp
__svc
__svc_indirect
__svc_indirect_r7
__value_in_regs
__weak
__writeonly
__declspec attributes
__declspec(dllexport)
__declspec(dllimport)
__declspec(noinline)
__declspec(noreturn)
__declspec(nothrow)
__declspec(notshared)
__declspec(thread)
Function attributes
__attribute__((alias))
__attribute__((always_inline))
__attribute__((const))
__attribute__((deprecated))
__attribute__((malloc))
__attribute__((noinline))
__attribute__((noreturn))
__attribute__((pure))
__attribute__((section))
__attribute__((unused))
__attribute__((used))
__attribute__((weak))
Type attributes
__attribute__((aligned))
__attribute((packed))
__attribute__((transparent_union))
Variable attributes
__attribute__((alias))
__attribute__((at(address)))
__attribute__((aligned))
__attribute__((deprecated))
__attribute__((packed))
__attribute__((section))
__attribute__((transparent_union))
__attribute__((unused))
__attribute__((used))
__attribute__((weak))
__attribute__((zero_init))
Pragmas
#pragma [no_]anon_unions
#pragma arm
#pragma arm section [section_sort_list]
#pragma diag_default tag[,tag,...]
#pragma diag_error tag[,tag,...]
#pragma diag_remark tag[,tag,...]
#pragma diag_suppress tag[,tag,...]
#pragma diag_warning tag[, tag, ...]
#pragma [no_]exceptions_unwind
#pragma hdrstop
#pragma import symbol_name
#pragma [no_]inline
#pragma no_pch
#pragma Onum
#pragma once
#pragma Ospace
#pragma Otime
#pragma pop
#pragma push
#pragma [no_]softfp_linkage
#pragma unroll [(n)]
#pragma unroll_completely
#pragma thumb
Instruction intrinsics
__breakpoint
__builtin_clz
__builtin_constant_p
__builtin_expect
__builtin_frame_address
__builtin_return_address
__builtin_popcount
__cdp
__clrex
__clz
__current_pc
__current_sp
__disable_fiq
__disable_irq
__enable_fiq
__enable_irq
__fabs
__fabsf
__force_stores
__ldrex
__ldrt
__memory_changed
__nop
__pld
__pli
__qadd
__qdbl
__qsub
__rbit
__rev
__return_address
__ror
__schedule_barrier
__semihost
__sev
__sqrt
__sqrtf
__ssat
__strex
__strt
__swp
__usat
__wfe
__wfi
__yield
ARMv6 SIMD intrinsics
ETSI basic operations
C55x intrinsics
Named register variables
Compiler predefines
Predefined macros
Function names
C and C++ Implementation Details
Via File Syntax
Standard C Implementation Definition
Standard C++ Implementation Definition
C and C++ Compiler Implementation Limits

__packed

4.1.11. __packed

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

  • there is no padding inserted to align the packed object

  • objects of packed type are read or written using unaligned accesses.

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
}
Copyright © 2007 ARM Limited. All rights reserved.ARM DUI 0376A