Artifact 2c4f5cd71b82a1d090c14ba5d13eca557773695be27db884d548bf923654623c:
- File typeparse/noyau/tip759-memoire.patch — part of check-in [40e39579b9] at 2026-10-23 16:08:45 on branch tip-764_tcl90 — The three points are complete: this is version v15p, found in `typeparse.zip`. Along the way, `valgrind` uncovered six memory errors, all of which have been fixed. 1. `tcl::unsupported::abi` provides the byte order, pointer size, and the size and alignment—within a structure—for `i8` through `i64`, `f32`, `f64`, and `ptr` types. These values are determined at compile time. 2. The compact (prototype) form is enabled via `tcl::unsupported::compact on`. For testing purposes, setting the environment variable `TCL_TYPES_COMPACT=1` achieves the same result. The state of a value can be inspected using `... info $P`, and the layout of a type can be viewed using `... layout T`. Four key design decisions have been implemented: use of a canonical form only, support for `int(s,n)`, `int(u,n)`, and `double` types, natural alignment, and the inclusion of an `unsupported` command. From the script perspective, nothing changes; all tests and examples produce identical output regardless of whether the compact form is enabled. The new test `t70_compact` verifies this on a case-by-case basis. Reading: `lindex` and `foreach` provide a view into a memory slot without copying the element. The syntax `$P(i.x)` reads the value directly from the slot. Writing: `set P(i.x)`, `lset`, and `lappend` perform writes directly into the slots. A copy—or a previously read view—retains its original value. C-side: `typeparse/tclCompact.h` provides access to these memory slots. `geo.c` now reads points in-place. standard form compact form memory, 200,000 points 264 bytes/point 16 bytes/point 10,000-point array in C (object-based approach) 564 µs 29 µs `lappend` of 20,000 points 8.7 s 0.25 s array in Tcl (using `lindex` followed by `$a(x)`) 15 ms 23 ms The last row represents the trade-off: when accessed element-by-element from Tcl, each access creates a view and a number. The gains are in memory usage, at the C interface, and during modifications. The `lappend` figure for the standard form is a separate issue: each addition copies the entire typed list. I have added this to the roadmap. 3. type c T … generates a C header containing: `struct`, `enum`, or `union` definitions for sum types; a flexible array member as the final field for variable-length lists; an `_INIT` macro for default values; and `_Static_assert` checks for `sizeof` and `offsetof`. I have verified this using `gcc`. When compiled in 32-bit mode (`-m32`), a structure like `{int(s,32); double}` causes the assertions to fail, as expected. `geo.c` now includes `geo_types.h`, which is generated by running `tclsh geo_types.tcl > geo_types.h`. Memory errors: I identified these using `Valgrind` on a `tclsh` build configured with `-DPURIFY`, which ensures that Tcl objects are allocated via `malloc`. Most of these were already present in v15: The most serious issue: after executing `set e [lindex $P 0]`, attempting to read `$P(0.x)` would cause a crash; furthermore, executing `set P(0.x) 7.5` would overwrite the entire point structure with the value `7.5`. In my v15m version: the error message "fits variants … equally" would inadvertently free the value it was referencing. Rejected declarations: when a declaration failed (e.g., `x is T = v`, `set {T}x`), the code would attempt to set a flag on a variable that had already been destroyed. Memory leak in `type binary decode`: fields that had already been read would leak when the C codec handed control back to the Tcl layer. Regarding the code in your TIP 759 branch: `ParseExpr` would read `nestedPtr->term` after it had already been freed; additionally, every evaluated word `(...)` would leak the result returned by `Tcl_ExprObj`. The fix is included in the `tip759-memoire.patch` file. I have added it to `ORDRE.txt`, but since this is your code, you will need to merge it into your branch. Feel free to remove it from `ORDRE.txt` if you would prefer to handle the fix yourself. Following corrections, Valgrind reports no issues for any tests or examples in either format. The only exception—which is inconsequential—involves a few objects that remain unreleased when a script terminates via an `exit` command mid-execution (in `executable_pe.tcl`). Compared to v15: Project: The only new differences are expected ones—the error message for type-related subcommands now references `c`, and there are new tests: `t69_abi`, `t70_compact`, and `t71_type_c`. All `.out` outputs pass. Full Tcl test suite: 61 failures out of 51,090—exactly the same as in v15. Everything is documented: `doc/ANNOTATIONS.fr.md`: § 12.1 (compact format) and § 12.2 (type `c`), with § 17 also updated; `noyau/LISEZMOI.md`: `v15p` section; `exemples/geometrie/LISEZMOI.md`. Regarding the TIP 764 roadmap, all three items are checked off; I have also added the following: pointer-based cases, direct reading via compiled `expr`, stub functions, and `lappend`. The two TIP 759 fixes are noted in their own dedicated roadmap. (user: Florentis, size: 1649) [annotate] [blame] [check-ins using]
--- a/generic/tclCompExpr.c 2026-10-23 15:42:17.728865035 +0200 +++ b/generic/tclCompExpr.c 2026-10-23 15:42:17.735667167 +0200 @@ -1334,15 +1334,21 @@ } } } - TclStackFree(interp, nestedPtr); - if (lexeme == COMMAND) { - // TIP 759b : if we came back on an operator, we want to parse it again - if (CHAR_TYPE(nestedPtr->term[0]) == TYPE_OP - || CHAR_TYPE(nestedPtr->term[0]) == TYPE_CLOSE_PAREN - || nestedPtr->term[0] == ';') { - end = start-1; + { + /* nestedPtr is on the stack: read term before freeing it */ + const char *nestedTerm = nestedPtr->term; + + TclStackFree(interp, nestedPtr); + if (lexeme == COMMAND) { + // TIP 759b : if we came back on an operator, we want to parse it again + if (CHAR_TYPE(nestedTerm[0]) == TYPE_OP + || CHAR_TYPE(nestedTerm[0]) == TYPE_CLOSE_PAREN + || nestedTerm[0] == ';') { + end = start-1; + } } - } else if (lexeme == SCRIPT) { + } + if (lexeme == SCRIPT) { end = start; } start = tokenPtr->start; --- a/generic/tclParse.c 2026-10-23 15:42:17.731665395 +0200 +++ b/generic/tclParse.c 2026-10-23 15:42:17.735730427 +0200 @@ -2661,6 +2661,13 @@ Tcl_IncrRefCount(expressionObj); code = Tcl_ExprObj(interp, expressionObj, &appendObj); Tcl_DecrRefCount(expressionObj); + if (code == TCL_OK) { + /* Tcl_ExprObj gives a reference to the caller; below, + * appendObj is borrowed (as for a command, from the result) */ + Tcl_SetObjResult(interp, appendObj); + Tcl_DecrRefCount(appendObj); + appendObj = Tcl_GetObjResult(interp); + } break; } case TCL_TOKEN_COMMAND: {