4 * This file contains a collection of procedures that are used
5 * to make sure that widget records and other data structures
6 * aren't reallocated when there are nested procedures that
7 * depend on their existence.
9 * Copyright (c) 1990-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
11 * Copyright (c) 1995 Christian Werner
13 * See the file "license.terms" for information on usage and redistribution
14 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
20 #if (TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4)
23 * The following data structure is used to keep track of all the
24 * Ck_Preserve calls that are still in effect. It grows as needed
25 * to accommodate any number of calls in effect.
29 ClientData clientData; /* Address of preserved block. */
30 int refCount; /* Number of Ck_Preserve calls in effect
32 int mustFree; /* Non-zero means Ck_EventuallyFree was
33 * called while a Ck_Preserve call was in
34 * effect, so the structure must be freed
35 * when refCount becomes zero. */
36 Ck_FreeProc *freeProc; /* Procedure to call to free. */
39 static Reference *refArray; /* First in array of references. */
40 static int spaceAvl = 0; /* Total number of structures available
42 static int inUse = 0; /* Count of structures currently in use
44 #define INITIAL_SIZE 2
47 *----------------------------------------------------------------------
51 * This procedure is used by a procedure to declare its interest
52 * in a particular block of memory, so that the block will not be
53 * reallocated until a matching call to Ck_Release has been made.
59 * Information is retained so that the block of memory will
60 * not be freed until at least the matching call to Ck_Release.
62 *----------------------------------------------------------------------
66 Ck_Preserve(clientData)
67 ClientData clientData; /* Pointer to malloc'ed block of memory. */
69 register Reference *refPtr;
73 * See if there is already a reference for this pointer. If so,
74 * just increment its reference count.
77 for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
78 if (refPtr->clientData == clientData) {
85 * Make a reference array if it doesn't already exist, or make it
86 * bigger if it is full.
89 if (inUse == spaceAvl) {
91 refArray = (Reference *) ckalloc((unsigned)
92 (INITIAL_SIZE*sizeof(Reference)));
93 spaceAvl = INITIAL_SIZE;
97 new = (Reference *) ckalloc((unsigned)
98 (2*spaceAvl*sizeof(Reference)));
99 memcpy((VOID *) new, (VOID *) refArray, spaceAvl*sizeof(Reference));
100 ckfree((char *) refArray);
107 * Make a new entry for the new reference.
110 refPtr = &refArray[inUse];
111 refPtr->clientData = clientData;
112 refPtr->refCount = 1;
113 refPtr->mustFree = 0;
118 *----------------------------------------------------------------------
122 * This procedure is called to cancel a previous call to
123 * Ck_Preserve, thereby allowing a block of memory to be
124 * freed (if no one else cares about it).
130 * If Ck_EventuallyFree has been called for clientData, and if
131 * no other call to Ck_Preserve is still in effect, the block of
134 *----------------------------------------------------------------------
138 Ck_Release(clientData)
139 ClientData clientData; /* Pointer to malloc'ed block of memory. */
141 register Reference *refPtr;
144 for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
145 if (refPtr->clientData != clientData) {
149 if (refPtr->refCount == 0) {
150 if (refPtr->mustFree) {
151 if (refPtr->freeProc == (Ck_FreeProc *) free) {
152 ckfree((char *) refPtr->clientData);
154 (*refPtr->freeProc)(refPtr->clientData);
159 * Copy down the last reference in the array to fill the
160 * hole left by the unused reference.
165 refArray[i] = refArray[inUse];
172 * Reference not found. This is a bug in the caller.
175 panic("Ck_Release couldn't find reference for 0x%x", clientData);
179 *----------------------------------------------------------------------
181 * Ck_EventuallyFree --
183 * Free up a block of memory, unless a call to Ck_Preserve is in
184 * effect for that block. In this case, defer the free until all
185 * calls to Ck_Preserve have been undone by matching calls to
192 * Ptr may be released by calling free().
194 *----------------------------------------------------------------------
198 Ck_EventuallyFree(clientData, freeProc)
199 ClientData clientData; /* Pointer to malloc'ed block of memory. */
200 Ck_FreeProc *freeProc; /* Procedure to actually do free. */
202 register Reference *refPtr;
206 * See if there is a reference for this pointer. If so, set its
207 * "mustFree" flag (the flag had better not be set already!).
210 for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
211 if (refPtr->clientData != clientData) {
214 if (refPtr->mustFree) {
215 panic("Ck_EventuallyFree called twice for 0x%x\n", clientData);
217 refPtr->mustFree = 1;
218 refPtr->freeProc = freeProc;
223 * No reference for this block. Free it now.
226 if (freeProc == (Ck_FreeProc *) free) {
227 ckfree((char *) clientData);
229 (*freeProc)(clientData);
233 #endif /* TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION <= 4 */