removed debug template parameter

This commit is contained in:
Jeff Hill
2002-03-26 19:36:32 +00:00
parent fb753fcc02
commit 0f354b5c0d

View File

@@ -1,4 +1,7 @@
#ifndef tsFreeList_h
#define tsFreeList_h
/* $Id$
*
* L O S A L A M O S
@@ -10,15 +13,6 @@
* Author: Jeff Hill
*/
#ifndef tsFreeList_h
#define tsFreeList_h
#include <new>
#ifdef EPICS_FREELIST_DEBUG
# include <string.h>
#endif
//
// To allow your class to be allocated off of a free list
// using the new operator:
@@ -50,18 +44,18 @@
// 3) Setting N to zero causes the free list to be bypassed
//
#include <stdio.h>
#include <string.h>
#include <typeinfo>
#include "epicsMutex.h"
#include "epicsGuard.h"
#ifdef EPICS_FREELIST_DEBUG
# define tsFreeListDebugBypass 1
#else
# define tsFreeListDebugBypass 0
#endif
#include <new>
#include "string.h"
#include "epicsMutex.h"
#include "epicsGuard.h"
// these versions of the microsoft compiler incorrectly
// warn about a missing delete operator if only the
// newly preferred delete operator with a size argument
@@ -71,77 +65,57 @@
# pragma warning ( disable : 4291 )
#endif
template < class T, unsigned DEBUG_LEVEL >
union tsFreeListItem;
template < class T, unsigned N, unsigned DEBUG_LEVEL>
struct tsFreeListChunk;
template < class T > union tsFreeListItem;
template < class T, unsigned N> struct tsFreeListChunk;
template < class T, unsigned N = 0x400,
class MUTEX = epicsMutex, unsigned DEBUG_LEVEL = 0u >
class MUTEX = epicsMutex >
class tsFreeList {
public:
tsFreeList ();
~tsFreeList ();
void * allocate ( size_t size );
void release ( void *p, size_t size );
void release ( void * p, size_t size );
private:
tsFreeListItem < T, DEBUG_LEVEL > *pFreeList;
tsFreeListChunk < T, N, DEBUG_LEVEL > *pChunkList;
MUTEX mutex;
tsFreeListItem < T, DEBUG_LEVEL > * allocateFromNewChunk ();
tsFreeListItem < T > * pFreeList;
tsFreeListChunk < T, N > * pChunkList;
tsFreeListItem < T > * allocateFromNewChunk ();
};
template < class T, unsigned DEBUG_LEVEL >
template < class T >
union tsFreeListItem {
public:
char pad [ sizeof ( T ) ];
tsFreeListItem < T, DEBUG_LEVEL > *pNext;
tsFreeListItem < T > * pNext;
};
template < class T, unsigned N = 0x400, unsigned DEBUG_LEVEL = 0u >
template < class T, unsigned N = 0x400 >
struct tsFreeListChunk {
tsFreeListItem < T, DEBUG_LEVEL > items [N];
tsFreeListChunk < T, N, DEBUG_LEVEL > *pNext;
tsFreeListItem < T > items [N];
tsFreeListChunk < T, N > * pNext;
};
template < class T, unsigned N, class MUTEX, unsigned DEBUG_LEVEL >
inline tsFreeList < T, N, MUTEX, DEBUG_LEVEL > :: tsFreeList () :
template < class T, unsigned N, class MUTEX >
inline tsFreeList < T, N, MUTEX > :: tsFreeList () :
pFreeList ( 0 ), pChunkList ( 0 ) {}
template < class T, unsigned N, class MUTEX, unsigned DEBUG_LEVEL >
tsFreeList < T, N, MUTEX, DEBUG_LEVEL > :: ~tsFreeList ()
template < class T, unsigned N, class MUTEX >
tsFreeList < T, N, MUTEX > :: ~tsFreeList ()
{
tsFreeListChunk < T, N, DEBUG_LEVEL > *pChunk;
unsigned nChunks;
if ( DEBUG_LEVEL > 1u ) {
nChunks = 0u;
}
tsFreeListChunk < T, N > * pChunk;
while ( ( pChunk = this->pChunkList ) ) {
this->pChunkList = this->pChunkList->pNext;
delete pChunk;
if ( DEBUG_LEVEL > 1u ) {
nChunks++;
}
}
if ( DEBUG_LEVEL > 1u ) {
fprintf ( stderr, "free list destructor for class %s returned %u objects to pool\n",
typeid ( T ).name (), N * nChunks );
}
}
template < class T, unsigned N, class MUTEX, unsigned DEBUG_LEVEL >
void * tsFreeList < T, N, MUTEX, DEBUG_LEVEL >::allocate ( size_t size )
template < class T, unsigned N, class MUTEX >
void * tsFreeList < T, N, MUTEX >::allocate ( size_t size )
{
if ( DEBUG_LEVEL > 1 ) {
fprintf ( stderr, "creating a new %s of size %u\n",
typeid ( T ).name (), sizeof ( T ) );
}
if ( size != sizeof ( T ) || N == 0u || tsFreeListDebugBypass ) {
void *p = ::operator new ( size );
void * p = ::operator new ( size );
if ( tsFreeListDebugBypass ) {
memset ( p, 0xaa, size );
}
@@ -150,7 +124,7 @@ void * tsFreeList < T, N, MUTEX, DEBUG_LEVEL >::allocate ( size_t size )
epicsGuard < MUTEX > guard ( this->mutex );
tsFreeListItem < T, DEBUG_LEVEL > *p = this->pFreeList;
tsFreeListItem < T > * p = this->pFreeList;
if ( p ) {
this->pFreeList = p->pNext;
}
@@ -161,12 +135,12 @@ void * tsFreeList < T, N, MUTEX, DEBUG_LEVEL >::allocate ( size_t size )
return static_cast < void * > ( p );
}
template < class T, unsigned N, class MUTEX, unsigned DEBUG_LEVEL >
tsFreeListItem < T, DEBUG_LEVEL > *
tsFreeList < T, N, MUTEX, DEBUG_LEVEL >::allocateFromNewChunk ()
template < class T, unsigned N, class MUTEX >
tsFreeListItem < T > *
tsFreeList < T, N, MUTEX >::allocateFromNewChunk ()
{
tsFreeListChunk < T, N, DEBUG_LEVEL > *pChunk =
new tsFreeListChunk < T, N, DEBUG_LEVEL >;
tsFreeListChunk < T, N > * pChunk =
new tsFreeListChunk < T, N >;
for ( unsigned i=1u; i < N-1; i++ ) {
pChunk->items[i].pNext = &pChunk->items[i+1];
@@ -181,14 +155,10 @@ tsFreeListItem < T, DEBUG_LEVEL > *
return &pChunk->items[0];
}
template < class T, unsigned N, class MUTEX, unsigned DEBUG_LEVEL >
void tsFreeList < T, N, MUTEX, DEBUG_LEVEL >::release
( void *pCadaver, size_t size )
template < class T, unsigned N, class MUTEX >
void tsFreeList < T, N, MUTEX >::release
( void * pCadaver, size_t size )
{
if ( DEBUG_LEVEL > 1 ) {
fprintf ( stderr, "releasing a %s of size %u\n",
typeid ( T ).name (), sizeof ( T ) );
}
if ( size != sizeof ( T ) || N == 0u || tsFreeListDebugBypass ) {
if ( tsFreeListDebugBypass ) {
memset ( pCadaver, 0xdd, size );
@@ -197,8 +167,8 @@ void tsFreeList < T, N, MUTEX, DEBUG_LEVEL >::release
}
else if ( pCadaver ) {
epicsGuard < MUTEX > guard ( this->mutex );
tsFreeListItem < T, DEBUG_LEVEL > *p =
static_cast < tsFreeListItem < T, DEBUG_LEVEL > *> ( pCadaver );
tsFreeListItem < T > * p =
static_cast < tsFreeListItem < T > * > ( pCadaver );
p->pNext = this->pFreeList;
this->pFreeList = p;
}