[Compiler : 64-bit data model]
First of all, you must check the 64-bit data model of your compiler.
In case of 64-bit supporting compiler,
there is the 64-bit data model(about the length of the c primitive types).
When you search about the 64-bit programming,
you can see the many articles such as LLP64 / LP64 / ILP64(Integer, Long, Pointer).
- LLP64 : only pointer type is 64-bit. (MSVC)
- LP64 : long & pointer types are 64-bit (GCC)
- ILP64 : int & long & pointer types are 64-bit (just few case)
* You can find the detail information on http://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models.
I learned that the INTEGER type follows the size of CPU generic register,
in the time of changing from 16-bit to 32-bit.
But, in this time that information is wrong.
I guess that the gurus decide to avoid the huge modification of legacy codes
by changing the size of INTEGER.
(Maybe, the Microsoft decides to retain the length of LONG type because of this reason.)
[Important types : size_t / ssize_t / offset_t]
The 64-bit system can handle the large amount of memory and storage
with these types.
You can see that these types are defined as LONG
in the header files of Linux. (LP system)
Of cause the 32-bit system must be able to handle the large storage,
so there is "off64_t" when you use the "_LAGEFILE64_SOURCE" or "_FILE_OFFSET_BITS=64" in compile time.
We have to understand of these type for designing the compatible library both 32-bit and 64-bit.
The system calls those we frequently use "read", "memcpy", "strncpy"
are defined as below:
ssize_t read(int fd, void *buf, size_t count);
void *memcpy(void *dest, const void *src, size_t n);
char *strncpy(char *dest, const char *src, size_t n);
MSVC also has similar types such as "SIZE_T", "SSIZE_T",
and the size of those is changed from 32-bit system to 64-bit.
[Use the : intptr_t / uintptr_t]
In case of the "pthread" API,
"pthread_t" type to be used as the thread handle is LONG.
It looks like a decimal value, but the real value is the pointer address.
In windows programming, the "WPARAM" and "LPARAM" to be used in event loop
are cast to the pointer value frequently.
It is vary useful to cast between INTEGER and POINTER
for hiding the structure and eliminating the dependency.
But, in the LP data model, the size of INTEGER is still 32-bit,
and the size of POINTER is 64-bit.
This difference can cause the bugs and system fault.
For solving this problem, there is "intptr_t" type in the C99 specification.
The standard header file "stdint.h" is
introduced for converting a pointer to integer exactly.
So, you can be free just using the "intptr_t" in that case.
[Examples]
You can see the custom type definition
in "glib" or "APR(Apache Portable Runtime)".
It is used for overcoming the differences of systems or compilers.
So, those custom definitions are like as below :
/************************************************
* Primitive Types Definition
* ----------------------------------------------
* References the inttypes.h
* Both Win32 and Linux are equal.
************************************************/
typedef void xvoid; ///< define the type : void to xvoid
#ifdef __cplusplus
typedef bool xbool; ///< define the type : bool to xbool
#else // !__cplusplus
typedef unsigned char xbool; ///< define the type : unsigned char to xbool
#endif // __cplusplus
typedef char xchar; ///< define the type : char to xchar
typedef char xint8; ///< define the type : char to xint8
typedef unsigned char xuint8; ///< define the type : unsigned char to xuint8
typedef short xint16; ///< define the type : short to xint16
typedef unsigned short xuint16; ///< define the type : unsigned short to xuint16
typedef int xint32; ///< define the type : int to xint32
typedef unsigned int xuint32; ///< define the type : unsigned int to xuint32
typedef long long xint64; ///< define the type : long long to xint64
typedef unsigned long long xuint64; ///< define the type : unsigned long long to xuint64
typedef float xfloat32; ///< define the type : float to xfloat32
typedef double xfloat64; ///< define the type : double to xfloat64
/************************************************
* Pointer Type Definition
* ----------------------------------------------
* for both 32-bit and 64-bit (LP Architecture)
************************************************/
#ifdef _WIN32
# ifdef _WIN64
typedef __int64 xintptr; ///< define the long to integer-pointer
typedef unsigned __int64 xuintptr; ///< define the unsigned long to unsigned integer-pointer
# else // !_WIN64
typedef long xintptr; ///< define the long to integer-pointer
typedef unsigned long xuintptr; ///< define the unsigned long to unsigned integer-pointer
# endif // _WIN64
#else // !_WIN32
typedef long xintptr; ///< define the long to integer-pointer
typedef unsigned long xuintptr; ///< define the unsigned long to unsigned integer-pointer
#endif // _WIN32
/************************************************
* Size and Offset Type Definition
* ----------------------------------------------
* for both 32-bit and 64-bit (LP Architecture)
************************************************/
#if defined(__x86_64) || defined(__amd64)
typedef long unsigned int xsize;
typedef long xoff64; ///< define the off64 to long (64bit)
#else // !(define(__x86_64) || define(__amd64))
typedef unsigned int xsize;
typedef long long xoff64; ///< define the off64 to long long (64bit)
#endif // define(__x86_64) || define(__amd64)
/**
* Copies len bytes from memory area src to memory area dest.
* The memory areas should not overlap.
*
* @param dest The destination pointer to be copied
* @param src The source pointer to be copied
* @param len The length to be copied
* @return Same pointer as the destination pointer
*/
xvoid *xi_mem_copy(xvoid *dest, const xvoid *src, xsize len);