C/C++ Programmer's Guide (G06.27+, H06.08+, J06.03+)
Sharing Data
Using pointers to share data is easier and safer than trying to match declarations in both languages.
Using pointers also eliminates problems associated with where the data is placed.
The default code generation for pointer dereferencing operations (REFALIGNED 8) expects the
pointer to contain an address that satisfies the alignment requirements of the object being pointed
to. For example, a 4-byte object should have an address that is a multiple of 4.
If the object is at an address that does not satisfy its alignment requirements, the default code
generation will cause a compatibility trap. To avoid this, specify the REFALIGNED 2 C pragma or
the REFALIGNED 2 pTAL directive on the pointer to the object. This will result in code generation
that assumes that the dereferenced object is not properly aligned and will compensate for that.
To share data by using pointers, first decide whether the pTAL module or the C module is to declare
the data:
• If the pTAL module is to declare the data, follow the guidelines in Sharing pTAL Data With C
Using Pointers (page 130).
• If the C module is to declare the data, follow the guidelines in Sharing C Data With pTAL
Using Pointers (page 131)
• If both the pTAL and the C modules are to declare the data, follow the guidelines in Sharing
pTAL Data With C Using BLOCK Declarations (page 132) or in Sharing pTAL Global Data
With C/C++ Using BLOCK Declarations (page 132).
For 64-bit unsigned data, use the FIELDALIGN pragma to select the data alignment that is
appropriate for the compiler you use:
• SHARED2 directs the TNS C, native C, and native C++ compilers to lay out components using
the default TAL compiler alignment rules.
• SHARED8 directs the TNS C, native C, and native C++ compilers to lay out components using
the pTAL compiler SHARED8 alignment rules.
FIELDALIGN AUTO is recommended when the data layout is target independent and not shared.
FIELDALIGN PLATFORM is used to share data across languages for the same hardware platform.
It lays out components using the platform's C compiler AUTO rules. That is, PLATFORM directs the
TNS C compiler to lay out components using the SHARED2 alignment rules that are the AUTO rules
for TNS platforms, and directs the native C and C++ compilers to lay out components using the
AUTO alignment rules for their respective TNS/R and TNS/E platforms.
Sharing pTAL Data With C Using Pointers
To share pTAL global data with C modules, follow these steps:
1. In the pTAL module, declare the data using C-compatible identifiers, data types, and alignments.
(Alignments depend on byte or word addressing and variable layouts as described in
Parameters and Variables (page 134).)
2. In the C module, declare pointers to the data, using pTAL-compatible data types.
3. In the C module, declare a routine to which pTAL can pass the addresses of the shared data.
4. In the C routine, initialize the pointers with the addresses sent by the pTAL module.
5. Use the pointers in the C module to access the pTAL data.
This example shows how to share pTAL data with a C module. The pTAL module passes to a C
routine the addresses of two pTAL arrays. The C routine assigns the array addresses to C pointers.
C Code
short *c_int_ptr; /* pointer to pTAL data */
char *c_char_ptr; /* pointer to pTAL data */
short INIT_C_PTRS (short *tal_intptr, char *tal_strptr)
{ /* called from pTAL */
c_int_ptr = tal_intptr;
c_char_ptr = tal_strptr;
130 Mixed-Language Programming for TNS/R and TNS/E Native Programs