OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [generic/] [tclPreserve.c] - Diff between revs 578 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 578 Rev 1765
/*
/*
 * tclPreserve.c --
 * tclPreserve.c --
 *
 *
 *      This file contains a collection of procedures that are used
 *      This file contains a collection of procedures that are used
 *      to make sure that widget records and other data structures
 *      to make sure that widget records and other data structures
 *      aren't reallocated when there are nested procedures that
 *      aren't reallocated when there are nested procedures that
 *      depend on their existence.
 *      depend on their existence.
 *
 *
 * Copyright (c) 1991-1994 The Regents of the University of California.
 * Copyright (c) 1991-1994 The Regents of the University of California.
 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
 *
 *
 * See the file "license.terms" for information on usage and redistribution
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 *
 * RCS: @(#) $Id: tclPreserve.c,v 1.1.1.1 2002-01-16 10:25:28 markom Exp $
 * RCS: @(#) $Id: tclPreserve.c,v 1.1.1.1 2002-01-16 10:25:28 markom Exp $
 */
 */
 
 
#include "tclInt.h"
#include "tclInt.h"
 
 
/*
/*
 * The following data structure is used to keep track of all the
 * The following data structure is used to keep track of all the
 * Tcl_Preserve calls that are still in effect.  It grows as needed
 * Tcl_Preserve calls that are still in effect.  It grows as needed
 * to accommodate any number of calls in effect.
 * to accommodate any number of calls in effect.
 */
 */
 
 
typedef struct {
typedef struct {
    ClientData clientData;      /* Address of preserved block. */
    ClientData clientData;      /* Address of preserved block. */
    int refCount;               /* Number of Tcl_Preserve calls in effect
    int refCount;               /* Number of Tcl_Preserve calls in effect
                                 * for block. */
                                 * for block. */
    int mustFree;               /* Non-zero means Tcl_EventuallyFree was
    int mustFree;               /* Non-zero means Tcl_EventuallyFree was
                                 * called while a Tcl_Preserve call was in
                                 * called while a Tcl_Preserve call was in
                                 * effect, so the structure must be freed
                                 * effect, so the structure must be freed
                                 * when refCount becomes zero. */
                                 * when refCount becomes zero. */
    Tcl_FreeProc *freeProc;     /* Procedure to call to free. */
    Tcl_FreeProc *freeProc;     /* Procedure to call to free. */
} Reference;
} Reference;
 
 
static Reference *refArray;     /* First in array of references. */
static Reference *refArray;     /* First in array of references. */
static int spaceAvl = 0; /* Total number of structures available
static int spaceAvl = 0; /* Total number of structures available
                                 * at *firstRefPtr. */
                                 * at *firstRefPtr. */
static int inUse = 0;            /* Count of structures currently in use
static int inUse = 0;            /* Count of structures currently in use
                                 * in refArray. */
                                 * in refArray. */
#define INITIAL_SIZE 2
#define INITIAL_SIZE 2
 
 
/*
/*
 * Static routines in this file:
 * Static routines in this file:
 */
 */
 
 
static void     PreserveExitProc _ANSI_ARGS_((ClientData clientData));
static void     PreserveExitProc _ANSI_ARGS_((ClientData clientData));
 
 


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * PreserveExitProc --
 * PreserveExitProc --
 *
 *
 *      Called during exit processing to clean up the reference array.
 *      Called during exit processing to clean up the reference array.
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      Frees the storage of the reference array.
 *      Frees the storage of the reference array.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
        /* ARGSUSED */
        /* ARGSUSED */
static void
static void
PreserveExitProc(clientData)
PreserveExitProc(clientData)
    ClientData clientData;              /* NULL -Unused. */
    ClientData clientData;              /* NULL -Unused. */
{
{
    if (spaceAvl != 0) {
    if (spaceAvl != 0) {
        ckfree((char *) refArray);
        ckfree((char *) refArray);
        refArray = (Reference *) NULL;
        refArray = (Reference *) NULL;
        inUse = 0;
        inUse = 0;
        spaceAvl = 0;
        spaceAvl = 0;
    }
    }
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * Tcl_Preserve --
 * Tcl_Preserve --
 *
 *
 *      This procedure is used by a procedure to declare its interest
 *      This procedure is used by a procedure to declare its interest
 *      in a particular block of memory, so that the block will not be
 *      in a particular block of memory, so that the block will not be
 *      reallocated until a matching call to Tcl_Release has been made.
 *      reallocated until a matching call to Tcl_Release has been made.
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      Information is retained so that the block of memory will
 *      Information is retained so that the block of memory will
 *      not be freed until at least the matching call to Tcl_Release.
 *      not be freed until at least the matching call to Tcl_Release.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
void
void
Tcl_Preserve(clientData)
Tcl_Preserve(clientData)
    ClientData clientData;      /* Pointer to malloc'ed block of memory. */
    ClientData clientData;      /* Pointer to malloc'ed block of memory. */
{
{
    Reference *refPtr;
    Reference *refPtr;
    int i;
    int i;
 
 
    /*
    /*
     * See if there is already a reference for this pointer.  If so,
     * See if there is already a reference for this pointer.  If so,
     * just increment its reference count.
     * just increment its reference count.
     */
     */
 
 
    for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
    for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
        if (refPtr->clientData == clientData) {
        if (refPtr->clientData == clientData) {
            refPtr->refCount++;
            refPtr->refCount++;
            return;
            return;
        }
        }
    }
    }
 
 
    /*
    /*
     * Make a reference array if it doesn't already exist, or make it
     * Make a reference array if it doesn't already exist, or make it
     * bigger if it is full.
     * bigger if it is full.
     */
     */
 
 
    if (inUse == spaceAvl) {
    if (inUse == spaceAvl) {
        if (spaceAvl == 0) {
        if (spaceAvl == 0) {
            Tcl_CreateExitHandler((Tcl_ExitProc *) PreserveExitProc,
            Tcl_CreateExitHandler((Tcl_ExitProc *) PreserveExitProc,
                    (ClientData) NULL);
                    (ClientData) NULL);
            refArray = (Reference *) ckalloc((unsigned)
            refArray = (Reference *) ckalloc((unsigned)
                    (INITIAL_SIZE*sizeof(Reference)));
                    (INITIAL_SIZE*sizeof(Reference)));
            spaceAvl = INITIAL_SIZE;
            spaceAvl = INITIAL_SIZE;
        } else {
        } else {
            Reference *new;
            Reference *new;
 
 
            new = (Reference *) ckalloc((unsigned)
            new = (Reference *) ckalloc((unsigned)
                    (2*spaceAvl*sizeof(Reference)));
                    (2*spaceAvl*sizeof(Reference)));
            memcpy((VOID *) new, (VOID *) refArray,
            memcpy((VOID *) new, (VOID *) refArray,
                    spaceAvl*sizeof(Reference));
                    spaceAvl*sizeof(Reference));
            ckfree((char *) refArray);
            ckfree((char *) refArray);
            refArray = new;
            refArray = new;
            spaceAvl *= 2;
            spaceAvl *= 2;
        }
        }
    }
    }
 
 
    /*
    /*
     * Make a new entry for the new reference.
     * Make a new entry for the new reference.
     */
     */
 
 
    refPtr = &refArray[inUse];
    refPtr = &refArray[inUse];
    refPtr->clientData = clientData;
    refPtr->clientData = clientData;
    refPtr->refCount = 1;
    refPtr->refCount = 1;
    refPtr->mustFree = 0;
    refPtr->mustFree = 0;
    refPtr->freeProc = TCL_STATIC;
    refPtr->freeProc = TCL_STATIC;
    inUse += 1;
    inUse += 1;
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * Tcl_Release --
 * Tcl_Release --
 *
 *
 *      This procedure is called to cancel a previous call to
 *      This procedure is called to cancel a previous call to
 *      Tcl_Preserve, thereby allowing a block of memory to be
 *      Tcl_Preserve, thereby allowing a block of memory to be
 *      freed (if no one else cares about it).
 *      freed (if no one else cares about it).
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      If Tcl_EventuallyFree has been called for clientData, and if
 *      If Tcl_EventuallyFree has been called for clientData, and if
 *      no other call to Tcl_Preserve is still in effect, the block of
 *      no other call to Tcl_Preserve is still in effect, the block of
 *      memory is freed.
 *      memory is freed.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
void
void
Tcl_Release(clientData)
Tcl_Release(clientData)
    ClientData clientData;      /* Pointer to malloc'ed block of memory. */
    ClientData clientData;      /* Pointer to malloc'ed block of memory. */
{
{
    Reference *refPtr;
    Reference *refPtr;
    int mustFree;
    int mustFree;
    Tcl_FreeProc *freeProc;
    Tcl_FreeProc *freeProc;
    int i;
    int i;
 
 
    for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
    for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
        if (refPtr->clientData != clientData) {
        if (refPtr->clientData != clientData) {
            continue;
            continue;
        }
        }
        refPtr->refCount--;
        refPtr->refCount--;
        if (refPtr->refCount == 0) {
        if (refPtr->refCount == 0) {
 
 
            /*
            /*
             * Must remove information from the slot before calling freeProc
             * Must remove information from the slot before calling freeProc
             * to avoid reentrancy problems if the freeProc calls Tcl_Preserve
             * to avoid reentrancy problems if the freeProc calls Tcl_Preserve
             * on the same clientData. Copy down the last reference in the
             * on the same clientData. Copy down the last reference in the
             * array to overwrite the current slot.
             * array to overwrite the current slot.
             */
             */
 
 
            freeProc = refPtr->freeProc;
            freeProc = refPtr->freeProc;
            mustFree = refPtr->mustFree;
            mustFree = refPtr->mustFree;
            inUse--;
            inUse--;
            if (i < inUse) {
            if (i < inUse) {
                refArray[i] = refArray[inUse];
                refArray[i] = refArray[inUse];
            }
            }
            if (mustFree) {
            if (mustFree) {
                if ((freeProc == TCL_DYNAMIC) ||
                if ((freeProc == TCL_DYNAMIC) ||
                        (freeProc == (Tcl_FreeProc *) free)) {
                        (freeProc == (Tcl_FreeProc *) free)) {
                    ckfree((char *) clientData);
                    ckfree((char *) clientData);
                } else {
                } else {
                    (*freeProc)((char *) clientData);
                    (*freeProc)((char *) clientData);
                }
                }
            }
            }
        }
        }
        return;
        return;
    }
    }
 
 
    /*
    /*
     * Reference not found.  This is a bug in the caller.
     * Reference not found.  This is a bug in the caller.
     */
     */
 
 
    panic("Tcl_Release couldn't find reference for 0x%x", clientData);
    panic("Tcl_Release couldn't find reference for 0x%x", clientData);
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * Tcl_EventuallyFree --
 * Tcl_EventuallyFree --
 *
 *
 *      Free up a block of memory, unless a call to Tcl_Preserve is in
 *      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
 *      effect for that block.  In this case, defer the free until all
 *      calls to Tcl_Preserve have been undone by matching calls to
 *      calls to Tcl_Preserve have been undone by matching calls to
 *      Tcl_Release.
 *      Tcl_Release.
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      Ptr may be released by calling free().
 *      Ptr may be released by calling free().
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
void
void
Tcl_EventuallyFree(clientData, freeProc)
Tcl_EventuallyFree(clientData, freeProc)
    ClientData clientData;      /* Pointer to malloc'ed block of memory. */
    ClientData clientData;      /* Pointer to malloc'ed block of memory. */
    Tcl_FreeProc *freeProc;     /* Procedure to actually do free. */
    Tcl_FreeProc *freeProc;     /* Procedure to actually do free. */
{
{
    Reference *refPtr;
    Reference *refPtr;
    int i;
    int i;
 
 
    /*
    /*
     * See if there is a reference for this pointer.  If so, set its
     * See if there is a reference for this pointer.  If so, set its
     * "mustFree" flag (the flag had better not be set already!).
     * "mustFree" flag (the flag had better not be set already!).
     */
     */
 
 
    for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
    for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
        if (refPtr->clientData != clientData) {
        if (refPtr->clientData != clientData) {
            continue;
            continue;
        }
        }
        if (refPtr->mustFree) {
        if (refPtr->mustFree) {
            panic("Tcl_EventuallyFree called twice for 0x%x\n", clientData);
            panic("Tcl_EventuallyFree called twice for 0x%x\n", clientData);
        }
        }
        refPtr->mustFree = 1;
        refPtr->mustFree = 1;
        refPtr->freeProc = freeProc;
        refPtr->freeProc = freeProc;
        return;
        return;
    }
    }
 
 
    /*
    /*
     * No reference for this block.  Free it now.
     * No reference for this block.  Free it now.
     */
     */
 
 
    if ((freeProc == TCL_DYNAMIC)
    if ((freeProc == TCL_DYNAMIC)
            || (freeProc == (Tcl_FreeProc *) free)) {
            || (freeProc == (Tcl_FreeProc *) free)) {
        ckfree((char *) clientData);
        ckfree((char *) clientData);
    } else {
    } else {
        (*freeProc)((char *)clientData);
        (*freeProc)((char *)clientData);
    }
    }
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.