1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook MathML Module V1.1b1//EN"
3
"http://www.oasis-open.org/docbook/xml/mathml/1.1CR1/dbmathml.dtd">
8
<keyword>Attributes of Types</keyword>
13
<refentrytitle>Attributes of Types</refentrytitle>
17
<year>2007-2011</year>
18
<holder>The Khronos Group Inc.
19
Permission is hereby granted, free of charge, to any person obtaining a
20
copy of this software and/or associated documentation files (the
21
"Materials"), to deal in the Materials without restriction, including
22
without limitation the rights to use, copy, modify, merge, publish,
23
distribute, sublicense, and/or sell copies of the Materials, and to
24
permit persons to whom the Materials are furnished to do so, subject to
25
the condition that this copyright notice and permission notice shall be included
26
in all copies or substantial portions of the Materials.</holder>
29
<manvolnum>3</manvolnum>
32
<!-- ================================ SYNOPSIS -->
34
<refnamediv id="AttributesofTypes">
35
<refname>Attributes of Types</refname>
42
<!-- ALTERNATIVE SYNTAX SYNOPSIS (NON-FUNCTION) -->
44
<refsect2 id="synopsis">
48
<informaltable frame="none">
49
<tgroup cols="1" align="left" colsep="0" rowsep="0">
50
<colspec colname="col1" colnum="1" />
54
__attribute__ ((aligned))
55
__attribute__ ((aligned (<emphasis>n</emphasis>)))
56
__attribute__ ((packed))
64
<!-- ================================ DESCRIPTION -->
66
<refsect1 id="description"><title>Description</title>
68
The keyword <constant>__attribute__</constant> allows you to specify special attributes
69
of <code>enum</code>, <code>struct</code> and <code>union</code> types when you
70
define such types. This keyword is followed by an attribute specification inside
71
double parentheses. Two attributes are currently defined for types: aligned, and packed.
75
You may specify type attributes in an <code>enum</code>, <code>struct</code>,
76
or <code>union</code> type declaration or definition, or for other types in a
77
<code>typedef</code> declaration.
81
For an <code>enum</code>, <code>struct</code>, or <code>union</code> type, you
82
may specify attributes either between the <code>enum</code>, <code>struct</code>
83
or <code>union</code> tag and the name of the type, or just past the closing curly
84
brace of the <emphasis>definition</emphasis>. The former syntax is preferred.
88
<refsect2 id="aligned"><title>aligned (<varname>alignment</varname>)</title>
89
<!-- Using refsect2 so we can have shaded backgrounds behind examples -->
91
This attribute specifies a minimum alignment (in bytes) for variables of the specified
92
type. For example, the declarations:
96
<informaltable frame="none">
97
<tgroup cols="1" align="left" colsep="0" rowsep="0">
98
<colspec colname="col1" colnum="1" />
101
<entry> <code>struct S { short f[3]; } __attribute__ ((aligned (8)));
102
typedef int more_aligned_int __attribute__ ((aligned (8)));</code></entry>
110
force the compiler to ensure (as far as it can) that each variable whose type is
111
<code>struct S</code> or <code>more_aligned_int</code> will be allocated and aligned
112
<emphasis>at least</emphasis> on a 8-byte boundary.
116
Note that the alignment of any given <code>struct</code> or <code>union</code> type
117
is required by the ISO C standard to be at least a perfect multiple of the lowest
118
common multiple of the alignments of all of the members of the <code>struct</code>
119
or <code>union</code> in question and must also be a power of two. This means that
120
you <emphasis>can</emphasis> effectively adjust the alignment of a <code>struct</code>
121
or <code>union</code> type by attaching an aligned attribute to any one of the members
122
of such a type, but the notation illustrated in the example above is a more obvious,
123
intuitive, and readable way to request the compiler to adjust the alignment of an entire
124
<code>struct</code> or <code>union</code> type.
128
As in the preceding example, you can explicitly specify the alignment (in bytes) that
129
you wish the compiler to use for a given <code>struct</code> or <code>union</code>
130
type. Alternatively, you can leave out the alignment factor and just ask the compiler
131
to align a type to the maximum useful alignment for the target machine you are compiling
132
for. For example, you could write:
136
<informaltable frame="none">
137
<tgroup cols="1" align="left" colsep="0" rowsep="0">
138
<colspec colname="col1" colnum="1" />
141
<entry> <code>struct S { short f[3]; } __attribute__ ((aligned));</code></entry>
149
Whenever you leave out the alignment factor in an <code>aligned</code> attribute
150
specification, the compiler automatically sets the alignment for the type to the largest
151
alignment which is ever used for any data type on the target machine you are compiling
152
for. In the example above, the size of each <code>short</code> is 2 bytes, and therefore
153
the size of the entire <code>struct S</code> type is 6 bytes. The smallest power of two
154
which is greater than or equal to that is 8, so the compiler sets the alignment for the
155
entire <code>struct S</code> type to 8 bytes.
159
Note that the effectiveness of aligned attributes may be limited by inherent limitations
160
of the OpenCL device and compiler. For some devices, the OpenCL compiler may only be
161
able to arrange for variables to be aligned up to a certain maximum alignment. If the
162
OpenCL compiler is only able to align variables up to a maximum of 8 byte alignment, then
163
specifying <code>aligned(16)</code> in an <code>__attribute__</code> will still only provide
164
you with 8 byte alignment. See your platform-specific documentation for further information.
168
The <code>aligned</code> attribute can only increase the alignment; but you can decrease
169
it by specifying <code>packed</code> as well. See below.
174
<refsect2 id="packed"><title>packed</title>
175
<!-- Using refsect2 so we can have shaded backgrounds behind examples -->
178
The <constant>packed</constant> attribute, attached to the <code>struct</code>
179
or <code>union</code> type definition, specifies that each member of the structure or
180
union is placed to minimize the memory required. When attached to an <code>enum</code>
181
definition, it indicates that the smallest integral type should be used.
185
Specifying this attribute for <code>struct</code> and <code>union</code> types is equivalent
186
to specifying the <code>packed</code> attribute on each of the structure or union members.
190
In the following example <code>struct my_packed_struct</code>'s members are packed closely
191
together, but the internal layout of its <code>s</code> member is not packed. To do that,
192
<code>struct my_unpacked_struct</code> would need to be packed, too.
196
<informaltable frame="none">
197
<tgroup cols="1" align="left" colsep="0" rowsep="0">
198
<colspec colname="col1" colnum="1" /> <tbody>
200
<entry> <code>struct my_unpacked_struct
205
struct __attribute__ ((packed)) my_packed_struct
209
struct my_unpacked_struct s;
218
You may only specify this attribute on the definition of a <code>enum</code>,
219
<code>struct</code>, or <code>union</code>, not on a <code>typedef</code> which does not
220
also define the enumerated type, structure or union.
224
<!-- ================================ EXAMPLE -->
225
<!-- DO NOT DELETE IN CASE AN EXAMPLE IS ADDED IN THE FUTURE -->
227
<refsect2 id="example1">
232
<informaltable frame="none">
233
<tgroup cols="1" align="left" colsep="0" rowsep="0">
234
<colspec colname="col1" colnum="1" />
238
Example goes here - it will be set in "code" type with white space preserved.
247
<!-- ================================ SPECIFICATION -->
248
<!-- Set the "uri" attribute in the <olink /> element to the "named destination" for the PDF page
250
<refsect1 id="specification"><title>Specification</title>
253
<imagedata fileref="pdficon_small1.gif" format="gif" />
256
<olink uri="attributes-types">OpenCL Specification</olink>
260
<!-- ================================ ALSO SEE -->
262
<refsect1 id="seealso"><title>Also see</title>
264
<citerefentry href="attribute"><refentrytitle>__attribute__</refentrytitle></citerefentry>,
265
<citerefentry href="attributes-blocksAndControlFlow"><refentrytitle>Blocks and Control-Flow Statement Attributes Attributes</refentrytitle></citerefentry>,
266
<citerefentry href="attributes-variables"><refentrytitle>Variable Attributes</refentrytitle></citerefentry>
270
<!-- ============================== COPYRIGHT -->
271
<!-- Content included from copyright.inc.xsl -->
273
<refsect3 id="Copyright"><title></title>
275
<imagedata fileref="KhronosLogo.jpg" format="jpg" />