64-bit insider volume 1 issue 1
TRANSCRIPT
-
8/6/2019 64-Bit Insider Volume 1 Issue 1
1/4
64-Bit InsiderVolume I, Issue I
Alignment in 64-bit Processors:
Part 1
The 64-bit Advantage
The computer industry is chang-
ing, and 64-bit technology is the
next, inevitable step. The 64-bit
Insider newsletter will help you
adopt this technology by provid-
ing tips and tricks for a successful
port.
64-bit development and migration
is not as complicated and complex
as the 16-bit to 32-bit transition.
However, as with any new tech-
nology, there are several areas that
require close examination and
consideration. The goal of the 64-
bit Insidernewsletteris to identify
potential migration issues and
provide viable, effective solutions
to these issues. With a plethora of
Web sites already focused on 64-
bit technology, the intention of
this newsletter is not to repeat
previously published information.
Instead, it will focus on 64-bitissues that are somewhat isolated
yet extremely important to under-
stand. It will also connect you to
reports and findings from 64-bit
experts.
Understanding AlignmentAlignment refers to where and how data is laid out in mem-ory. For performance reasons, processors specify rules that
determine valid locations for data. When these rules are bro-
ken, the processor follows one of two paths: it throws an ex-ception, possibly crashing the application, or it accepts the
unaligned data but runs slower than usual.
The rules change with 64-bit processors, causing the primi-
tive data types and compound data structures inside an appli-
cation to change both in size and shape. Consequently, some
common but nonportable habits of C programmers createissues when they compile code on 64-bit processors.
Ideally, the compiler would transparently handle any align-ment issues. And such is the case when programmers write
code that is portable and follows the relevant best practices
of C coding. In this instance, the compiler automaticallytakes care of any changes in alignment rules, padding, and
structure sizes so that the associated application works on a
64-bit platform.
64-bit Insider NewsletterVolume 1, Issue 1
Page 1/4
-
8/6/2019 64-Bit Insider Volume 1 Issue 1
2/4
-
8/6/2019 64-Bit Insider Volume 1 Issue 1
3/4
typedef struct _mburst{
int seq_no; // Sequence number for this burstsize_t size; // Size of burstchar* data; // Observed data_mburst* next; // Pointer to next burst or NULL
} mburst;
Figure 1: 32-bit and 64-bit view of the mburst data structure
Alignment Issues: Hard-Coding Structure
Sizes
As you can see in Figure 1, the 64-bit version of themburst object is larger than the 32-bit version. This
is because the size of the pointers and padding
within the structure has increased to keep themembers of the structure properly aligned. Thus, the
following code, used to create a single mburst
object, works only on the 32-bit platform:
mburst* create_mburst(){
mburst* p = (mburst*) malloc(16);p->seq_no = -1;
p->size = 0;p->data = NULL;p->next = NULL;return p;
}
Because the mburstobject changes in size (from 16 to 32 bits) on the 64-bit platform,when this code runs on a 64-bit platform it will work only until you write data into the
allocated memory. At this point, the application may crash immediately, or it may con-
tinue to run for a short time and eventuallycrash elsewhere in your code.
.. the 64-bit version of the mburst
object is larger than the 32-bit
version. This is because the size of
the pointers and padding withinthe structure has increased to
keep the members of the structure
properly aligned....
Even attempts to be smart by asking the sys-
tem for the size of each member in the structure(as demonstrated in the following code) will not
succeed. This failure occurs because the
solution does not take into account the paddingthat is added on the 64-bit platform.
Note: malloc() always allocates memoryaligned on 16-byte boundaries on a 32-bit platform.
64-bit Insider NewsletterVolume 1, Issue 1
Page 3/4
-
8/6/2019 64-Bit Insider Volume 1 Issue 1
4/4
mburst* create_mburst(){
mburst* p = (mburst*) malloc(sizeof(int)+sizeof(size_t)+2*sizeof(void*));
p->seq_no = -1;p->size = 0;p->data = NULL;
p->next = NULL;return p;
}
As a general rule, you should not try to outsmart the compiler. Modern compilers are
smart and will get things right more often than you might think. Consequently, to get thesize of a structure, use the sizeof() operator on the structure itself, as shown in the follow-
ing code. The sizeof() operator takes into account all of the padding that has been inserted
into a structureincluding the padding that has sometimes been added to the end of astructure.
... As a general rule,
you should not try to out-smart the compiler. Mod-
ern compilers are very
smart and will get things
right more often than you
might think.
mburst* create_mburst(){
mburst* p = (mburst*) malloc(sizeof(mburst));p->seq_no = -1;p->size = 0;p->data = NULL;p->next = NULL;return p;
}
Upcoming Newsletter
The next 64-bit Insidernewsletter discusses more align-
ment topics, such as hard-coding structure sizes in C++,
miscalculating offsets, custom layouts, packing, and alignment exceptions.
Recommended ReadingLarry Ostermans Weblog: Alignment (part 1)
http://blogs.msdn.com/larryosterman/archive/2005/04/07/406252.aspx
Intel article: Data Alignment when Migrating to 64-bit Architecture
http://www.intel.com/cd/ids/developer/asmo-na/eng/170533.htm
MicrosoftVisual Studio technical article:Windows Data Alignment on IPF, x86, and x86-64
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dv_vstechart/html/vcconWindowsDataAlignmentOnIPFX86X86-64.asp
64-bit Insider NewsletterVolume 1, Issue 1
Page 4/4
http://blogs.msdn.com/larryosterman/archive/2005/04/07/406252.aspxhttp://www.intel.com/cd/ids/developer/asmo-na/eng/170533.htmhttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconWindowsDataAlignmentOnIPFX86X86-64.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconWindowsDataAlignmentOnIPFX86X86-64.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconWindowsDataAlignmentOnIPFX86X86-64.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vcconWindowsDataAlignmentOnIPFX86X86-64.asphttp://www.intel.com/cd/ids/developer/asmo-na/eng/170533.htmhttp://blogs.msdn.com/larryosterman/archive/2005/04/07/406252.aspx