United States

Compaq C

# Compaq C
Run-Time Library Reference Manual for OpenVMS Systems

Previous | Contents | Index

## 1.7.1 Reentrancy

The Compaq C RTL supports an improved and enhanced reentrancy. The following types of reentrancy are supported:

- AST reentrancy uses the _BBSSI built-in function to perform simple locking around critical sections of RTL code, but it may also disable asynchronous system traps (AST)s in locked regions of code. This type of locking should be used when AST code contains calls to Compaq C RTL I/O routines.
  Failure to specify AST reentrancy might cause I/O routines to fail, setting `errno` to EALREADY.
- MULTITHREAD reentrancy is designed to be used in threaded programs such as those that use the DECthreads library. It performs DECthreads locking and never disables ASTs. DECthreads must be available on your system to use this form of reentrancy.
- TOLERANT reentrancy uses the _BBSSI built-in function to perform simple locking around critical sections of RTL code, but ASTs are not disabled. This type of locking should be used when ASTs are used and must be delivered immediately.
- NONE gives optimal performance in the Compaq C RTL, but does absolutely no locking around critical sections of RTL code. It should only be used in a single-threaded environment when there is no chance that the thread of execution will be interrupted by an AST that would call the Compaq C RTL.

The default reentrancy type is TOLERANT.

You can set the reentrancy type by compiling with the /REENTRANCY command-line qualifier or by calling the `decc$set_reentrancy` function. This function must be called exclusively from non-AST level.

When programming an application using multiple threads or ASTs, you should consider three classes of functions:

- Functions with no internal data
- Functions with thread-local internal data
- Functions with process-wide internal data

Most functions have no internal data at all. For these functions, synchronization is necessary only if the parameter is used by the application in multiple threads or in both AST and non-AST context. For example, although the `strcat` function is ordinarily safe, the following is an example of unsafe usage:

    extern char buffer[100];
    void routine1(char *data) {
        strcat( buffer, data );
    }

If `routine1` executed concurrently in multiple threads, or if `routine1` is interrupted by an AST routine that calls it, the results of the `strcat` call are unpredictable.

The second class of functions are those that have thread-local static data. Typically, these are routines in the library that return a string where the application is not permitted to free the storage for the string. These routines are thread-safe but not AST-reentrant. This means they can safely be called concurrently, and each thread will have its own copy of the data. They cannot be called from AST routines if it is possible that the same routine was executing in non-AST context. The routines in this class are:

    asctime        stat
    ctermid        strerror
    ctime          strtok
    cuserid        VAXC$ESTABLISH
    gmtime         the errno variable
    localtime      wcstok
    perror

All the socket functions are also included in this list if the TCP/IP product in use is thread-safe.

The third class of functions are those that affect process-wide data. These functions are neither thread-safe nor AST-reentrant. For example, `sigsetmask` establishes the process-wide signal mask. Consider a routine like the following:

    void update_data
    base()
    {
        int old_mask;

        old_mask = sigsetmask( 1 << (SIGINT - 1));
            /* Do work here that should not be aborted. */
        sigsetmask( old_mask );
    }

If `update_database` was called concurrently in multiple threads, thread 1 might unblock SIGINT while thread 2 was still performing work that should not be aborted.

The routines in this class are:

- All the `signal` routines
- All the `exec` routines
- The `exit` , `_exit` , `nice` , `system` , `wait` , `getitimer` , `setitimer` , and `setlocale` routines.

Note Generally speaking, UTC-based time functions can affect in-memory time-zone information, which is process-wide data. However, if the system time zone remains the same during the execution of the application (which is the common case) and the cache of timezone files is enabled (which is the default), then the `_r` variant of the time functions `asctime_r` , `ctime_r` , `gmtime_r` and `localtime_r` is both thread-safe and AST-reentrant. If, however, the system time zone can change during the execution of the application or the cache of timezone files is not enabled, then both variants of the UTC-based time functions belong to the third class of functions, which are neither thread-safe nor AST-reentrant.

## 1.7.2 Multithread Restrictions

Mixing the multithread programming model and the OpenVMS AST programming model in the same application is not recommended. The application has no mechanism to control which thread gets interrupted by an AST. This can result in a resource deadlock if the thread holds a resource that is also needed by the AST routine. The following routines use mutexes. To avoid a potential resource deadlock, do not call them from AST routines in a multithreaded application.

- All the I/O routines
- All the socket routines
- All the signal routines
- `vfork` , `exec` , `wait` , `system`
- `catgets`
- `set_new_handler` (C++ only)
- `getenv`
- `rand` and `srand`
- `exit` and `_exit`
- `clock`
- `nice`
- `times`
- `ctime` , `localtime` , `asctime` , `mktime`

# 1.8 64-bit Pointer Support (ALPHA ONLY)

This section is for application developers who need to use 64-bit virtual memory addressing on OpenVMS Alpha Version 7.0 or higher.

OpenVMS Alpha 64-bit virtual addressing support makes the 64-bit virtual address space defined by the Alpha architecture available to both the OpenVMS operating system and its users. It also allows per-process virtual addressing for accessing dynamically mapped data beyond traditional 32-bit limits.

The Compaq C Run-Time Library on OpenVMS Alpha Version 7.0 systems and higher includes the following features in support of 64-bit pointers:

- Guaranteed binary and source compatibility of existing programs
- No impact on applications that are not modified to exploit 64-bit support
- Enhanced memory allocation routines that allocate 64-bit memory
- Widened function parameters to accommodate 64-bit pointers
- Dual implementations of functions that need to know the pointer size used by the caller
- New information available to the DEC C Version 5.2 compiler or higher to seamlessly call the correct implementation
- Ability to explicitly call either the 32-bit or 64-bit form of functions for applications that mix pointer sizes
- A single shareable image for use by 32-bit and 64-bit applications

## 1.8.1 Using the Compaq C Run-Time Library

The Compaq C Run-Time library on OpenVMS Alpha Version 7.0 systems and higher can generate and accept 64-bit pointers. Functions that require a second interface to be used with 64-bit pointers reside in the same object libraries and shareable images as their 32-bit counterparts. No new object libraries or shareable images are introduced. Using 64-bit pointers does not require changes to your link command or link options files.

The Compaq C 64-bit environment allows an application to use both 32-bit and 64-bit addresses. For more information about how to manipulate pointer sizes, see the /POINTER_SIZE qualifier and `#pragma pointer_size` and `#pragma required_pointer_size` preprocessor directives in the Compaq C User's Guide for OpenVMS Systems.

The /POINTER_SIZE qualifier requires you to specify a value of 32 or 64. This value is used as the default pointer size within the compilation unit. As an application programmer, you can compile one set of modules by using 32-bit pointers and another set by using 64-bit pointers. Care must be taken when these two separate groups of modules call each other.

Use of the /POINTER_SIZE qualifier also influences the processing of Compaq C RTL header files. For those functions that have a 32-bit and 64-bit implementation, specifying /POINTER_SIZE enables function prototypes to access both functions, regardless of the actual value supplied to the qualifier. In addition, the value specified to the qualifier determines the default implementation to call during that compilation unit.

The `#pragma pointer_size` and `#pragma required_pointer_size` preprocessor directives can be used to change the pointer size in effect within a compilation unit. You can default pointers to 32-bit pointers and then declare specific pointers within the module as 64-bit pointers. You would also need to specifically call the `_malloc64` form of `malloc` to obtain memory from the 64-bit memory area.

## 1.8.2 Obtaining 64-bit Pointers to Memory

The Compaq C RTL has many functions that return pointers to newly allocated memory. In each of these functions, the application owns the memory pointed to and is responsible for freeing that memory.

Functions that allocate memory are:

> `malloc`
> `calloc`
> `realloc`
> `strdup`

Each of these functions have a 32-bit and a 64-bit implementation. When the /POINTER_SIZE qualifier is used, the following functions can also be called:

> `_malloc32` , `_malloc64`
> `_calloc32` , `_calloc64`
> `_realloc32` , `_realloc64`
> `_strdup32` , `_strdup64`

When /POINTER_SIZE=32 is specified, all `malloc` calls default to `_malloc32` .

When /POINTER_SIZE=64 is specified, all `malloc` calls default to `_malloc64` .

Regardless of whether the application calls a 32-bit or 64-bit memory allocation routine, there is still a single `free` function. This function accepts either pointer size.

Be aware that the memory allocation functions are the only ones that return pointers to 64-bit memory. All Compaq C RTL structure pointers returned to the calling application (such as a FILE, WINDOW, or DIR) are always 32-bit pointers. This allows both 32-bit and 64-bit callers to pass these structure pointers within the application.

## 1.8.3 Compaq C Header Files

The header files distributed with DEC C Version 5.2 and higher support 64-bit pointers. Each function prototype whose signature contains a pointer is constructed to indicate the size of the pointer accepted.

A 32-bit pointer can be passed as an argument to functions that accept either a 32-bit or 64-bit pointer for that argument.

A 64-bit pointer, however, cannot be passed as an argument to a function that accepts a 32-bit pointer. Attempts to do this are diagnosed by the compiler with a MAYLOSEDATA message. The diagnostic message IMPLICITFUNC means the compiler can do no additional pointer-size validation for calls to that function. If this function is a Compaq C RTL function, refer to the reference section of this manual for the name of the header file that defines that function.

You might find the following pointer-size compiler diagnostics useful:

- %CC-IMPLICITFUNC
  A function prototype was not found before using the specified function. The compiler and run-time system rely on prototype definitions to detect incorrect pointer-size usage. Failure to include the proper header files can lead to incorrect results and/or pointer truncation.
- %CC-MAYLOSEDATA
  A truncation is necessary to do this operation. The operation could be passing a 64-bit pointer to a function that does not support a 64-bit pointer in the given context. It could also be a function returning a 64-bit pointer to a calling application that is trying to store that return value in a 32-bit pointer.
- %CC-MAYHIDELOSS
  This message (when enabled) helps expose real MAYLOSEDATA messages that are being suppressed because of a cast operation. To enable this warning, compile with the qualifier /WARNINGS=ENABLE=MAYHIDELOSS.

## 1.8.4 Functions Affected

The Compaq C RTL shipped with OpenVMS Alpha Version 7.0 accommodates applications that use only 32-bit pointers, only 64-bit pointers, or combinations of both. To use 64-bit memory, you must, at a minimum, recompile and relink an application. The amount of source code change required depends on the application itself, calls to other runtime libraries, and the combinations of pointer sizes used.

With respect to 64-bit pointer support, the functions in the Compaq C RTL fall into four categories:

- Functions not impacted by choice of pointer size
- Functions enhanced to accept either pointer size
- Functions having a 32-bit and 64-bit implementation
- Functions that accept only 32-bit pointers

From an application developer's perspective, the first two types of functions are the easiest to use in either a single or mixed-pointer mode.

The third type requires no modifications when used in a single-pointer compilation, but might require source code changes when used in a mixed-pointer mode.

The fourth type requires careful attention whenever 64-bit pointers are used.

### 1.8.4.1 No Pointer-Size Impact

The choice of pointer-size has no impact on a function if its prototype contains no pointer-related parameters or return values. The mathematical functions are good examples of this.

Even some functions in this category that do have pointers in their prototype are not impacted by pointer size. For example, `strerror` has the prototype:

    char * strerror (int error_number);

This function returns a pointer to a character string, but this string is allocated by the Compaq C RTL. As a result, to support both 32-bit and 64-bit applications, these types of pointers are guaranteed to fit in a 32-bit pointer.

### 1.8.4.2 Functions Accepting Both Pointer Sizes

The Alpha architecture supports 64-bit pointers. The OpenVMS Alpha calling standard specifies that all arguments are actually passed as 64-bit values. Before OpenVMS Alpha Version 7.0, all 32-bit addresses passed to procedures were sign-extended into this 64-bit parameter. The called function declared the parameters as 32-bit addresses, which caused the compiler to generate 32-bit instructions (such as LDL) to manipulate these parameters.

Many functions in the Compaq C RTL are enhanced to receive the full 64-bit address. For example, consider `strlen` :

    size_t strlen (const char *string);

The only pointer in this function is the character-string pointer. If the caller passes a 32-bit pointer, the function works with the sign-extended 64-bit address. If the caller passes a 64-bit address, the function works with that address directly.

The Compaq C RTL continues to have only a single entry point for functions in this category. There are no source-code changes required to add any of the four pointer-size options for functions of this type. The OpenVMS documentation refers to these functions as 64-bit friendly.

### 1.8.4.3 Functions with Two Implementations

There are many reasons why a function might need one implementation for 32-bit pointers, and another for 64-bit pointers. Some of these reasons include:

- The pointer size of the return value is the same size as the pointer size of one of the arguments. If the argument is 32-bits, the return value is 32-bits. If the argument is 64-bits, the return value is 64-bits.
- One of the arguments is a pointer to an object whose size is pointer-size sensitive. To know how many bytes are being pointed to, the function must know if the code was compiled in 32-bit or 64-bit pointer-size mode.
- The function returns the address of dynamically allocated memory. The memory is allocated in 32-bit space when compiled for 32-bit pointers, and is allocated in 64-bit space when compiled for 64-bit pointers.

From the application developer's point of view, there are three function prototypes for each of these functions. The `<string.h>` header file contains many functions whose return value is dependent upon the pointer size used as the first argument to the function call. For example, consider the `memset` function. The header file defines three entry points for this function:

    void * memset   (void *memory_pointer, int character, size_t size);
    void *_memset32 (void *memory_pointer, int character, size_t size);
    void *_memset64 (void *memory_pointer, int character, size_t size);

The first prototype is the function that your application would currently call if using this function. The compiler changes a call to `memset` into a call to either `_memset32` when compiled /POINTER_SIZE=32, or `_memset64` when compiled /POINTER_SIZE=64.

You can override this default behavior by directly calling either the 32-bit or the 64-bit form of the function. This accommodates applications using mixed pointer sizes, regardless of the default pointer size specified with the /POINTER_SIZE qualifier.

If the application is compiled without specifying the /POINTER_SIZE qualifier, neither the 32-bit specific nor the 64-bit specific function prototypes are defined. In this case, the compiler automatically calls the 32-bit interface for all interfaces having dual implementations.

Table 1-5 shows the Compaq C RTL functions that have dual implementations in support of 64-bit pointer size. When compiling with the /POINTER_SIZE qualifier, calls to the unmodified function names are changed to calls to the function interface that matches the pointer size specified on the qualifier.

Table 1-5 Functions with Dual Implementations
`basename` | `malloc` | `strpbrk` | `wcsncat`
`bsearch` | `mbsrtowcs` | `strptime` | `wcsncpy`
`calloc` | `memccpy` | `strrchr` | `wcspbrk`
`catgets` | `memchr` | `strsep` | `wcsrchr`
`ctermid` | `memcpy` | `strstr` | `wcsrtombs`
`cuserid` | `memmove` | `strtod` | `wcsstr`
`dirname` | `memset` | `strtok` | `wcstok`
`fgetname` | `mktemp` | `strtol` | `wcstol`
`fgets` | `mmap` | `strtoll` | `wcstoul`
`fgetws` | `qsort` | `strtoq` | `wcswcs`
`fullname` | `realloc` | `strtoul` | `wmemchr`
`gcvt` | `rindex` | `strtoull` | `wmemcpy`
`getcap` | `strcat` | `strtouq` | `wmemmove`
`getcwd` | `strchr` | `tgetstr` | `wmemset`
`getname` | `strcpy` | `tmpnam` |
`gets` | `strdup` | `wcscat` |
`index` | `strncat` | `wcschr` |
`longname` | `strncpy` | `wcscpy` |

Previous | Next | Contents | Index

privacy and legal statement