home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tk3.3b1 / doc / Preserve.3 < prev    next >
Encoding:
Text File  |  1993-04-01  |  4.7 KB  |  115 lines

  1. '\"
  2. '\" Copyright (c) 1990 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/wish/man/RCS/Preserve.3,v 1.5 93/04/01 09:41:54 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. .so man.macros
  24. .HS Tk_Preserve tkc
  25. .BS
  26. .SH NAME
  27. Tk_Preserve, Tk_Release, Tk_EventuallyFree \- avoid freeing storage while it's being used
  28. .SH SYNOPSIS
  29. .nf
  30. \fB#include <tk.h>\fR
  31. .sp
  32. \fBTk_Preserve\fR(\fIclientData\fR)
  33. .sp
  34. \fBTk_Release\fR(\fIclientData\fR)
  35. .sp
  36. \fBTk_EventuallyFree\fR(\fIclientData, freeProc\fR)
  37. .SH ARGUMENTS
  38. .AS Tk_FreeProc clientData
  39. .AP ClientData clientData in
  40. Token describing structure to be freed or reallocated.  Usually a pointer
  41. to memory for structure.
  42. .AP Tk_FreeProc *freeProc in
  43. Procedure to invoke to free \fIclientData\fR.
  44. .BE
  45.  
  46. .SH DESCRIPTION
  47. .PP
  48. These three procedures help implement a simple reference count mechanism
  49. for managing storage.  They are designed to solve a problem
  50. having to do with widget deletion.  When a widget is deleted, its
  51. widget record (the structure holding information specific to the
  52. widget) must be returned to the storage allocator.
  53. However, it's possible that the widget record is in active use
  54. by one of the procedures on the stack at the time of the deletion.
  55. This can happen, for example, if the command associated with a button
  56. widget causes the button to be destroyed:  an X event causes an
  57. event-handling C procedure in the button to be invoked, which in
  58. turn causes the button's associated Tcl command to be executed,
  59. which in turn causes the button to be deleted, which in turn causes
  60. the button's widget record to be de-allocated.
  61. Unfortunately, when the Tcl command returns, the button's
  62. event-handling procedure will need to reference the
  63. button's widget record.
  64. Because of this, the widget record must not be freed as part of the
  65. deletion, but must be retained until the event-handling procedure has
  66. finished with it.
  67. In other situations where the widget is deleted, it may be possible
  68. to free the widget record immediately.
  69. .PP
  70. \fBTk_Preserve\fR and \fBTk_Release\fR
  71. implement short-term reference counts for their \fIclientData\fR
  72. argument.
  73. The \fIclientData\fR argument identifies an object and usually
  74. consists of the address of a structure.
  75. The reference counts guarantee that an object will not be freed
  76. until each call to \fBTk_Preserve\fR for the object has been
  77. matched by calls to \fBTk_Release\fR.
  78. There may be any number of unmatched \fBTk_Preserve\fR calls
  79. in effect at once.
  80. .PP
  81. \fBTk_EventuallyFree\fR is invoked to free up its \fIclientData\fR
  82. argument.
  83. It checks to see if there are unmatched \fBTk_Preserve\fR calls
  84. for the object.
  85. If not, then \fBTk_EventuallyFree\fR calls \fIfreeProc\fR immediately.
  86. Otherwise \fBTk_EventuallyFree\fR records the fact that \fIclientData\fR
  87. needs eventually to be freed.
  88. When all calls to \fBTk_Preserve\fR have been matched with
  89. calls to \fBTk_Release\fR then \fIfreeProc\fR will be called by
  90. \fBTk_Release\fR to do the cleanup.
  91. .PP
  92. All the work of freeing the object is carried out by \fIfreeProc\fR.
  93. \fIFreeProc\fR must have arguments and result that match the
  94. type \fBTk_FreeProc\fR:
  95. .nf
  96. .RS
  97. typedef void Tk_FreeProc(ClientData \fIclientData\fR);
  98. .RE
  99. .fi
  100. The \fIclientData\fR argument to \fIfreeProc\fR will be the
  101. same as the \fIclientData\fR argument to \fBTk_EventuallyFree\fR.
  102. .PP
  103. This mechanism can be used to solve the problem described above
  104. by placing \fBTk_Preserve\fR and \fBTk_Release\fR calls around
  105. actions that may cause undesired storage re-allocation.  The
  106. mechanism is intended only for short-term use (i.e. while procedures
  107. are pending on the stack);  it will not work efficiently as a
  108. mechanism for long-term reference counts.
  109. The implementation does not depend in any way on the internal
  110. structure of the objects being freed;  it keeps the reference
  111. counts in a separate structure.
  112.  
  113. .SH KEYWORDS
  114. free, reference count, storage
  115.