Debian Codegraph @

doc/Preserve.3

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