133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
-
+
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
-
+
+
+
+
|
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 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.
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 the internal List type. To make this possible, the custom type must implement a set of accessor functions corresponding to list operations.
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).
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).
### Memory management of list elements returned by Tcl_ListObjIndex
## Tcl Lists
When dealing with list elements, a common micro-optimization practice involves
skipping `Tcl_IncrRefCount` and `Tcl_DecrRefCount` sequences for a `Tcl_Obj`
element returned by `Tcl_ListObjIndex` when the use is local and temporary. In
Tcl 8, this posed no issues because the list implementation internally held a
reference to each element, ensuring that the element would be freed (assuming no
other references) along with the parent list.
However, with the introduction of Abstract Lists in Tcl 9, this guarantee no
longer holds. Abstract lists may or may not retain a reference to `Tcl_Obj`
elements returned from `Tcl_ListObjIndex`. Specifically, it is possible for
elements to be created on demand and returned by the index operation without an
internal reference being held. In the absence of a `Tcl_DecrRefCount` call
on the element, the `Tcl_Obj` would never be freed resulting in a memory leak.
The caller needs to explicitly call `Tcl_DecrRefCount` on the element
`Tcl_Obj` to ensure it is freed.
On the flip side, an abstract list implementation may mimic Tcl 8 behavior by
returning an existing internal reference to a `Tcl_Obj` without incrementing its
reference count. In such instances, calling `Tcl_DecrRefCount` on the element
without a corresponding `Tcl_IncrRefCount` would release an internal reference
held by the abstract list implementation and prematurely free the element resulting
in use-after-free bugs.
To handle both types of implementations correctly, the caller should always use
the `Tcl_BounceRefCount` function on the returned element `Tcl_Obj` when
finished with it. This function will free the passed `Tcl_Obj` if, and only if, there are
### Tcl_Obj reference counting
no existing references to it.
If the extension needs to maintain compatibility with Tcl 8.6, an alternative
approach is to perform a pair of `Tcl_IncrRefCount` and `Tcl_DecrRefCount` calls
on the element. While this achieves the same effect as the `Tcl_BounceRefCount`
function, it comes at the cost of a slight efficiency trade-off.
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.
### Prefer Tcl_ListObjIndex to Tcl_ListObjGetElements
TODO
## Tommath
Tcl 9 optionally allows the linking of an external `libtommath` library for multiple-precision
integer arithmetic in lieu of Tcl's internal version. This has some consequences for extensions:
* If including `tclTomMath.h`, Tcl's stubs interface to `libtommath` is used and only a subset of
|