/*
* tclPreserve.c --
*
* This file contains a collection of procedures that are used
* to make sure that widget records and other data structures
* aren't reallocated when there are nested procedures that
* depend on their existence.
*
* Copyright (c) 1991-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclPreserve.c 1.17 96/07/23 16:15:34
*/
#include "tclInt.h"
/*
* The following data structure is used to keep track of all the
* Tcl_Preserve calls that are still in effect. It grows as needed
* to accommodate any number of calls in effect.
*/
typedef struct {
* for block. */
* called while a Tcl_Preserve call was in
* effect, so the structure must be freed
* when refCount becomes zero. */
} Reference;
* at *firstRefPtr. */
* in refArray. */
/*
* Static routines in this file:
*/
/*
*----------------------------------------------------------------------
*
* PreserveExitProc --
*
* Called during exit processing to clean up the reference array.
*
* Results:
* None.
*
* Side effects:
* Frees the storage of the reference array.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static void
{
if (spaceAvl != 0) {
inUse = 0;
spaceAvl = 0;
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_Preserve --
*
* This procedure is used by a procedure to declare its interest
* in a particular block of memory, so that the block will not be
* reallocated until a matching call to Tcl_Release has been made.
*
* Results:
* None.
*
* Side effects:
* Information is retained so that the block of memory will
* not be freed until at least the matching call to Tcl_Release.
*
*----------------------------------------------------------------------
*/
void
{
int i;
/*
* See if there is already a reference for this pointer. If so,
* just increment its reference count.
*/
return;
}
}
/*
* Make a reference array if it doesn't already exist, or make it
* bigger if it is full.
*/
if (spaceAvl == 0) {
(ClientData) NULL);
(INITIAL_SIZE*sizeof(Reference)));
} else {
spaceAvl *= 2;
}
}
/*
* Make a new entry for the new reference.
*/
inUse += 1;
}
/*
*----------------------------------------------------------------------
*
* Tcl_Release --
*
* This procedure is called to cancel a previous call to
* Tcl_Preserve, thereby allowing a block of memory to be
* freed (if no one else cares about it).
*
* Results:
* None.
*
* Side effects:
* If Tcl_EventuallyFree has been called for clientData, and if
* no other call to Tcl_Preserve is still in effect, the block of
* memory is freed.
*
*----------------------------------------------------------------------
*/
void
{
int mustFree;
int i;
continue;
}
/*
* Must remove information from the slot before calling freeProc
* to avoid reentrancy problems if the freeProc calls Tcl_Preserve
* on the same clientData. Copy down the last reference in the
* array to overwrite the current slot.
*/
inUse--;
if (i < inUse) {
}
if (mustFree) {
if ((freeProc == TCL_DYNAMIC) ||
ckfree((char *) clientData);
} else {
(*freeProc)((char *) clientData);
}
}
}
return;
}
/*
* Reference not found. This is a bug in the caller.
*/
}
/*
*----------------------------------------------------------------------
*
* Tcl_EventuallyFree --
*
* Free up a block of memory, unless a call to Tcl_Preserve is in
* effect for that block. In this case, defer the free until all
* calls to Tcl_Preserve have been undone by matching calls to
* Tcl_Release.
*
* Results:
* None.
*
* Side effects:
* Ptr may be released by calling free().
*
*----------------------------------------------------------------------
*/
void
{
int i;
/*
* See if there is a reference for this pointer. If so, set its
* "mustFree" flag (the flag had better not be set already!).
*/
continue;
}
}
return;
}
/*
* No reference for this block. Free it now.
*/
if ((freeProc == TCL_DYNAMIC)
ckfree((char *) clientData);
} else {
(*freeProc)((char *)clientData);
}
}