gtpa2m2e | Application Programming |
The following 3 sample code examples correlate to the RENT standard described in TPF Programming Standards.
Sample 1
/**********************************************************************/ /* c$xmp1.h: header file showing declarations of external linkage */ /* objects. */ /**********************************************************************/ /***************************************/ /* Declarations of writable externals. */ /***************************************/ extern int i; #define NUM_ELEMENTS 3 extern int ia[NUM_ELEMENTS]; extern const char *ccp; /* non-const pointer to const char */ /**********************************************************************/ /* Declarations of NORENT read-only externals. There are various */ /* reasons that you may need to specify NORENT externals, including: */ /* */ /* 1. Defining externals in a runtime library which cannot contain */ /* RENT static for performance reasons. */ /* */ /* 2. Referring to an external which is defined in assembler. */ /* */ /* Note that #pragma variable does not work on static variables with */ /* internal linkage (i.e. declared with the "static" keyword) or */ /* string literals (such as "Hello, world!\n"); for these you need */ /* the NORENT compile time option or #pragma strings(readonly). */ /* */ /* Note also that all NORENT variables must be read only and should */ /* therefore be declared using the "const" keyword. */ /**********************************************************************/ #pragma variable(const_i,NORENT) extern const int const_i; #pragma variable(const_ia,NORENT) extern const int const_ia[NUM_ELEMENTS]; #pragma variable(cp_const,NORENT) extern char * const cp_const; /* const pointer to non-const char */
Sample 2
/**********************************************************************/ /* xmp1d.c: C source file showing definitions of external linkage */ /* objects. */ /* */ /* Note: This module is not naturally reentrant because of the */ /* modifiable static duration objects that it defines, so it */ /* must be compiled with the RENT option. It also defines a */ /* writable static object with internal linkage (see variable */ /* sca below). */ /**********************************************************************/ /**********************************************************************/ /* The header file (above) containing the external declarations is */ /* included to guarantee that the definitions and the RENTness are */ /* consistent with the declarations which are #included in source */ /* files that reference these externals. */ /**********************************************************************/ #include <c$xmp1.h> /* include declarations of externals */ /**********************************/ /* Define the writable externals. */ /**********************************/ int i = 17; int ia[NUM_ELEMENTS] = { 0, 1, 2 }; const char *ccp = "ccp can be changed to point to another string."; /********************************/ /* Define the NORENT externals. */ /********************************/ const int const_i = 42; const int const_ia[NUM_ELEMENTS] = { 0xC0, 0xC1, 0xC2 }; static char sca[] = "cp_const will always point to this array, but the "contents of the array can change."; char * const cp_const = sca;
Sample 3
/**********************************************************************/ /* xmp1u.c: C source file showing use of external linkage objects. */ /* */ /* Note: This module does not define any static duration objects, */ /* but it does REFER to the static duration objects which are */ /* compiled with the RENT option; therefore, it must also be */ /* compiled with the RENT option. */ /**********************************************************************/ #include <string.h> #include <c$xmp1.h> /* include declarations of externals */ int foo(void) { /****************************************************************/ /* Only the RENT/writable (non-const) variables can be changed. */ /****************************************************************/ for (i = 0; i < NUM_ELEMENTS; ++i) { ia[i] = const_ia[i]; } strcpy(cp_const, ccp); /* change the non-const chars */ ccp = cp_const; /* change the non-const pointer */ return const_i; }