Preserve.3
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:5k
- '"
- '" Copyright (c) 1990 The Regents of the University of California.
- '" Copyright (c) 1994-1996 Sun Microsystems, Inc.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: Preserve.3,v 1.4 2002/02/26 02:22:20 hobbs Exp $
- '"
- .so man.macros
- .TH Tcl_Preserve 3 7.5 Tcl "Tcl Library Procedures"
- .BS
- .SH NAME
- Tcl_Preserve, Tcl_Release, Tcl_EventuallyFree - avoid freeing storage while it's being used
- .SH SYNOPSIS
- .nf
- fB#include <tcl.h>fR
- .sp
- fBTcl_PreservefR(fIclientDatafR)
- .sp
- fBTcl_ReleasefR(fIclientDatafR)
- .sp
- fBTcl_EventuallyFreefR(fIclientData, freeProcfR)
- .SH ARGUMENTS
- .AS Tcl_FreeProc clientData
- .AP ClientData clientData in
- Token describing structure to be freed or reallocated. Usually a pointer
- to memory for structure.
- .AP Tcl_FreeProc *freeProc in
- Procedure to invoke to free fIclientDatafR.
- .BE
- .SH DESCRIPTION
- .PP
- These three procedures help implement a simple reference count mechanism
- for managing storage. They are designed to solve a problem
- having to do with widget deletion, but are also useful in many other
- situations. When a widget is deleted, its
- 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
- fBTcl_PreservefR and fBTcl_ReleasefR
- implement short-term reference counts for their fIclientDatafR
- argument.
- The fIclientDatafR argument identifies an object and usually
- consists of the address of a structure.
- The reference counts guarantee that an object will not be freed
- until each call to fBTcl_PreservefR for the object has been
- matched by calls to fBTcl_ReleasefR.
- There may be any number of unmatched fBTcl_PreservefR calls
- in effect at once.
- .PP
- fBTcl_EventuallyFreefR is invoked to free up its fIclientDatafR
- argument.
- It checks to see if there are unmatched fBTcl_PreservefR calls
- for the object.
- If not, then fBTcl_EventuallyFreefR calls fIfreeProcfR immediately.
- Otherwise fBTcl_EventuallyFreefR records the fact that fIclientDatafR
- needs eventually to be freed.
- When all calls to fBTcl_PreservefR have been matched with
- calls to fBTcl_ReleasefR then fIfreeProcfR will be called by
- fBTcl_ReleasefR to do the cleanup.
- .PP
- All the work of freeing the object is carried out by fIfreeProcfR.
- fIFreeProcfR must have arguments and result that match the
- type fBTcl_FreeProcfR:
- .CS
- typedef void Tcl_FreeProc(char *fIblockPtrfR);
- .CE
- The fIblockPtrfR argument to fIfreeProcfR will be the
- same as the fIclientDatafR argument to fBTcl_EventuallyFreefR.
- The type of fIblockPtrfR (fBchar *fR) is different than the type of the
- fIclientDatafR argument to fBTcl_EventuallyFreefR for historical
- reasons, but the value is the same.
- .PP
- When the fIclientDatafR argument to fBTcl_EventuallyFreefR
- refers to storage allocated and returned by a prior call to
- fBTcl_AllocfR, fBckallocfR, or another function of the Tcl library,
- then the fIfreeProcfR argument should be given the special value of
- fBTCL_DYNAMICfR.
- .PP
- This mechanism can be used to solve the problem described above
- by placing fBTcl_PreservefR and fBTcl_ReleasefR calls around
- 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 "SEE ALSO"
- Tcl_Interp, Tcl_Alloc
- .SH KEYWORDS
- free, reference count, storage