archie/tcl7.6/generic/tclPreserve.c

278 lines
7.0 KiB
C
Raw Permalink Normal View History

2024-05-27 16:13:40 +02:00
/*
2024-05-27 16:40:40 +02:00
* tclPreserve.c --
2024-05-27 16:13:40 +02:00
*
* 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.
*
2024-05-27 16:40:40 +02:00
* 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.18 96/08/05 13:15:08
2024-05-27 16:13:40 +02:00
*/
2024-05-27 16:40:40 +02:00
#include "tclInt.h"
2024-05-27 16:13:40 +02:00
/*
* The following data structure is used to keep track of all the
2024-05-27 16:40:40 +02:00
* Tcl_Preserve calls that are still in effect. It grows as needed
2024-05-27 16:13:40 +02:00
* to accommodate any number of calls in effect.
*/
typedef struct {
ClientData clientData; /* Address of preserved block. */
2024-05-27 16:40:40 +02:00
int refCount; /* Number of Tcl_Preserve calls in effect
2024-05-27 16:13:40 +02:00
* for block. */
2024-05-27 16:40:40 +02:00
int mustFree; /* Non-zero means Tcl_EventuallyFree was
* called while a Tcl_Preserve call was in
2024-05-27 16:13:40 +02:00
* effect, so the structure must be freed
* when refCount becomes zero. */
2024-05-27 16:40:40 +02:00
Tcl_FreeProc *freeProc; /* Procedure to call to free. */
2024-05-27 16:13:40 +02:00
} Reference;
static Reference *refArray; /* First in array of references. */
static int spaceAvl = 0; /* Total number of structures available
* at *firstRefPtr. */
static int inUse = 0; /* Count of structures currently in use
* in refArray. */
#define INITIAL_SIZE 2
2024-05-27 16:40:40 +02:00
/*
* Static routines in this file:
*/
static void PreserveExitProc _ANSI_ARGS_((ClientData clientData));
2024-05-27 16:13:40 +02:00
/*
*----------------------------------------------------------------------
*
2024-05-27 16:40:40 +02:00
* 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
PreserveExitProc(clientData)
ClientData clientData; /* NULL -Unused. */
{
if (spaceAvl != 0) {
ckfree((char *) refArray);
refArray = (Reference *) NULL;
inUse = 0;
spaceAvl = 0;
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_Preserve --
2024-05-27 16:13:40 +02:00
*
* This procedure is used by a procedure to declare its interest
* in a particular block of memory, so that the block will not be
2024-05-27 16:40:40 +02:00
* reallocated until a matching call to Tcl_Release has been made.
2024-05-27 16:13:40 +02:00
*
* Results:
* None.
*
* Side effects:
* Information is retained so that the block of memory will
2024-05-27 16:40:40 +02:00
* not be freed until at least the matching call to Tcl_Release.
2024-05-27 16:13:40 +02:00
*
*----------------------------------------------------------------------
*/
void
2024-05-27 16:40:40 +02:00
Tcl_Preserve(clientData)
2024-05-27 16:13:40 +02:00
ClientData clientData; /* Pointer to malloc'ed block of memory. */
{
2024-05-27 16:40:40 +02:00
Reference *refPtr;
2024-05-27 16:13:40 +02:00
int i;
/*
* See if there is already a reference for this pointer. If so,
* just increment its reference count.
*/
for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
if (refPtr->clientData == clientData) {
refPtr->refCount++;
return;
}
}
/*
* Make a reference array if it doesn't already exist, or make it
* bigger if it is full.
*/
if (inUse == spaceAvl) {
if (spaceAvl == 0) {
2024-05-27 16:40:40 +02:00
Tcl_CreateExitHandler((Tcl_ExitProc *) PreserveExitProc,
(ClientData) NULL);
2024-05-27 16:13:40 +02:00
refArray = (Reference *) ckalloc((unsigned)
(INITIAL_SIZE*sizeof(Reference)));
spaceAvl = INITIAL_SIZE;
} else {
Reference *new;
new = (Reference *) ckalloc((unsigned)
(2*spaceAvl*sizeof(Reference)));
2024-05-27 16:40:40 +02:00
memcpy((VOID *) new, (VOID *) refArray,
spaceAvl*sizeof(Reference));
2024-05-27 16:13:40 +02:00
ckfree((char *) refArray);
refArray = new;
spaceAvl *= 2;
}
}
/*
* Make a new entry for the new reference.
*/
refPtr = &refArray[inUse];
refPtr->clientData = clientData;
refPtr->refCount = 1;
refPtr->mustFree = 0;
2024-05-27 16:40:40 +02:00
refPtr->freeProc = TCL_STATIC;
2024-05-27 16:13:40 +02:00
inUse += 1;
}
/*
*----------------------------------------------------------------------
*
2024-05-27 16:40:40 +02:00
* Tcl_Release --
2024-05-27 16:13:40 +02:00
*
* This procedure is called to cancel a previous call to
2024-05-27 16:40:40 +02:00
* Tcl_Preserve, thereby allowing a block of memory to be
2024-05-27 16:13:40 +02:00
* freed (if no one else cares about it).
*
* Results:
* None.
*
* Side effects:
2024-05-27 16:40:40 +02:00
* If Tcl_EventuallyFree has been called for clientData, and if
* no other call to Tcl_Preserve is still in effect, the block of
2024-05-27 16:13:40 +02:00
* memory is freed.
*
*----------------------------------------------------------------------
*/
void
2024-05-27 16:40:40 +02:00
Tcl_Release(clientData)
2024-05-27 16:13:40 +02:00
ClientData clientData; /* Pointer to malloc'ed block of memory. */
{
2024-05-27 16:40:40 +02:00
Reference *refPtr;
int mustFree;
Tcl_FreeProc *freeProc;
2024-05-27 16:13:40 +02:00
int i;
for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
if (refPtr->clientData != clientData) {
continue;
}
refPtr->refCount--;
if (refPtr->refCount == 0) {
2024-05-27 16:40:40 +02:00
/*
* 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.
*/
2024-05-27 16:13:40 +02:00
2024-05-27 16:40:40 +02:00
freeProc = refPtr->freeProc;
mustFree = refPtr->mustFree;
2024-05-27 16:13:40 +02:00
inUse--;
if (i < inUse) {
refArray[i] = refArray[inUse];
}
2024-05-27 16:40:40 +02:00
if (mustFree) {
if ((freeProc == TCL_DYNAMIC) ||
(freeProc == (Tcl_FreeProc *) free)) {
ckfree((char *) clientData);
} else {
(*freeProc)((char *) clientData);
}
}
2024-05-27 16:13:40 +02:00
}
return;
}
/*
* Reference not found. This is a bug in the caller.
*/
2024-05-27 16:40:40 +02:00
panic("Tcl_Release couldn't find reference for 0x%x", clientData);
2024-05-27 16:13:40 +02:00
}
/*
*----------------------------------------------------------------------
*
2024-05-27 16:40:40 +02:00
* Tcl_EventuallyFree --
2024-05-27 16:13:40 +02:00
*
2024-05-27 16:40:40 +02:00
* Free up a block of memory, unless a call to Tcl_Preserve is in
2024-05-27 16:13:40 +02:00
* effect for that block. In this case, defer the free until all
2024-05-27 16:40:40 +02:00
* calls to Tcl_Preserve have been undone by matching calls to
* Tcl_Release.
2024-05-27 16:13:40 +02:00
*
* Results:
* None.
*
* Side effects:
* Ptr may be released by calling free().
*
*----------------------------------------------------------------------
*/
void
2024-05-27 16:40:40 +02:00
Tcl_EventuallyFree(clientData, freeProc)
2024-05-27 16:13:40 +02:00
ClientData clientData; /* Pointer to malloc'ed block of memory. */
2024-05-27 16:40:40 +02:00
Tcl_FreeProc *freeProc; /* Procedure to actually do free. */
2024-05-27 16:13:40 +02:00
{
2024-05-27 16:40:40 +02:00
Reference *refPtr;
2024-05-27 16:13:40 +02:00
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!).
*/
for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
if (refPtr->clientData != clientData) {
continue;
}
if (refPtr->mustFree) {
2024-05-27 16:40:40 +02:00
panic("Tcl_EventuallyFree called twice for 0x%x\n", clientData);
2024-05-27 16:13:40 +02:00
}
refPtr->mustFree = 1;
refPtr->freeProc = freeProc;
return;
}
/*
* No reference for this block. Free it now.
*/
2024-05-27 16:40:40 +02:00
if ((freeProc == TCL_DYNAMIC)
|| (freeProc == (Tcl_FreeProc *) free)) {
2024-05-27 16:13:40 +02:00
ckfree((char *) clientData);
} else {
2024-05-27 16:40:40 +02:00
(*freeProc)((char *)clientData);
2024-05-27 16:13:40 +02:00
}
}