archie/tcl7.6/doc/Preserve.3

104 lines
4.2 KiB
Groff
Raw Normal View History

2024-05-27 16:13:40 +02:00
'\"
'\" Copyright (c) 1990 The Regents of the University of California.
2024-05-27 16:40:40 +02:00
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
2024-05-27 16:13:40 +02:00
'\"
2024-05-27 16:40:40 +02:00
'\" See the file "license.terms" for information on usage and redistribution
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
2024-05-27 16:13:40 +02:00
'\"
2024-05-27 16:40:40 +02:00
'\" SCCS: @(#) Preserve.3 1.13 96/05/28 09:26:12
2024-05-27 16:13:40 +02:00
'\"
.so man.macros
2024-05-27 16:40:40 +02:00
.TH Tcl_Preserve 3 7.5 Tcl "Tcl Library Procedures"
2024-05-27 16:13:40 +02:00
.BS
.SH NAME
2024-05-27 16:40:40 +02:00
Tcl_Preserve, Tcl_Release, Tcl_EventuallyFree \- avoid freeing storage while it's being used
2024-05-27 16:13:40 +02:00
.SH SYNOPSIS
.nf
2024-05-27 16:40:40 +02:00
\fB#include <tcl.h>\fR
2024-05-27 16:13:40 +02:00
.sp
2024-05-27 16:40:40 +02:00
\fBTcl_Preserve\fR(\fIclientData\fR)
2024-05-27 16:13:40 +02:00
.sp
2024-05-27 16:40:40 +02:00
\fBTcl_Release\fR(\fIclientData\fR)
2024-05-27 16:13:40 +02:00
.sp
2024-05-27 16:40:40 +02:00
\fBTcl_EventuallyFree\fR(\fIclientData, freeProc\fR)
2024-05-27 16:13:40 +02:00
.SH ARGUMENTS
2024-05-27 16:40:40 +02:00
.AS Tcl_FreeProc clientData
2024-05-27 16:13:40 +02:00
.AP ClientData clientData in
Token describing structure to be freed or reallocated. Usually a pointer
to memory for structure.
2024-05-27 16:40:40 +02:00
.AP Tcl_FreeProc *freeProc in
2024-05-27 16:13:40 +02:00
Procedure to invoke to free \fIclientData\fR.
.BE
.SH DESCRIPTION
.PP
These three procedures help implement a simple reference count mechanism
for managing storage. They are designed to solve a problem
2024-05-27 16:40:40 +02:00
having to do with widget deletion, but are also useful in many other
situations. When a widget is deleted, its
2024-05-27 16:13:40 +02:00
widget record (the structure holding information specific to the
widget) must be returned to the storage allocator.
However, it's possible that the widget record is in active use
by one of the procedures on the stack at the time of the deletion.
This can happen, for example, if the command associated with a button
widget causes the button to be destroyed: an X event causes an
event-handling C procedure in the button to be invoked, which in
turn causes the button's associated Tcl command to be executed,
which in turn causes the button to be deleted, which in turn causes
the button's widget record to be de-allocated.
Unfortunately, when the Tcl command returns, the button's
event-handling procedure will need to reference the
button's widget record.
Because of this, the widget record must not be freed as part of the
deletion, but must be retained until the event-handling procedure has
finished with it.
In other situations where the widget is deleted, it may be possible
to free the widget record immediately.
.PP
2024-05-27 16:40:40 +02:00
\fBTcl_Preserve\fR and \fBTcl_Release\fR
2024-05-27 16:13:40 +02:00
implement short-term reference counts for their \fIclientData\fR
argument.
The \fIclientData\fR argument identifies an object and usually
consists of the address of a structure.
The reference counts guarantee that an object will not be freed
2024-05-27 16:40:40 +02:00
until each call to \fBTcl_Preserve\fR for the object has been
matched by calls to \fBTcl_Release\fR.
There may be any number of unmatched \fBTcl_Preserve\fR calls
2024-05-27 16:13:40 +02:00
in effect at once.
.PP
2024-05-27 16:40:40 +02:00
\fBTcl_EventuallyFree\fR is invoked to free up its \fIclientData\fR
2024-05-27 16:13:40 +02:00
argument.
2024-05-27 16:40:40 +02:00
It checks to see if there are unmatched \fBTcl_Preserve\fR calls
2024-05-27 16:13:40 +02:00
for the object.
2024-05-27 16:40:40 +02:00
If not, then \fBTcl_EventuallyFree\fR calls \fIfreeProc\fR immediately.
Otherwise \fBTcl_EventuallyFree\fR records the fact that \fIclientData\fR
2024-05-27 16:13:40 +02:00
needs eventually to be freed.
2024-05-27 16:40:40 +02:00
When all calls to \fBTcl_Preserve\fR have been matched with
calls to \fBTcl_Release\fR then \fIfreeProc\fR will be called by
\fBTcl_Release\fR to do the cleanup.
2024-05-27 16:13:40 +02:00
.PP
All the work of freeing the object is carried out by \fIfreeProc\fR.
\fIFreeProc\fR must have arguments and result that match the
2024-05-27 16:40:40 +02:00
type \fBTcl_FreeProc\fR:
.CS
typedef void Tcl_FreeProc(char *\fIblockPtr\fR);
.CE
The \fIblockPtr\fR argument to \fIfreeProc\fR will be the
same as the \fIclientData\fR argument to \fBTcl_EventuallyFree\fR.
The type of \fIblockPtr\fR (\fBchar *\fR) is different than the type of the
\fIclientData\fR argument to \fBTcl_EventuallyFree\fR for historical
reasons, but the value is the same.
2024-05-27 16:13:40 +02:00
.PP
This mechanism can be used to solve the problem described above
2024-05-27 16:40:40 +02:00
by placing \fBTcl_Preserve\fR and \fBTcl_Release\fR calls around
2024-05-27 16:13:40 +02:00
actions that may cause undesired storage re-allocation. The
mechanism is intended only for short-term use (i.e. while procedures
are pending on the stack); it will not work efficiently as a
mechanism for long-term reference counts.
The implementation does not depend in any way on the internal
structure of the objects being freed; it keeps the reference
counts in a separate structure.
.SH KEYWORDS
free, reference count, storage