64-bit insider volume 1 issue 3

4
 64-bit Insider Volume I, Issue 3 Guidelines for Writing Effective Polymorphic Code: 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. Using Separate Code Bases The need to create applications that target different archi- tectures is not new. Throughout the history of computing, it has been proven that there are advantages to generat- ing—from a single code base—different executables that can run on a diverse set of platforms. This approach gener- ates an application that has a competitive edge over other applications that run on a single platform or that require a substantial amount of work to be deployed on different ar- chitectures. However, it is also important to consider the various factors that have an impact on the level of effort required to migrate an application to a different platform. One of the most important impacts being the portability of the language on which the application has been written. 64-bit development and migration is not as complicated as the 16-bit to 32-bit transition. However, as with any new technology, there are several areas that require close examination and consideration. The goal of the 64-bit Insider newsletter is to identify potential migration issues and provide vi- able, 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 previ- ously published information. In- stead, it will focus on 64-bit issues that are somewhat isolated yet extremely important to under- stand. It will also connect you to reports and findings from 64-bit experts. This edition of the 64-bit Insider newsletter introduces poly- morphic data types, which allow you to create applications written in C or C++ that, from a single source code, can be compiled to run on x86, x64, and Itanium processors. 64-bit Insider Newsletter Volume 1, Issue 3 Page 1/4 

Upload: nayeemkhan

Post on 08-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 64-Bit Insider Volume 1 Issue 3

8/6/2019 64-Bit Insider Volume 1 Issue 3

http://slidepdf.com/reader/full/64-bit-insider-volume-1-issue-3 1/4

 

64-bit InsiderVolume I, Issue 3

Guidelines for Writing EffectivePolymorphic Code: Part 1

The 64-bit AdvantageThe 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.

Using Separate Code Bases The need to create applications that target different archi-

tectures is not new. Throughout the history of computing,

it has been proven that there are advantages to generat-

ing—from a single code base—different executables that

can run on a diverse set of platforms. This approach gener-

ates an application that has a competitive edge over other

applications that run on a single platform or that require a

substantial amount of work to be deployed on different ar-

chitectures. However, it is also important to consider the

various factors that have an impact on the level of effort

required to migrate an application to a different platform.

One of the most important impacts being the portability of 

the language on which the application has been written.

64-bit development and migration

is not as complicated as the 16-bit

to 32-bit transition. However, as

with any new technology, there

are several areas that require closeexamination and consideration.

The goal of the 64-bit Insider 

newsletter  is to identify potential

migration issues and provide vi-

able, 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 previ-

ously published information. In-

stead, it will focus on 64-bit issues

that are somewhat isolated yet

extremely important to under-stand. It will also connect you to

reports and findings from 64-bit

experts.

This edition of the 64-bit Insider newsletter introduces poly-

morphic data types, which allow you to create applications

written in C or C++ that, from a single source code, can be

compiled to run on x86, x64, and Itanium processors.

64-bit Insider Newsletter

Volume 1, Issue 3

Page 1/4 

Page 2: 64-Bit Insider Volume 1 Issue 3

8/6/2019 64-Bit Insider Volume 1 Issue 3

http://slidepdf.com/reader/full/64-bit-insider-volume-1-issue-3 2/4

 Introducing Polymorphic Data Types

Polymorphic data types consist of several new pointer-

precision types that have a different size depending on

the program’s target architecture. This size matches that

of the pointer in the architecture to which the

application is to be compiled.

For example, consider the INT_PTR type. If this type is

being compiled for a 32-bit processor, then it will be

4 bytes in size. However, if the target platform is a 64-

bit processor, then the size of  INT_PTR type will be 8 bytes.

The polymorphism on the INT_PTR type is defined in BaseTsd.h, as follows.

#if defined(_WIN64)

typedef __int64 INT_PTR;

#elsetypedef int INT_PTR;

#endif

The Importance of Polymorphic Data Types

With the introduction of the 64-bit version of Microsoft Windows operating system

comes a need to deploy applications that target various platforms. In some cases, this ap-

proach requires applications to be built from scratch. However, many existing 32-bit ap-

plications can be migrated to the 64-bit platform.

When migrating an application, one of the factors that will determine the success of the

migration is whether an easy migration path has been provided. And while there aremany key points to consider when migrating an application, one of the most important

involves pointer truncation.

Pointer truncation is a plausible threat when completing a 32-bit to 64-bit migration be-

cause the pointer size in the 64-bit environment is twice as big as the pointer size in a 32-

bit environment. This size difference means that, while integer and pointer types can be

typecast from one to the other without any major complications in the 32-bit environ-

ment, such is not the case when migrating to the 64-bit

environment. When a pointer has been converted to an

integer type in the 64-bit environment, the higher 32

bits of the pointer will be lost or truncated because youcannot fit 64 bits into 32 bits of data.

“... while there are many

key points to consider whenmigrating an application,

one of the most important 

involves pointer trunca-

tion.”

When programming in C or C++ on 32-bit Windows,

the types int and long, and also the pointers are all 32

bits in size. This is known as the ILP32 (int , long,

pointer) data model.

64-bit Insider Newsletter

Volume 1, Issue 3

Page 2/4 

Page 3: 64-Bit Insider Volume 1 Issue 3

8/6/2019 64-Bit Insider Volume 1 Issue 3

http://slidepdf.com/reader/full/64-bit-insider-volume-1-issue-3 3/4

However, 64-bit Windows uses the LLP64 data model (also known as the P64 data

model), the name of which indicates that only pointers have changed size to 64 bits.

When performing typecasts in the ILP32 model, you can mix pointers and integer types

without losing any data. However, in the LLP64 data model, typecasting from pointers to

integer types is not safe because of the differences in size. Any attempt to use a truncated

pointer can compromise the application’s stability and

raise an exception because the pointer’s address might

have been modified during the cast.“... if you want to create a

common code base, you

must review your code and 

eliminate any pointer trun-

cation issues.”

In typecasting situations, polymorphic data types can be

extremely useful. Polymorphic data types provide an

easy way to target both 32-bit and 64-bit platforms

without having to worry about pointer typecasting

problems. But to use polymorphic data types, you must

follow some important guidelines.

Using Polymorphic Data TypesThe first and most important rule to follow when typecasting pointers in 64-bit Windows

is to only cast between compatible types. In a 32-bit application pointer types are com-

patible with the integer types int , long, ULONG, DWORD, and many others. In a 64-bit

application this is no longer true. If you run 32-bit and 64-bit versions of your application

from the same source code, unexpected results will show up at runtime in your 64-bit ap-

plication because of pointer truncation problems.

Therefore, if you want to create a common code base, you must review your code and

eliminate any pointer truncation issues. This procedure might seem like a daunting task.

But luckily you can use the compiler to pinpoint possible typecasting errors by adding the

 /Wp64 flag as a parameter during compile time.

Let’s look at a simple example that shows pointer casting to integer types.

Line 118: void *newPtr = (void *)((int)ptrToCast);

When you compile code using the /Wp64 flag, the compiler reports each possible port-

ability error.

pointertruncation.cpp(118) : warning C4311: 'type cast' : pointer

truncation from 'void *' to 'int'

pointertruncation.cpp(118) : warning C4312: 'type cast' : conversion from 'int' to 'void *' of greater size

64-bit Insider Newsletter

Volume 1, Issue 3

Page 3/4 

Page 4: 64-Bit Insider Volume 1 Issue 3

8/6/2019 64-Bit Insider Volume 1 Issue 3

http://slidepdf.com/reader/full/64-bit-insider-volume-1-issue-3 4/4

 Note Please remember that the /Wp64 flag can be used for both 32-bit and 64-bit compilers. The warnings dis-

played for both compilers will be the same.

It is important to analyze all warnings that result from using the  /Wp64 flag. In this ex-

ample, there are two portability warnings on line 118. To address these warnings, you

first must understand why they occurred. The remaining discussion assumes the target isa 64-bit platform.

On line 118, ptrToCast is a void pointer, which means that its current size is 8 bytes.

However, this pointer is being typecast to int , which is only 4 bytes in size. As a result,

pointer truncation occurs because the upper 32 bits of ptrToCast will be lost.

In addition, after ptrToCast has been

typecast to int , it will be typecast again

to a void pointer. In this case a 4-byte 

int will be converted to an 8-byte

pointer and the upper 32 bits will be

meaningless.

64-bit Insider Newsletter

Volume 1, Issue 3

Page 4/4 

To avoid truncation, it is necessary to

perform all castings to types that have

the same size.

Upcoming Newsletter

The next issue of the 64-bit Insider 

newsletter will further examine how to use polymorphic types to fix your code to be 32-

bit and 64-bit platform-compliant. In addition, Issue 4 will discuss how pointer truncation

can result in unstable code, and how you can resolve this problem.

 Recommended Reading

Rules for Using Pointers

http://msdn.microsoft.com/library/default.asp?url=/library/en-

us/win64/win64/rules_for_using_pointers.asp

 

Tricks for Porting Applications to 64-Bit Windows 

http://www.devx.com/amd/Article/17783

 

The New Data Types

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/win64/win64/the_new_data_types.asp