summaryrefslogtreecommitdiffstats
path: root/generic/tclAlloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'generic/tclAlloc.c')
-rw-r--r--generic/tclAlloc.c512
1 files changed, 243 insertions, 269 deletions
diff --git a/generic/tclAlloc.c b/generic/tclAlloc.c
index ae61e85..7b9c807 100644
--- a/generic/tclAlloc.c
+++ b/generic/tclAlloc.c
@@ -1,10 +1,10 @@
-/*
+/*
* tclAlloc.c --
*
- * This is a very fast storage allocator. It allocates blocks of a small
- * number of different sizes, and keeps free lists of each size. Blocks
- * that don't exactly fit are passed up to the next larger size. Blocks
- * over a certain size are directly allocated from the system.
+ * This is a very fast storage allocator. It allocates blocks of a
+ * small number of different sizes, and keeps free lists of each size.
+ * Blocks 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 (c) 1983 Regents of the University of California.
* Copyright (c) 1996-1997 Sun Microsystems, Inc.
@@ -12,8 +12,8 @@
*
* Portions contributed by Chris Kingsley, Jack Jansen and Ray Johnson.
*
- * See the file "license.terms" for information on usage and redistribution of
- * this file, and for a DISCLAIMER OF ALL WARRANTIES.
+ * See the file "license.terms" for information on usage and redistribution
+ * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
/*
@@ -21,58 +21,69 @@
* that has significantly reduced lock contention.
*/
+#if !defined(TCL_THREADS) || !defined(USE_THREAD_ALLOC) || defined(TCL_MEM_DEBUG)
+
#include "tclInt.h"
-#if !defined(TCL_THREADS) || !defined(USE_THREAD_ALLOC)
+#include "tclPort.h"
#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.
+ * 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(__MINGW32__) || defined(__BORLANDC__)
+#if defined(MAC_TCL) || defined(_MSC_VER) || defined(__MINGW32__) || defined(__BORLANDC__)
typedef unsigned long caddr_t;
#endif
/*
- * The overhead on a block is at least 8 bytes. When free, this space contains
- * a pointer to the next free block, and the bottom two bits must be zero.
- * When in use, the first byte is set to MAGIC, and the second byte is the
- * size index. The remaining bytes are for alignment. If range checking is
- * enabled then a second word holds the size of the requested block, less 1,
- * rounded up to a multiple of sizeof(RMAGIC). The order of elements is
- * critical: ov.magic must overlay the low order bits of ov.next, and ov.magic
- * can not be a valid ov.next bit pattern.
+ * Alignment for allocated memory.
+ */
+
+#if defined(__APPLE__)
+#define ALLOCALIGN 16
+#else
+#define ALLOCALIGN 8
+#endif
+
+/*
+ * The overhead on a block is at least 8 bytes. When free, this space
+ * contains a pointer to the next free block, and the bottom two bits must
+ * be zero. When in use, the first byte is set to MAGIC, and the second
+ * byte is the size index. The remaining bytes are for alignment.
+ * If range checking is enabled then a second word holds the size of the
+ * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
+ * The order of elements is critical: ov_magic must overlay the low order
+ * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
*/
union overhead {
- union overhead *next; /* when free */
- unsigned char padding[TCL_ALLOCALIGN]; /* align struct to TCL_ALLOCALIGN bytes */
+ union overhead *ov_next; /* when free */
+ unsigned char ov_padding[ALLOCALIGN];/* align struct to ALLOCALIGN bytes */
struct {
- unsigned char magic0; /* magic number */
- unsigned char index; /* bucket # */
- unsigned char unused; /* unused */
- unsigned char magic1; /* other magic number */
+ unsigned char ovu_magic0; /* magic number */
+ unsigned char ovu_index; /* bucket # */
+ unsigned char ovu_unused; /* unused */
+ unsigned char ovu_magic1; /* other magic number */
#ifndef NDEBUG
- unsigned short rmagic; /* range magic number */
- unsigned long size; /* actual block size */
- unsigned short unused2; /* padding to 8-byte align */
+ unsigned short ovu_rmagic; /* range magic number */
+ unsigned long ovu_size; /* actual block size */
+ unsigned short ovu_unused2; /* padding to 8-byte align */
#endif
} ovu;
-#define overMagic0 ovu.magic0
-#define overMagic1 ovu.magic1
-#define bucketIndex ovu.index
-#define rangeCheckMagic ovu.rmagic
-#define realBlockSize ovu.size
+#define ov_magic0 ovu.ovu_magic0
+#define ov_magic1 ovu.ovu_magic1
+#define ov_index ovu.ovu_index
+#define ov_rmagic ovu.ovu_rmagic
+#define ov_size ovu.ovu_size
};
-#define MAGIC 0xef /* magic # on accounting info */
-#define RMAGIC 0x5555 /* magic # on range 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
@@ -80,45 +91,38 @@ union overhead {
#define OVERHEAD (sizeof(union overhead) + RSLOP)
/*
- * Macro to make it easier to refer to the end-of-block guard magic.
- */
-
-#define BLOCK_END(overPtr) \
- (*(unsigned short *)((caddr_t)((overPtr) + 1) + (overPtr)->realBlockSize))
-
-/*
- * nextf[i] is the pointer to the next free block of size 2^(i+3). The
+ * nextf[i] is the pointer to the next free block of size 2^(i+3). The
* smallest allocatable block is MINBLOCK bytes. The overhead information
* precedes the data area returned to the user.
*/
-#define MINBLOCK ((sizeof(union overhead) + (TCL_ALLOCALIGN-1)) & ~(TCL_ALLOCALIGN-1))
+#define MINBLOCK ((sizeof(union overhead) + (ALLOCALIGN-1)) & ~(ALLOCALIGN-1))
#define NBUCKETS (13 - (MINBLOCK >> 4))
#define MAXMALLOC (1<<(NBUCKETS+2))
-static union overhead *nextf[NBUCKETS];
+static union overhead *nextf[NBUCKETS];
-/*
- * The following structure is used to keep track of all system memory
- * currently owned by Tcl. When finalizing, all this memory will be returned
- * to the system.
+/*
+ * The following structure is used to keep track of all system memory
+ * currently owned by Tcl. When finalizing, all this memory will
+ * be returned to the system.
*/
struct block {
struct block *nextPtr; /* Linked list. */
- struct block *prevPtr; /* Linked list for big blocks, ensures 8-byte
+ struct block *prevPtr; /* Linked list for big blocks, ensures 8-byte
* alignment for suballocated blocks. */
};
-static struct block *blockList; /* Tracks the suballocated blocks. */
-static struct block bigBlocks={ /* Big blocks aren't suballocated. */
+static struct block *blockList; /* Tracks the suballocated blocks. */
+static struct block bigBlocks = { /* Big blocks aren't suballocated. */
&bigBlocks, &bigBlocks
};
/*
- * The allocator is protected by a special mutex that must be explicitly
- * 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.
+ * The allocator is protected by a special mutex that must be
+ * explicitly 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.
*/
#ifdef TCL_THREADS
@@ -126,19 +130,21 @@ static Tcl_Mutex *allocMutexPtr;
#endif
static int allocInit = 0;
+
#ifdef MSTATS
/*
- * numMallocs[i] is the difference between the number of mallocs and frees for
- * a given block size.
+ * nmalloc[i] is the difference between the number of mallocs and frees
+ * for a given block size.
*/
-static unsigned int numMallocs[NBUCKETS+1];
+static unsigned int nmalloc[NBUCKETS+1];
+#include <stdio.h>
#endif
#if !defined(NDEBUG)
-#define ASSERT(p) if (!(p)) Tcl_Panic(# p)
-#define RANGE_ASSERT(p) if (!(p)) Tcl_Panic(# p)
+#define ASSERT(p) if (!(p)) panic(# p)
+#define RANGE_ASSERT(p) if (!(p)) panic(# p)
#else
#define ASSERT(p)
#define RANGE_ASSERT(p)
@@ -148,7 +154,8 @@ static unsigned int numMallocs[NBUCKETS+1];
* Prototypes for functions used only in this file.
*/
-static void MoreCore(int bucket);
+static void MoreCore _ANSI_ARGS_((int bucket));
+
/*
*-------------------------------------------------------------------------
@@ -167,7 +174,7 @@ static void MoreCore(int bucket);
*/
void
-TclInitAlloc(void)
+TclInitAlloc()
{
if (!allocInit) {
allocInit = 1;
@@ -182,26 +189,27 @@ TclInitAlloc(void)
*
* TclFinalizeAllocSubsystem --
*
- * Release all resources being used by this subsystem, including
- * aggressively freeing all memory allocated by TclpAlloc() that has not
- * yet been released with TclpFree().
- *
- * After this function is called, all memory allocated with TclpAlloc()
- * should be considered unusable.
+ * Release all resources being used by this subsystem, including
+ * aggressively freeing all memory allocated by TclpAlloc() that
+ * has not yet been released with TclpFree().
+ *
+ * After this function is called, all memory allocated with
+ * TclpAlloc() should be considered unusable.
*
* Results:
* None.
*
* Side effects:
- * This subsystem is self-initializing, since memory can be allocated
- * before Tcl is formally initialized. After this call, this subsystem
- * has been reset to its initial state and is usable again.
+ * This subsystem is self-initializing, since memory can be
+ * allocated before Tcl is formally initialized. After this call,
+ * this subsystem has been reset to its initial state and is
+ * usable again.
*
*-------------------------------------------------------------------------
*/
void
-TclFinalizeAllocSubsystem(void)
+TclFinalizeAllocSubsystem()
{
unsigned int i;
struct block *blockPtr, *nextPtr;
@@ -221,14 +229,14 @@ TclFinalizeAllocSubsystem(void)
bigBlocks.nextPtr = &bigBlocks;
bigBlocks.prevPtr = &bigBlocks;
- for (i=0 ; i<NBUCKETS ; i++) {
+ for (i = 0; i < NBUCKETS; i++) {
nextf[i] = NULL;
#ifdef MSTATS
- numMallocs[i] = 0;
+ nmalloc[i] = 0;
#endif
}
#ifdef MSTATS
- numMallocs[i] = 0;
+ nmalloc[i] = 0;
#endif
Tcl_MutexUnlock(allocMutexPtr);
}
@@ -250,32 +258,30 @@ TclFinalizeAllocSubsystem(void)
*/
char *
-TclpAlloc(
- unsigned int numBytes) /* Number of bytes to allocate. */
+TclpAlloc(nbytes)
+ unsigned int nbytes; /* Number of bytes to allocate. */
{
- register union overhead *overPtr;
+ register union overhead *op;
register long bucket;
- register unsigned amount;
+ register unsigned amt;
struct block *bigBlockPtr = NULL;
if (!allocInit) {
/*
- * We have to make the "self initializing" because Tcl_Alloc may be
- * used before any other part of Tcl. E.g., see main() for tclsh!
+ * We have to make the "self initializing" because Tcl_Alloc
+ * may be used before any other part of Tcl. E.g., see
+ * main() for tclsh!
*/
-
TclInitAlloc();
}
Tcl_MutexLock(allocMutexPtr);
-
/*
- * First the simple case: we simple allocate big blocks directly.
+ * First the simple case: we simple allocate big blocks directly
*/
-
- if (numBytes >= MAXMALLOC - OVERHEAD) {
- if (numBytes <= UINT_MAX - OVERHEAD -sizeof(struct block)) {
- bigBlockPtr = (struct block *) TclpSysAlloc((unsigned)
- (sizeof(struct block) + OVERHEAD + numBytes), 0);
+ if (nbytes >= MAXMALLOC - OVERHEAD) {
+ if (nbytes <= UINT_MAX - OVERHEAD - sizeof(struct block)) {
+ bigBlockPtr = (struct block *) TclpSysAlloc((unsigned)
+ (sizeof(struct block) + OVERHEAD + nbytes), 0);
}
if (bigBlockPtr == NULL) {
Tcl_MutexUnlock(allocMutexPtr);
@@ -286,83 +292,74 @@ TclpAlloc(
bigBlockPtr->prevPtr = &bigBlocks;
bigBlockPtr->nextPtr->prevPtr = bigBlockPtr;
- overPtr = (union overhead *) (bigBlockPtr + 1);
- overPtr->overMagic0 = overPtr->overMagic1 = MAGIC;
- overPtr->bucketIndex = 0xff;
+ op = (union overhead *) (bigBlockPtr + 1);
+ op->ov_magic0 = op->ov_magic1 = MAGIC;
+ op->ov_index = 0xff;
#ifdef MSTATS
- numMallocs[NBUCKETS]++;
+ nmalloc[NBUCKETS]++;
#endif
-
#ifndef NDEBUG
/*
- * Record allocated size of block and bound space with magic numbers.
+ * Record allocated size of block and
+ * bound space with magic numbers.
*/
-
- overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
- overPtr->rangeCheckMagic = RMAGIC;
- BLOCK_END(overPtr) = RMAGIC;
+ op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
+ op->ov_rmagic = RMAGIC;
+ *(unsigned short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
#endif
-
Tcl_MutexUnlock(allocMutexPtr);
- return (void *)(overPtr+1);
+ return (void *)(op+1);
}
-
/*
- * Convert amount of memory requested into closest block size stored in
- * hash buckets which satisfies request. Account for space used per block
- * for accounting.
+ * Convert amount of memory requested into closest block size
+ * stored in hash buckets which satisfies request.
+ * Account for space used per block for accounting.
*/
- amount = MINBLOCK; /* size of first bucket */
+ amt = MINBLOCK; /* size of first bucket */
bucket = MINBLOCK >> 4;
- while (numBytes + OVERHEAD > amount) {
- amount <<= 1;
- if (amount == 0) {
+ while (nbytes + OVERHEAD > amt) {
+ amt <<= 1;
+ if (amt == 0) {
Tcl_MutexUnlock(allocMutexPtr);
- return NULL;
+ return (NULL);
}
bucket++;
}
- ASSERT(bucket < NBUCKETS);
+ ASSERT( bucket < NBUCKETS );
/*
- * If nothing in hash bucket right now, request more memory from the
- * system.
+ * If nothing in hash bucket right now,
+ * request more memory from the system.
*/
-
- if ((overPtr = nextf[bucket]) == NULL) {
+ if ((op = nextf[bucket]) == NULL) {
MoreCore(bucket);
- if ((overPtr = nextf[bucket]) == NULL) {
+ if ((op = nextf[bucket]) == NULL) {
Tcl_MutexUnlock(allocMutexPtr);
- return NULL;
+ return (NULL);
}
}
-
/*
* Remove from linked list
*/
-
- nextf[bucket] = overPtr->next;
- overPtr->overMagic0 = overPtr->overMagic1 = MAGIC;
- overPtr->bucketIndex = (unsigned char) bucket;
-
+ nextf[bucket] = op->ov_next;
+ op->ov_magic0 = op->ov_magic1 = MAGIC;
+ op->ov_index = (unsigned char) bucket;
#ifdef MSTATS
- numMallocs[bucket]++;
+ nmalloc[bucket]++;
#endif
-
#ifndef NDEBUG
/*
- * Record allocated size of block and bound space with magic numbers.
+ * Record allocated size of block and
+ * bound space with magic numbers.
*/
-
- overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
- overPtr->rangeCheckMagic = RMAGIC;
- BLOCK_END(overPtr) = RMAGIC;
+ op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
+ op->ov_rmagic = RMAGIC;
+ *(unsigned short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
#endif
-
Tcl_MutexUnlock(allocMutexPtr);
- return ((char *)(overPtr + 1));
+ return ((char *)(op + 1));
}
/*
@@ -384,29 +381,28 @@ TclpAlloc(
*/
static void
-MoreCore(
- int bucket) /* What bucket to allocat to. */
+MoreCore(bucket)
+ int bucket; /* What bucket to allocat to. */
{
- register union overhead *overPtr;
- register long size; /* size of desired block */
- long amount; /* amount to allocate */
- int numBlocks; /* how many blocks we get */
+ register union overhead *op;
+ register long sz; /* size of desired block */
+ long amt; /* amount to allocate */
+ int nblks; /* how many blocks we get */
struct block *blockPtr;
/*
- * sbrk_size <= 0 only for big, FLUFFY, requests (about 2^30 bytes on a
- * VAX, I think) or for a negative arg.
+ * sbrk_size <= 0 only for big, FLUFFY, requests (about
+ * 2^30 bytes on a VAX, I think) or for a negative arg.
*/
+ sz = 1 << (bucket + 3);
+ ASSERT(sz > 0);
- size = 1 << (bucket + 3);
- ASSERT(size > 0);
+ amt = MAXMALLOC;
+ nblks = amt / sz;
+ ASSERT(nblks*sz == amt);
- amount = MAXMALLOC;
- numBlocks = amount / size;
- ASSERT(numBlocks*size == amount);
-
- blockPtr = (struct block *) TclpSysAlloc((unsigned)
- (sizeof(struct block) + amount), 1);
+ blockPtr = (struct block *) TclpSysAlloc((unsigned)
+ (sizeof(struct block) + amt), 1);
/* no more room! */
if (blockPtr == NULL) {
return;
@@ -414,18 +410,18 @@ MoreCore(
blockPtr->nextPtr = blockList;
blockList = blockPtr;
- overPtr = (union overhead *) (blockPtr + 1);
-
+ op = (union overhead *) (blockPtr + 1);
+
/*
- * Add new memory allocated to that on free list for this hash bucket.
+ * Add new memory allocated to that on
+ * free list for this hash bucket.
*/
-
- nextf[bucket] = overPtr;
- while (--numBlocks > 0) {
- overPtr->next = (union overhead *)((caddr_t)overPtr + size);
- overPtr = (union overhead *)((caddr_t)overPtr + size);
+ nextf[bucket] = op;
+ while (--nblks > 0) {
+ op->ov_next = (union overhead *)((caddr_t)op + sz);
+ op = (union overhead *)((caddr_t)op + sz);
}
- overPtr->next = NULL;
+ op->ov_next = (union overhead *)NULL;
}
/*
@@ -445,51 +441,47 @@ MoreCore(
*/
void
-TclpFree(
- char *oldPtr) /* Pointer to memory to free. */
-{
+TclpFree(cp)
+ char *cp; /* Pointer to memory to free. */
+{
register long size;
- register union overhead *overPtr;
+ register union overhead *op;
struct block *bigBlockPtr;
- if (oldPtr == NULL) {
+ if (cp == NULL) {
return;
}
Tcl_MutexLock(allocMutexPtr);
- overPtr = (union overhead *)((caddr_t)oldPtr - sizeof(union overhead));
+ op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
- ASSERT(overPtr->overMagic0 == MAGIC); /* make sure it was in use */
- ASSERT(overPtr->overMagic1 == MAGIC);
- if (overPtr->overMagic0 != MAGIC || overPtr->overMagic1 != MAGIC) {
+ ASSERT(op->ov_magic0 == MAGIC); /* make sure it was in use */
+ ASSERT(op->ov_magic1 == MAGIC);
+ if (op->ov_magic0 != MAGIC || op->ov_magic1 != MAGIC) {
Tcl_MutexUnlock(allocMutexPtr);
return;
}
- RANGE_ASSERT(overPtr->rangeCheckMagic == RMAGIC);
- RANGE_ASSERT(BLOCK_END(overPtr) == RMAGIC);
- size = overPtr->bucketIndex;
- if (size == 0xff) {
+ RANGE_ASSERT(op->ov_rmagic == RMAGIC);
+ RANGE_ASSERT(*(unsigned short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
+ size = op->ov_index;
+ if ( size == 0xff ) {
#ifdef MSTATS
- numMallocs[NBUCKETS]--;
+ nmalloc[NBUCKETS]--;
#endif
-
- bigBlockPtr = (struct block *) overPtr - 1;
+ bigBlockPtr = (struct block *) op - 1;
bigBlockPtr->prevPtr->nextPtr = bigBlockPtr->nextPtr;
bigBlockPtr->nextPtr->prevPtr = bigBlockPtr->prevPtr;
TclpSysFree(bigBlockPtr);
-
Tcl_MutexUnlock(allocMutexPtr);
return;
}
ASSERT(size < NBUCKETS);
- overPtr->next = nextf[size]; /* also clobbers overMagic */
- nextf[size] = overPtr;
-
+ op->ov_next = nextf[size]; /* also clobbers ov_magic */
+ nextf[size] = op;
#ifdef MSTATS
- numMallocs[size]--;
+ nmalloc[size]--;
#endif
-
Tcl_MutexUnlock(allocMutexPtr);
}
@@ -510,34 +502,34 @@ TclpFree(
*/
char *
-TclpRealloc(
- char *oldPtr, /* Pointer to alloced block. */
- unsigned int numBytes) /* New size of memory. */
-{
+TclpRealloc(cp, nbytes)
+ char *cp; /* Pointer to alloced block. */
+ unsigned int nbytes; /* New size of memory. */
+{
int i;
- union overhead *overPtr;
+ union overhead *op;
struct block *bigBlockPtr;
int expensive;
- unsigned long maxSize;
+ unsigned long maxsize;
- if (oldPtr == NULL) {
- return TclpAlloc(numBytes);
+ if (cp == NULL) {
+ return (TclpAlloc(nbytes));
}
Tcl_MutexLock(allocMutexPtr);
- overPtr = (union overhead *)((caddr_t)oldPtr - sizeof(union overhead));
+ op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
- ASSERT(overPtr->overMagic0 == MAGIC); /* make sure it was in use */
- ASSERT(overPtr->overMagic1 == MAGIC);
- if (overPtr->overMagic0 != MAGIC || overPtr->overMagic1 != MAGIC) {
+ ASSERT(op->ov_magic0 == MAGIC); /* make sure it was in use */
+ ASSERT(op->ov_magic1 == MAGIC);
+ if (op->ov_magic0 != MAGIC || op->ov_magic1 != MAGIC) {
Tcl_MutexUnlock(allocMutexPtr);
return NULL;
}
- RANGE_ASSERT(overPtr->rangeCheckMagic == RMAGIC);
- RANGE_ASSERT(BLOCK_END(overPtr) == RMAGIC);
- i = overPtr->bucketIndex;
+ RANGE_ASSERT(op->ov_rmagic == RMAGIC);
+ RANGE_ASSERT(*(unsigned short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
+ i = op->ov_index;
/*
* If the block isn't in a bin, just realloc it.
@@ -545,11 +537,11 @@ TclpRealloc(
if (i == 0xff) {
struct block *prevPtr, *nextPtr;
- bigBlockPtr = (struct block *) overPtr - 1;
+ bigBlockPtr = (struct block *) op - 1;
prevPtr = bigBlockPtr->prevPtr;
nextPtr = bigBlockPtr->nextPtr;
- bigBlockPtr = (struct block *) TclpSysRealloc(bigBlockPtr,
- sizeof(struct block) + OVERHEAD + numBytes);
+ bigBlockPtr = (struct block *) TclpSysRealloc(bigBlockPtr,
+ sizeof(struct block) + OVERHEAD + nbytes);
if (bigBlockPtr == NULL) {
Tcl_MutexUnlock(allocMutexPtr);
return NULL;
@@ -557,69 +549,63 @@ TclpRealloc(
if (prevPtr->nextPtr != bigBlockPtr) {
/*
- * If the block has moved, splice the new block into the list
- * where the old block used to be.
+ * If the block has moved, splice the new block into the list where
+ * the old block used to be.
*/
prevPtr->nextPtr = bigBlockPtr;
nextPtr->prevPtr = bigBlockPtr;
}
- overPtr = (union overhead *) (bigBlockPtr + 1);
-
+ op = (union overhead *) (bigBlockPtr + 1);
#ifdef MSTATS
- numMallocs[NBUCKETS]++;
+ nmalloc[NBUCKETS]++;
#endif
-
#ifndef NDEBUG
/*
* Record allocated size of block and update magic number bounds.
*/
- overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
- BLOCK_END(overPtr) = RMAGIC;
+ op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
+ *(unsigned short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
#endif
-
Tcl_MutexUnlock(allocMutexPtr);
- return (char *)(overPtr+1);
+ return (char *)(op+1);
}
- maxSize = 1 << (i+3);
+ maxsize = 1 << (i+3);
expensive = 0;
- if (numBytes+OVERHEAD > maxSize) {
+ if ( nbytes + OVERHEAD > maxsize ) {
expensive = 1;
- } else if (i>0 && numBytes+OVERHEAD < maxSize/2) {
+ } else if ( i > 0 && nbytes + OVERHEAD < (maxsize/2) ) {
expensive = 1;
}
if (expensive) {
- void *newPtr;
+ void *newp;
Tcl_MutexUnlock(allocMutexPtr);
- newPtr = TclpAlloc(numBytes);
- if (newPtr == NULL) {
+ newp = TclpAlloc(nbytes);
+ if ( newp == NULL ) {
return NULL;
}
- maxSize -= OVERHEAD;
- if (maxSize < numBytes) {
- numBytes = maxSize;
- }
- memcpy(newPtr, oldPtr, (size_t) numBytes);
- TclpFree(oldPtr);
- return newPtr;
+ maxsize -= OVERHEAD;
+ if ( maxsize < nbytes )
+ nbytes = maxsize;
+ memcpy((VOID *) newp, (VOID *) cp, (size_t) nbytes);
+ TclpFree(cp);
+ return newp;
}
-
+
/*
* Ok, we don't have to copy, it fits as-is
*/
-
#ifndef NDEBUG
- overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
- BLOCK_END(overPtr) = RMAGIC;
+ op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
+ *(unsigned short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
#endif
-
Tcl_MutexUnlock(allocMutexPtr);
- return(oldPtr);
+ return(cp);
}
/*
@@ -627,9 +613,9 @@ TclpRealloc(
*
* mstats --
*
- * Prints two lines of numbers, one showing the length of the free list
- * for each size category, the second showing the number of mallocs -
- * frees for each size category.
+ * Prints two lines of numbers, one showing the length of the
+ * free list for each size category, the second showing the
+ * number of mallocs - frees for each size category.
*
* Results:
* None.
@@ -642,39 +628,35 @@ TclpRealloc(
#ifdef MSTATS
void
-mstats(
- char *s) /* Where to write info. */
+mstats(s)
+ char *s; /* Where to write info. */
{
register int i, j;
- register union overhead *overPtr;
- int totalFree = 0, totalUsed = 0;
+ register union overhead *p;
+ int totfree = 0,
+ totused = 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++) {
+ for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
fprintf(stderr, " %d", j);
- }
- totalFree += j * (1 << (i + 3));
+ totfree += j * (1 << (i + 3));
}
-
fprintf(stderr, "\nused:\t");
for (i = 0; i < NBUCKETS; i++) {
- fprintf(stderr, " %d", numMallocs[i]);
- totalUsed += numMallocs[i] * (1 << (i + 3));
+ fprintf(stderr, " %d", nmalloc[i]);
+ totused += nmalloc[i] * (1 << (i + 3));
}
-
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]);
-
+ totused, totfree);
+ fprintf(stderr, "\n\tNumber of big (>%d) blocks in use: %d\n",
+ MAXMALLOC, nmalloc[NBUCKETS]);
Tcl_MutexUnlock(allocMutexPtr);
}
#endif
-#else /* !USE_TCLALLOC */
+#else /* !USE_TCLALLOC */
/*
*----------------------------------------------------------------------
@@ -693,10 +675,10 @@ mstats(
*/
char *
-TclpAlloc(
- unsigned int numBytes) /* Number of bytes to allocate. */
+TclpAlloc(nbytes)
+ unsigned int nbytes; /* Number of bytes to allocate. */
{
- return (char *) malloc(numBytes);
+ return (char*) malloc(nbytes);
}
/*
@@ -716,10 +698,10 @@ TclpAlloc(
*/
void
-TclpFree(
- char *oldPtr) /* Pointer to memory to free. */
-{
- free(oldPtr);
+TclpFree(cp)
+ char *cp; /* Pointer to memory to free. */
+{
+ free(cp);
return;
}
@@ -740,20 +722,12 @@ TclpFree(
*/
char *
-TclpRealloc(
- char *oldPtr, /* Pointer to alloced block. */
- unsigned int numBytes) /* New size of memory. */
-{
- return (char *) realloc(oldPtr, numBytes);
+TclpRealloc(cp, nbytes)
+ char *cp; /* Pointer to alloced block. */
+ unsigned int nbytes; /* New size of memory. */
+{
+ return (char*) realloc(cp, nbytes);
}
#endif /* !USE_TCLALLOC */
#endif /* !TCL_THREADS */
-
-/*
- * Local Variables:
- * mode: c
- * c-basic-offset: 4
- * fill-column: 78
- * End:
- */