![]() |
![]() |
![]() |
![]() |
![]() |
Integration with C Data Types
The package ctypes presented below contains a number of types and generators that is intended to directly support C data types in SDL. The package ctypes can also be used in OS integrations and with Cmicro. However, usage of C pointers (generator Ref) might cause problems, due to potential memory leaks and potential memory access protection between OS tasks.
The file ctypes.sdl is a SDL/PR version of this package suitable to use in an include statement in an SDL/PR system, while ctypes.sun is a SDL/GR version of the package.
In an SDL/GR system it is only necessary to insert a use clause, i.e.
use ctypes;at a proper place. The Organizer will then by itself include the ctypes package, for example when the system is to be analyzed. To use the ctypes package in an SDL/PR system the following structure should be used.
/*#include `ctypes.sdl'*/use ctypes;system example;...endsystem;The ctypes package consists of the following newtypes, syntypes, and generators:
All the newtypes and syntypes introduce type names for "standard types" in C.
Some of the types and generators are briefly described below.
Charstar
In Charstar there is a literal and some operators included:
LITERALSNull;OPERATORScstar2cstring : Charstar -> Charstring;cstring2cstar : Charstring -> Charstar;cstar2vstar : Charstar -> Voidstar;vstar2cstar : Voidstar -> Charstar;cstar2vstarstar : Charstar -> Voidstarstar;Note that the operators cstar2cstring and cstring2cstar are not available when using Cmicro.
The operators are all conversion routines to convert a value from one type to another. Note that Charstar and Charstring are not the same types even if they both corresponds to char * in C. Note also that freeing allocated memory for Charstar is the responsibility of the user, as there is not enough information to handle this automatically (as for Charstring). For more information about how to free memory, see the Ref generator below.
Voidstarstar
The Voidstarstar type has all the properties of the Ref generator (see below). This means that *>, &, +, and - can be used and that the following literal and operators are defined:
LITERALSNull,Alloc;OPERATORSvstarstar2vstar : Voidstarstar -> Voidstar;vstar2vstarstar : Voidstar -> Voidstarstar;Carray
The generator Carray has the following parameters:
GENERATOR Carray (CONSTANT Length, TYPE Itemsort)where Length is an integer giving the number of elements of the array (index from 0 to Length-1), and Itemsort gives the type of each element in the array. A Carray in SDL is translated to an array in C. Indexing a Carray variable in SDL follows the same rules as for ordinary SDL Arrays.
Ref
The generator Ref has the following definition:
GENERATOR Ref (TYPE Itemsort)LITERALSNull,Alloc;OPERATORS"*>" : Ref, Itemsort -> Ref;"*>" : Ref -> Itemsort;"&" : Itemsort -> Ref;make! : Itemsort -> Ref;free : in/out Ref;"+" : Ref, Integer -> Ref;"-" : Ref, Integer -> Ref;Ref2VStar : Ref -> Voidstar;VStar2Ref : Voidstar -> Ref;Ref2VStarStar : Ref /*#REF*/ -> Voidstarstar;DEFAULT Null;ENDGENERATOR Ref;procedure Free; fpar p Voidstarstar;external;Instantiating the Ref generator creates a pointer type on the type given as generator parameter. The literals and operators have the following behavior:
- Null: The NULL value (= 0) for the pointer type. This is also the default value for a pointer variable.
- Alloc: An operator without parameters that returns a new allocated data area with the size of the Itemsort.
- *>: This is the extract! and modify! operator and can be used to reference the value referenced by a pointer. If p is a pointer type, p*> is the value the pointer refers to. Comparing with C, p*> is the same as *p. If p is a pointer to a struct, then p*>!a is the same as (*p).a.
- &: The & operator corresponds to the C operator with the same name. It can be used to take the address of a variable. Comparing with C, &p and &(p) in SDL is the same as &p in C.
- make!: The make! operator, which as usual in SDL has the syntax (. .), is a short hand for creating memory and initializing it to a given value. The statement:
a := (. 2 .);
has the same meaning as
a := Alloc, a*> := 2;- free: The Free operator is used to deallocate memory referenced by a Ref pointer. If the component type contains automatically handled pointers (Charstring, Octet_string, Bit_string, Bags, Own pointers, and so on) the memory for these components is also deallocated.
- +, -: These operator have the meaning of pointer arithmetics in exactly the same way as in C. For example, p+1 (if p is of a pointer type) will add the Itemsort size to the value p. The + and - operators are mostly used to step through an array in C.
- ref2vstar, vstar2ref, ref2vstarstar: These operators are conversion operators, that can be used to cast between pointers and void * and void **.
- procedure Free: NOTE: Old feature provided for backward compatibility. Use operator free above instead.
- This external procedure is closely connected to the Ref generator. It should be used to deallocate memory allocated by the Alloc operator. Free should be passed the pointer variable that references the data area to be released. The variable should be casted to Voidstarstar. After calling Free the pointer variable will have the value Null.
- Example: Free(Ref2VStarStar(variable_name))
- Apart from the difficult syntax for calling the Free procedure it has another problem, it does not free components inside the referenced data area as the free operator above does.
The SDL Analyzer can allow implicit type conversion of pointer data types created by the Ref generator; see Implicit Type Conversions.
http://www.ibm.com/rational |
![]() |
![]() |
![]() |
![]() |