Skip to content
Tcl Source Code Changes To Migrating C extensions to Tcl 9
Login

Changes To Migrating C extensions to Tcl 9

Changes to "Migrating C extensions to Tcl 9" between 2023-10-30 21:34:51 and 2023-11-08 17:23:32

1
2
3

4
























5


6



7





























8
9
10
11
12
13
14
15
16
17

18
19

20
21
22
23
24
25

26
27
28
29
30
31


32
1
2

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

29
30
31
32
33
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

73
74

75
76
77
78
79
80

81
82
83
84
85
86
87
88
89
90


-
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+

+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+









-
+

-
+





-
+






+
+

(Work in progress...)

## 64-bit support
## 64-bit data support

On 64-bit platforms, Tcl 9 no longer limits data size (lengths of strings, lists
etc.) and indices to 32-bits. For portability of extensions between 32- and
64-bit platforms, a new typedef `Tcl_Size` has been defined. This maps to a
64-bit or 32-bit signed integer type on the respective platforms. This affects
all public structures and parameter types as appropriate. Variables that hold
lengths or indices that were typed as `int` for Tcl 8 should be changed to be of
type `Tcl_Size`. Care must be taken that related variables, for example loop
iterators, are also modified accordingly.

For example, in the following fragment

```
int len;
int i;
Tcl_ListObjLength(interp, objPtr, &len);
for (i = 0; i < len; ++i) {
   ....
}
```

the declaration of both `i` and `len` should be changed

```
Tcl_Size len;
`Tcl_Size` etc.
Tcl_Size i;
```

When converting sizes and indices to and from string representation,
replace the use of `Tcl_GetIntFromObj` and `Tcl_NewIntObj` with the
`Tcl_Size` analogues `Tcl_GetSizeIntFromObj` and `Tcl_NewSizeIntObj`.
TBD

When checking string limits etc. appropriately for both platform widths,
the constant `TCL_SIZE_MAX` should be used in lieu of `INT_MAX`.

When formatting as a string through a `printf` style varargs function, a size
modifier appropriate for the platform width needs to be used. This is defined
as `TCL_SIZE_MODIFIER`. For example,

```
Tcl_Size len;
Tcl_Obj *objP = Tcl_ObjPrintf("The length is %" TCL_SIZE_MODIFIER "d.", len);
```

If the ability to build against Tcl 8 is desired after making the above
changes require for Tcl 9, add the following fragment to the appropriate
header for the extension.

```
#ifndef TCL_SIZE_MAX
typedef int Tcl_Size;
# define Tcl_GetSizeIntFromObj Tcl_GetIntFromObj
# define Tcl_NewSizeIntObj Tcl_NewIntObj
# define TCL_SIZE_MAX      INT_MAX
# define TCL_SIZE_MODIFIER ""
#endif

```

See [TIP 660](https://core.tcl-lang.org/tips/doc/trunk/tip/660.md)

## Unicode support

32-bit Tcl_UniChar, profiles...

TBD

## `Tcl_ObjType` versioning

The Tcl value system has been extended. The Tcl\_ObjType has additional (optional) fields.  Any Tcl extensions, or embedded integration with Tcl, that has a custom Tcl\_ObjType value defined, should review these changes.  Existing code should compile and execute just fine, unless the particular implementation is using any "creative" programming. In simpler terms, the Tcl\_ObjType structure is larger. It should be initialzed properly without any changes needed, but YMMV.
The Tcl value system has been extended. The `Tcl_ObjType` has additional (optional) fields.  Any Tcl extensions, or embedded integration with Tcl, that has a custom `Tcl_ObjType` value defined, should review these changes.  Existing code should compile and execute just fine, unless the particular implementation is using any "creative" programming. In simpler terms, the `Tcl_ObjType` structure is larger. It should be initialized properly without any changes needed, but YMMV.

Please see the Tcl_RegisterObjType manual page for more details, as well as TIPs #644 and #636.
Please see the `Tcl_RegisterObjType` manual page for more details, as well as TIP's [636](https://core.tcl-lang.org/tips/doc/trunk/tip/636.md) and [644](https://core.tcl-lang.org/tips/doc/trunk/tip/644.md).

## Abstract lists

An abstract list is a custom Tcl Value (custom Tcl_ObjType) that can be treated as a Tcl List at the script level without causing the value to be converted to a List type. To make this possible, the custom type must implment a set of accessor functions that allow the various list commands to access the value "elements" directly. 

See the Tcl_RegisterObjType manual page for more details, as well as TIPs #644 and #636.
See the Tcl_RegisterObjType manual page for more details, as well as TIP's [636](https://core.tcl-lang.org/tips/doc/trunk/tip/636.md) and [644](https://core.tcl-lang.org/tips/doc/trunk/tip/644.md).

## Tcl Lists

### Tcl_Obj reference counting

It is often common practice when indexing a Tcl List, to not include Incr and Decr RefCounts on the element Tcl_Obj when the use is local and temporary. This was because the List already has a reference to the element, and the Incr/Decr sequence would have no material effect. A minor performance gain.  With the introduction of Abstract Lists, it is possible for elements to be created on demand by the Index operation, so the new element Obj may have a refCount of 0. Therefore, existing code that skip RefCounting operations could be at risk of leaking memory.  Such code should include appropriate Incr/Decr calls, or make use of the new Tcl_BounceRefCount() function. This new function will check for a zero refCount and free the Tcl_Obj. 

## Tommath