Debian Codegraph @

pTk/mTk/tclGeneric/tclPreserve.c

raw symbols SCIP
1 /*
2 * tclPreserve.c --
3 *
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.
8 *
9 * Copyright (c) 1991-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1998 Sun Microsystems, Inc.
11 *
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 *
15 * RCS: @(#) $Id: tclPreserve.c,v 1.3.34.1 2003/07/16 21:25:07 hobbs Exp $
16 */
17
18 #include "tkPort.h"
19 #include "Lang.h"
20
21 /*
22 * The following data structure is used to keep track of all the
23 * Tcl_Preserve calls that are still in effect. It grows as needed
24 * to accommodate any number of calls in effect.
25 */
26
27 typedef struct {
28 ClientData clientData; /* Address of preserved block. */
29 int refCount; /* Number of Tcl_Preserve calls in effect
30 * for block. */
31 int mustFree; /* Non-zero means Tcl_EventuallyFree was
32 * called while a Tcl_Preserve call was in
33 * effect, so the structure must be freed
34 * when refCount becomes zero. */
35 Tcl_FreeProc *freeProc; /* Procedure to call to free. */
36 } Reference;
37
38 static Reference *refArray; /* First in array of references. */
39 static int spaceAvl = 0; /* Total number of structures available
40 * at *firstRefPtr. */
41 static int inUse = 0; /* Count of structures currently in use
42 * in refArray. */
43 #define INITIAL_SIZE 2
44 TCL_DECLARE_MUTEX(preserveMutex)/* To protect the above statics */
45
46 /*
47 * The following data structure is used to keep track of whether an
48 * arbitrary block of memory has been deleted. This is used by the
49 * TclHandle code to avoid the more time-expensive algorithm of
50 * Tcl_Preserve(). This mechanism is mainly used when we have lots of
51 * references to a few big, expensive objects that we don't want to live
52 * any longer than necessary.
53 */
54
55 typedef struct HandleStruct {
56 VOID *ptr; /* Pointer to the memory block being
57 * tracked. This field will become NULL when
58 * the memory block is deleted. This field
59 * must be the first in the structure. */
60 #ifdef TCL_MEM_DEBUG
61 VOID *ptr2; /* Backup copy of the abpve pointer used to
62 * ensure that the contents of the handle are
63 * not changed by anyone else. */
64 #endif
65 int refCount; /* Number of TclHandlePreserve() calls in
66 * effect on this handle. */
67 } HandleStruct;
68
69
70 /*
71 * Static routines in this file:
72 */
73
74 static void PreserveExitProc _ANSI_ARGS_((ClientData clientData));
75
76
77 /*
78 *----------------------------------------------------------------------
79 *
80 * PreserveExitProc --
81 *
82 * Called during exit processing to clean up the reference array.
83 *
84 * Results:
85 * None.
86 *
87 * Side effects:
88 * Frees the storage of the reference array.
89 *
90 *----------------------------------------------------------------------
91 */
92
93 /* ARGSUSED */
94 static void
95 PreserveExitProc(clientData)
96 ClientData clientData; /* NULL -Unused. */
97 {
98 Tcl_MutexLock(&preserveMutex);
99 if (spaceAvl != 0) {
100 ckfree((char *) refArray);
101 refArray = (Reference *) NULL;
102 inUse = 0;
103 spaceAvl = 0;
104 }
105 Tcl_MutexUnlock(&preserveMutex);
106 }
107
108 /*
109 *----------------------------------------------------------------------
110 *
111 * Tcl_Preserve --
112 *
113 * This procedure is used by a procedure to declare its interest
114 * in a particular block of memory, so that the block will not be
115 * reallocated until a matching call to Tcl_Release has been made.
116 *
117 * Results:
118 * None.
119 *
120 * Side effects:
121 * Information is retained so that the block of memory will
122 * not be freed until at least the matching call to Tcl_Release.
123 *
124 *----------------------------------------------------------------------
125 */
126
127 void
128 Tcl_Preserve(clientData)
129 ClientData clientData; /* Pointer to malloc'ed block of memory. */
130 {
131 Reference *refPtr;
132 int i;
133
134 /*
135 * See if there is already a reference for this pointer. If so,
136 * just increment its reference count.
137 */
138
139 Tcl_MutexLock(&preserveMutex);
140 for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
141 if (refPtr->clientData == clientData) {
142 refPtr->refCount++;
143 Tcl_MutexUnlock(&preserveMutex);
144 return;
145 }
146 }
147
148 /*
149 * Make a reference array if it doesn't already exist, or make it
150 * bigger if it is full.
151 */
152
153 if (inUse == spaceAvl) {
154 if (spaceAvl == 0) {
155 Tcl_CreateExitHandler((Tcl_ExitProc *) PreserveExitProc,
156 (ClientData) NULL);
157 refArray = (Reference *) ckalloc((unsigned)
158 (INITIAL_SIZE*sizeof(Reference)));
159 spaceAvl = INITIAL_SIZE;
160 } else {
161 Reference *new;
162
163 new = (Reference *) ckalloc((unsigned)
164 (2*spaceAvl*sizeof(Reference)));
165 memcpy((VOID *) new, (VOID *) refArray,
166 spaceAvl*sizeof(Reference));
167 ckfree((char *) refArray);
168 refArray = new;
169 spaceAvl *= 2;
170 }
171 }
172
173 /*
174 * Make a new entry for the new reference.
175 */
176
177 refPtr = &refArray[inUse];
178 refPtr->clientData = clientData;
179 refPtr->refCount = 1;
180 refPtr->mustFree = 0;
181 refPtr->freeProc = TCL_STATIC;
182 inUse += 1;
183 Tcl_MutexUnlock(&preserveMutex);
184 }
185
186 /*
187 *----------------------------------------------------------------------
188 *
189 * Tcl_Release --
190 *
191 * This procedure is called to cancel a previous call to
192 * Tcl_Preserve, thereby allowing a block of memory to be
193 * freed (if no one else cares about it).
194 *
195 * Results:
196 * None.
197 *
198 * Side effects:
199 * If Tcl_EventuallyFree has been called for clientData, and if
200 * no other call to Tcl_Preserve is still in effect, the block of
201 * memory is freed.
202 *
203 *----------------------------------------------------------------------
204 */
205
206 void
207 Tcl_Release(clientData)
208 ClientData clientData; /* Pointer to malloc'ed block of memory. */
209 {
210 Reference *refPtr;
211 int mustFree;
212 Tcl_FreeProc *freeProc;
213 int i;
214
215 Tcl_MutexLock(&preserveMutex);
216 for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
217 if (refPtr->clientData != clientData) {
218 continue;
219 }
220 refPtr->refCount--;
221 if (refPtr->refCount == 0) {
222
223 /*
224 * Must remove information from the slot before calling freeProc
225 * to avoid reentrancy problems if the freeProc calls Tcl_Preserve
226 * on the same clientData. Copy down the last reference in the
227 * array to overwrite the current slot.
228 */
229
230 freeProc = refPtr->freeProc;
231 mustFree = refPtr->mustFree;
232 inUse--;
233 if (i < inUse) {
234 refArray[i] = refArray[inUse];
235 }
236 if (mustFree) {
237 if (freeProc == TCL_DYNAMIC) {
238 ckfree((char *) clientData);
239 } else {
240 Tcl_MutexUnlock(&preserveMutex);
241 (*freeProc)((char *) clientData);
242 return;
243 }
244 }
245 }
246 Tcl_MutexUnlock(&preserveMutex);
247 return;
248 }
249 Tcl_MutexUnlock(&preserveMutex);
250
251 /*
252 * Reference not found. This is a bug in the caller.
253 */
254
255 panic("Tcl_Release couldn't find reference for 0x%x", clientData);
256 }
257
258 /*
259 *----------------------------------------------------------------------
260 *
261 * Tcl_EventuallyFree --
262 *
263 * Free up a block of memory, unless a call to Tcl_Preserve is in
264 * effect for that block. In this case, defer the free until all
265 * calls to Tcl_Preserve have been undone by matching calls to
266 * Tcl_Release.
267 *
268 * Results:
269 * None.
270 *
271 * Side effects:
272 * Ptr may be released by calling free().
273 *
274 *----------------------------------------------------------------------
275 */
276
277 void
278 Tcl_EventuallyFree(clientData, freeProc)
279 ClientData clientData; /* Pointer to malloc'ed block of memory. */
280 Tcl_FreeProc *freeProc; /* Procedure to actually do free. */
281 {
282 Reference *refPtr;
283 int i;
284
285 /*
286 * See if there is a reference for this pointer. If so, set its
287 * "mustFree" flag (the flag had better not be set already!).
288 */
289
290 Tcl_MutexLock(&preserveMutex);
291 for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
292 if (refPtr->clientData != clientData) {
293 continue;
294 }
295 if (refPtr->mustFree) {
296 panic("Tcl_EventuallyFree called twice for 0x%x\n", clientData);
297 }
298 refPtr->mustFree = 1;
299 refPtr->freeProc = freeProc;
300 Tcl_MutexUnlock(&preserveMutex);
301 return;
302 }
303 Tcl_MutexUnlock(&preserveMutex);
304
305 /*
306 * No reference for this block. Free it now.
307 */
308
309 if (freeProc == TCL_DYNAMIC) {
310 ckfree((char *) clientData);
311 } else {
312 (*freeProc)((char *)clientData);
313 }
314 }
315
316 /*
317 *---------------------------------------------------------------------------
318 *
319 * TclHandleCreate --
320 *
321 * Allocate a handle that contains enough information to determine
322 * if an arbitrary malloc'd block has been deleted. This is
323 * used to avoid the more time-expensive algorithm of Tcl_Preserve().
324 *
325 * Results:
326 * The return value is a TclHandle that refers to the given malloc'd
327 * block. Doubly dereferencing the returned handle will give
328 * back the pointer to the block, or will give NULL if the block has
329 * been deleted.
330 *
331 * Side effects:
332 * The caller must keep track of this handle (generally by storing
333 * it in a field in the malloc'd block) and call TclHandleFree()
334 * on this handle when the block is deleted. Everything else that
335 * wishes to keep track of whether the malloc'd block has been deleted
336 * should use calls to TclHandlePreserve() and TclHandleRelease()
337 * on the associated handle.
338 *
339 *---------------------------------------------------------------------------
340 */
341
342 TclHandle
343 TclHandleCreate(ptr)
344 VOID *ptr; /* Pointer to an arbitrary block of memory
345 * to be tracked for deletion. Must not be
346 * NULL. */
347 {
348 HandleStruct *handlePtr;
349
350 handlePtr = (HandleStruct *) ckalloc(sizeof(HandleStruct));
351 handlePtr->ptr = ptr;
352 #ifdef TCL_MEM_DEBUG
353 handlePtr->ptr2 = ptr;
354 #endif
355 handlePtr->refCount = 0;
356 return (TclHandle) handlePtr;
357 }
358
359 /*
360 *---------------------------------------------------------------------------
361 *
362 * TclHandleFree --
363 *
364 * Called when the arbitrary malloc'd block associated with the
365 * handle is being deleted. Modifies the handle so that doubly
366 * dereferencing it will give NULL. This informs any user of the
367 * handle that the block of memory formerly referenced by the
368 * handle has been freed.
369 *
370 * Results:
371 * None.
372 *
373 * Side effects:
374 * If nothing is referring to the handle, the handle will be reclaimed.
375 *
376 *---------------------------------------------------------------------------
377 */
378
379 void
380 TclHandleFree(handle)
381 TclHandle handle; /* Previously created handle associated
382 * with a malloc'd block that is being
383 * deleted. The handle is modified so that
384 * doubly dereferencing it will give NULL. */
385 {
386 HandleStruct *handlePtr;
387
388 handlePtr = (HandleStruct *) handle;
389 #ifdef TCL_MEM_DEBUG
390 if (handlePtr->refCount == 0x61616161) {
391 panic("using previously disposed TclHandle %x", handlePtr);
392 }
393 if (handlePtr->ptr2 != handlePtr->ptr) {
394 panic("someone has changed the block referenced by the handle %x\nfrom %x to %x",
395 handlePtr, handlePtr->ptr2, handlePtr->ptr);
396 }
397 #endif
398 handlePtr->ptr = NULL;
399 if (handlePtr->refCount == 0) {
400 ckfree((char *) handlePtr);
401 }
402 }
403
404 /*
405 *---------------------------------------------------------------------------
406 *
407 * TclHandlePreserve --
408 *
409 * Declare an interest in the arbitrary malloc'd block associated
410 * with the handle.
411 *
412 * Results:
413 * The return value is the handle argument, with its ref count
414 * incremented.
415 *
416 * Side effects:
417 * For each call to TclHandlePreserve(), there should be a matching
418 * call to TclHandleRelease() when the caller is no longer interested
419 * in the malloc'd block associated with the handle.
420 *
421 *---------------------------------------------------------------------------
422 */
423
424 TclHandle
425 TclHandlePreserve(handle)
426 TclHandle handle; /* Declare an interest in the block of
427 * memory referenced by this handle. */
428 {
429 HandleStruct *handlePtr;
430
431 handlePtr = (HandleStruct *) handle;
432 #ifdef TCL_MEM_DEBUG
433 if (handlePtr->refCount == 0x61616161) {
434 panic("using previously disposed TclHandle %x", handlePtr);
435 }
436 if ((handlePtr->ptr != NULL)
437 && (handlePtr->ptr != handlePtr->ptr2)) {
438 panic("someone has changed the block referenced by the handle %x\nfrom %x to %x",
439 handlePtr, handlePtr->ptr2, handlePtr->ptr);
440 }
441 #endif
442 handlePtr->refCount++;
443
444 return handle;
445 }
446
447 /*
448 *---------------------------------------------------------------------------
449 *
450 * TclHandleRelease --
451 *
452 * This procedure is called to release an interest in the malloc'd
453 * block associated with the handle.
454 *
455 * Results:
456 * None.
457 *
458 * Side effects:
459 * The ref count of the handle is decremented. If the malloc'd block
460 * has been freed and if no one is using the handle any more, the
461 * handle will be reclaimed.
462 *
463 *---------------------------------------------------------------------------
464 */
465
466 void
467 TclHandleRelease(handle)
468 TclHandle handle; /* Unregister interest in the block of
469 * memory referenced by this handle. */
470 {
471 HandleStruct *handlePtr;
472
473 handlePtr = (HandleStruct *) handle;
474 #ifdef TCL_MEM_DEBUG
475 if (handlePtr->refCount == 0x61616161) {
476 panic("using previously disposed TclHandle %x", handlePtr);
477 }
478 if ((handlePtr->ptr != NULL)
479 && (handlePtr->ptr != handlePtr->ptr2)) {
480 panic("someone has changed the block referenced by the handle %x\nfrom %x to %x",
481 handlePtr, handlePtr->ptr2, handlePtr->ptr);
482 }
483 #endif
484 handlePtr->refCount--;
485 if ((handlePtr->refCount == 0) && (handlePtr->ptr == NULL)) {
486 ckfree((char *) handlePtr);
487 }
488 }
489