[5] | 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-1995 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.1 2008-06-04 13:58:10 demin Exp $ |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | #include "tclInt.h" |
---|
| 19 | |
---|
| 20 | /* |
---|
| 21 | * The following data structure is used to keep track of all the |
---|
| 22 | * Tcl_Preserve calls that are still in effect. It grows as needed |
---|
| 23 | * to accommodate any number of calls in effect. |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | typedef struct { |
---|
| 27 | ClientData clientData; /* Address of preserved block. */ |
---|
| 28 | int refCount; /* Number of Tcl_Preserve calls in effect |
---|
| 29 | * for block. */ |
---|
| 30 | int mustFree; /* Non-zero means Tcl_EventuallyFree was |
---|
| 31 | * called while a Tcl_Preserve call was in |
---|
| 32 | * effect, so the structure must be freed |
---|
| 33 | * when refCount becomes zero. */ |
---|
| 34 | Tcl_FreeProc *freeProc; /* Procedure to call to free. */ |
---|
| 35 | } Reference; |
---|
| 36 | |
---|
| 37 | static Reference *refArray; /* First in array of references. */ |
---|
| 38 | static int inUse = 0; /* Count of structures currently in use |
---|
| 39 | * in refArray. */ |
---|
| 40 | #define INITIAL_SIZE 2 |
---|
| 41 | |
---|
| 42 | /* |
---|
| 43 | *---------------------------------------------------------------------- |
---|
| 44 | * |
---|
| 45 | * Tcl_EventuallyFree -- |
---|
| 46 | * |
---|
| 47 | * Free up a block of memory, unless a call to Tcl_Preserve is in |
---|
| 48 | * effect for that block. In this case, defer the free until all |
---|
| 49 | * calls to Tcl_Preserve have been undone by matching calls to |
---|
| 50 | * Tcl_Release. |
---|
| 51 | * |
---|
| 52 | * Results: |
---|
| 53 | * None. |
---|
| 54 | * |
---|
| 55 | * Side effects: |
---|
| 56 | * Ptr may be released by calling free(). |
---|
| 57 | * |
---|
| 58 | *---------------------------------------------------------------------- |
---|
| 59 | */ |
---|
| 60 | |
---|
| 61 | void |
---|
| 62 | Tcl_EventuallyFree(clientData, freeProc) |
---|
| 63 | ClientData clientData; /* Pointer to malloc'ed block of memory. */ |
---|
| 64 | Tcl_FreeProc *freeProc; /* Procedure to actually do free. */ |
---|
| 65 | { |
---|
| 66 | Reference *refPtr; |
---|
| 67 | int i; |
---|
| 68 | |
---|
| 69 | /* |
---|
| 70 | * See if there is a reference for this pointer. If so, set its |
---|
| 71 | * "mustFree" flag (the flag had better not be set already!). |
---|
| 72 | */ |
---|
| 73 | |
---|
| 74 | for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) { |
---|
| 75 | if (refPtr->clientData != clientData) { |
---|
| 76 | continue; |
---|
| 77 | } |
---|
| 78 | if (refPtr->mustFree) { |
---|
| 79 | panic("Tcl_EventuallyFree called twice for 0x%x\n", clientData); |
---|
| 80 | } |
---|
| 81 | refPtr->mustFree = 1; |
---|
| 82 | refPtr->freeProc = freeProc; |
---|
| 83 | return; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /* |
---|
| 87 | * No reference for this block. Free it now. |
---|
| 88 | */ |
---|
| 89 | |
---|
| 90 | if ((freeProc == TCL_DYNAMIC) |
---|
| 91 | || (freeProc == (Tcl_FreeProc *) free)) { |
---|
| 92 | ckfree((char *) clientData); |
---|
| 93 | } else { |
---|
| 94 | (*freeProc)((char *)clientData); |
---|
| 95 | } |
---|
| 96 | } |
---|