![]() |
![]() |
![]() |
![]() |
![]() |
Adaptation to Compilers
In this section the necessary changes to the source code to adapt it to a new environment are discussed. Adapting to a new environment could mean moving the code to new hardware or using a new compiler.
There are two parts of the source code that might need changes:
- In scttypes.h there is a section defining the properties of different compilers, where a new compiler can be added.
- In sctos.c the functions that depend on the operating system or hardware are collected. These might need to be changed due to a new compiler, a new OS, or a new hardware.
In Compiler Definition Section in scttypes.h the compiler definition section in scttypes.h is discussed in detail, while sctos.c is treated in The sctos.c File.
Compiler Definition Section in scttypes.h
In scttypes.h the properties of the compiler is recognized by the compiler/computer dependent switches set by the compiler:
#if defined(__linux)#define SCT_POSIX#elif defined(__sun)#define SCT_POSIX#elif defined(__hpux)#define SCT_POSIX#elif defined(__CYGWIN__)#define SCT_POSIX#elif defined(QNX4_CC)#define SCT_POSIX#elif defined(__BORLANDC__)#define SCT_WINDOWS#elif defined(_MSC_VER)#define SCT_WINDOWS#else#include "user_cc.h"#endifBasically this section distinguishes between Unix-like/POSIX compilers and Windows compilers. In the case the compiler is not in the list above, the user must configure it himself by writing a file user_cc.h, which is best placed in the target directory.
- __linux : gcc on linux
- __sun : different compilers on SUN
- __hpux : different compilers on HP
- __CYGWIN__ : gcc on windows, for more information please see
http://sources.redhat.com/cygwin/- QNX4_CC : QNX
- __BORLANDC__ : Borland compiler on Windows
- _MSC_VER : Microsoft compiler on Windows
After this compiler configuration section a general configuration section follows:
#if defined(SCT_POSIX) || defined(SCT_WINDOWS)#define XMULTIBYTE_SUPPORT#endif#include <string.h>#include <stdlib.h>#include <limits.h>#include <stdarg.h>#ifdef XREADANDWRITEF#include <stdio.h>#ifdef XMULTIBYTE_SUPPORT#include <locale.h>#endif#endif#ifndef GETINTRAND#define GETINTRAND rand()#endif#ifndef GETINTRAND_MAX#define GETINTRAND_MAX RAND_MAX#endif#ifndef xptrint#if (UINT_MAX < 4294967295)#define xptrint unsigned long#define X_XPTRINT_LONG#else#define xptrint unsigned#endif#endif#ifndef xint32#if (INT_MAX >= 2147483647)#define xint32 int#define X_XINT32_INT#else#define xint32 long int#endif#endifFirst, the presence of multi-byte character support is set up. Then a number of standard include files are included, followed by setting up properties for random number generation. Last the two types, xptrint, which defines an unsigned int type with the same size as an address, and xint32, which defines a 32-bits int type, is configured.
The last three parts in this section handle the utility functions needed by sctos.c to implement some of the operating system dependent functions. Please see below where sctos.c is discussed in detail.
The sctos.c File
The following important functions are defined in sctos.c
extern void * xAlloc (xptrint Size);extern void xFree (void **P);extern void xHalt (void);#ifdef XCLOCKextern SDL_Time SDL_Clock (void);#endif#if defined(XCLOCK) && !defined(XENV)extern void xSleepUntil (SDL_Time WakeUpTime);#endif#if defined(XPMCOMM) && !defined(XENV)extern int xGlobalNodeNumber (void);#endif#if defined(XMONITOR) && !defined(XNOSELECT)extern xbool xCheckForKeyboardInput (long xKeyboardTimeout);#endifSeveral of these functions have three different implementations, one for SCT_POSIX, one for SCT_WINDOWS and one for other cases. The other cases solution is "an empty implementation" that does not do anything. If the standard solutions in sctos.c do not fit the needs of a certain application, any of the functions above can be supplied by the user instead. By defining some of the switches:
- XUSER_ALLOC_FUNC
- XUSER_FREE_FUNC
- XUSER_HALT_FUNC
- XUSER_CLOCK_FUNC
- XUSER_SLEEP_FUNC
- XUSER_KEYBOARD_FUNC
the corresponding function or functions are removed from sctos.o and have to be supplied by the user instead.
xAlloc
The function xAlloc is used to allocate dynamic memory and is used throughout the runtime library and in generated code. The function is given a size in bytes and should return a pointer to a data area of the requested size. All bytes in this data area are set to zero. The standard implementation of this function uses the C function calloc.
A user who wants to estimate the need for dynamic memory can introduce statements in xAlloc to record the number of calls of xAlloc and the total requested size of dynamic memory. Please note two things. A program using the monitor requires more dynamic memory than a program not using the monitor, so estimates should be made with the appropriate compilation switches. A call of calloc will actually allocate more memory than is requested to make it possible for the C runtime system to deallocate and reuse memory. The size of this additional memory is compiler-dependent.
A user who wants to handle the case when no more memory is available at an allocation request can implement that in xAlloc. In the standard implementation for xAlloc a test if calloc returns 0 can be introduced, at which the program can be terminated with an appropriate message.
xFree
The function xFree is used to return memory to the list of free memory so it can be reused by subsequent calls of xAlloc. The standard implementation of this function uses the C function free. In very simple cases, no data types using dynamic memory are used and no other introduction of dynamic data by the user, this function will not be used.
The parameter of the xFree function, is the address of the pointer to the allocated memory.
Example 519 Using the xFree function
unsigned char *ptr;ptr = xAlloc(100);xFree (&ptr); /* NOTE: Not xFree(ptr); */xHalt
The function xHalt is used to exit from a program and is in the standard implementation using the C function exit to perform its task.
SDL_Clock
The function SDL_Clock should return the current time, read from a clock somewhere in the OS or hardware. The return value is of type SDL_Time, that is a struct with two 32-bits integer components, representing seconds and nanoseconds in the time value.
typedef struct {xint32 s; /* for seconds */xint32 ns; /* for nanoseconds */} SDL_Time;The standard implementation of SDL_Clock uses the C function time, which returns the number of seconds since some defined date.
In an embedded system or any other application that requires better time resolution, or when the C function time is not available, SDL_Clock should be implemented by the user.
A typical implementation in an embedded system is to have hardware generating interrupts at a predefined rate. At each such interrupt a variable containing the current time is updated. This variable can then be read by SDL_Clock to return the current time.
xSleep_Until
The function xSleep_Until is given a time value, as a value of type SDL_Time (see above) and should suspend the executing until this time is reached, when it should return.
This function is used only when real time is used (the switch XCLOCK is defined) and when there is no environment functions (XENV is not defined). The xSleep_Until function is used to wait until the next event is scheduled when there is no environment that can generate events.
xGlobalNodeNumber
The function xGlobalNodeNumber is used to assign unique numbers to each SDL system which is part of an application.
If environment functions are used for an SDL system this function should be implemented there. If, however, we have communicating simulations, there are no env functions and the xGlobalNodeNumber function is defined in sctos.c instead.
So the xGlobalNodeNumber function is only used if XPMCOMM is defined and XENV is not defined. As this function is only used in the case of a communicating simulation, it is only necessary to implement it for computers/compilers that communicate with the SDL Suite, which means that it is not interesting for a user to change the standard implementation of this function. The implementation calls the function getpid, and uses thus the OS process number as global node number.
xCheckForKeyboardInput
The function xCheckForKeyboardInput is used to determine if there is a line typed on the keyboard (stdin) or not. If this is difficult to implement it can instead determine if there are any characters typed on the keyboard or not. This function is only used by the monitor system, (when XMONITOR is defined).
The xCheckForKeyboardInput function is used to implement the possibility to interrupt the execution of SDL transitions by typing <Return> and to handle polling of the environment (xInEnv or its equivalent when communicating simulations is used) when the program is waiting at the "Command :" prompt in the monitor.
http://www.ibm.com/rational |
![]() |
![]() |
![]() |
![]() |