2009年12月9日 星期三

alignment

一個基本型態(可能是int, char,....)varible是naturally aligned指的是該varible所在的記憶體位址是該varible的byte size的倍數。例如一個4-byte int varible 是是naturally aligned,其記憶體位址必是4的倍數。

一個srtuct 型態變數 compiler 會利用增加padding 讓構成該struct的element是naturally aligned,且該srtuct 型態變數的記憶體位置必是最大byte size的倍數。我們可以用struct宣告後加上"__attribute__ ((packed))"讓compiler 不增加padding,不過這樣會降低存取該struct varible的效率。

reference:

1)

http://zylix666.blogspot.com/2007/10/cstruct.html

2)

"Linux System Programming", by Robert Love

Alignment

The alignment of data refers to the relation between its address and memory chunks
as measured by the hardware. A variable located at a memory address that is a multiple
of its size is said to be naturally aligned. For example, a 32-bit variable is naturally
aligned if it is located in memory at an address that is a multiple of 4—that is, if the
address’ lowest two bits are 0. Thus, a type that is 2n bytes in size must have an
address with the n least-significant bits set to 0.

Rules pertaining to alignment derive from hardware. Some machine architectures
have very stringent requirements on the alignment of data. On some systems, a load
of unaligned data results in a processor trap. On other systems, accessing unaligned
data is safe, but results in a degradation of performance. When writing portable
code, alignment issues must be avoided, and all types should be naturally aligned.

Other alignment concerns

Alignment concerns extend beyond natural alignment of the standard types and
dynamic memory allocations. For example, nonstandard and complex types have
more complex requirements than the standard types. Further, alignment concerns
are doubly important when assigning values between pointers of varying types and
using typecasting.

Nonstandard types

Nonstandard and complex data types possess alignment requirements
beyond the simple requirement of natural alignment. Four useful rules follow:

• The alignment requirement of a structure is that of its largest constituent type.
For example, if a structure’s largest type is a 32-bit integer that is aligned along a
4 byte boundary, the structure must be aligned along at least a 4 byte boundary
as well.

• Structures also introduce the need for padding, which is used to ensure that each
constituent type is properly aligned to that type’s own requirement. Thus, if a
char (with a probable alignment of one byte) finds itself followed by an int (with
a probable alignment of four bytes), the compiler will insert three bytes of padding
between the two types to ensure that the int lives on a four-byte boundary.
Programmers sometimes order the members of a structure—for example, by
descending size—to minimize the space “wasted” by padding. The GCC option
-Wpadded can aid in this endeavor, as it generates a warning whenever the compiler
inserts implicit padding.

• The alignment requirement of a union is that of the largest unionized type.

• The alignment requirement of an array is that of the base type. Thus, arrays
carry no requirement beyond a single instance of their type. This behavior results
in the natural alignment of all members of an array.

1 則留言:

匿名 提到...
網誌管理員已經移除這則留言。