Porting native code from 32-bit to 64-bit systems

If your Java™ application uses native code, you must first ensure that the native code continues to work correctly when it is ported to the 64-bit environment. You must also make appropriate changes to ensure that the native code works with the 64-bit versions of the JNI and JVMTI.

Consult the documentation relating to your operating system and to your C/C++ compiler for changes that are needed to port code from 32 bit to 64 bit. In particular, you should note the following differences:
  • Windows: On 32-bit systems, integers, longs and pointers are all 32 bits. On 64-bit systems, integers and longs remain 32 bits, but pointers become 64 bits and long longs are 64 bits.
  • AIX® and Linux®: On 32-bit systems, integers, longs and pointers are all 32 bits. On 64-bit systems, integers remain 32 bits and longs and pointers become 64 bits.

All native code must be adjusted to work with these differences. The best way to do this is to make changes that allow a single copy of the source code to be compiled either for a 32-bit target system or for a 64-bit target system. Although there is no one best way to achieve this goal, a good way to achieve this is to use abstract types for any integers that need to be of the same size as a pointer. For example, when making address calculations, the abstract integer types map either to 32 bits or to 64 bits depending on the size of a pointer.

On AIX and Linux systems, be careful when using long types. If possible, avoid the use of long integers because they change size between 32-bit systems and 64-bit systems.

You must also make sure that data types are correctly aligned. Incorrect alignment can cause serious loss of code performance. The correct alignment for various data types is shown in the following table:

Table 1. Size or alignment for C and C++ data types on various operating systems
 C/C++ data type Alignment on 32-bit AIX or Linux, or 31-bit z/OS operating systems Alignment on 64-bit AIX, Linux, or z/OS operating systems Alignment on 32-bit Windows operating systems Alignment on 64-bit Windows operating systems
char 1/1 1/1 1/1 1/1
short 2/2 2/2 2/2 2/2
int 4/4 4/4 4/4 4/4
long 4/4 8/8 4/4 4/4
long long 8/8 8/8 8/8 8/8
pointer 4/4 8/8 4/4 8/8
float 4/4 4/4 4/4 4/4
double 8/4 8/4 8/8 8/8
long double 16/16 16/16 8/8 8/8

When the native code has been adjusted to take account of the differences between 32-bit systems and 64-bit systems, tune the code to accommodate differences in the interfaces to the Java VM: the JNI and the JVMTI.