diff options
Diffstat (limited to 'generic/tclAlloc.c')
| -rw-r--r-- | generic/tclAlloc.c | 127 |
1 files changed, 63 insertions, 64 deletions
diff --git a/generic/tclAlloc.c b/generic/tclAlloc.c index 800b0ae..675e1a4 100644 --- a/generic/tclAlloc.c +++ b/generic/tclAlloc.c @@ -6,9 +6,9 @@ * that don't exactly fit are passed up to the next larger size. Blocks * over a certain size are directly allocated from the system. * - * Copyright © 1983 Regents of the University of California. - * Copyright © 1996-1997 Sun Microsystems, Inc. - * Copyright © 1998-1999 Scriptics Corporation. + * Copyright (c) 1983 Regents of the University of California. + * Copyright (c) 1996-1997 Sun Microsystems, Inc. + * Copyright (c) 1998-1999 by Scriptics Corporation. * * Portions contributed by Chris Kingsley, Jack Jansen and Ray Johnson. * @@ -22,17 +22,17 @@ */ #include "tclInt.h" -#if !TCL_THREADS || !defined(USE_THREAD_ALLOC) +#if !defined(TCL_THREADS) || !defined(USE_THREAD_ALLOC) -#if defined(USE_TCLALLOC) && USE_TCLALLOC +#if USE_TCLALLOC /* * We should really make use of AC_CHECK_TYPE(caddr_t) here, but it can wait * until Tcl uses config.h properly. */ -#if defined(_MSC_VER) || defined(__MSVCRT__) -typedef size_t caddr_t; +#if defined(_MSC_VER) || defined(__MSVCRT__) || defined(__BORLANDC__) +typedef unsigned long caddr_t; #endif /* @@ -56,7 +56,7 @@ union overhead { unsigned char magic1; /* other magic number */ #ifndef NDEBUG unsigned short rmagic; /* range magic number */ - size_t size; /* actual block size */ + unsigned long size; /* actual block size */ unsigned short unused2; /* padding to 8-byte align */ #endif } ovu; @@ -68,11 +68,11 @@ union overhead { }; -#define MAGIC 0xEF /* magic # on accounting info */ +#define MAGIC 0xef /* magic # on accounting info */ #define RMAGIC 0x5555 /* magic # on range info */ #ifndef NDEBUG -#define RSLOP sizeof(unsigned short) +#define RSLOP sizeof (unsigned short) #else #define RSLOP 0 #endif @@ -94,7 +94,7 @@ union overhead { #define MINBLOCK ((sizeof(union overhead) + (TCL_ALLOCALIGN-1)) & ~(TCL_ALLOCALIGN-1)) #define NBUCKETS (13 - (MINBLOCK >> 4)) -#define MAXMALLOC ((size_t)1 << (NBUCKETS+2)) +#define MAXMALLOC (1<<(NBUCKETS+2)) static union overhead *nextf[NBUCKETS]; /* @@ -116,12 +116,12 @@ static struct block bigBlocks={ /* Big blocks aren't suballocated. */ /* * The allocator is protected by a special mutex that must be explicitly - * initialized. Furthermore, because Tcl_Alloc may be used before anything else + * initialized. Futhermore, because Tcl_Alloc may be used before anything else * in Tcl, we make this module self-initializing after all with the allocInit * variable. */ -#if TCL_THREADS +#ifdef TCL_THREADS static Tcl_Mutex *allocMutexPtr; #endif static int allocInit = 0; @@ -133,7 +133,8 @@ static int allocInit = 0; * a given block size. */ -static size_t numMallocs[NBUCKETS+1]; +static unsigned int numMallocs[NBUCKETS+1]; +#include <stdio.h> #endif #if !defined(NDEBUG) @@ -148,7 +149,7 @@ static size_t numMallocs[NBUCKETS+1]; * Prototypes for functions used only in this file. */ -static void MoreCore(size_t bucket); +static void MoreCore(int bucket); /* *------------------------------------------------------------------------- @@ -171,7 +172,7 @@ TclInitAlloc(void) { if (!allocInit) { allocInit = 1; -#if TCL_THREADS +#ifdef TCL_THREADS allocMutexPtr = Tcl_GetAllocMutex(); #endif } @@ -249,13 +250,13 @@ TclFinalizeAllocSubsystem(void) *---------------------------------------------------------------------- */ -void * +char * TclpAlloc( unsigned int numBytes) /* Number of bytes to allocate. */ { - union overhead *overPtr; - size_t bucket; - unsigned amount; + register union overhead *overPtr; + register long bucket; + register unsigned amount; struct block *bigBlockPtr = NULL; if (!allocInit) { @@ -274,8 +275,8 @@ TclpAlloc( if (numBytes >= MAXMALLOC - OVERHEAD) { if (numBytes <= UINT_MAX - OVERHEAD -sizeof(struct block)) { - bigBlockPtr = (struct block *) TclpSysAlloc( - sizeof(struct block) + OVERHEAD + numBytes, 0); + bigBlockPtr = (struct block *) TclpSysAlloc((unsigned) + (sizeof(struct block) + OVERHEAD + numBytes), 0); } if (bigBlockPtr == NULL) { Tcl_MutexUnlock(allocMutexPtr); @@ -288,7 +289,7 @@ TclpAlloc( overPtr = (union overhead *) (bigBlockPtr + 1); overPtr->overMagic0 = overPtr->overMagic1 = MAGIC; - overPtr->bucketIndex = 0xFF; + overPtr->bucketIndex = 0xff; #ifdef MSTATS numMallocs[NBUCKETS]++; #endif @@ -304,7 +305,7 @@ TclpAlloc( #endif Tcl_MutexUnlock(allocMutexPtr); - return (char *)(overPtr+1); + return (void *)(overPtr+1); } /* @@ -345,7 +346,7 @@ TclpAlloc( nextf[bucket] = overPtr->next; overPtr->overMagic0 = overPtr->overMagic1 = MAGIC; - overPtr->bucketIndex = UCHAR(bucket); + overPtr->bucketIndex = (unsigned char) bucket; #ifdef MSTATS numMallocs[bucket]++; @@ -385,12 +386,12 @@ TclpAlloc( static void MoreCore( - size_t bucket) /* What bucket to allocate to. */ + int bucket) /* What bucket to allocat to. */ { - union overhead *overPtr; - size_t size; /* size of desired block */ - size_t amount; /* amount to allocate */ - size_t numBlocks; /* how many blocks we get */ + register union overhead *overPtr; + register long size; /* size of desired block */ + long amount; /* amount to allocate */ + int numBlocks; /* how many blocks we get */ struct block *blockPtr; /* @@ -398,14 +399,14 @@ MoreCore( * VAX, I think) or for a negative arg. */ - size = ((size_t)1) << (bucket + 3); + size = 1 << (bucket + 3); ASSERT(size > 0); amount = MAXMALLOC; numBlocks = amount / size; ASSERT(numBlocks*size == amount); - blockPtr = (struct block *) TclpSysAlloc( + blockPtr = (struct block *) TclpSysAlloc((unsigned) (sizeof(struct block) + amount), 1); /* no more room! */ if (blockPtr == NULL) { @@ -446,10 +447,10 @@ MoreCore( void TclpFree( - void *oldPtr) /* Pointer to memory to free. */ + char *oldPtr) /* Pointer to memory to free. */ { - size_t size; - union overhead *overPtr; + register long size; + register union overhead *overPtr; struct block *bigBlockPtr; if (oldPtr == NULL) { @@ -457,7 +458,7 @@ TclpFree( } Tcl_MutexLock(allocMutexPtr); - overPtr = (union overhead *)((caddr_t)oldPtr - sizeof(union overhead)); + overPtr = (union overhead *)((caddr_t)oldPtr - sizeof (union overhead)); ASSERT(overPtr->overMagic0 == MAGIC); /* make sure it was in use */ ASSERT(overPtr->overMagic1 == MAGIC); @@ -469,7 +470,7 @@ TclpFree( RANGE_ASSERT(overPtr->rangeCheckMagic == RMAGIC); RANGE_ASSERT(BLOCK_END(overPtr) == RMAGIC); size = overPtr->bucketIndex; - if (size == 0xFF) { + if (size == 0xff) { #ifdef MSTATS numMallocs[NBUCKETS]--; #endif @@ -509,16 +510,16 @@ TclpFree( *---------------------------------------------------------------------- */ -void * +char * TclpRealloc( - void *oldPtr, /* Pointer to alloc'ed block. */ + char *oldPtr, /* Pointer to alloced block. */ unsigned int numBytes) /* New size of memory. */ { int i; union overhead *overPtr; struct block *bigBlockPtr; int expensive; - size_t maxSize; + unsigned long maxSize; if (oldPtr == NULL) { return TclpAlloc(numBytes); @@ -526,7 +527,7 @@ TclpRealloc( Tcl_MutexLock(allocMutexPtr); - overPtr = (union overhead *)((caddr_t)oldPtr - sizeof(union overhead)); + overPtr = (union overhead *)((caddr_t)oldPtr - sizeof (union overhead)); ASSERT(overPtr->overMagic0 == MAGIC); /* make sure it was in use */ ASSERT(overPtr->overMagic1 == MAGIC); @@ -543,7 +544,7 @@ TclpRealloc( * If the block isn't in a bin, just realloc it. */ - if (i == 0xFF) { + if (i == 0xff) { struct block *prevPtr, *nextPtr; bigBlockPtr = (struct block *) overPtr - 1; prevPtr = bigBlockPtr->prevPtr; @@ -581,9 +582,9 @@ TclpRealloc( #endif Tcl_MutexUnlock(allocMutexPtr); - return (void *)(overPtr+1); + return (char *)(overPtr+1); } - maxSize = (size_t)1 << (i+3); + maxSize = 1 << (i+3); expensive = 0; if (numBytes+OVERHEAD > maxSize) { expensive = 1; @@ -604,13 +605,13 @@ TclpRealloc( if (maxSize < numBytes) { numBytes = maxSize; } - memcpy(newPtr, oldPtr, numBytes); + memcpy(newPtr, oldPtr, (size_t) numBytes); TclpFree(oldPtr); return newPtr; } /* - * No need to copy. It fits as-is. + * Ok, we don't have to copy, it fits as-is */ #ifndef NDEBUG @@ -645,29 +646,29 @@ void mstats( char *s) /* Where to write info. */ { - unsigned int i, j; - union overhead *overPtr; - size_t totalFree = 0, totalUsed = 0; + register int i, j; + register union overhead *overPtr; + int totalFree = 0, totalUsed = 0; Tcl_MutexLock(allocMutexPtr); fprintf(stderr, "Memory allocation statistics %s\nTclpFree:\t", s); for (i = 0; i < NBUCKETS; i++) { for (j=0, overPtr=nextf[i]; overPtr; overPtr=overPtr->next, j++) { - fprintf(stderr, " %u", j); + fprintf(stderr, " %d", j); } - totalFree += ((size_t)j) * ((size_t)1 << (i + 3)); + totalFree += j * (1 << (i + 3)); } fprintf(stderr, "\nused:\t"); for (i = 0; i < NBUCKETS; i++) { - fprintf(stderr, " %" TCL_Z_MODIFIER "u", numMallocs[i]); - totalUsed += numMallocs[i] * ((size_t)1 << (i + 3)); + fprintf(stderr, " %d", numMallocs[i]); + totalUsed += numMallocs[i] * (1 << (i + 3)); } - fprintf(stderr, "\n\tTotal small in use: %" TCL_Z_MODIFIER "u, total free: %" TCL_Z_MODIFIER "u\n", - totalUsed, totalFree); - fprintf(stderr, "\n\tNumber of big (>%" TCL_Z_MODIFIER "u) blocks in use: %" TCL_Z_MODIFIER "u\n", + fprintf(stderr, "\n\tTotal small in use: %d, total free: %d\n", + totalUsed, totalFree); + fprintf(stderr, "\n\tNumber of big (>%d) blocks in use: %d\n", MAXMALLOC, numMallocs[NBUCKETS]); Tcl_MutexUnlock(allocMutexPtr); @@ -692,11 +693,11 @@ mstats( *---------------------------------------------------------------------- */ -void * +char * TclpAlloc( unsigned int numBytes) /* Number of bytes to allocate. */ { - return malloc(numBytes); + return (char*) malloc(numBytes); } /* @@ -717,7 +718,7 @@ TclpAlloc( void TclpFree( - void *oldPtr) /* Pointer to memory to free. */ + char *oldPtr) /* Pointer to memory to free. */ { free(oldPtr); return; @@ -739,17 +740,15 @@ TclpFree( *---------------------------------------------------------------------- */ -void * +char * TclpRealloc( - void *oldPtr, /* Pointer to alloced block. */ + char *oldPtr, /* Pointer to alloced block. */ unsigned int numBytes) /* New size of memory. */ { - return realloc(oldPtr, numBytes); + return (char*) realloc(oldPtr, numBytes); } #endif /* !USE_TCLALLOC */ -#else -TCL_MAC_EMPTY_FILE(generic_tclAlloc_c) #endif /* !TCL_THREADS */ /* |
