📄 preserve.3
字号:
'\"'\" 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.'\" '\" SCCS: @(#) Preserve.3 1.13 96/05/28 09:26:12'\" .so man.macros.TH Tcl_Preserve 3 7.5 Tcl "Tcl Library Procedures".BS.SH NAMETcl_Preserve, Tcl_Release, Tcl_EventuallyFree \- avoid freeing storage while it's being used.SH SYNOPSIS.nf\fB#include <tcl.h>\fR.sp\fBTcl_Preserve\fR(\fIclientData\fR).sp\fBTcl_Release\fR(\fIclientData\fR).sp\fBTcl_EventuallyFree\fR(\fIclientData, freeProc\fR).SH ARGUMENTS.AS Tcl_FreeProc clientData.AP ClientData clientData inToken describing structure to be freed or reallocated. Usually a pointerto memory for structure..AP Tcl_FreeProc *freeProc inProcedure to invoke to free \fIclientData\fR..BE.SH DESCRIPTION.PPThese three procedures help implement a simple reference count mechanismfor managing storage. They are designed to solve a problemhaving to do with widget deletion, but are also useful in many othersituations. When a widget is deleted, itswidget record (the structure holding information specific to thewidget) must be returned to the storage allocator.However, it's possible that the widget record is in active useby one of the procedures on the stack at the time of the deletion.This can happen, for example, if the command associated with a buttonwidget causes the button to be destroyed: an X event causes anevent-handling C procedure in the button to be invoked, which inturn causes the button's associated Tcl command to be executed,which in turn causes the button to be deleted, which in turn causesthe button's widget record to be de-allocated.Unfortunately, when the Tcl command returns, the button'sevent-handling procedure will need to reference thebutton's widget record.Because of this, the widget record must not be freed as part of thedeletion, but must be retained until the event-handling procedure hasfinished with it.In other situations where the widget is deleted, it may be possibleto free the widget record immediately..PP\fBTcl_Preserve\fR and \fBTcl_Release\fRimplement short-term reference counts for their \fIclientData\fRargument.The \fIclientData\fR argument identifies an object and usuallyconsists of the address of a structure.The reference counts guarantee that an object will not be freeduntil each call to \fBTcl_Preserve\fR for the object has beenmatched by calls to \fBTcl_Release\fR.There may be any number of unmatched \fBTcl_Preserve\fR callsin effect at once..PP\fBTcl_EventuallyFree\fR is invoked to free up its \fIclientData\fRargument.It checks to see if there are unmatched \fBTcl_Preserve\fR callsfor the object.If not, then \fBTcl_EventuallyFree\fR calls \fIfreeProc\fR immediately.Otherwise \fBTcl_EventuallyFree\fR records the fact that \fIclientData\fRneeds eventually to be freed.When all calls to \fBTcl_Preserve\fR have been matched withcalls to \fBTcl_Release\fR then \fIfreeProc\fR will be called by\fBTcl_Release\fR to do the cleanup..PPAll the work of freeing the object is carried out by \fIfreeProc\fR.\fIFreeProc\fR must have arguments and result that match thetype \fBTcl_FreeProc\fR:.CStypedef void Tcl_FreeProc(char *\fIblockPtr\fR);.CEThe \fIblockPtr\fR argument to \fIfreeProc\fR will be thesame as the \fIclientData\fR argument to \fBTcl_EventuallyFree\fR.The type of \fIblockPtr\fR (\fBchar *\fR) is different than the type of the\fIclientData\fR argument to \fBTcl_EventuallyFree\fR for historicalreasons, but the value is the same..PPThis mechanism can be used to solve the problem described aboveby placing \fBTcl_Preserve\fR and \fBTcl_Release\fR calls aroundactions that may cause undesired storage re-allocation. Themechanism is intended only for short-term use (i.e. while proceduresare pending on the stack); it will not work efficiently as amechanism for long-term reference counts.The implementation does not depend in any way on the internalstructure of the objects being freed; it keeps the referencecounts in a separate structure..SH KEYWORDSfree, reference count, storage
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -