Hex Artifact Content

Artifact ca1b399e32f75a380507e92e8c4531db6e6bcf07:


0000: 2f 2a 20 0a 20 2a 20 74 6b 50 72 65 73 65 72 76  /* . * tkPreserv
0010: 65 2e 63 20 28 43 54 6b 29 20 2d 2d 0a 20 2a 0a  e.c (CTk) --. *.
0020: 20 2a 09 54 68 69 73 20 66 69 6c 65 20 63 6f 6e   *.This file con
0030: 74 61 69 6e 73 20 61 20 63 6f 6c 6c 65 63 74 69  tains a collecti
0040: 6f 6e 20 6f 66 20 70 72 6f 63 65 64 75 72 65 73  on of procedures
0050: 20 74 68 61 74 20 61 72 65 20 75 73 65 64 0a 20   that are used. 
0060: 2a 09 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  *.to make sure t
0070: 68 61 74 20 77 69 64 67 65 74 20 72 65 63 6f 72  hat widget recor
0080: 64 73 20 61 6e 64 20 6f 74 68 65 72 20 64 61 74  ds and other dat
0090: 61 20 73 74 72 75 63 74 75 72 65 73 0a 20 2a 09  a structures. *.
00a0: 61 72 65 6e 27 74 20 72 65 61 6c 6c 6f 63 61 74  aren't reallocat
00b0: 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 61 72  ed when there ar
00c0: 65 20 6e 65 73 74 65 64 20 70 72 6f 63 65 64 75  e nested procedu
00d0: 72 65 73 20 74 68 61 74 0a 20 2a 09 64 65 70 65  res that. *.depe
00e0: 6e 64 20 6f 6e 20 74 68 65 69 72 20 65 78 69 73  nd on their exis
00f0: 74 65 6e 63 65 2e 0a 20 2a 0a 20 2a 20 43 6f 70  tence.. *. * Cop
0100: 79 72 69 67 68 74 20 28 63 29 20 31 39 39 31 2d  yright (c) 1991-
0110: 31 39 39 34 20 54 68 65 20 52 65 67 65 6e 74 73  1994 The Regents
0120: 20 6f 66 20 74 68 65 20 55 6e 69 76 65 72 73 69   of the Universi
0130: 74 79 20 6f 66 20 43 61 6c 69 66 6f 72 6e 69 61  ty of California
0140: 2e 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20 28  .. * Copyright (
0150: 63 29 20 31 39 39 34 20 53 75 6e 20 4d 69 63 72  c) 1994 Sun Micr
0160: 6f 73 79 73 74 65 6d 73 2c 20 49 6e 63 2e 0a 20  osystems, Inc.. 
0170: 2a 20 43 6f 70 79 72 69 67 68 74 20 28 63 29 20  * Copyright (c) 
0180: 31 39 39 35 20 43 6c 65 76 65 6c 61 6e 64 20 43  1995 Cleveland C
0190: 6c 69 6e 69 63 20 46 6f 75 6e 64 61 74 69 6f 6e  linic Foundation
01a0: 0a 20 2a 0a 20 2a 20 53 65 65 20 74 68 65 20 66  . *. * See the f
01b0: 69 6c 65 20 22 6c 69 63 65 6e 73 65 2e 74 65 72  ile "license.ter
01c0: 6d 73 22 20 66 6f 72 20 69 6e 66 6f 72 6d 61 74  ms" for informat
01d0: 69 6f 6e 20 6f 6e 20 75 73 61 67 65 20 61 6e 64  ion on usage and
01e0: 20 72 65 64 69 73 74 72 69 62 75 74 69 6f 6e 0a   redistribution.
01f0: 20 2a 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2c   * of this file,
0200: 20 61 6e 64 20 66 6f 72 20 61 20 44 49 53 43 4c   and for a DISCL
0210: 41 49 4d 45 52 20 4f 46 20 41 4c 4c 20 57 41 52  AIMER OF ALL WAR
0220: 52 41 4e 54 49 45 53 2e 0a 20 2a 0a 20 2a 20 40  RANTIES.. *. * @
0230: 28 23 29 20 24 49 64 3a 20 63 74 6b 2e 73 68 61  (#) $Id: ctk.sha
0240: 72 2c 76 20 31 2e 35 30 20 31 39 39 36 2f 30 31  r,v 1.50 1996/01
0250: 2f 31 35 20 31 34 3a 34 37 3a 31 36 20 61 6e 64  /15 14:47:16 and
0260: 72 65 77 6d 20 45 78 70 20 61 6e 64 72 65 77 6d  rewm Exp andrewm
0270: 20 24 0a 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65   $. */..#include
0280: 20 22 74 6b 50 6f 72 74 2e 68 22 0a 23 69 6e 63   "tkPort.h".#inc
0290: 6c 75 64 65 20 22 74 6b 2e 68 22 0a 0a 2f 2a 0a  lude "tk.h"../*.
02a0: 20 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67   * The following
02b0: 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
02c0: 69 73 20 75 73 65 64 20 74 6f 20 6b 65 65 70 20  is used to keep 
02d0: 74 72 61 63 6b 20 6f 66 20 61 6c 6c 20 74 68 65  track of all the
02e0: 0a 20 2a 20 54 6b 5f 50 72 65 73 65 72 76 65 20  . * Tk_Preserve 
02f0: 63 61 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73  calls that are s
0300: 74 69 6c 6c 20 69 6e 20 65 66 66 65 63 74 2e 20  till in effect. 
0310: 20 49 74 20 67 72 6f 77 73 20 61 73 20 6e 65 65   It grows as nee
0320: 64 65 64 0a 20 2a 20 74 6f 20 61 63 63 6f 6d 6d  ded. * to accomm
0330: 6f 64 61 74 65 20 61 6e 79 20 6e 75 6d 62 65 72  odate any number
0340: 20 6f 66 20 63 61 6c 6c 73 20 69 6e 20 65 66 66   of calls in eff
0350: 65 63 74 2e 0a 20 2a 2f 0a 0a 74 79 70 65 64 65  ect.. */..typede
0360: 66 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 43  f struct {.    C
0370: 6c 69 65 6e 74 44 61 74 61 20 63 6c 69 65 6e 74  lientData client
0380: 44 61 74 61 3b 09 2f 2a 20 41 64 64 72 65 73 73  Data;./* Address
0390: 20 6f 66 20 70 72 65 73 65 72 76 65 64 20 62 6c   of preserved bl
03a0: 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ock. */.    int 
03b0: 72 65 66 43 6f 75 6e 74 3b 09 09 2f 2a 20 4e 75  refCount;../* Nu
03c0: 6d 62 65 72 20 6f 66 20 54 6b 5f 50 72 65 73 65  mber of Tk_Prese
03d0: 72 76 65 20 63 61 6c 6c 73 20 69 6e 20 65 66 66  rve calls in eff
03e0: 65 63 74 0a 09 09 09 09 20 2a 20 66 6f 72 20 62  ect..... * for b
03f0: 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20 69 6e 74  lock. */.    int
0400: 20 6d 75 73 74 46 72 65 65 3b 09 09 2f 2a 20 4e   mustFree;../* N
0410: 6f 6e 2d 7a 65 72 6f 20 6d 65 61 6e 73 20 54 6b  on-zero means Tk
0420: 5f 45 76 65 6e 74 75 61 6c 6c 79 46 72 65 65 20  _EventuallyFree 
0430: 77 61 73 0a 09 09 09 09 20 2a 20 63 61 6c 6c 65  was..... * calle
0440: 64 20 77 68 69 6c 65 20 61 20 54 6b 5f 50 72 65  d while a Tk_Pre
0450: 73 65 72 76 65 20 63 61 6c 6c 20 77 61 73 20 69  serve call was i
0460: 6e 0a 09 09 09 09 20 2a 20 65 66 66 65 63 74 2c  n..... * effect,
0470: 20 73 6f 20 74 68 65 20 73 74 72 75 63 74 75 72   so the structur
0480: 65 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 0a  e must be freed.
0490: 09 09 09 09 20 2a 20 77 68 65 6e 20 72 65 66 43  .... * when refC
04a0: 6f 75 6e 74 20 62 65 63 6f 6d 65 73 20 7a 65 72  ount becomes zer
04b0: 6f 2e 20 2a 2f 0a 20 20 20 20 54 6b 5f 46 72 65  o. */.    Tk_Fre
04c0: 65 50 72 6f 63 20 2a 66 72 65 65 50 72 6f 63 3b  eProc *freeProc;
04d0: 09 2f 2a 20 50 72 6f 63 65 64 75 72 65 20 74 6f  ./* Procedure to
04e0: 20 63 61 6c 6c 20 74 6f 20 66 72 65 65 2e 20 2a   call to free. *
04f0: 2f 0a 7d 20 52 65 66 65 72 65 6e 63 65 3b 0a 0a  /.} Reference;..
0500: 73 74 61 74 69 63 20 52 65 66 65 72 65 6e 63 65  static Reference
0510: 20 2a 72 65 66 41 72 72 61 79 3b 09 2f 2a 20 46   *refArray;./* F
0520: 69 72 73 74 20 69 6e 20 61 72 72 61 79 20 6f 66  irst in array of
0530: 20 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a   references. */.
0540: 73 74 61 74 69 63 20 69 6e 74 20 73 70 61 63 65  static int space
0550: 41 76 6c 20 3d 20 30 3b 09 2f 2a 20 54 6f 74 61  Avl = 0;./* Tota
0560: 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 74 72 75  l number of stru
0570: 63 74 75 72 65 73 20 61 76 61 69 6c 61 62 6c 65  ctures available
0580: 0a 09 09 09 09 20 2a 20 61 74 20 2a 66 69 72 73  ..... * at *firs
0590: 74 52 65 66 50 74 72 2e 20 2a 2f 0a 73 74 61 74  tRefPtr. */.stat
05a0: 69 63 20 69 6e 74 20 69 6e 55 73 65 20 3d 20 30  ic int inUse = 0
05b0: 3b 09 09 2f 2a 20 43 6f 75 6e 74 20 6f 66 20 73  ;../* Count of s
05c0: 74 72 75 63 74 75 72 65 73 20 63 75 72 72 65 6e  tructures curren
05d0: 74 6c 79 20 69 6e 20 75 73 65 0a 09 09 09 09 20  tly in use..... 
05e0: 2a 20 69 6e 20 72 65 66 41 72 72 61 79 2e 20 2a  * in refArray. *
05f0: 2f 0a 23 64 65 66 69 6e 65 20 49 4e 49 54 49 41  /.#define INITIA
0600: 4c 5f 53 49 5a 45 20 32 0a 0c 0a 2f 2a 0a 20 2a  L_SIZE 2.../*. *
0610: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0620: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0630: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0640: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0650: 2d 2d 2d 2d 2d 2d 0a 20 2a 0a 20 2a 20 54 6b 5f  ------. *. * Tk_
0660: 50 72 65 73 65 72 76 65 20 2d 2d 0a 20 2a 0a 20  Preserve --. *. 
0670: 2a 09 54 68 69 73 20 70 72 6f 63 65 64 75 72 65  *.This procedure
0680: 20 69 73 20 75 73 65 64 20 62 79 20 61 20 70 72   is used by a pr
0690: 6f 63 65 64 75 72 65 20 74 6f 20 64 65 63 6c 61  ocedure to decla
06a0: 72 65 20 69 74 73 20 69 6e 74 65 72 65 73 74 0a  re its interest.
06b0: 20 2a 09 69 6e 20 61 20 70 61 72 74 69 63 75 6c   *.in a particul
06c0: 61 72 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f  ar block of memo
06d0: 72 79 2c 20 73 6f 20 74 68 61 74 20 74 68 65 20  ry, so that the 
06e0: 62 6c 6f 63 6b 20 77 69 6c 6c 20 6e 6f 74 20 62  block will not b
06f0: 65 0a 20 2a 09 72 65 61 6c 6c 6f 63 61 74 65 64  e. *.reallocated
0700: 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 69 6e   until a matchin
0710: 67 20 63 61 6c 6c 20 74 6f 20 54 6b 5f 52 65 6c  g call to Tk_Rel
0720: 65 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d 61  ease has been ma
0730: 64 65 2e 0a 20 2a 0a 20 2a 20 52 65 73 75 6c 74  de.. *. * Result
0740: 73 3a 0a 20 2a 09 4e 6f 6e 65 2e 0a 20 2a 0a 20  s:. *.None.. *. 
0750: 2a 20 53 69 64 65 20 65 66 66 65 63 74 73 3a 0a  * Side effects:.
0760: 20 2a 09 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 69   *.Information i
0770: 73 20 72 65 74 61 69 6e 65 64 20 73 6f 20 74 68  s retained so th
0780: 61 74 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20  at the block of 
0790: 6d 65 6d 6f 72 79 20 77 69 6c 6c 0a 20 2a 09 6e  memory will. *.n
07a0: 6f 74 20 62 65 20 66 72 65 65 64 20 75 6e 74 69  ot be freed unti
07b0: 6c 20 61 74 20 6c 65 61 73 74 20 74 68 65 20 6d  l at least the m
07c0: 61 74 63 68 69 6e 67 20 63 61 6c 6c 20 74 6f 20  atching call to 
07d0: 54 6b 5f 52 65 6c 65 61 73 65 2e 0a 20 2a 0a 20  Tk_Release.. *. 
07e0: 2a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *---------------
07f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0800: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0810: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0820: 2d 2d 2d 2d 2d 2d 2d 0a 20 2a 2f 0a 0a 76 6f 69  -------. */..voi
0830: 64 0a 54 6b 5f 50 72 65 73 65 72 76 65 28 63 6c  d.Tk_Preserve(cl
0840: 69 65 6e 74 44 61 74 61 29 0a 20 20 20 20 43 6c  ientData).    Cl
0850: 69 65 6e 74 44 61 74 61 20 63 6c 69 65 6e 74 44  ientData clientD
0860: 61 74 61 3b 09 2f 2a 20 50 6f 69 6e 74 65 72 20  ata;./* Pointer 
0870: 74 6f 20 6d 61 6c 6c 6f 63 27 65 64 20 62 6c 6f  to malloc'ed blo
0880: 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 2a 2f  ck of memory. */
0890: 0a 7b 0a 20 20 20 20 72 65 67 69 73 74 65 72 20  .{.    register 
08a0: 52 65 66 65 72 65 6e 63 65 20 2a 72 65 66 50 74  Reference *refPt
08b0: 72 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20  r;.    int i;.. 
08c0: 20 20 20 2f 2a 0a 20 20 20 20 20 2a 20 53 65 65     /*.     * See
08d0: 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6c 72   if there is alr
08e0: 65 61 64 79 20 61 20 72 65 66 65 72 65 6e 63 65  eady a reference
08f0: 20 66 6f 72 20 74 68 69 73 20 70 6f 69 6e 74 65   for this pointe
0900: 72 2e 20 20 49 66 20 73 6f 2c 0a 20 20 20 20 20  r.  If so,.     
0910: 2a 20 6a 75 73 74 20 69 6e 63 72 65 6d 65 6e 74  * just increment
0920: 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20 63   its reference c
0930: 6f 75 6e 74 2e 0a 20 20 20 20 20 2a 2f 0a 0a 20  ount..     */.. 
0940: 20 20 20 66 6f 72 20 28 69 20 3d 20 30 2c 20 72     for (i = 0, r
0950: 65 66 50 74 72 20 3d 20 72 65 66 41 72 72 61 79  efPtr = refArray
0960: 3b 20 69 20 3c 20 69 6e 55 73 65 3b 20 69 2b 2b  ; i < inUse; i++
0970: 2c 20 72 65 66 50 74 72 2b 2b 29 20 7b 0a 09 69  , refPtr++) {..i
0980: 66 20 28 72 65 66 50 74 72 2d 3e 63 6c 69 65 6e  f (refPtr->clien
0990: 74 44 61 74 61 20 3d 3d 20 63 6c 69 65 6e 74 44  tData == clientD
09a0: 61 74 61 29 20 7b 0a 09 20 20 20 20 72 65 66 50  ata) {..    refP
09b0: 74 72 2d 3e 72 65 66 43 6f 75 6e 74 2b 2b 3b 0a  tr->refCount++;.
09c0: 09 20 20 20 20 72 65 74 75 72 6e 3b 0a 09 7d 0a  .    return;..}.
09d0: 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20      }..    /*.  
09e0: 20 20 20 2a 20 4d 61 6b 65 20 61 20 72 65 66 65     * Make a refe
09f0: 72 65 6e 63 65 20 61 72 72 61 79 20 69 66 20 69  rence array if i
0a00: 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61 64  t doesn't alread
0a10: 79 20 65 78 69 73 74 2c 20 6f 72 20 6d 61 6b 65  y exist, or make
0a20: 20 69 74 0a 20 20 20 20 20 2a 20 62 69 67 67 65   it.     * bigge
0a30: 72 20 69 66 20 69 74 20 69 73 20 66 75 6c 6c 2e  r if it is full.
0a40: 0a 20 20 20 20 20 2a 2f 0a 0a 20 20 20 20 69 66  .     */..    if
0a50: 20 28 69 6e 55 73 65 20 3d 3d 20 73 70 61 63 65   (inUse == space
0a60: 41 76 6c 29 20 7b 0a 09 69 66 20 28 73 70 61 63  Avl) {..if (spac
0a70: 65 41 76 6c 20 3d 3d 20 30 29 20 7b 0a 09 20 20  eAvl == 0) {..  
0a80: 20 20 72 65 66 41 72 72 61 79 20 3d 20 28 52 65    refArray = (Re
0a90: 66 65 72 65 6e 63 65 20 2a 29 20 63 6b 61 6c 6c  ference *) ckall
0aa0: 6f 63 28 28 75 6e 73 69 67 6e 65 64 29 0a 09 09  oc((unsigned)...
0ab0: 20 20 20 20 28 49 4e 49 54 49 41 4c 5f 53 49 5a      (INITIAL_SIZ
0ac0: 45 2a 73 69 7a 65 6f 66 28 52 65 66 65 72 65 6e  E*sizeof(Referen
0ad0: 63 65 29 29 29 3b 0a 09 20 20 20 20 73 70 61 63  ce)));..    spac
0ae0: 65 41 76 6c 20 3d 20 49 4e 49 54 49 41 4c 5f 53  eAvl = INITIAL_S
0af0: 49 5a 45 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09  IZE;..} else {..
0b00: 20 20 20 20 52 65 66 65 72 65 6e 63 65 20 2a 6e      Reference *n
0b10: 65 77 3b 0a 0a 09 20 20 20 20 6e 65 77 20 3d 20  ew;...    new = 
0b20: 28 52 65 66 65 72 65 6e 63 65 20 2a 29 20 63 6b  (Reference *) ck
0b30: 61 6c 6c 6f 63 28 28 75 6e 73 69 67 6e 65 64 29  alloc((unsigned)
0b40: 0a 09 09 20 20 20 20 28 32 2a 73 70 61 63 65 41  ...    (2*spaceA
0b50: 76 6c 2a 73 69 7a 65 6f 66 28 52 65 66 65 72 65  vl*sizeof(Refere
0b60: 6e 63 65 29 29 29 3b 0a 09 20 20 20 20 6d 65 6d  nce)));..    mem
0b70: 63 70 79 28 28 56 4f 49 44 20 2a 29 20 6e 65 77  cpy((VOID *) new
0b80: 2c 20 28 56 4f 49 44 20 2a 29 20 72 65 66 41 72  , (VOID *) refAr
0b90: 72 61 79 2c 20 73 70 61 63 65 41 76 6c 2a 73 69  ray, spaceAvl*si
0ba0: 7a 65 6f 66 28 52 65 66 65 72 65 6e 63 65 29 29  zeof(Reference))
0bb0: 3b 0a 09 20 20 20 20 63 6b 66 72 65 65 28 28 63  ;..    ckfree((c
0bc0: 68 61 72 20 2a 29 20 72 65 66 41 72 72 61 79 29  har *) refArray)
0bd0: 3b 0a 09 20 20 20 20 72 65 66 41 72 72 61 79 20  ;..    refArray 
0be0: 3d 20 6e 65 77 3b 0a 09 20 20 20 20 73 70 61 63  = new;..    spac
0bf0: 65 41 76 6c 20 2a 3d 20 32 3b 0a 09 7d 0a 20 20  eAvl *= 2;..}.  
0c00: 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
0c10: 20 2a 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 6e   * Make a new en
0c20: 74 72 79 20 66 6f 72 20 74 68 65 20 6e 65 77 20  try for the new 
0c30: 72 65 66 65 72 65 6e 63 65 2e 0a 20 20 20 20 20  reference..     
0c40: 2a 2f 0a 0a 20 20 20 20 72 65 66 50 74 72 20 3d  */..    refPtr =
0c50: 20 26 72 65 66 41 72 72 61 79 5b 69 6e 55 73 65   &refArray[inUse
0c60: 5d 3b 0a 20 20 20 20 72 65 66 50 74 72 2d 3e 63  ];.    refPtr->c
0c70: 6c 69 65 6e 74 44 61 74 61 20 3d 20 63 6c 69 65  lientData = clie
0c80: 6e 74 44 61 74 61 3b 0a 20 20 20 20 72 65 66 50  ntData;.    refP
0c90: 74 72 2d 3e 72 65 66 43 6f 75 6e 74 20 3d 20 31  tr->refCount = 1
0ca0: 3b 0a 20 20 20 20 72 65 66 50 74 72 2d 3e 6d 75  ;.    refPtr->mu
0cb0: 73 74 46 72 65 65 20 3d 20 30 3b 0a 20 20 20 20  stFree = 0;.    
0cc0: 69 6e 55 73 65 20 2b 3d 20 31 3b 0a 7d 0a 0c 0a  inUse += 1;.}...
0cd0: 2f 2a 0a 20 2a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  /*. *-----------
0ce0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0cf0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 2a 0a 20  -----------. *. 
0d20: 2a 20 54 6b 5f 52 65 6c 65 61 73 65 20 2d 2d 0a  * Tk_Release --.
0d30: 20 2a 0a 20 2a 09 54 68 69 73 20 70 72 6f 63 65   *. *.This proce
0d40: 64 75 72 65 20 69 73 20 63 61 6c 6c 65 64 20 74  dure is called t
0d50: 6f 20 63 61 6e 63 65 6c 20 61 20 70 72 65 76 69  o cancel a previ
0d60: 6f 75 73 20 63 61 6c 6c 20 74 6f 0a 20 2a 09 54  ous call to. *.T
0d70: 6b 5f 50 72 65 73 65 72 76 65 2c 20 74 68 65 72  k_Preserve, ther
0d80: 65 62 79 20 61 6c 6c 6f 77 69 6e 67 20 61 20 62  eby allowing a b
0d90: 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 74  lock of memory t
0da0: 6f 20 62 65 0a 20 2a 09 66 72 65 65 64 20 28 69  o be. *.freed (i
0db0: 66 20 6e 6f 20 6f 6e 65 20 65 6c 73 65 20 63 61  f no one else ca
0dc0: 72 65 73 20 61 62 6f 75 74 20 69 74 29 2e 0a 20  res about it).. 
0dd0: 2a 0a 20 2a 20 52 65 73 75 6c 74 73 3a 0a 20 2a  *. * Results:. *
0de0: 09 4e 6f 6e 65 2e 0a 20 2a 0a 20 2a 20 53 69 64  .None.. *. * Sid
0df0: 65 20 65 66 66 65 63 74 73 3a 0a 20 2a 09 49 66  e effects:. *.If
0e00: 20 54 6b 5f 45 76 65 6e 74 75 61 6c 6c 79 46 72   Tk_EventuallyFr
0e10: 65 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  ee has been call
0e20: 65 64 20 66 6f 72 20 63 6c 69 65 6e 74 44 61 74  ed for clientDat
0e30: 61 2c 20 61 6e 64 20 69 66 0a 20 2a 09 6e 6f 20  a, and if. *.no 
0e40: 6f 74 68 65 72 20 63 61 6c 6c 20 74 6f 20 54 6b  other call to Tk
0e50: 5f 50 72 65 73 65 72 76 65 20 69 73 20 73 74 69  _Preserve is sti
0e60: 6c 6c 20 69 6e 20 65 66 66 65 63 74 2c 20 74 68  ll in effect, th
0e70: 65 20 62 6c 6f 63 6b 20 6f 66 0a 20 2a 09 6d 65  e block of. *.me
0e80: 6d 6f 72 79 20 69 73 20 66 72 65 65 64 2e 0a 20  mory is freed.. 
0e90: 2a 0a 20 2a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *. *------------
0ea0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0eb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ec0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ed0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 2a 2f 0a 0a  ----------. */..
0ee0: 76 6f 69 64 0a 54 6b 5f 52 65 6c 65 61 73 65 28  void.Tk_Release(
0ef0: 63 6c 69 65 6e 74 44 61 74 61 29 0a 20 20 20 20  clientData).    
0f00: 43 6c 69 65 6e 74 44 61 74 61 20 63 6c 69 65 6e  ClientData clien
0f10: 74 44 61 74 61 3b 09 2f 2a 20 50 6f 69 6e 74 65  tData;./* Pointe
0f20: 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 65 64 20 62  r to malloc'ed b
0f30: 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 2e 20  lock of memory. 
0f40: 2a 2f 0a 7b 0a 20 20 20 20 72 65 67 69 73 74 65  */.{.    registe
0f50: 72 20 52 65 66 65 72 65 6e 63 65 20 2a 72 65 66  r Reference *ref
0f60: 50 74 72 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  Ptr;.    int i;.
0f70: 0a 20 20 20 20 66 6f 72 20 28 69 20 3d 20 30 2c  .    for (i = 0,
0f80: 20 72 65 66 50 74 72 20 3d 20 72 65 66 41 72 72   refPtr = refArr
0f90: 61 79 3b 20 69 20 3c 20 69 6e 55 73 65 3b 20 69  ay; i < inUse; i
0fa0: 2b 2b 2c 20 72 65 66 50 74 72 2b 2b 29 20 7b 0a  ++, refPtr++) {.
0fb0: 09 69 66 20 28 72 65 66 50 74 72 2d 3e 63 6c 69  .if (refPtr->cli
0fc0: 65 6e 74 44 61 74 61 20 21 3d 20 63 6c 69 65 6e  entData != clien
0fd0: 74 44 61 74 61 29 20 7b 0a 09 20 20 20 20 63 6f  tData) {..    co
0fe0: 6e 74 69 6e 75 65 3b 0a 09 7d 0a 09 72 65 66 50  ntinue;..}..refP
0ff0: 74 72 2d 3e 72 65 66 43 6f 75 6e 74 2d 2d 3b 0a  tr->refCount--;.
1000: 09 69 66 20 28 72 65 66 50 74 72 2d 3e 72 65 66  .if (refPtr->ref
1010: 43 6f 75 6e 74 20 3d 3d 20 30 29 20 7b 0a 09 20  Count == 0) {.. 
1020: 20 20 20 69 66 20 28 72 65 66 50 74 72 2d 3e 6d     if (refPtr->m
1030: 75 73 74 46 72 65 65 29 20 7b 0a 09 09 69 66 20  ustFree) {...if 
1040: 28 72 65 66 50 74 72 2d 3e 66 72 65 65 50 72 6f  (refPtr->freePro
1050: 63 20 3d 3d 20 28 54 6b 5f 46 72 65 65 50 72 6f  c == (Tk_FreePro
1060: 63 20 2a 29 20 66 72 65 65 29 20 7b 0a 09 09 20  c *) free) {... 
1070: 20 20 20 63 6b 66 72 65 65 28 28 63 68 61 72 20     ckfree((char 
1080: 2a 29 20 72 65 66 50 74 72 2d 3e 63 6c 69 65 6e  *) refPtr->clien
1090: 74 44 61 74 61 29 3b 0a 09 09 7d 20 65 6c 73 65  tData);...} else
10a0: 20 7b 0a 09 09 20 20 20 20 28 2a 72 65 66 50 74   {...    (*refPt
10b0: 72 2d 3e 66 72 65 65 50 72 6f 63 29 28 72 65 66  r->freeProc)(ref
10c0: 50 74 72 2d 3e 63 6c 69 65 6e 74 44 61 74 61 29  Ptr->clientData)
10d0: 3b 0a 09 09 7d 0a 09 20 20 20 20 7d 0a 0a 09 20  ;...}..    }... 
10e0: 20 20 20 2f 2a 0a 09 20 20 20 20 20 2a 20 43 6f     /*..     * Co
10f0: 70 79 20 64 6f 77 6e 20 74 68 65 20 6c 61 73 74  py down the last
1100: 20 72 65 66 65 72 65 6e 63 65 20 69 6e 20 74 68   reference in th
1110: 65 20 61 72 72 61 79 20 74 6f 20 66 69 6c 6c 20  e array to fill 
1120: 74 68 65 0a 09 20 20 20 20 20 2a 20 68 6f 6c 65  the..     * hole
1130: 20 6c 65 66 74 20 62 79 20 74 68 65 20 75 6e 75   left by the unu
1140: 73 65 64 20 72 65 66 65 72 65 6e 63 65 2e 0a 09  sed reference...
1150: 20 20 20 20 20 2a 2f 0a 0a 09 20 20 20 20 69 6e       */...    in
1160: 55 73 65 2d 2d 3b 0a 09 20 20 20 20 69 66 20 28  Use--;..    if (
1170: 69 20 3c 20 69 6e 55 73 65 29 20 7b 0a 09 09 72  i < inUse) {...r
1180: 65 66 41 72 72 61 79 5b 69 5d 20 3d 20 72 65 66  efArray[i] = ref
1190: 41 72 72 61 79 5b 69 6e 55 73 65 5d 3b 0a 09 20  Array[inUse];.. 
11a0: 20 20 20 7d 0a 09 7d 0a 09 72 65 74 75 72 6e 3b     }..}..return;
11b0: 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
11c0: 20 20 20 20 2a 20 52 65 66 65 72 65 6e 63 65 20      * Reference 
11d0: 6e 6f 74 20 66 6f 75 6e 64 2e 20 20 54 68 69 73  not found.  This
11e0: 20 69 73 20 61 20 62 75 67 20 69 6e 20 74 68 65   is a bug in the
11f0: 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20 2a 2f   caller..     */
1200: 0a 0a 20 20 20 20 70 61 6e 69 63 28 22 54 6b 5f  ..    panic("Tk_
1210: 52 65 6c 65 61 73 65 20 63 6f 75 6c 64 6e 27 74  Release couldn't
1220: 20 66 69 6e 64 20 72 65 66 65 72 65 6e 63 65 20   find reference 
1230: 66 6f 72 20 30 78 25 78 22 2c 20 63 6c 69 65 6e  for 0x%x", clien
1240: 74 44 61 74 61 29 3b 0a 7d 0a 0c 0a 2f 2a 0a 20  tData);.}.../*. 
1250: 2a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *---------------
1260: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1270: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1280: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1290: 2d 2d 2d 2d 2d 2d 2d 0a 20 2a 0a 20 2a 20 54 6b  -------. *. * Tk
12a0: 5f 45 76 65 6e 74 75 61 6c 6c 79 46 72 65 65 20  _EventuallyFree 
12b0: 2d 2d 0a 20 2a 0a 20 2a 09 46 72 65 65 20 75 70  --. *. *.Free up
12c0: 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f   a block of memo
12d0: 72 79 2c 20 75 6e 6c 65 73 73 20 61 20 63 61 6c  ry, unless a cal
12e0: 6c 20 74 6f 20 54 6b 5f 50 72 65 73 65 72 76 65  l to Tk_Preserve
12f0: 20 69 73 20 69 6e 0a 20 2a 09 65 66 66 65 63 74   is in. *.effect
1300: 20 66 6f 72 20 74 68 61 74 20 62 6c 6f 63 6b 2e   for that block.
1310: 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20    In this case, 
1320: 64 65 66 65 72 20 74 68 65 20 66 72 65 65 20 75  defer the free u
1330: 6e 74 69 6c 20 61 6c 6c 0a 20 2a 09 63 61 6c 6c  ntil all. *.call
1340: 73 20 74 6f 20 54 6b 5f 50 72 65 73 65 72 76 65  s to Tk_Preserve
1350: 20 68 61 76 65 20 62 65 65 6e 20 75 6e 64 6f 6e   have been undon
1360: 65 20 62 79 20 6d 61 74 63 68 69 6e 67 20 63 61  e by matching ca
1370: 6c 6c 73 20 74 6f 0a 20 2a 09 54 6b 5f 52 65 6c  lls to. *.Tk_Rel
1380: 65 61 73 65 2e 0a 20 2a 0a 20 2a 20 52 65 73 75  ease.. *. * Resu
1390: 6c 74 73 3a 0a 20 2a 09 4e 6f 6e 65 2e 0a 20 2a  lts:. *.None.. *
13a0: 0a 20 2a 20 53 69 64 65 20 65 66 66 65 63 74 73  . * Side effects
13b0: 3a 0a 20 2a 09 50 74 72 20 6d 61 79 20 62 65 20  :. *.Ptr may be 
13c0: 72 65 6c 65 61 73 65 64 20 62 79 20 63 61 6c 6c  released by call
13d0: 69 6e 67 20 66 72 65 65 28 29 2e 0a 20 2a 0a 20  ing free().. *. 
13e0: 2a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *---------------
13f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1400: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1410: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1420: 2d 2d 2d 2d 2d 2d 2d 0a 20 2a 2f 0a 0a 76 6f 69  -------. */..voi
1430: 64 0a 54 6b 5f 45 76 65 6e 74 75 61 6c 6c 79 46  d.Tk_EventuallyF
1440: 72 65 65 28 63 6c 69 65 6e 74 44 61 74 61 2c 20  ree(clientData, 
1450: 66 72 65 65 50 72 6f 63 29 0a 20 20 20 20 43 6c  freeProc).    Cl
1460: 69 65 6e 74 44 61 74 61 20 63 6c 69 65 6e 74 44  ientData clientD
1470: 61 74 61 3b 09 2f 2a 20 50 6f 69 6e 74 65 72 20  ata;./* Pointer 
1480: 74 6f 20 6d 61 6c 6c 6f 63 27 65 64 20 62 6c 6f  to malloc'ed blo
1490: 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 2a 2f  ck of memory. */
14a0: 0a 20 20 20 20 54 6b 5f 46 72 65 65 50 72 6f 63  .    Tk_FreeProc
14b0: 20 2a 66 72 65 65 50 72 6f 63 3b 09 2f 2a 20 50   *freeProc;./* P
14c0: 72 6f 63 65 64 75 72 65 20 74 6f 20 61 63 74 75  rocedure to actu
14d0: 61 6c 6c 79 20 64 6f 20 66 72 65 65 2e 20 2a 2f  ally do free. */
14e0: 0a 7b 0a 20 20 20 20 72 65 67 69 73 74 65 72 20  .{.    register 
14f0: 52 65 66 65 72 65 6e 63 65 20 2a 72 65 66 50 74  Reference *refPt
1500: 72 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 0a 20  r;.    int i;.. 
1510: 20 20 20 2f 2a 0a 20 20 20 20 20 2a 20 53 65 65     /*.     * See
1520: 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 72   if there is a r
1530: 65 66 65 72 65 6e 63 65 20 66 6f 72 20 74 68 69  eference for thi
1540: 73 20 70 6f 69 6e 74 65 72 2e 20 20 49 66 20 73  s pointer.  If s
1550: 6f 2c 20 73 65 74 20 69 74 73 0a 20 20 20 20 20  o, set its.     
1560: 2a 20 22 6d 75 73 74 46 72 65 65 22 20 66 6c 61  * "mustFree" fla
1570: 67 20 28 74 68 65 20 66 6c 61 67 20 68 61 64 20  g (the flag had 
1580: 62 65 74 74 65 72 20 6e 6f 74 20 62 65 20 73 65  better not be se
1590: 74 20 61 6c 72 65 61 64 79 21 29 2e 0a 20 20 20  t already!)..   
15a0: 20 20 2a 2f 0a 0a 20 20 20 20 66 6f 72 20 28 69    */..    for (i
15b0: 20 3d 20 30 2c 20 72 65 66 50 74 72 20 3d 20 72   = 0, refPtr = r
15c0: 65 66 41 72 72 61 79 3b 20 69 20 3c 20 69 6e 55  efArray; i < inU
15d0: 73 65 3b 20 69 2b 2b 2c 20 72 65 66 50 74 72 2b  se; i++, refPtr+
15e0: 2b 29 20 7b 0a 09 69 66 20 28 72 65 66 50 74 72  +) {..if (refPtr
15f0: 2d 3e 63 6c 69 65 6e 74 44 61 74 61 20 21 3d 20  ->clientData != 
1600: 63 6c 69 65 6e 74 44 61 74 61 29 20 7b 0a 09 20  clientData) {.. 
1610: 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 09 7d 0a     continue;..}.
1620: 09 69 66 20 28 72 65 66 50 74 72 2d 3e 6d 75 73  .if (refPtr->mus
1630: 74 46 72 65 65 29 20 7b 0a 09 20 20 20 20 70 61  tFree) {..    pa
1640: 6e 69 63 28 22 54 6b 5f 45 76 65 6e 74 75 61 6c  nic("Tk_Eventual
1650: 6c 79 46 72 65 65 20 63 61 6c 6c 65 64 20 74 77  lyFree called tw
1660: 69 63 65 20 66 6f 72 20 30 78 25 78 5c 6e 22 2c  ice for 0x%x\n",
1670: 20 63 6c 69 65 6e 74 44 61 74 61 29 3b 0a 20 20   clientData);.  
1680: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1690: 72 65 66 50 74 72 2d 3e 6d 75 73 74 46 72 65 65  refPtr->mustFree
16a0: 20 3d 20 31 3b 0a 09 72 65 66 50 74 72 2d 3e 66   = 1;..refPtr->f
16b0: 72 65 65 50 72 6f 63 20 3d 20 66 72 65 65 50 72  reeProc = freePr
16c0: 6f 63 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  oc;.        retu
16d0: 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rn;.    }..    /
16e0: 2a 0a 20 20 20 20 20 2a 20 4e 6f 20 72 65 66 65  *.     * No refe
16f0: 72 65 6e 63 65 20 66 6f 72 20 74 68 69 73 20 62  rence for this b
1700: 6c 6f 63 6b 2e 20 20 46 72 65 65 20 69 74 20 6e  lock.  Free it n
1710: 6f 77 2e 0a 20 20 20 20 20 2a 2f 0a 0a 20 20 20  ow..     */..   
1720: 20 69 66 20 28 66 72 65 65 50 72 6f 63 20 3d 3d   if (freeProc ==
1730: 20 28 54 6b 5f 46 72 65 65 50 72 6f 63 20 2a 29   (Tk_FreeProc *)
1740: 20 66 72 65 65 29 20 7b 0a 09 63 6b 66 72 65 65   free) {..ckfree
1750: 28 28 63 68 61 72 20 2a 29 20 63 6c 69 65 6e 74  ((char *) client
1760: 44 61 74 61 29 3b 0a 20 20 20 20 7d 20 65 6c 73  Data);.    } els
1770: 65 20 7b 0a 09 28 2a 66 72 65 65 50 72 6f 63 29  e {..(*freeProc)
1780: 28 63 6c 69 65 6e 74 44 61 74 61 29 3b 0a 20 20  (clientData);.  
1790: 20 20 7d 0a 7d 0a                                  }.}.