1Tcl_Preserve(3)             Tcl Library Procedures             Tcl_Preserve(3)
2
3
4
5______________________________________________________________________________
6

NAME

8       Tcl_Preserve,  Tcl_Release,  Tcl_EventuallyFree - avoid freeing storage
9       while it is being used
10

SYNOPSIS

12       #include <tcl.h>
13
14       Tcl_Preserve(clientData)
15
16       Tcl_Release(clientData)
17
18       Tcl_EventuallyFree(clientData, freeProc)
19

ARGUMENTS

21       ClientData clientData (in)            Token describing structure to  be
22                                             freed  or reallocated.  Usually a
23                                             pointer to memory for structure.
24
25       Tcl_FreeProc *freeProc (in)           Procedure  to  invoke   to   free
26                                             clientData.
27_________________________________________________________________
28
29

DESCRIPTION

31       These  three  procedures help implement a simple reference count mecha‐
32       nism for managing storage.  They are designed to solve a problem having
33       to  do  with  widget deletion, but are also useful in many other situa‐
34       tions.  When a widget is deleted,  its  widget  record  (the  structure
35       holding  information  specific  to  the widget) must be returned to the
36       storage allocator.  However, it is possible that the widget  record  is
37       in  active use by one of the procedures on the stack at the time of the
38       deletion.  This can happen, for example, if the command associated with
39       a  button  widget causes the button to be destroyed:  an X event causes
40       an event-handling C procedure in the button to  be  invoked,  which  in
41       turn  causes  the button's associated Tcl command to be executed, which
42       in turn causes the button to be deleted, which in turn causes the  but‐
43       ton's  widget  record  to be de-allocated.  Unfortunately, when the Tcl
44       command returns, the button's event-handling  procedure  will  need  to
45       reference  the  button's  widget  record.   Because of this, the widget
46       record must not be freed as part of the deletion, but must be  retained
47       until the event-handling procedure has finished with it.  In other sit‐
48       uations where the widget is deleted, it may be  possible  to  free  the
49       widget record immediately.
50
51       Tcl_Preserve  and Tcl_Release implement short-term reference counts for
52       their clientData  argument.   The  clientData  argument  identifies  an
53       object  and usually consists of the address of a structure.  The refer‐
54       ence counts guarantee that an object will not be freed until each  call
55       to   Tcl_Preserve   for  the  object  has  been  matched  by  calls  to
56       Tcl_Release.  There may be any number of unmatched  Tcl_Preserve  calls
57       in effect at once.
58
59       Tcl_EventuallyFree  is  invoked to free up its clientData argument.  It
60       checks to see if there are unmatched Tcl_Preserve calls for the object.
61       If  not, then Tcl_EventuallyFree calls freeProc immediately.  Otherwise
62       Tcl_EventuallyFree records the fact that clientData needs eventually to
63       be  freed.  When all calls to Tcl_Preserve have been matched with calls
64       to Tcl_Release then freeProc will be called by Tcl_Release  to  do  the
65       cleanup.
66
67       All  the work of freeing the object is carried out by freeProc.  FreeP‐
68       roc must have arguments and result that match the type Tcl_FreeProc:
69              typedef void Tcl_FreeProc(char *blockPtr);
70       The blockPtr argument to freeProc will be the same  as  the  clientData
71       argument  to Tcl_EventuallyFree.  The type of blockPtr (char *) is dif‐
72       ferent than the type of the clientData argument  to  Tcl_EventuallyFree
73       for historical reasons, but the value is the same.
74
75       When  the  clientData  argument to Tcl_EventuallyFree refers to storage
76       allocated and returned by  a  prior  call  to  Tcl_Alloc,  ckalloc,  or
77       another  function of the Tcl library, then the freeProc argument should
78       be given the special value of TCL_DYNAMIC.
79
80       This mechanism can be used to solve  the  problem  described  above  by
81       placing  Tcl_Preserve  and  Tcl_Release  calls  around actions that may
82       cause undesired storage re-allocation.  The mechanism is intended  only
83       for  short-term  use  (i.e. while procedures are pending on the stack);
84       it will not work efficiently as a  mechanism  for  long-term  reference
85       counts.   The implementation does not depend in any way on the internal
86       structure of the objects being freed;  it keeps the reference counts in
87       a separate structure.
88
89

SEE ALSO

91       Tcl_Interp, Tcl_Alloc
92
93

KEYWORDS

95       free, reference count, storage
96
97
98
99Tcl                                   7.5                      Tcl_Preserve(3)
Impressum