#define X __declspec(align(16))是什么意思

king_of_hj 2009-01-06 08:29:23
特别是align(16)不懂
...全文
1250 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
六脉神剑绝学 2009-03-22
  • 打赏
  • 举报
回复
To summarize:

Unless overridden with __declspec(align(#)), the alignment of a scalar structure member is the minimum of its size and the current packing.

Unless overridden with __declspec(align(#)), the alignment of a structure is the maximum of the individual alignments of its member(s).

A structure member is placed at an offset from the beginning of its parent structure which is the smallest multiple of its alignment greater than or equal to the offset of the end of the previous member.

The size of a structure is the smallest multiple of its alignment larger greater than or equal the offset of the end of its last member.

Note that sizeof(struct Str1) is equal to 32, such that, if an array of Str1 objects is created, and the base of the array is 32-byte aligned, then each member of the array will also be 32-byte aligned. To create an array whose base is properly aligned, use _aligned_malloc, or write your own allocator. Note that normal allocators, such as malloc, C++ operator new, and the Win32 allocators return memory that will most likely not be sufficiently aligned for __declspec(align(#)) structures or arrays of structures.

The sizeof value for any structure is the offset of the final member, plus that member's size, rounded up to the nearest multiple of the largest member alignment value or the whole structure alignment value, whichever is greater.

__declspec(align(#)) can only increase alignment restrictions.

For more information, see:

align Examples

Defining New Types with __declspec(align(#))

Aligning Data in Thread Local Storage

How align Works with Data Packing

Examples of Structure Alignment (x64 specific)

align Examples
The following examples show how __declspec(align(#)) affects the size and alignment of data structures. The examples assume the following definitions:

Copy Code
#define CACHE_LINE 32
#define CACHE_ALIGN __declspec(align(CACHE_LINE))


In the following example, the S1 structure is defined with __declspec(align(32)). All uses of S1, whether for a variable definition or other type declarations, ensure that this structure data is 32-byte aligned. sizeof(struct S1) returns 32, and S1 has 16 padding bytes following the 16 bytes required to hold the four integers. Each int member requires 4-byte alignment, but the alignment of the structure itself is declared to be 32, so the overall alignment is 32.

Copy Code
struct CACHE_ALIGN S1 { // cache align all instances of S1
int a, b, c, d;
};
struct S1 s1; // s1 is 32-byte cache aligned


In the following example, sizeof(struct S2) will return 16, which is exactly the sum of the member sizes, because that happens to be a multiple of the largest alignment requirement (a multiple of 8).

Copy Code
__declspec(align(8)) struct S2 {
int a, b, c, d;
};


In the following example, sizeof(struct S3) returns 64.

Copy Code
struct S3 {
struct S1 s1; // S3 inherits cache alignment requirement
// from S1 declaration
int a; // a is now cache aligned because of s1
// 28 bytes of trailing padding
};


In the following example, note that a has only the alignment of natural type, in this case, 4 bytes. However, S1 must be 32-byte aligned. Twenty-eight bytes of padding follow a, so that s1 starts at offset 32. S4 then inherits the alignment requirement of S1, because it is the largest alignment requirement in the structure. sizeof(struct S4) returns 64.

Copy Code
struct S4 {
int a;
// 28 bytes padding
struct S1 s1; // S4 inherits cache alignment requirement of S1
};


The following three variable declarations also use __declspec(align(#)). In each case, the variable must be 32-byte aligned. In the case of the array, the base address of the array, not each array member, is 32-byte aligned. The sizeof value for each array member is not affected by the use of __declspec(align(#)).

Copy Code
CACHE_ALIGN int i;
CACHE_ALIGN int array[128];
CACHE_ALIGN struct s2 s;


To align each individual member of an array, code such as the following should be used:

Copy Code
typedef CACHE_ALIGN struct { int a; } S5;
S5 array[10];


In the following example, note that aligning the structure itself and aligning the first element are identical:

Copy Code
CACHE_ALIGN struct S6 {
int a;
int b;
};

struct S7 {
CACHE_ALIGN int a;
int b;
};


S6 and S7 have identical alignment, allocation, and size characteristics.

In the following example, the alignment of the starting addresses of a, b, c, and d are 4, 1, 4, and 1, respectively.

Copy Code
void fn() {
int a;
char b;
long c;
char d[10]
}


The alignment if the memory were allocated on heap depends on which allocation function is called. For example, if you use malloc, the result depends on the operand size. If arg >= 8, alignment will be 8 byte aligned. If arg < 8, alignment will be the first power of 2 less than arg. For example, if you use malloc(7), alignment is 4 bytes.

Defining New Types with __declspec(align(#))
You can define a type with an alignment characteristic.

For example, you can define a struct with an alignment value as follows:

Copy Code
struct aType {int a; int b;};
typedef __declspec(align(32)) struct aType bType;


Now, aType and bType are the same size (8 bytes) but variables of type bType will be 32-byte aligned.

Aligning Data in Thread Local Storage
Static thread-local storage (TLS) created with the __declspec(thread) attribute and put in the TLS section in the image works for alignment exactly like normal static data. The operating system creates TLS data by allocating data the size of the TLS section and respecting the TLS section alignment attribute.

The following example shows various ways to place aligned data into thread local storage.

Copy Code
// put an aligned integer in TLS
__declspec(thread) __declspec(align(32)) int a;

// define an aligned structure and put a variable of the struct type
// into TLS
__declspec(thread) __declspec(align(32)) struct F1 { int a; int b; } a;

// create an aligned structure
struct CACHE_ALIGN S9 {
int a;
int b;
};
// put a variable of the structure type into TLS
__declspec(thread) struct S9 a;


How align Works with Data Packing
The /Zp compiler option and the pack pragma have the effect of packing data for structure and union members. This example shows how /Zp and __declspec(align( # )) work together:

Copy Code
struct S {
char a;
short b;
double c;
CACHE_ALIGN double d;
char e;
double f;
};


The following table lists the offset of each member under a variety of /Zp (or #pragma pack) values, showing how the two interact.

Variable
/Zp1
/Zp2
/Zp4
/Zp8

A
0
0
0
0

B
1
2
2
2

C
3
4
4
8

D
32
32
32
32

E
40
40
40
40

F
41
42
44
48

sizeof(S)
64
64
64
64


For more information, see /Zp (Struct Member Alignment).

Thus, the offset of an object is based on the offset of the previous object and the current packing setting, unless the object has a __declspec(align( # )) attribute, in which case the alignment is based on the offset of the previous object and the __declspec(align( # )) value for the object.

See Also
Reference
六脉神剑绝学 2009-03-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hqin6 的回复:]
还是vs2005的文档比较全!
[/Quote]
以下来自VS2008Msdn:
align
Use the __declspec(align(alignment number)) declarator to precisely control the alignment of global data. By aligning frequently used data to the cache line size of a specific processor, you improve cache performance.

For example, if you define a structure whose size is less than 32 bytes, you might want to align it to 32 bytes to ensure that objects of that structure type are efficiently cached. The alignment number is the alignment value. Valid entries are powers of two from 1 to 8192 (bytes), such as 2, 4, 8, 16, 32, or 64. The declarator is the global data that you declare as aligned. You can use __declspec(align(alignment number)) when you define a structure, union, or class, or when you declare a variable. Without __declspec(align(alignment number)), the compiler aligns data on natural boundaries based on the size of the data, such as 4-byte integers on 4-byte boundaries, and 8-byte doubles on 8-byte boundaries. Data in classes or structures is aligned within the class or structure at the minimum of its natural alignment and the current packing setting (from the /Zp compiler option). You cannot specify alignment for stack variables.

The following is an example of using __declspec(align(alignment number)):

__declspec(align(32)) struct Str1 {
int a, b, c, d, e;
};



After using __declspec(align(32)) on the struct Str1 type, struct Str1 has a 32-byte alignment attribute, meaning that all instances must start on a 32-byte boundary. Note that sizeof(struct Str1) is equal to 32 to support creation of aligned array members using code like malloc(n * sizeof(struct Str1)). Additional structure types declared with this type as a member preserve this type's alignment attribute (that is, any structure with struct Str1 as an element has an alignment attribute of at least 32).

The sizeof value for any structure is the offset of the final member, plus that member's size, rounded up to the nearest multiple of the largest member alignment value or the whole structure alignment value, whichever is greater.

=========================================
以下来自VS2008msdn,too
==========================================
align (C++)
See Also Send Feedback

Updated: November 2007

Microsoft Specific

Use __declspec(align(#)) to precisely control the alignment of user-defined data (for example, static allocations or automatic data in a function).

For more information on alignment, see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconwindowsdataalignmentonipfx86x86-64.asp?frame=true.


__declspec( align( # ) ) declarator


Remarks
Writing applications that use the latest processor instructions introduces some new constraints and issues. In particular, many new instructions require that data must be aligned to 16-byte boundaries. Additionally, by aligning frequently used data to the cache line size of a specific processor, you improve cache performance. For example, if you define a structure whose size is less than 32 bytes, you may want to align it to 32 bytes to ensure that objects of that structure type are efficiently cached.

# is the alignment value. Valid entries are integer powers of two from 1 to 8192 (bytes), such as 2, 4, 8, 16, 32, or 64. declarator is the data that you are declaring as aligned.

See __alignof for information on how to return a value of type size_t that is the alignment requirement of the type. and __unaligned for information on how to declare unaligned pointers when targeting 64-bit processors.

You can use __declspec(align(#)) when you define a struct, union, or class, or when you declare a variable.

Without __declspec(align(#)), Visual C++ aligns data on natural boundaries based on the size of the data, for example 4-byte integers on 4-byte boundaries and 8-byte doubles on 8-byte boundaries. Data in classes or structures is aligned within the class or structure at the minimum of its natural alignment and the current packing setting (from #pragma pack or the /Zp compiler option).

You cannot specify alignment for function parameters.

For example:

Copy Code
__declspec(align(32)) struct Str1{
int a, b, c, d, e;
};


This type now has a 32-byte alignment attribute, meaning that all instances must start on a 32-byte boundary. Additional structure types declared with this type as a member preserve this type's alignment attribute, that is, any structure with Str1 as an element will have an alignment attribute of at least 32.




……回复内容过长!,您只能输入 8000 个字符
Cpp权哥 2009-01-06
  • 打赏
  • 举报
回复
就是自定义类型的变量在分配空间的时候按16的整数倍分配字节数。
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hqin6 的回复:]
还是vs2005的文档比较全!
[/Quote]
俺的是2008的 嘿嘿
太乙 2009-01-06
  • 打赏
  • 举报
回复
还是vs2005的文档比较全!
太乙 2009-01-06
  • 打赏
  • 举报
回复

Microsoft Specific

Use __declspec(align(#)) to precisely control the alignment of user-defined data (for example, static or automatic data in a function).

For more information on alignment, see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconwindowsdataalignmentonipfx86x86-64.asp?frame=true.


__declspec( align( # ) ) declarator


Remarks
Writing applications that use the latest processor instructions introduces some new constraints and issues. In particular, many new instructions require that data must be aligned to 16-byte boundaries. Additionally, by aligning frequently used data to the cache line size of a specific processor, you improve cache performance. For example, if you define a structure whose size is less than 32 bytes, you may want to align it to 32 bytes to ensure that objects of that structure type are efficiently cached.

# is the alignment value. Valid entries are integer powers of two from 1 to 8192 (bytes), such as 2, 4, 8, 16, 32, or 64. declarator is the data that you are declaring as aligned.

See __alignof for information on how to return a value of type size_t that is the alignment requirement of the type. and __unaligned for information on how to declare unaligned pointers when targeting 64-bit processors.

You can use __declspec(align(#)) when you define a struct, union, or class, or when you declare a variable.

Without __declspec(align(#)), Visual C++ aligns data on natural boundaries based on the size of the data, for example 4-byte integers on 4-byte boundaries and 8-byte doubles on 8-byte boundaries. Data in classes or structures is aligned within the class or structure at the minimum of its natural alignment and the current packing setting (from #pragma pack or the /Zp compiler option).

You cannot specify alignment for function parameters.

For example:

Copy Code
__declspec(align(32)) struct Str1{
int a, b, c, d, e;
};


This type now has a 32-byte alignment attribute, meaning that all instances must start on a 32-byte boundary. Additional structure types declared with this type as a member preserve this type's alignment attribute, that is, any structure with Str1 as an element will have an alignment attribute of at least 32.

To summarize:

Unless overridden with __declspec(align(#)), the alignment of a scalar structure member is the minimum of its size and the current packing.

Unless overridden with __declspec(align(#)), the alignment of a structure is the maximum of the individual alignments of its member(s).

A structure member is placed at an offset from the beginning of its parent structure which is the smallest multiple of its alignment greater than or equal to the offset of the end of the previous member.

The size of a structure is the smallest multiple of its alignment larger greater than or equal the offset of the end of its last member.

Note that sizeof(struct Str1) is equal to 32, such that, if an array of Str1 objects is created, and the base of the array is 32-byte aligned, then each member of the array will also be 32-byte aligned. To create an array whose base is properly aligned, use _aligned_malloc, or write your own allocator. Note that normal allocators, such as malloc, C++ operator new, and the Win32 allocators return memory that will most likely not be sufficiently aligned for __declspec(align(#)) structures or arrays of structures.

The sizeof value for any structure is the offset of the final member, plus that member's size, rounded up to the nearest multiple of the largest member alignment value or the whole structure alignment value, whichever is greater.

__declspec(align(#)) can only increase alignment restrictions.

For more information, see:

align Examples

Defining New Types with __declspec(align(#))

Aligning Data in Thread Local Storage

How align Works with Data Packing

Structure Alignment Examples (x64 specific)

align Examples
The following examples show how __declspec(align(#)) affects the size and alignment of data structures. The examples assume the following definitions:

Copy Code
#define CACHE_LINE 32
#define CACHE_ALIGN __declspec(align(CACHE_LINE))


In the following example, the S1 structure is defined with __declspec(align(32)). All uses of S1, whether for a variable definition or other type declarations, ensure that this structure data is 32-byte aligned. sizeof(struct S1) returns 32, and S1 has 16 padding bytes following the 16 bytes required to hold the four integers. Each int member requires 4-byte alignment, but the alignment of the structure itself is declared to be 32, so the overall alignment is 32.

Copy Code
struct CACHE_ALIGN S1 // cache align all instances of S1{
int a, b, c, d;
};
struct S1 s1; // s1 is 32-byte cache aligned


In the following example, sizeof(struct S2) will return 16, which is exactly the sum of the member sizes, because that happens to be a multiple of the largest alignment requirement (a multiple of 8).

Copy Code
__declspec(align(8)) struct S2 {
int a, b, c, d;
};


In the following example, sizeof(struct S3) returns 64.

Copy Code
struct S3 {
struct S1 s1; // S3 inherits cache alignment requirement
// from S1 declaration
int a; // a is now cache aligned because of s1
// 28 bytes of trailing padding
};


In the following example, note that a has only the alignment of natural type, in this case, 4 bytes. However, S1 must be 32-byte aligned. Twenty-eight bytes of padding follow a, so that s1 starts at offset 32. S4 then inherits the alignment requirement of S1, because it is the largest alignment requirement in the structure. sizeof(struct S4) returns 64.

Copy Code
struct S4 {
int a;
// 28 bytes padding
struct S1 s1; // S4 inherits cache alignment requirement of S1
};


The following three variable declarations also use __declspec(align(#)). In each case, the variable must be 32-byte aligned. In the case of the array, the base address of the array, not each array member, is 32-byte aligned. The sizeof value for each array member is not affected by the use of __declspec(align(#)).

Copy Code
CACHE_ALIGN int i;
CACHE_ALIGN int array[128];
CACHE_ALIGN struct s2 s;


To align each individual member of an array, code such as the following should be used:

Copy Code
typedef CACHE_ALIGN struct { int a; } S5;
S5 array[10];


In the following example, note that aligning the structure itself and aligning the first element are identical:

Copy Code
CACHE_ALIGN struct S6 {
int a;
int b;
};

struct S7 {
CACHE_ALIGN int a;
int b;
};


S6 and S7 have identical alignment, allocation, and size characteristics.

太乙 2009-01-06
  • 打赏
  • 举报
回复
还是绿色免安装的!
太乙 2009-01-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 akirya 的回复:]
Use __declspec(align(#)) to precisely control the alignment of user-defined data (for example, static allocations or automatic data in a function).

For more information on alignment, see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconwindowsdataalignmentonipfx86x86-64.asp?frame=true
[/Quote]

看来我这msdn太落伍了!~~哎~~~
2000年的!
lightnut 2009-01-06
  • 打赏
  • 举报
回复
好象是用来控制字节对齐的.__declspec(align(16)) , 好象表示用户定义的结构的大小一定是16(byte)的倍数.
  • 打赏
  • 举报
回复
Use __declspec(align(#)) to precisely control the alignment of user-defined data (for example, static allocations or automatic data in a function).

For more information on alignment, see:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconwindowsdataalignmentonipfx86x86-64.asp?frame=true

太乙 2009-01-06
  • 打赏
  • 举报
回复
__declspec
Microsoft Specific

__declspec ( extended-attribute ) declarator

extended-attribute
allocate(“segname”)
dllimport
dllexport
naked
noreturn
nothrow
novtable
property({get=get_func_name|, put=put_func_name})
selectany
thread
uuid(“ComObjectGUID”)


没见到你那个align啊!!

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧