summaryrefslogtreecommitdiffstats
path: root/generic/tclPreserve.c
diff options
context:
space:
mode:
Diffstat (limited to 'generic/tclPreserve.c')
-rw-r--r--generic/tclPreserve.c103
1 files changed, 43 insertions, 60 deletions
diff --git a/generic/tclPreserve.c b/generic/tclPreserve.c
index 8281bc4..0bd8f93 100644
--- a/generic/tclPreserve.c
+++ b/generic/tclPreserve.c
@@ -10,8 +10,6 @@
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
- *
- * RCS: @(#) $Id: tclPreserve.c,v 1.7 2005/07/24 22:56:43 dkf Exp $
*/
#include "tclInt.h"
@@ -38,7 +36,7 @@ typedef struct {
* These variables are protected by "preserveMutex".
*/
-static Reference *refArray; /* First in array of references. */
+static Reference *refArray = NULL; /* 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
@@ -56,12 +54,12 @@ TCL_DECLARE_MUTEX(preserveMutex)/* To protect the above statics */
*/
typedef struct HandleStruct {
- VOID *ptr; /* Pointer to the memory block being tracked.
+ void *ptr; /* Pointer to the memory block being tracked.
* This field will become NULL when the memory
* block is deleted. This field must be the
* first in the structure. */
#ifdef TCL_MEM_DEBUG
- VOID *ptr2; /* Backup copy of the above pointer used to
+ void *ptr2; /* Backup copy of the above pointer used to
* ensure that the contents of the handle are
* not changed by anyone else. */
#endif
@@ -87,14 +85,14 @@ typedef struct HandleStruct {
/* ARGSUSED */
void
-TclFinalizePreserve()
+TclFinalizePreserve(void)
{
Tcl_MutexLock(&preserveMutex);
if (spaceAvl != 0) {
- ckfree((char *) refArray);
- refArray = (Reference *) NULL;
- inUse = 0;
- spaceAvl = 0;
+ ckfree(refArray);
+ refArray = NULL;
+ inUse = 0;
+ spaceAvl = 0;
}
Tcl_MutexUnlock(&preserveMutex);
}
@@ -119,8 +117,8 @@ TclFinalizePreserve()
*/
void
-Tcl_Preserve(clientData)
- ClientData clientData; /* Pointer to malloc'ed block of memory. */
+Tcl_Preserve(
+ ClientData clientData) /* Pointer to malloc'ed block of memory. */
{
Reference *refPtr;
int i;
@@ -145,21 +143,8 @@ Tcl_Preserve(clientData)
*/
if (inUse == spaceAvl) {
- if (spaceAvl == 0) {
- refArray = (Reference *) ckalloc((unsigned)
- (INITIAL_SIZE*sizeof(Reference)));
- spaceAvl = INITIAL_SIZE;
- } else {
- Reference *new;
-
- new = (Reference *) ckalloc((unsigned)
- (2*spaceAvl*sizeof(Reference)));
- memcpy((VOID *) new, (VOID *) refArray,
- spaceAvl*sizeof(Reference));
- ckfree((char *) refArray);
- refArray = new;
- spaceAvl *= 2;
- }
+ spaceAvl = spaceAvl ? 2*spaceAvl : INITIAL_SIZE;
+ refArray = ckrealloc(refArray, spaceAvl * sizeof(Reference));
}
/*
@@ -195,8 +180,8 @@ Tcl_Preserve(clientData)
*/
void
-Tcl_Release(clientData)
- ClientData clientData; /* Pointer to malloc'ed block of memory. */
+Tcl_Release(
+ ClientData clientData) /* Pointer to malloc'ed block of memory. */
{
Reference *refPtr;
int i;
@@ -239,9 +224,9 @@ Tcl_Release(clientData)
Tcl_MutexUnlock(&preserveMutex);
if (mustFree) {
if (freeProc == TCL_DYNAMIC) {
- ckfree((char *) clientData);
+ ckfree(clientData);
} else {
- (*freeProc)((char *) clientData);
+ freeProc(clientData);
}
}
return;
@@ -252,7 +237,7 @@ Tcl_Release(clientData)
* Reference not found. This is a bug in the caller.
*/
- Tcl_Panic("Tcl_Release couldn't find reference for 0x%x", clientData);
+ Tcl_Panic("Tcl_Release couldn't find reference for %p", clientData);
}
/*
@@ -274,9 +259,9 @@ Tcl_Release(clientData)
*/
void
-Tcl_EventuallyFree(clientData, freeProc)
- ClientData clientData; /* Pointer to malloc'ed block of memory. */
- Tcl_FreeProc *freeProc; /* Function to actually do free. */
+Tcl_EventuallyFree(
+ ClientData clientData, /* Pointer to malloc'ed block of memory. */
+ Tcl_FreeProc *freeProc) /* Function to actually do free. */
{
Reference *refPtr;
int i;
@@ -292,13 +277,12 @@ Tcl_EventuallyFree(clientData, freeProc)
continue;
}
if (refPtr->mustFree) {
- Tcl_Panic("Tcl_EventuallyFree called twice for 0x%x\n",
- clientData);
- }
- refPtr->mustFree = 1;
+ Tcl_Panic("Tcl_EventuallyFree called twice for %p", clientData);
+ }
+ refPtr->mustFree = 1;
refPtr->freeProc = freeProc;
Tcl_MutexUnlock(&preserveMutex);
- return;
+ return;
}
Tcl_MutexUnlock(&preserveMutex);
@@ -307,9 +291,9 @@ Tcl_EventuallyFree(clientData, freeProc)
*/
if (freeProc == TCL_DYNAMIC) {
- ckfree((char *) clientData);
+ ckfree(clientData);
} else {
- (*freeProc)((char *)clientData);
+ freeProc(clientData);
}
}
@@ -338,14 +322,13 @@ Tcl_EventuallyFree(clientData, freeProc)
*/
TclHandle
-TclHandleCreate(ptr)
- VOID *ptr; /* Pointer to an arbitrary block of memory to
+TclHandleCreate(
+ void *ptr) /* Pointer to an arbitrary block of memory to
* be tracked for deletion. Must not be
* NULL. */
{
- HandleStruct *handlePtr;
+ HandleStruct *handlePtr = ckalloc(sizeof(HandleStruct));
- handlePtr = (HandleStruct *) ckalloc(sizeof(HandleStruct));
handlePtr->ptr = ptr;
#ifdef TCL_MEM_DEBUG
handlePtr->ptr2 = ptr;
@@ -374,8 +357,8 @@ TclHandleCreate(ptr)
*/
void
-TclHandleFree(handle)
- TclHandle handle; /* Previously created handle associated with a
+TclHandleFree(
+ TclHandle handle) /* Previously created handle associated with a
* malloc'd block that is being deleted. The
* handle is modified so that doubly
* dereferencing it will give NULL. */
@@ -385,16 +368,16 @@ TclHandleFree(handle)
handlePtr = (HandleStruct *) handle;
#ifdef TCL_MEM_DEBUG
if (handlePtr->refCount == 0x61616161) {
- Tcl_Panic("using previously disposed TclHandle %x", handlePtr);
+ Tcl_Panic("using previously disposed TclHandle %p", handlePtr);
}
if (handlePtr->ptr2 != handlePtr->ptr) {
- Tcl_Panic("someone has changed the block referenced by the handle %x\nfrom %x to %x",
+ Tcl_Panic("someone has changed the block referenced by the handle %p\nfrom %p to %p",
handlePtr, handlePtr->ptr2, handlePtr->ptr);
}
#endif
handlePtr->ptr = NULL;
if (handlePtr->refCount == 0) {
- ckfree((char *) handlePtr);
+ ckfree(handlePtr);
}
}
@@ -419,8 +402,8 @@ TclHandleFree(handle)
*/
TclHandle
-TclHandlePreserve(handle)
- TclHandle handle; /* Declare an interest in the block of memory
+TclHandlePreserve(
+ TclHandle handle) /* Declare an interest in the block of memory
* referenced by this handle. */
{
HandleStruct *handlePtr;
@@ -428,10 +411,10 @@ TclHandlePreserve(handle)
handlePtr = (HandleStruct *) handle;
#ifdef TCL_MEM_DEBUG
if (handlePtr->refCount == 0x61616161) {
- Tcl_Panic("using previously disposed TclHandle %x", handlePtr);
+ Tcl_Panic("using previously disposed TclHandle %p", handlePtr);
}
if ((handlePtr->ptr != NULL) && (handlePtr->ptr != handlePtr->ptr2)) {
- Tcl_Panic("someone has changed the block referenced by the handle %x\nfrom %x to %x",
+ Tcl_Panic("someone has changed the block referenced by the handle %p\nfrom %p to %p",
handlePtr, handlePtr->ptr2, handlePtr->ptr);
}
#endif
@@ -460,8 +443,8 @@ TclHandlePreserve(handle)
*/
void
-TclHandleRelease(handle)
- TclHandle handle; /* Unregister interest in the block of memory
+TclHandleRelease(
+ TclHandle handle) /* Unregister interest in the block of memory
* referenced by this handle. */
{
HandleStruct *handlePtr;
@@ -469,16 +452,16 @@ TclHandleRelease(handle)
handlePtr = (HandleStruct *) handle;
#ifdef TCL_MEM_DEBUG
if (handlePtr->refCount == 0x61616161) {
- Tcl_Panic("using previously disposed TclHandle %x", handlePtr);
+ Tcl_Panic("using previously disposed TclHandle %p", handlePtr);
}
if ((handlePtr->ptr != NULL) && (handlePtr->ptr != handlePtr->ptr2)) {
- Tcl_Panic("someone has changed the block referenced by the handle %x\nfrom %x to %x",
+ Tcl_Panic("someone has changed the block referenced by the handle %p\nfrom %p to %p",
handlePtr, handlePtr->ptr2, handlePtr->ptr);
}
#endif
handlePtr->refCount--;
if ((handlePtr->refCount == 0) && (handlePtr->ptr == NULL)) {
- ckfree((char *) handlePtr);
+ ckfree(handlePtr);
}
}