diff options
Diffstat (limited to 'generic/tclCkalloc.c')
-rw-r--r-- | generic/tclCkalloc.c | 154 |
1 files changed, 67 insertions, 87 deletions
diff --git a/generic/tclCkalloc.c b/generic/tclCkalloc.c index 5263e82..9a3b4e3 100644 --- a/generic/tclCkalloc.c +++ b/generic/tclCkalloc.c @@ -13,6 +13,8 @@ * this file, and for a DISCLAIMER OF ALL WARRANTIES. * * This code contributed by Karl Lehenbauer and Mark Diekhans + * + * RCS: @(#) $Id: tclCkalloc.c,v 1.36 2009/06/18 09:41:26 dkf Exp $ */ #include "tclInt.h" @@ -52,7 +54,7 @@ struct mem_header { struct mem_header *blink; MemTag *tagPtr; /* Tag from "memory tag" command; may be * NULL. */ - CONST char *file; + const char *file; long length; int line; unsigned char low_guard[LOW_GUARD_SIZE]; @@ -81,12 +83,12 @@ static struct mem_header *allocHead = NULL; /* List of allocated structures */ */ #define BODY_OFFSET \ - ((size_t) (&((struct mem_header *) 0)->body)) + ((unsigned long) (&((struct mem_header *) 0)->body)) static int total_mallocs = 0; static int total_frees = 0; -static size_t current_bytes_malloced = 0; -static size_t maximum_bytes_malloced = 0; +static int current_bytes_malloced = 0; +static int maximum_bytes_malloced = 0; static int current_malloc_packets = 0; static int maximum_malloc_packets = 0; static int break_on_malloc = 0; @@ -126,11 +128,11 @@ static int ckallocInit = 0; */ static int CheckmemCmd(ClientData clientData, Tcl_Interp *interp, - int argc, CONST char *argv[]); + int argc, const char *argv[]); static int MemoryCmd(ClientData clientData, Tcl_Interp *interp, - int argc, CONST char *argv[]); + int argc, const char *argv[]); static void ValidateMemory(struct mem_header *memHeaderP, - CONST char *file, int line, int nukeGuards); + const char *file, int line, int nukeGuards); /* *---------------------------------------------------------------------- @@ -150,10 +152,6 @@ TclInitDbCkalloc(void) if (!ckallocInit) { ckallocInit = 1; ckallocMutexPtr = Tcl_GetAllocMutex(); -#ifndef TCL_THREADS - /* Silence compiler warning */ - (void)ckallocMutexPtr; -#endif } } @@ -167,32 +165,22 @@ TclInitDbCkalloc(void) *---------------------------------------------------------------------- */ -int -TclDumpMemoryInfo(ClientData clientData, int flags) +void +TclDumpMemoryInfo( + FILE *outFile) { - char buf[1024]; - - if (clientData == NULL) { return 0; } - sprintf(buf, - "total mallocs %10d\n" - "total frees %10d\n" - "current packets allocated %10d\n" - "current bytes allocated %10lu\n" - "maximum packets allocated %10d\n" - "maximum bytes allocated %10lu\n", - total_mallocs, - total_frees, - current_malloc_packets, - (unsigned long)current_bytes_malloced, - maximum_malloc_packets, - (unsigned long)maximum_bytes_malloced); - if (flags == 0) { - fprintf((FILE *)clientData, buf); - } else { - /* Assume objPtr to append to */ - Tcl_AppendToObj((Tcl_Obj *) clientData, buf, -1); - } - return 1; + fprintf(outFile,"total mallocs %10d\n", + total_mallocs); + fprintf(outFile,"total frees %10d\n", + total_frees); + fprintf(outFile,"current packets allocated %10d\n", + current_malloc_packets); + fprintf(outFile,"current bytes allocated %10d\n", + current_bytes_malloced); + fprintf(outFile,"maximum packets allocated %10d\n", + maximum_malloc_packets); + fprintf(outFile,"maximum bytes allocated %10d\n", + maximum_bytes_malloced); } /* @@ -216,7 +204,7 @@ static void ValidateMemory( struct mem_header *memHeaderP, /* Memory chunk to validate */ - CONST char *file, /* File containing the call to + const char *file, /* File containing the call to * Tcl_ValidateAllMemory */ int line, /* Line number of call to * Tcl_ValidateAllMemory */ @@ -240,9 +228,9 @@ ValidateMemory( } } if (guard_failed) { - TclDumpMemoryInfo((ClientData) stderr, 0); + TclDumpMemoryInfo(stderr); fprintf(stderr, "low guard failed at %lx, %s %d\n", - (long unsigned int) memHeaderP->body, file, line); + (long unsigned) memHeaderP->body, file, line); fflush(stderr); /* In case name pointer is bad. */ fprintf(stderr, "%ld bytes allocated at (%s %d)\n", memHeaderP->length, memHeaderP->file, memHeaderP->line); @@ -262,9 +250,9 @@ ValidateMemory( } if (guard_failed) { - TclDumpMemoryInfo((ClientData) stderr, 0); + TclDumpMemoryInfo(stderr); fprintf(stderr, "high guard failed at %lx, %s %d\n", - (long unsigned int) memHeaderP->body, file, line); + (long unsigned) memHeaderP->body, file, line); fflush(stderr); /* In case name pointer is bad. */ fprintf(stderr, "%ld bytes allocated at (%s %d)\n", memHeaderP->length, memHeaderP->file, @@ -297,7 +285,7 @@ ValidateMemory( void Tcl_ValidateAllMemory( - CONST char *file, /* File from which Tcl_ValidateAllMemory was + const char *file, /* File from which Tcl_ValidateAllMemory was * called. */ int line) /* Line number of call to * Tcl_ValidateAllMemory */ @@ -331,7 +319,7 @@ Tcl_ValidateAllMemory( int Tcl_DumpActiveMemory( - CONST char *fileName) /* Name of the file to write info to */ + const char *fileName) /* Name of the file to write info to */ { FILE *fileP; struct mem_header *memScanP; @@ -348,10 +336,10 @@ Tcl_DumpActiveMemory( Tcl_MutexLock(ckallocMutexPtr); for (memScanP = allocHead; memScanP != NULL; memScanP = memScanP->flink) { - address = &memScanP->body [0]; + address = &memScanP->body[0]; fprintf(fileP, "%8lx - %8lx %7ld @ %s %d %s", - (long unsigned int) address, - (long unsigned int) address + memScanP->length - 1, + (long unsigned) address, + (long unsigned) address + memScanP->length - 1, memScanP->length, memScanP->file, memScanP->line, (memScanP->tagPtr == NULL) ? "" : memScanP->tagPtr->string); (void) fputc('\n', fileP); @@ -385,23 +373,20 @@ Tcl_DumpActiveMemory( char * Tcl_DbCkalloc( unsigned int size, - CONST char *file, + const char *file, int line) { - struct mem_header *result = NULL; + struct mem_header *result; if (validate_memory) { Tcl_ValidateAllMemory(file, line); } - /* Don't let size argument to TclpAlloc overflow */ - if (size <= UINT_MAX - HIGH_GUARD_SIZE -sizeof(struct mem_header)) { - result = (struct mem_header *) TclpAlloc((unsigned)size + - sizeof(struct mem_header) + HIGH_GUARD_SIZE); - } + result = (struct mem_header *) TclpAlloc((unsigned)size + + sizeof(struct mem_header) + HIGH_GUARD_SIZE); if (result == NULL) { fflush(stdout); - TclDumpMemoryInfo((ClientData) stderr, 0); + TclDumpMemoryInfo(stderr); Tcl_Panic("unable to alloc %u bytes, %s line %d", size, file, line); } @@ -479,23 +464,20 @@ Tcl_DbCkalloc( char * Tcl_AttemptDbCkalloc( unsigned int size, - CONST char *file, + const char *file, int line) { - struct mem_header *result = NULL; + struct mem_header *result; if (validate_memory) { Tcl_ValidateAllMemory(file, line); } - /* Don't let size argument to TclpAlloc overflow */ - if (size <= UINT_MAX - HIGH_GUARD_SIZE - sizeof(struct mem_header)) { - result = (struct mem_header *) TclpAlloc((unsigned)size + - sizeof(struct mem_header) + HIGH_GUARD_SIZE); - } + result = (struct mem_header *) TclpAlloc((unsigned)size + + sizeof(struct mem_header) + HIGH_GUARD_SIZE); if (result == NULL) { fflush(stdout); - TclDumpMemoryInfo((ClientData) stderr, 0); + TclDumpMemoryInfo(stderr); return NULL; } @@ -590,7 +572,7 @@ Tcl_AttemptDbCkalloc( void Tcl_DbCkfree( char *ptr, - CONST char *file, + const char *file, int line) { struct mem_header *memp; @@ -607,7 +589,7 @@ Tcl_DbCkfree( * words on these machines). */ - memp = (struct mem_header *) (((size_t) ptr) - BODY_OFFSET); + memp = (struct mem_header *) (((unsigned long) ptr) - BODY_OFFSET); if (alloc_tracing) { fprintf(stderr, "ckfree %lx %ld %s %d\n", @@ -669,7 +651,7 @@ char * Tcl_DbCkrealloc( char *ptr, unsigned int size, - CONST char *file, + const char *file, int line) { char *newPtr; @@ -684,7 +666,7 @@ Tcl_DbCkrealloc( * See comment from Tcl_DbCkfree before you change the following line. */ - memp = (struct mem_header *) (((size_t) ptr) - BODY_OFFSET); + memp = (struct mem_header *) (((unsigned long) ptr) - BODY_OFFSET); copySize = size; if (copySize > (unsigned int) memp->length) { @@ -700,7 +682,7 @@ char * Tcl_AttemptDbCkrealloc( char *ptr, unsigned int size, - CONST char *file, + const char *file, int line) { char *newPtr; @@ -715,7 +697,7 @@ Tcl_AttemptDbCkrealloc( * See comment from Tcl_DbCkfree before you change the following line. */ - memp = (struct mem_header *) (((size_t) ptr) - BODY_OFFSET); + memp = (struct mem_header *) (((unsigned long) ptr) - BODY_OFFSET); copySize = size; if (copySize > (unsigned int) memp->length) { @@ -818,13 +800,12 @@ MemoryCmd( ClientData clientData, Tcl_Interp *interp, int argc, - CONST char *argv[]) + const char *argv[]) { - CONST char *fileName; + const char *fileName; FILE *fileP; Tcl_DString buffer; int result; - size_t len; if (argc < 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], @@ -842,7 +823,7 @@ MemoryCmd( if (fileName == NULL) { return TCL_ERROR; } - result = Tcl_DumpActiveMemory (fileName); + result = Tcl_DumpActiveMemory(fileName); Tcl_DStringFree(&buffer); if (result != TCL_OK) { Tcl_AppendResult(interp, "error accessing ", argv[2], NULL); @@ -861,7 +842,7 @@ MemoryCmd( } if (strcmp(argv[1],"info") == 0) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( - "%-25s %10d\n%-25s %10d\n%-25s %10d\n%-25s %10lu\n%-25s %10d\n%-25s %10lu\n", + "%-25s %10d\n%-25s %10d\n%-25s %10d\n%-25s %10d\n%-25s %10d\n%-25s %10d\n", "total mallocs", total_mallocs, "total frees", total_frees, "current packets allocated", current_malloc_packets, "current bytes allocated", current_bytes_malloced, @@ -920,10 +901,9 @@ MemoryCmd( if ((curTagPtr != NULL) && (curTagPtr->refCount == 0)) { TclpFree((char *) curTagPtr); } - len = strlen(argv[2]); - curTagPtr = (MemTag *) TclpAlloc(TAG_SIZE(len)); + curTagPtr = (MemTag *) TclpAlloc(TAG_SIZE(strlen(argv[2]))); curTagPtr->refCount = 0; - memcpy(curTagPtr->string, argv[2], len + 1); + strcpy(curTagPtr->string, argv[2]); return TCL_OK; } if (strcmp(argv[1],"trace") == 0) { @@ -990,7 +970,7 @@ CheckmemCmd( ClientData clientData, /* Not used. */ Tcl_Interp *interp, /* Interpreter for evaluation. */ int argc, /* Number of arguments. */ - CONST char *argv[]) /* String values of arguments. */ + const char *argv[]) /* String values of arguments. */ { if (argc != 2) { Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], @@ -1076,7 +1056,7 @@ Tcl_Alloc( char * Tcl_DbCkalloc( unsigned int size, - CONST char *file, + const char *file, int line) { char *result; @@ -1114,7 +1094,7 @@ Tcl_AttemptAlloc( char * Tcl_AttemptDbCkalloc( unsigned int size, - CONST char *file, + const char *file, int line) { char *result; @@ -1153,7 +1133,7 @@ char * Tcl_DbCkrealloc( char *ptr, unsigned int size, - CONST char *file, + const char *file, int line) { char *result; @@ -1193,7 +1173,7 @@ char * Tcl_AttemptDbCkrealloc( char *ptr, unsigned int size, - CONST char *file, + const char *file, int line) { char *result; @@ -1224,7 +1204,7 @@ Tcl_Free( void Tcl_DbCkfree( char *ptr, - CONST char *file, + const char *file, int line) { TclpFree(ptr); @@ -1249,22 +1229,22 @@ Tcl_InitMemory( int Tcl_DumpActiveMemory( - CONST char *fileName) + const char *fileName) { return TCL_OK; } void Tcl_ValidateAllMemory( - CONST char *file, + const char *file, int line) { } -int -TclDumpMemoryInfo(ClientData clientData, int flags) +void +TclDumpMemoryInfo( + FILE *outFile) { - return 1; } #endif /* TCL_MEM_DEBUG */ |