TIP 668: Consolidate all Tcl_Attempt* functions.

Bounty program for improvements to Tcl and certain Tcl packages.
Author:		Jan Nijtmans <[email protected]>
State:		Draft
Type:		Project
Created:	11-05-2023
Tcl-Version:	9.0
Tcl-Branch:	tip-668


The normal functions Tcl_Alloc(), Tcl_Realloc() (and a few more) result in a Panic when the memory allocation fails. There are alternative functions Tcl_AtteptAlloc()/Tcl_AttemptRealloc() which return a NULL-pointer in this case. The advantage of the Attempt form of those functions is that we no longer have to check before the function-call whether the size parameter isn't too big, we can just make the call and check afterwards whether it was possible or not. This TIP proposes to make this behavior available to the normal Tcl_Alloc()/Tcl_Realloc()


The functions Tcl_Alloc(), Tcl_Realloc() have been removed, and Tcl_AtteptAlloc()/Tcl_AttemptRealloc() renamed to Tcl_Alloc(), Tcl_Realloc(). This means that everything will keep functioning as before. The only visible change is that instead of panicing, those functions might unexpectedly return a NULL-pointer. Most likely, those null-pointers eventually result in a null-pointer-exception, which is just as bad as panicing.

Implementation is in Tcl branch "tip-668".

Eventually all Tcl_Alloc(), Tcl_Realloc() calls should check their result, and handle a clean recovery when allocation fails. Some newer code (like the improved list implementation) already does that by using the Attempt* functions. This change eliminates the need for special Attempt*-functions, it just provides this for all normal functions. This makes Tcl_Alloc()/Tcl_Realloc() behave the same as the standard C malloc(), realloc(), which also return a null-pointer on failure.





This document has been placed in the public domain.