static TclSmallCache sharedCache = {NULL, 0};
#if defined(TCL_THREADS)
#if defined(HAVE_FAST_TSD)
static __thread TclSmallCache tclSmallCache = {NULL, 0};
#else
#define SLOW_TSD
#endif
#endif
#if defined(PURIFY)
#define MAXOBJS 0
#define ALLOCOBJS 1
#else
#define MAXOBJS 300
#define ALLOCOBJS 100
#endif
static int maxObjs = MAXOBJS;
static int allocObjs = ALLOCOBJS;
/*
*----------------------------------------------------------------------
*
* Tcl_AttemptAlloc --
*
* Allocate more memory.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
Tcl_AttemptAlloc(
unsigned int numBytes) /* Number of bytes to allocate. */
{
return (char *) malloc(numBytes);
}
/*
*----------------------------------------------------------------------
*
* Tcl_Free --
*
* Free memory.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
Tcl_Free(
char *oldPtr) /* Pointer to memory to free. */
{
free(oldPtr);
return;
}
/*
*----------------------------------------------------------------------
*
* Tcl_AttemptRealloc --
*
* Reallocate memory.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
Tcl_AttemptRealloc(
char *oldPtr, /* Pointer to alloced block. */
unsigned int numBytes) /* New size of memory. */
{
return (char *) realloc(oldPtr, numBytes);
}
/*
*----------------------------------------------------------------------
*
* TclSmallAlloc --
*
* Allocate a Tcl_Obj sized block from the per-thread cache.
*
* Results:
* Pointer to uninitialized memory.
*
* Side effects:
* May move blocks from shared cached or allocate new blocks if
* list is empty.
*
*----------------------------------------------------------------------
*/
#ifdef SLOW_TSD
# define GETCACHE(cachePtr) \
do { \
(cachePtr) = TclpGetAllocCache(); \
if ((cachePtr) == NULL) { \
TclSmallCache *tcachePtr = \
malloc(sizeof(TclSmallAlloc)); \
TclpSetAllocCache(tcachePtr); \
(cachePtr) = tcachePtr; \
} \
} while (0)
#else
#define GETCACHE(cachePtr) ((cachePtr) = &tclSmallCache)
#endif
void *
TclSmallAlloc(void)
{
TclSmallCache *cachePtr;
void *ptr;
GETCACHE(cachePtr);
if (cachePtr->freeObjs == 0) {
GetObjs();
}
ptr = cachePtr->firstPtr;
cachePtr->firstPtr = *((void **) ptr);
--cachePtr->freeObjs;
++cachePtr->usedObjs;
return ptr;
}
/*
*----------------------------------------------------------------------
*
* TclSmallFree --
*
* Return a free Tcl_Obj-sized block to the per-thread cache.
*
* Results:
* None.
*
* Side effects:
* May move free blocks to shared list upon hitting high water mark.
*
*----------------------------------------------------------------------
*/
void
TclSmallFree(
void *ptr)
{
TclSmallCache *cachePtr;
GETCACHE(cachePtr);
*((void **) ptr) = cachePtr->firstPtr;
cachePtr->firstPtr = ptr;
++cachePtr->freeObjs;
--cachePtr->usedObjs;
if (cachePtr->freeObjs > maxObjs) {
if (maxObjs == 0) {
/*
* A purify run
*/
free((char *) ptr);
cachePtr->firstPtr = NULL;
cachePtr->freeObjs = 0;
cachePtr->usedObjs = 0;
return;
#if defined(TCL_THREADS)
} else {
if (cachePtr != &sharedCache) {
PutObjs(cachePtr, cachePtr->freeObjs - maxObjs/2);
}
#endif
}
}
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/