1. What is word alignment? Why Do We Need word alignment?
In modern computers, memory space is divided by byte. Theoretically, it seems that access to any type of variables can start from any address, however, the actual situation is that access to specific types of variables is often performed at specific memory addresses, which is alignment.
The reason for byte alignment is roughly as follows:
1. Platform reason (reason for transplantation): Not all hardware platforms can access any data on any address. Some hardware platforms can only retrieve certain types of data at some addresses, otherwise, a hardware exception is thrown.
2. Performance reasons: Data Structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access non-alignment memory, the processor needs to perform two memory accesses, while alignment memory access only needs one access.
Ii. Alignment rules
Each compiler on a specific platform has its own default "alignment coefficient" (also called alignment modulus ). Programmers can use the pre-compiled command # pragma pack (N), n =, and 16 to change this coefficient. n is the alignment coefficient you want to specify ".
Rules:
1. data member alignment rules: data member of a structure (struct) (or union). The first data member is placed in a place where the offset is 0, in the future, the alignment of each data member is performed according to the value specified by # pragma pack and the smaller value of the data member's length.
2. overall alignment rules for a structure (or union): After data members align themselves, the structure (or union) itself also needs to be aligned, alignment is performed according to the value specified by # pragma pack and the smaller value in the maximum data member length of the structure (or union.
3. combined with 1 and 2, we can infer: first, if n is greater than or equal to the number of bytes occupied by the variable, the offset must satisfy the default alignment, second, if n is less than the number of bytes occupied by the variable type, the offset is a multiple of N, and the default alignment is not required.
Iii. x86 alignment Experiment
Next we will briefly review and explain the above alignment rules and analyze them with examples:
1. alignment value of the Data Type itself: For char data, its alignment value is 1 byte, for short data is 2 byte, for int, float, double type, its own alignment value is 4 bytes.
2. The alignment value of a struct: The value with the largest alignment value among its members.
3. Specify the alignment value: # pragma pack (n) to set the variable to n-byte alignment. N-byte alignment means the offset of the Start address of the variable. First, if n is greater than or equal to the number of bytes occupied by the variable, the offset must meet the default alignment mode, second, if n is less than the number of bytes occupied by the variable type, the offset is a multiple of N, and the default alignment is not required.
4. valid alignment values of data members and structures: the alignment values of data members (data types) and data structures and the values smaller than the specified alignment values, data members are naturally aligned with the data structure.
To understand the four basic concepts above, we will begin to discuss the members of specific data structures and their alignment methods. Valid alignment value n is the final value used to determine the data storage address. Valid alignment means "alignment on N", that is, "Storage Start address % n = 0" of the data ". Data variables in the data structure are discharged in the defined order. The starting address of the first data variable is the starting address of the data structure. The member variables of the struct must be aligned and discharged, and the struct itself must be rounded according to its own valid alignment values (the total length occupied by the member variables of the struct must be an integer multiple of the valid alignment values of the struct ). The following is an example of the compiling environment in vs2005:
Example B:
Struct B
{
Char B;
Int;
Short C;
};
Assume that B is discharged from the address space 0x0000. In this example, the alignment value n is not explicitly specified, and the default value of vs2005 is 4.
The alignment value of member variable B is 1, which is smaller than the specified or default alignment value 4. Therefore, the valid alignment value is 1, and its storage address 0x0000 is 0 x 0000% 1 = 0, meets the byte alignment principle.
The alignment value of member variable A is 4, which is equal to the specified or default alignment value 4. Therefore, the valid alignment value is 4. To ensure byte alignment, member variable A can only be stored in the four consecutive bytes from the starting address 0x0004 to 0x0007. Review 0 x 0004% 4 = 0.
The alignment value of member variable C is 2, which is smaller than the specified or default alignment value 4. Therefore, the valid alignment value is 2, which can be stored in 0x0008 to 0x0009 bytes in sequence, 0 x 0008% 2 = 0.
So far, the data member's byte alignment is satisfied, and then the data structure B is aligned. The alignment value of Data Structure B is the maximum alignment value (that is, member variable B) 4 of its variables. Therefore, the valid alignment value of structure B is also 4. According to the requirements of the structure, 0x0009 to 0x0000 = 10 bytes, (10 + 2) % 4 = 0. Therefore, 0x0000a to 0x000b is also occupied by struct B. Therefore, B has 12 bytes from 0x0000 to 0x000b, and sizeof (struct B) is 12.
The reason for adding 2 bytes in variable C is that the compiler needs to quickly and effectively access the structure array. Imagine if the first structure starting address is 0 if the B structure array is defined, but what about the second structure? According to the definition of the array, all elements in the array are adjacent. If the structure size is not supplemented as an integer multiple of the alignment value (4, the starting address of the next structure will be 0x0000a, which obviously cannot satisfy the address alignment of the structure.
Example C analysis:
/* Specify 2-byte alignment */
_ Align (2) struct C
{
Char B;
Int;
Short C;
};/* Cancel the specified alignment and restore the default alignment */
Similarly, in example C, the member variable B's own alignment value is 1, and the specified alignment value is 2, so the effect alignment value is 1. Suppose C starts from 0x0000, then B is stored in 0x0000, which complies with 0 x 0000% 1 = 0 and meets the byte alignment principle.
The alignment value of member variable A is 4 and the alignment value is 2. Therefore, the valid alignment value is 2, the sequence is stored in four consecutive Bytes: 0x0002, 0x0003, 0x0004, and 0x0005. The values conform to the byte alignment principle of 0 x 0002% 2 = 0.
The alignment value of member variable C is 2, which is equal to the specified alignment value. Therefore, the valid alignment value is 2, which is stored in the order of 0x0006 and 0x0007, conforms to 0 x 0006% 2 = 0 and meets the byte alignment principle.
From 0x0000 to 0x00007, a total of eight characters are stored in the variable C of the struct. The alignment value of the structure C itself is 4, which is greater than the specified alignment value 2. Therefore, the valid alignment value of C is 2, because 8% 2 = 0, c only occupies eight bytes from 0x0000 to 0x0007. So sizeof (struct c) = 8, fully compliant with the byte alignment principle.
In addition to different alignment values, the address of the data structure may be stored differently.
Iv. Alignment of the ARM platform
In arm, there are two Commands: arm and thumb.
Arm command: each time a command is executed, the PC value is added to four bytes (32 bits ). when you access the 4-byte content at a time, the starting address of the byte must be at the 4-byte alignment position, that is, the low two digits of the address must be bits [0b00], that is, the address must be a multiple of 4.
Thumb command: each time a command is executed, the PC value is added to 2 bytes (16 bits ).). when a two-byte content is accessed at a time, the starting address of the byte must be at the 2-byte alignment position, that is, the low two digits of the address must be bits [0b0], that is, the address must be a multiple of 2.
The preceding method is called alignment (AlignedIs called non-alignment (Unaligned.
5. ARM platform byte alignment keywords
1. _ align (Num)
Used to modify the byte boundary of the highest level object.
A. When ldrd or strd is used in assembly, this command _ align (8) is used for modification restrictions. To ensure that the data objects are aligned accordingly.
B. The command for modifying an object can contain a maximum of 8 bytes, so that a 2-Byte object can be 4 bytes long.
Alignment, but cannot make the 4-Byte object 2-byte alignment.
C, _ align is a storage class modification. It only modifies objects of the highest level type and cannot be used for structure or function objects.
2. _ packed
_ Packed performs one-byte alignment.
A. the packed object cannot be aligned;
B. Non-Alignment access is performed for read and write access to all objects;
C. Float and objects that contain Float Structures and do not use _ packed cannot be aligned in bytes;
D. _ packed has no effect on local integer variables;
D. It is undefined to force the unpacked object to be converted to the packed object. The integer pointer can be set legally.
The value is packed _ packed int * P; // _ packed Int.
3. _ unaligned
Used to modify the variable for Non-Alignment access.
6. How to find problems with byte alignment
If alignment or assignment occurs, first view:
1. Compiler's big little side settings;
2. Check whether the system supports non-alignment access;
3. If alignment or alignment is supported, some special modifications are required to mark special access operations.
VII. Conclusion
For the data structure used locally by 32-bit processors, the four-byte alignment mode is adopted to improve memory access efficiency. Meanwhile, to reduce memory overhead, the location of the structure members is reasonably arranged, reduce the gap between members caused by four-byte alignment and reduce memory overhead.
For the data structure between processors, it is necessary to ensure that the length of the message structure does not change because of different compilation platforms and processors, and the message structure is tightened by one-byte alignment; to ensure the memory access efficiency of the data structure of messages between processors, the four-byte alignment of the members in the message is performed by the byte filling method.
The data structure should take into account the relationship between members, data access efficiency and space utilization. The principle of order arrangement is: Four bytes are placed at the beginning, two bytes are followed by the last four bytes, one byte is followed by the last two bytes, And the padding byte is placed at the end. Example:
Typedef struct tag_t_msg {
Long paraa;
Long parab;
Short parac;
Char Parad;
Char pad;/* fill in bytes */
} T_msg;
Original article address:Http://apps.hi.baidu.com/share/detail/18228315