| 1 |
# Copyright (c) 1990 The Regents of the University of California. |
| 2 |
# Copyright (c) 1994 Sun Microsystems, Inc. |
| 3 |
# See the file "license.terms" for information on usage and redistribution |
| 4 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 5 |
# |
| 6 |
# @(#) Preserve.3 1.7 95/05/06 15:30:19 |
| 7 |
# |
| 8 |
|
| 9 |
=head1 NAME |
| 10 |
|
| 11 |
Tk_Preserve, Tk_Release, Tk_EventuallyFree - avoid freeing storage while it's being used |
| 12 |
|
| 13 |
=for category C Programming |
| 14 |
|
| 15 |
=head1 SYNOPSIS |
| 16 |
|
| 17 |
B<#include E<lt>tk.hE<gt>> |
| 18 |
|
| 19 |
B<Tk_Preserve>(I<clientData>) |
| 20 |
|
| 21 |
B<Tk_Release>(I<clientData>) |
| 22 |
|
| 23 |
B<Tk_EventuallyFree>(I<clientData, freeProc>) |
| 24 |
|
| 25 |
=head1 ARGUMENTS |
| 26 |
|
| 27 |
=over 4 |
| 28 |
|
| 29 |
=item ClientData clientData (in) |
| 30 |
|
| 31 |
Token describing structure to be freed or reallocated. Usually a pointer |
| 32 |
to memory for structure. |
| 33 |
|
| 34 |
=item Tk_FreeProc *freeProc (in) |
| 35 |
|
| 36 |
Procedure to invoke to free I<clientData>. |
| 37 |
|
| 38 |
=back |
| 39 |
|
| 40 |
=head1 DESCRIPTION |
| 41 |
|
| 42 |
These three procedures help implement a simple reference count mechanism |
| 43 |
for managing storage. They are designed to solve a problem |
| 44 |
having to do with widget deletion. When a widget is deleted, its |
| 45 |
widget record (the structure holding information specific to the |
| 46 |
widget) must be returned to the storage allocator. |
| 47 |
However, it's possible that the widget record is in active use |
| 48 |
by one of the procedures on the stack at the time of the deletion. |
| 49 |
This can happen, for example, if the command associated with a button |
| 50 |
widget causes the button to be destroyed: an X event causes an |
| 51 |
event-handling C procedure in the button to be invoked, which in |
| 52 |
turn causes the button's associated Tcl command to be executed, |
| 53 |
which in turn causes the button to be deleted, which in turn causes |
| 54 |
the button's widget record to be de-allocated. |
| 55 |
Unfortunately, when the Tcl command returns, the button's |
| 56 |
event-handling procedure will need to reference the |
| 57 |
button's widget record. |
| 58 |
Because of this, the widget record must not be freed as part of the |
| 59 |
deletion, but must be retained until the event-handling procedure has |
| 60 |
finished with it. |
| 61 |
In other situations where the widget is deleted, it may be possible |
| 62 |
to free the widget record immediately. |
| 63 |
|
| 64 |
B<Tk_Preserve> and B<Tk_Release> |
| 65 |
implement short-term reference counts for their I<clientData> |
| 66 |
argument. |
| 67 |
The I<clientData> argument identifies an object and usually |
| 68 |
consists of the address of a structure. |
| 69 |
The reference counts guarantee that an object will not be freed |
| 70 |
until each call to B<Tk_Preserve> for the object has been |
| 71 |
matched by calls to B<Tk_Release>. |
| 72 |
There may be any number of unmatched B<Tk_Preserve> calls |
| 73 |
in effect at once. |
| 74 |
|
| 75 |
B<Tk_EventuallyFree> is invoked to free up its I<clientData> |
| 76 |
argument. |
| 77 |
It checks to see if there are unmatched B<Tk_Preserve> calls |
| 78 |
for the object. |
| 79 |
If not, then B<Tk_EventuallyFree> calls I<freeProc> immediately. |
| 80 |
Otherwise B<Tk_EventuallyFree> records the fact that I<clientData> |
| 81 |
needs eventually to be freed. |
| 82 |
When all calls to B<Tk_Preserve> have been matched with |
| 83 |
calls to B<Tk_Release> then I<freeProc> will be called by |
| 84 |
B<Tk_Release> to do the cleanup. |
| 85 |
|
| 86 |
All the work of freeing the object is carried out by I<freeProc>. |
| 87 |
I<FreeProc> must have arguments and result that match the |
| 88 |
type B<Tk_FreeProc>: |
| 89 |
|
| 90 |
=over 4 |
| 91 |
|
| 92 |
typedef void Tk_FreeProc(ClientData I<clientData>); |
| 93 |
|
| 94 |
=back |
| 95 |
|
| 96 |
The I<clientData> argument to I<freeProc> will be the |
| 97 |
same as the I<clientData> argument to B<Tk_EventuallyFree>. |
| 98 |
|
| 99 |
This mechanism can be used to solve the problem described above |
| 100 |
by placing B<Tk_Preserve> and B<Tk_Release> calls around |
| 101 |
actions that may cause undesired storage re-allocation. The |
| 102 |
mechanism is intended only for short-term use (i.e. while procedures |
| 103 |
are pending on the stack); it will not work efficiently as a |
| 104 |
mechanism for long-term reference counts. |
| 105 |
The implementation does not depend in any way on the internal |
| 106 |
structure of the objects being freed; it keeps the reference |
| 107 |
counts in a separate structure. |
| 108 |
|
| 109 |
=head1 KEYWORDS |
| 110 |
|
| 111 |
free, reference count, storage |