Updated 2017-03-07 09:56:23 by rz

TIP content edit

Abstract

This TIP proposes adding a command, Tcl_LinkArray, to allow linking of C variables to Tcl lists. It also adds more types of linked variable.

Rationale

The current C-API allows one to only link single variables. The proposed Tcl_LinkArray function allows the linking of C-arrays to Tcl list variables. If the given size value is 1 then it works like the existing Tcl_LinkVar function. The automatic creation of needed space when no address value is provided could be used for testing scenarios.

The new link types also allow one to link from strings and binary arrays to fixed memory locations.

The use of arrays with a given size > 1 allows a safe and simple C-Tcl connection. Array overwrites create errors.

Specification

This document proposes the following changes to the Tcl core:

New C API

Add a new public Tcl_LinkArray function to provide links of single variable and array variables. The new function would have the same parameters as the Tcl_LinkVar function, plus an additional int size. If the given size is 1, then we have the same functionality as before. With size > 1, the linked Tcl variable is a list variable. If the given address is NULL then the function allocates the necessary space on the C side itself.
    `int Tcl_LinkArray(Tcl_Interp *interp, const char *varName, char *addr, int type, int size)`

The Tcl_LinkVar function will remain for compatibility. The function will call the new function with size=1.

The following link types will be defined in tcl.h:
#define TCL_LINK_INT                1        /* 32bit int -> int */
#define TCL_LINK_DOUBLE                2        /* 64bit double -> double */
#define TCL_LINK_BOOLEAN        3        /* tcl boolean -> int */
#define TCL_LINK_STRING                4        /* 8bit chars -> ckalloc'd string */
#define TCL_LINK_WIDE_INT        5        /* 64bit int -> Tcl_WideInt */
#define TCL_LINK_CHAR                6        /* 8bit int -> char */
#define TCL_LINK_UCHAR                7        /* 8bit uint -> unsigned char */
#define TCL_LINK_SHORT                8        /* 16bit int -> short */
#define TCL_LINK_USHORT                9        /* 16bit int -> unsigned short */
#define TCL_LINK_UINT                10        /* 32bit uint -> int */
#define TCL_LINK_LONG                11        /* long -> long */
#define TCL_LINK_ULONG                12        /* unsigned long -> unsigned long */
#define TCL_LINK_FLOAT                13        /* 32bit float -> double */
#define TCL_LINK_WIDE_UINT        14        /* 64bit uint -> wide TODO bignum */
#define TCL_LINK_CHARS                15      /* 8bit chars -> null terminated string
                                           last char will always set to \0 */
#define TCL_LINK_COMPLEX32        16      /* 32bit complex -> double+double */
#define TCL_LINK_COMPLEX64        17      /* 64bit complex -> double+double */
#define TCL_LINK_BINARY                18      /* fixed size binary byte array */
#define TCL_LINK_HEX8                19      /* 8bit uint -> string, 2 hex chars */
#define TCL_LINK_HEX16                20      /* 16bit uint -> string, 4 hex chars */
#define TCL_LINK_HEX32                21      /* 32bit uint -> string, 8 hex chars */
#define TCL_LINK_HEX64                22      /* 64bit uint -> string, 16 hex chars */
#define TCL_LINK_BITARRAY8        23      /* 8bit uint -> string, 8 chars 0|1 */
#define TCL_LINK_BITARRAY16        24      /* 16bit uint -> string, 16 chars 0|1 */
#define TCL_LINK_BITARRAY32        25      /* 32bit uint -> string, 32 chars 0|1 */
#define TCL_LINK_BITARRAY64        26      /* 64bit uint -> string, 64 chars 0|1 */
#define TCL_LINK_BOOL8                27      /* 8bit uint -> int (0|1) */
#define TCL_LINK_BOOL16                28      /* 16bit uint -> int (0|1) */
#define TCL_LINK_BOOL32                29      /* 32bit uint -> int (0|1) */
#define TCL_LINK_BOOL64                30      /* 64bit uint -> int (0|1) */
#define TCL_LINK_BIT8                31      /* bit in 8bit uint -> int (0|1) */
#define TCL_LINK_BIT16                32      /* bit in 16bit uint -> int (0|1) */
#define TCL_LINK_BIT32                33      /* bit in 32bit uint -> int (0|1) */
#define TCL_LINK_BIT64                34      /* bit in 64bit uint -> int (0|1) */
#define TCL_LINK_S5FLOAT        35      /* Siemens S5 32bit uint -> double */
#define TCL_LINK_S5TIME                36      /* Siemens S5 16bit uint -> double */

TCL_LINK_CHARS

The address of the C variable is used as a char *. The address remains always the same (different to TCL_LINK_STRING) and contains a \0 terminated string. The \0 counts towards the given size. The Tcl variable is used as a string and can contain up to size -1 characters.

TCL_LINK_BINARY

The address of the C variable is used as a unsigned char *. The address remains always the same. Read and write operations must always contain the full sized binary string.

Testing

The tests in the tcl test suite for the Tcl_LinkVar function are working.

Additional tests for the Tcl_LinkArray function are needed. To support this a testlinkarray command is included in the tclTest.c file. The implementing C-function TestlinkarrayCmd can be used as a starting point for extensions who will use the new functionality from tcl. The function is not exposed as normal tcl command because wrong usage can lead to segmenation faults.

Reference Implementation edit

To test the new functionality get 20170307_tip312.tgz from https://sourceforge.net/projects/kbskit/files/

Apply the patch files tcl.h.patch and tclTest.c.patch. And replace tclLink.c with the new one.

To use the new Tcl_LinkArray from outside you need to apply the tcl.decl.patch file and do a
make genstubs

MAN PAGE edit

NAME

Tcl_LinkVar, Tcl_UnlinkVar, Tcl_UpdateLinkedVar — link Tcl variable to C variable

SYNOPSIS

    #include <tcl.h>

    int Tcl_LinkVar(interp, varName, addr, type)
    int Tcl_LinkArray(interp, varName, addr, type, size)
    Tcl_UnlinkVar(interp, varName)
    Tcl_UpdateLinkedVar(interp, varName)

ARGUMENTS

Tcl_Interp *interp (in)

Interpreter that contains varName. Also used by Tcl_LinkVar to return error messages.

const char *varName (in)

Name of global variable.

char *addr (in)

Address of C variable that is to be linked to varName. If *addr is NULL then the needed memory will be allocated and returned as the interpreter result. The memory will be free'd if the variable is unlinked.

int type (in)

Type of C variable. Must be one of TCL_LINK_INT, TCL_LINK_DOUBLE, TCL_LINK_BOOLEAN, TCL_LINK_STRING, TCL_LINK_WIDE_INT, TCL_LINK_CHAR, TCL_LINK_UCHAR, TCL_LINK_SHORT, TCL_LINK_USHORT, TCL_LINK_UINT, TCL_LINK_LONG, TCL_LINK_ULONG, TCL_LINK_FLOAT, TCL_LINK_WIDE_UINT, TCL_LINK_CHARS, TCL_LINK_COMPLEX32, TCL_LINK_COMPLEX64, TCL_LINK_BINARY, TCL_LINK_HEX8, TCL_LINK_HEX16, TCL_LINK_HEX32, TCL_LINK_HEX64, TCL_LINK_BITARRAY8, TCL_LINK_BITARRAY16, TCL_LINK_BITARRAY32, TCL_LINK_BITARRAY64, TCL_LINK_BOOL8, TCL_LINK_BOOL16, TCL_LINK_BOOL32, TCL_LINK_BOOL64, TCL_LINK_BIT8, TCL_LINK_BIT16, TCL_LINK_BIT32, TCL_LINK_BIT64, TCL_LINK_S5FLOAT or TCL_LINK_S5TIME, optionally OR'ed with TCL_LINK_READ_ONLY to make Tcl variable read-only.

int size (in)

If "size" is greater then 1 then link to a tcl list variable.

DESCRIPTION

Tcl_LinkVar and Tcl_LinkArray uses variable traces to keep the Tcl variable named by varName in sync with the C variable at the address given by addr. If size is greater then 1 then the linked tcl variable contain a list of size array values. Whenever the Tcl variable is read the value of the C variable will be returned, and whenever the Tcl variable is written the C variable will be updated to have the same value. Tcl_LinkVar and Tcl_LinkArray normally returns TCL_OK; if an error occurs while setting up the link (e.g. because varName is the name of array) then TCL_ERROR is returned and the interpreter's result contains an error message. The type argument specifies the type of the C variable, and must have one of the following values, optionally OR'ed with TCL_LINK_READ_ONLY:

TCL_LINK_INT

The C variable is of type int. Any value written into the Tcl variable must have a proper integer form acceptable to Tcl_GetIntFromObj; attempts to write non-integer values into varName will be rejected with Tcl errors.

TCL_LINK_UINT

The C variable is of type unsigned int. Any value written into the Tcl variable must have a proper unsigned integer form acceptable to Tcl_GetWideIntFromObj and in the platform's defined range for the unsigned int type; attempts to write non-integer values (or values outside the range) into varName will be rejected with Tcl errors.

TCL_LINK_CHAR

The C variable is of type char. Any value written into the Tcl variable must have a proper integer form acceptable to Tcl_GetIntFromObj and be in the range of the char datatype; attempts to write non-integer or out-of-range values into varName will be rejected with Tcl errors.

TCL_LINK_UCHAR

The C variable is of type unsigned char. Any value written into the Tcl variable must have a proper unsigned integer form acceptable to Tcl_GetIntFromObj and in the platform's defined range for the unsigned char type; attempts to write non-integer values (or values outside the range) into varName will be rejected with Tcl errors.

TCL_LINK_SHORT

The C variable is of type short. Any value written into the Tcl variable must have a proper integer form acceptable to Tcl_GetIntFromObj and be in the range of the short datatype; attempts to write non-integer or out-of-range values into varName will be rejected with Tcl errors.

TCL_LINK_USHORT

The C variable is of type unsigned short. Any value written into the Tcl variable must have a proper unsigned integer form acceptable to Tcl_GetIntFromObj and in the platform's defined range for the unsigned short type; attempts to write non-integer values (or values outside the range) into varName will be rejected with Tcl errors.

TCL_LINK_LONG

The C variable is of type long. Any value written into the Tcl variable must have a proper integer form acceptable to Tcl_GetLongFromObj; attempts to write non-integer or out-of-range values into varName will be rejected with Tcl errors.

TCL_LINK_ULONG

The C variable is of type unsigned long. Any value written into the Tcl variable must have a proper unsigned integer form acceptable to Tcl_GetWideIntFromObj and in the platform's defined range for the unsigned long type; attempts to write non-integer values (or values outside the range) into varName will be rejected with Tcl errors.

TCL_LINK_DOUBLE

The C variable is of type double. Any value written into the Tcl variable must have a proper real form acceptable to Tcl_GetDoubleFromObj; attempts to write non-real values into varName will be rejected with Tcl errors.

TCL_LINK_FLOAT

The C variable is of type float. Any value written into the Tcl variable must have a proper real form acceptable to Tcl_GetDoubleFromObj and must be within the range acceptable for a float; attempts to write non-real values (or values outside the range) into varName will be rejected with Tcl errors.

TCL_LINK_WIDE_INT

The C variable is of type Tcl_WideInt (which is an integer type at least 64-bits wide on all platforms that can support it.) Any value written into the Tcl variable must have a proper integer form acceptable to Tcl_GetWideIntFromObj; attempts to write non-integer values into varName will be rejected with Tcl errors.

TCL_LINK_WIDE_UINT

The C variable is of type Tcl_WideUInt (which is an unsigned integer type at least 64-bits wide on all platforms that can support it.) Any value written into the Tcl variable must have a proper unsigned integer form acceptable to Tcl_GetWideIntFromObj (it will be cast to unsigned); attempts to write non-integer values into varName will be rejected with Tcl errors.

TCL_LINK_BOOLEAN

The C variable is of type int. If its value is zero then it will read from Tcl as “0”; otherwise it will read from Tcl as “1”. Whenever varName is modified, the C variable will be set to a 0 or 1 value. Any value written into the Tcl variable must have a proper boolean form acceptable to Tcl_GetBooleanFromObj; attempts to write non-boolean values into varName will be rejected with Tcl errors.

TCL_LINK_STRING

The C variable is of type char *. If its value is not NULL then it must be a pointer to a string allocated with Tcl_Alloc or ckalloc. Whenever the Tcl variable is modified the current C string will be freed and new memory will be allocated to hold a copy of the variable's new value. If the C variable contains a NULL pointer then the Tcl variable will read as “NULL”.

TCL_LINK_CHARS

The C variable is of type char *. If its value is not NULL then it must be a pointer to a string at least size bytes long. The C variable will contain a null terminated string. The last char will always be \0.

TCL_LINK_COMPLEX32

The C variable is a array of two float values. Any value written into the Tcl variable must be a list of two values having a proper real form acceptable to Tcl_GetDoubleFromObj and must be within the range acceptable for a float; attempts to write non-real values (or values outside the range) into varName will be rejected with Tcl errors.

TCL_LINK_COMPLEX64

The C variable is a array of two double values. Any value written into the Tcl variable must be a list of two values having a proper real form acceptable to Tcl_GetDoubleFromObj; attempts to write non-real values into varName will be rejected with Tcl errors.

TCL_LINK_BINARY

The C variable is a fixed size binary array. Any value written into the Tcl variable will be used with Tcl_NewByteArrayObj.

TCL_LINK_HEX8 TCL_LINK_HEX16 TCL_LINK_HEX32 TCL_LINK_HEX64

The C variable is a unsigned integer of 8,16,32 or 64 bit. Any value written into the Tcl variable must be in the expected range and containing 0-1 and upper or lower case a,b,c,d,e,f.

TCL_LINK_BITARRAY8 TCL_LINK_BITARRAY16 TCL_LINK_BITARRAY32 TCL_LINK_BITARRAY64

The C variable is a unsigned integer of 8,16,32 or 64 bit. Any value written into the Tcl variable must be a list of 0|1 values with 8, 16, 32 or 64 elements.

TCL_LINK_BOOL8 TCL_LINK_BOOL16 TCL_LINK_BOOL32 TCL_LINK_BOOL64

The C variable is of type unsigned integer with 8, 16, 32 or 64 bit. If its value is zero then it will read from Tcl as “0”; otherwise it will read from Tcl as “1”. Whenever varName is modified, the C variable will be set to a 0 or 1 value. Any value written into the Tcl variable must have a proper boolean form acceptable to Tcl_GetBooleanFromObj; attempts to write non-boolean values into varName will be rejected with Tcl errors.

TCL_LINK_BIT8 TCL_LINK_BIT16 TCL_LINK_BIT32 TCL_LINK_BIT64

The C variable is of type unsigned integer with 8, 16, 32 or 64 bit. Any value written into the Tcl variable must be 0|1 and will set the bit at position size-1.

TCL_LINK_S5FLOAT

The C variable is of type unsigned int and represents a 32 bit Siemens S5 float value. Any value written into the Tcl variable must be a proper flaot value.

TCL_LINK_S5TIME

The C variable is of type unsigned short and represents a 16 bit Siemens S5 time value. To convert to and from S5 time format the value in the C variable will be used to detect the time format. It is not possible to write a tcl value on an not already used C variable.

If the TCL_LINK_READ_ONLY flag is present in type then the variable will be read-only from Tcl, so that its value can only be changed by modifying the C variable. Attempts to write the variable from Tcl will be rejected with errors.

Tcl_UnlinkVar removes the link previously set up for the variable given by varName. If there does not exist a link for varName then the procedure has no effect.

Tcl_UpdateLinkedVar may be invoked after the C variable has changed to force the Tcl variable to be updated immediately. In many cases this procedure is not needed, since any attempt to read the Tcl variable will return the latest value of the C variable. However, if a trace has been set on the Tcl variable (such as a Tk widget that wishes to display the value of the variable), the trace will not trigger when the C variable has changed. Tcl_UpdateLinkedVar ensures that any traces on the Tcl variable are invoked.

Note that, as with any call to a Tcl interpreter, Tcl_UpdateLinkedVar must be called from the same thread that created the interpreter. The safest mechanism is to ensure that the C variable is only ever updated from the same thread that created the interpreter (possibly in response to an event posted with Tcl_ThreadQueueEvent), but when it is necessary to update the variable in a separate thread, it is advised that Tcl_AsyncMark be used to indicate to the thread hosting the interpreter that it is ready to run Tcl_UpdateLinkedVar.

REMARKS

It exists a testlinkarray command inside TclTest.c. With this command it is possible to use the functionality of Tcl_LinkArray from the tcl interpreter directly. The command is not exposed in normal tcl interpreters. It can crash the interpreter if links are done with wrong or to small addresses. Extension writers can use this code to provide own commands.

SEE ALSO

Tcl_TraceVar

KEYWORDS

boolean, integer, link, read-only, real, string, trace, variable