summaryrefslogtreecommitdiffstats
path: root/generic/tclAlloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'generic/tclAlloc.c')
-rw-r--r--generic/tclAlloc.c131
1 files changed, 60 insertions, 71 deletions
diff --git a/generic/tclAlloc.c b/generic/tclAlloc.c
index fcdd75d..ae61e85 100644
--- a/generic/tclAlloc.c
+++ b/generic/tclAlloc.c
@@ -14,8 +14,6 @@
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
- *
- * RCS: @(#) $Id: tclAlloc.c,v 1.22 2005/07/19 22:45:18 dkf Exp $
*/
/*
@@ -28,12 +26,6 @@
#if USE_TCLALLOC
-#ifdef TCL_DEBUG
-# define DEBUG
-/* #define MSTATS */
-# define RCHECK
-#endif
-
/*
* We should really make use of AC_CHECK_TYPE(caddr_t) here, but it can wait
* until Tcl uses config.h properly.
@@ -55,19 +47,19 @@ typedef unsigned long caddr_t;
*/
union overhead {
- union overhead *next; /* when free */
- unsigned char padding[8]; /* Ensure the structure is 8-byte aligned. */
+ union overhead *next; /* when free */
+ unsigned char padding[TCL_ALLOCALIGN]; /* align struct to TCL_ALLOCALIGN bytes */
struct {
- unsigned char magic0; /* magic number */
- unsigned char index; /* bucket # */
- unsigned char unused; /* unused */
- unsigned char magic1; /* other magic number */
-#ifdef RCHECK
- unsigned short rmagic; /* range magic number */
- unsigned long size; /* actual block size */
- unsigned short unused2; /* padding to 8-byte align */
+ unsigned char magic0; /* magic number */
+ unsigned char index; /* bucket # */
+ unsigned char unused; /* unused */
+ unsigned char 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 */
#endif
- } ovu;
+ } ovu;
#define overMagic0 ovu.magic0
#define overMagic1 ovu.magic1
#define bucketIndex ovu.index
@@ -79,8 +71,8 @@ union overhead {
#define MAGIC 0xef /* magic # on accounting info */
#define RMAGIC 0x5555 /* magic # on range info */
-#ifdef RCHECK
-#define RSLOP sizeof (unsigned short)
+#ifndef NDEBUG
+#define RSLOP sizeof(unsigned short)
#else
#define RSLOP 0
#endif
@@ -96,11 +88,12 @@ union overhead {
/*
* nextf[i] is the pointer to the next free block of size 2^(i+3). The
- * smallest allocatable block is 8 bytes. The overhead information precedes
- * the data area returned to the user.
+ * smallest allocatable block is MINBLOCK bytes. The overhead information
+ * precedes the data area returned to the user.
*/
-#define NBUCKETS 13
+#define MINBLOCK ((sizeof(union overhead) + (TCL_ALLOCALIGN-1)) & ~(TCL_ALLOCALIGN-1))
+#define NBUCKETS (13 - (MINBLOCK >> 4))
#define MAXMALLOC (1<<(NBUCKETS+2))
static union overhead *nextf[NBUCKETS];
@@ -141,10 +134,9 @@ static int allocInit = 0;
*/
static unsigned int numMallocs[NBUCKETS+1];
-#include <stdio.h>
#endif
-#if defined(DEBUG) || defined(RCHECK)
+#if !defined(NDEBUG)
#define ASSERT(p) if (!(p)) Tcl_Panic(# p)
#define RANGE_ASSERT(p) if (!(p)) Tcl_Panic(# p)
#else
@@ -156,7 +148,7 @@ static unsigned int numMallocs[NBUCKETS+1];
* Prototypes for functions used only in this file.
*/
-static void MoreCore _ANSI_ARGS_((int bucket));
+static void MoreCore(int bucket);
/*
*-------------------------------------------------------------------------
@@ -175,7 +167,7 @@ static void MoreCore _ANSI_ARGS_((int bucket));
*/
void
-TclInitAlloc()
+TclInitAlloc(void)
{
if (!allocInit) {
allocInit = 1;
@@ -209,9 +201,9 @@ TclInitAlloc()
*/
void
-TclFinalizeAllocSubsystem()
+TclFinalizeAllocSubsystem(void)
{
- int i;
+ unsigned int i;
struct block *blockPtr, *nextPtr;
Tcl_MutexLock(allocMutexPtr);
@@ -258,18 +250,18 @@ TclFinalizeAllocSubsystem()
*/
char *
-TclpAlloc(numBytes)
- unsigned int numBytes; /* Number of bytes to allocate. */
+TclpAlloc(
+ unsigned int numBytes) /* Number of bytes to allocate. */
{
register union overhead *overPtr;
register long bucket;
register unsigned amount;
- struct block *bigBlockPtr;
+ 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!
+ * used before any other part of Tcl. E.g., see main() for tclsh!
*/
TclInitAlloc();
@@ -280,9 +272,11 @@ TclpAlloc(numBytes)
* First the simple case: we simple allocate big blocks directly.
*/
- if (numBytes + OVERHEAD >= MAXMALLOC) {
- bigBlockPtr = (struct block *) TclpSysAlloc((unsigned)
- (sizeof(struct block) + OVERHEAD + numBytes), 0);
+ if (numBytes >= MAXMALLOC - OVERHEAD) {
+ if (numBytes <= UINT_MAX - OVERHEAD -sizeof(struct block)) {
+ bigBlockPtr = (struct block *) TclpSysAlloc((unsigned)
+ (sizeof(struct block) + OVERHEAD + numBytes), 0);
+ }
if (bigBlockPtr == NULL) {
Tcl_MutexUnlock(allocMutexPtr);
return NULL;
@@ -299,7 +293,7 @@ TclpAlloc(numBytes)
numMallocs[NBUCKETS]++;
#endif
-#ifdef RCHECK
+#ifndef NDEBUG
/*
* Record allocated size of block and bound space with magic numbers.
*/
@@ -319,13 +313,8 @@ TclpAlloc(numBytes)
* for accounting.
*/
-#ifndef RCHECK
- amount = 8; /* size of first bucket */
- bucket = 0;
-#else
- amount = 16; /* size of first bucket */
- bucket = 1;
-#endif
+ amount = MINBLOCK; /* size of first bucket */
+ bucket = MINBLOCK >> 4;
while (numBytes + OVERHEAD > amount) {
amount <<= 1;
@@ -362,7 +351,7 @@ TclpAlloc(numBytes)
numMallocs[bucket]++;
#endif
-#ifdef RCHECK
+#ifndef NDEBUG
/*
* Record allocated size of block and bound space with magic numbers.
*/
@@ -395,8 +384,8 @@ TclpAlloc(numBytes)
*/
static void
-MoreCore(bucket)
- int bucket; /* What bucket to allocat to. */
+MoreCore(
+ int bucket) /* What bucket to allocat to. */
{
register union overhead *overPtr;
register long size; /* size of desired block */
@@ -436,7 +425,7 @@ MoreCore(bucket)
overPtr->next = (union overhead *)((caddr_t)overPtr + size);
overPtr = (union overhead *)((caddr_t)overPtr + size);
}
- overPtr->next = (union overhead *)NULL;
+ overPtr->next = NULL;
}
/*
@@ -456,8 +445,8 @@ MoreCore(bucket)
*/
void
-TclpFree(oldPtr)
- char *oldPtr; /* Pointer to memory to free. */
+TclpFree(
+ char *oldPtr) /* Pointer to memory to free. */
{
register long size;
register union overhead *overPtr;
@@ -468,7 +457,7 @@ TclpFree(oldPtr)
}
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);
@@ -521,9 +510,9 @@ TclpFree(oldPtr)
*/
char *
-TclpRealloc(oldPtr, numBytes)
- char *oldPtr; /* Pointer to alloced block. */
- unsigned int numBytes; /* New size of memory. */
+TclpRealloc(
+ char *oldPtr, /* Pointer to alloced block. */
+ unsigned int numBytes) /* New size of memory. */
{
int i;
union overhead *overPtr;
@@ -537,7 +526,7 @@ TclpRealloc(oldPtr, numBytes)
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);
@@ -582,7 +571,7 @@ TclpRealloc(oldPtr, numBytes)
numMallocs[NBUCKETS]++;
#endif
-#ifdef RCHECK
+#ifndef NDEBUG
/*
* Record allocated size of block and update magic number bounds.
*/
@@ -615,7 +604,7 @@ TclpRealloc(oldPtr, numBytes)
if (maxSize < numBytes) {
numBytes = maxSize;
}
- memcpy((VOID *) newPtr, (VOID *) oldPtr, (size_t) numBytes);
+ memcpy(newPtr, oldPtr, (size_t) numBytes);
TclpFree(oldPtr);
return newPtr;
}
@@ -624,7 +613,7 @@ TclpRealloc(oldPtr, numBytes)
* Ok, we don't have to copy, it fits as-is
*/
-#ifdef RCHECK
+#ifndef NDEBUG
overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
BLOCK_END(overPtr) = RMAGIC;
#endif
@@ -653,8 +642,8 @@ TclpRealloc(oldPtr, numBytes)
#ifdef MSTATS
void
-mstats(s)
- char *s; /* Where to write info. */
+mstats(
+ char *s) /* Where to write info. */
{
register int i, j;
register union overhead *overPtr;
@@ -685,7 +674,7 @@ mstats(s)
}
#endif
-#else /* !USE_TCLALLOC */
+#else /* !USE_TCLALLOC */
/*
*----------------------------------------------------------------------
@@ -704,10 +693,10 @@ mstats(s)
*/
char *
-TclpAlloc(numBytes)
- unsigned int numBytes; /* Number of bytes to allocate. */
+TclpAlloc(
+ unsigned int numBytes) /* Number of bytes to allocate. */
{
- return (char*) malloc(numBytes);
+ return (char *) malloc(numBytes);
}
/*
@@ -727,8 +716,8 @@ TclpAlloc(numBytes)
*/
void
-TclpFree(oldPtr)
- char *oldPtr; /* Pointer to memory to free. */
+TclpFree(
+ char *oldPtr) /* Pointer to memory to free. */
{
free(oldPtr);
return;
@@ -751,11 +740,11 @@ TclpFree(oldPtr)
*/
char *
-TclpRealloc(oldPtr, numBytes)
- char *oldPtr; /* Pointer to alloced block. */
- unsigned int numBytes; /* New size of memory. */
+TclpRealloc(
+ char *oldPtr, /* Pointer to alloced block. */
+ unsigned int numBytes) /* New size of memory. */
{
- return (char*) realloc(oldPtr, numBytes);
+ return (char *) realloc(oldPtr, numBytes);
}
#endif /* !USE_TCLALLOC */