Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/Preserve.3 @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 4.5 KB
Line 
1'\"
2'\" Copyright (c) 1990 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\"
5'\" See the file "license.terms" for information on usage and redistribution
6'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7'\"
8'\" RCS: @(#) $Id: Preserve.3,v 1.6 2007/12/13 15:22:31 dgp Exp $
9'\"
10.so man.macros
11.TH Tcl_Preserve 3 7.5 Tcl "Tcl Library Procedures"
12.BS
13.SH NAME
14Tcl_Preserve, Tcl_Release, Tcl_EventuallyFree \- avoid freeing storage while it is being used
15.SH SYNOPSIS
16.nf
17\fB#include <tcl.h>\fR
18.sp
19\fBTcl_Preserve\fR(\fIclientData\fR)
20.sp
21\fBTcl_Release\fR(\fIclientData\fR)
22.sp
23\fBTcl_EventuallyFree\fR(\fIclientData, freeProc\fR)
24.SH ARGUMENTS
25.AS Tcl_FreeProc clientData
26.AP ClientData clientData in
27Token describing structure to be freed or reallocated.  Usually a pointer
28to memory for structure.
29.AP Tcl_FreeProc *freeProc in
30Procedure to invoke to free \fIclientData\fR.
31.BE
32
33.SH DESCRIPTION
34.PP
35These three procedures help implement a simple reference count mechanism
36for managing storage.  They are designed to solve a problem
37having to do with widget deletion, but are also useful in many other
38situations.  When a widget is deleted, its
39widget record (the structure holding information specific to the
40widget) must be returned to the storage allocator.
41However, it is possible that the widget record is in active use
42by one of the procedures on the stack at the time of the deletion.
43This can happen, for example, if the command associated with a button
44widget causes the button to be destroyed:  an X event causes an
45event-handling C procedure in the button to be invoked, which in
46turn causes the button's associated Tcl command to be executed,
47which in turn causes the button to be deleted, which in turn causes
48the button's widget record to be de-allocated.
49Unfortunately, when the Tcl command returns, the button's
50event-handling procedure will need to reference the
51button's widget record.
52Because of this, the widget record must not be freed as part of the
53deletion, but must be retained until the event-handling procedure has
54finished with it.
55In other situations where the widget is deleted, it may be possible
56to free the widget record immediately.
57.PP
58\fBTcl_Preserve\fR and \fBTcl_Release\fR
59implement short-term reference counts for their \fIclientData\fR
60argument.
61The \fIclientData\fR argument identifies an object and usually
62consists of the address of a structure.
63The reference counts guarantee that an object will not be freed
64until each call to \fBTcl_Preserve\fR for the object has been
65matched by calls to \fBTcl_Release\fR.
66There may be any number of unmatched \fBTcl_Preserve\fR calls
67in effect at once.
68.PP
69\fBTcl_EventuallyFree\fR is invoked to free up its \fIclientData\fR
70argument.
71It checks to see if there are unmatched \fBTcl_Preserve\fR calls
72for the object.
73If not, then \fBTcl_EventuallyFree\fR calls \fIfreeProc\fR immediately.
74Otherwise \fBTcl_EventuallyFree\fR records the fact that \fIclientData\fR
75needs eventually to be freed.
76When all calls to \fBTcl_Preserve\fR have been matched with
77calls to \fBTcl_Release\fR then \fIfreeProc\fR will be called by
78\fBTcl_Release\fR to do the cleanup.
79.PP
80All the work of freeing the object is carried out by \fIfreeProc\fR.
81\fIFreeProc\fR must have arguments and result that match the
82type \fBTcl_FreeProc\fR:
83.CS
84typedef void Tcl_FreeProc(char *\fIblockPtr\fR);
85.CE
86The \fIblockPtr\fR argument to \fIfreeProc\fR will be the
87same as the \fIclientData\fR argument to \fBTcl_EventuallyFree\fR.
88The type of \fIblockPtr\fR (\fBchar *\fR) is different than the type of the
89\fIclientData\fR argument to \fBTcl_EventuallyFree\fR for historical
90reasons, but the value is the same.
91.PP
92When the \fIclientData\fR argument to \fBTcl_EventuallyFree\fR
93refers to storage allocated and returned by a prior call to
94\fBTcl_Alloc\fR, \fBckalloc\fR, or another function of the Tcl library,
95then the \fIfreeProc\fR argument should be given the special value of
96\fBTCL_DYNAMIC\fR.
97.PP
98This mechanism can be used to solve the problem described above
99by placing \fBTcl_Preserve\fR and \fBTcl_Release\fR calls around
100actions that may cause undesired storage re-allocation.  The
101mechanism is intended only for short-term use (i.e. while procedures
102are pending on the stack);  it will not work efficiently as a
103mechanism for long-term reference counts.
104The implementation does not depend in any way on the internal
105structure of the objects being freed;  it keeps the reference
106counts in a separate structure.
107
108.SH "SEE ALSO"
109Tcl_Interp, Tcl_Alloc
110
111.SH KEYWORDS
112free, reference count, storage
Note: See TracBrowser for help on using the repository browser.