📄 allocate.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Base/Allocate.cpp,v $
* $Date: 2003/05/13 18:41:54 $
*
Description:
Memory management and debugging.
NDEBUG - disables all tracing etc.
\*____________________________________________________________________________*/
//$SourceTop:$
#include <stdlib.h>
#include <fstream.h>
#include "PICompat.h"
#include INCLUDE_ASSERT_H
#include "Allocate.h"
#if defined(USE_ALLOCATOR_DEBUG)
/*____________________________________________________________________________*\
*
Global declarations:
\*____________________________________________________________________________*/
TSignatureType Allocator::culSignature = 0;
size_t Allocator::tASize = 0;
size_t Allocator::tSSize = 0;
#endif
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
Allocator::Allocator()
: pMemPool( 0 )
#if defined(USE_ALLOCATOR_DEBUG)
,pDbgStream( 0 ),
ulIndex( 0 ),
iPtr( 0 )
#endif
{
#if defined(USE_ALLOCATOR_DEBUG)
tCurrentAllocatedMemory = 0;
tMaximumAllocatedMemory = 0;
Allocator::culSignature=0xF123E234;
Allocator::tASize = Allocator::InternalAlign( sizeof( AllocInfo ) );
Allocator::tSSize = Allocator::InternalAlign( sizeof( TSignatureType ) );
pDbgStream=PI_NEW( ofstream( "./mem.dbg" ) );
#endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
Allocator::~Allocator()
{
#if defined(USE_ALLOCATOR_DEBUG)
/* --- log any allocation errors --- */
InternalWriteOutList( GetDbgStream() );
PI_DELETE( pDbgStream );
pDbgStream=0;
#endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void Allocator::StartTransaction( void *pMem, size_t tSize)
{
pMemPool=pMem;
tPoolSize=tSize;
tPoolUsed=0;
# if defined(USE_ALLOCATOR_DEBUG)
if ( GetDbgStream() )
{
*GetDbgStream() << "Starting transaction, pool at "
<< pMem << " size " << (void *)tSize << endl;
};
# endif
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
size_t Allocator::EndTransaction()
{
# if defined(USE_ALLOCATOR_DEBUG)
if ( GetDbgStream() )
{
*GetDbgStream() << "Ending transaction, pool at "
<< pMemPool << " " << (void *)tPoolUsed
<< " bytes used " << endl;
};
# endif
pMemPool=0;
return tPoolUsed;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void *Allocator::InternalAllocate( size_t tRequestedSize )
{
if ( pMemPool )
{
void *pV=(void *)( ((size_t)pMemPool) + tPoolUsed );
if ( (tPoolUsed+=tRequestedSize)>tPoolSize )
{
assert( 0/* Transaction memory pool exhausted */ );
abort(); /* nothing else to do */
};
return pV;
}
else
return malloc( tRequestedSize );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void Allocator::InternalDeAllocate( void *pV )
{
pMemPool ? ((void)0) : free( pV );
}
#if defined(USE_ALLOCATOR_DEBUG)
/*____________________________________________________________________________*\
*
Function:
Synopsis: static, public
Description:
Pad out the size tSize so that consective elements in memory will
have correct bus address alignment
\*____________________________________________________________________________*/
/* --- this is used to deduce the machine specific address size
of primative types
--- */
union tAlignTest {
int iDummy;
long lDummy;
void *pDummy;
};
size_t Allocator::InternalAlign( size_t tSize )
{
size_t tAlignOn=sizeof( tAlignTest ); // if all else fails!!!
return ( tSize % tAlignOn )
? (( tSize / tAlignOn ) + 1) * tAlignOn :
tSize;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Care must be taken in this function that address alignment is
correct.
\*____________________________________________________________________________*/
void *Allocator::DbgAllocate( size_t tRequestedSize )
{
/* ---
Get size of elements properly address aligned for the bus
--- */
size_t tRSize = InternalAlign( tRequestedSize );
size_t tTotalSize = tASize + tRSize + tSSize;
/* --- allocate the chunk --- */
size_t tA=(size_t)InternalAllocate( tTotalSize );
assert( tA );
/* --- track normal memory usage --- */
tCurrentAllocatedMemory += tRequestedSize;
if ( tMaximumAllocatedMemory < tCurrentAllocatedMemory )
{ tMaximumAllocatedMemory = tCurrentAllocatedMemory; };
/* --- assign addresses --- */
AllocInfo *pAllocInfo=(AllocInfo *) ( tA );
void *pRequestedMemory=(void *) ( tA + tASize );
unsigned long *pSig=(unsigned long *) ( tA + tASize + tRSize );
/* --- setup the alloc info structure --- */
pAllocInfo->ulIndex=GetCounterValue();
pAllocInfo->tSize=tRequestedSize;
pAllocInfo->bPool=pMemPool ? true : false;
pAllocInfo->bFreed=false;
/* --- stamp bytes after the allocated space with culSignature --- */
*pSig = culSignature;
/* --- note this allocation --- */
AddToList( pAllocInfo );
/* --- write to debug file --- */
if ( GetDbgStream() )
{
*GetDbgStream() << "Ref." << pAllocInfo->ulIndex << " "
<< tRequestedSize << " at " << (void *)pRequestedMemory
<< endl;
};
/* --- done --- */
return pRequestedMemory;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void Allocator::DbgDeAllocate( void *pV )
{
if ( !pV )
{
return;
};
assert( pV );
size_t tA=((size_t)pV) - tASize;
assert( tA );
AllocInfo *pAllocInfo=(AllocInfo *) ( tA );
size_t tRSize = InternalAlign( pAllocInfo->tSize );
void *pRequestedMemory=(void *) ( tA + tASize );
unsigned long *pSig=(unsigned long *) ( tA + tASize + tRSize );
/* --- get and set some values from AllocInfo --- */
unsigned long ulCrossReference=pAllocInfo->ulIndex;
unsigned long ulSignature=*pSig;
bool bFreed=pAllocInfo->bFreed;
bool bPool=pAllocInfo->bPool;
/* --- note this deallocation, if its the first time --- */
if ( !bFreed )
{ DeleteFromList( pAllocInfo ); };
pAllocInfo->ulIndex=GetCounterValue();
pAllocInfo->bFreed=true;
/* --- write to debug file --- */
if ( GetDbgStream() )
{
*GetDbgStream() << "Ref." << pAllocInfo->ulIndex << " "
<< pAllocInfo->tSize << " at " << (void *)pRequestedMemory
<< " C.Ref." << ulCrossReference
<< endl;
/* --- check for errors --- */
if ( ulSignature!=culSignature )
{
*GetDbgStream() << " ERROR - Memory overwrite, invalid signature " <<
(void *)ulSignature << endl;
};
if ( bFreed )
{
*GetDbgStream() << " ERROR - Memory already free'd" << endl;
return; /* Abort deallocate operation */
};
if ( bPool && !pMemPool )
{
*GetDbgStream() << " ERROR - Memory allocated from Pool free'd to heap!"
<< endl;
return; /* Abort deallocate operation */
};
if ( !bPool && pMemPool )
{
*GetDbgStream() << " ERROR - Memory allocated from heap free'd to Pool!"
<< endl;
return; /* Abort deallocate operation */
};
};
/* --- track normal memory usage --- */
tCurrentAllocatedMemory -= tRSize;
/* --- trash the memory to highlight some problems --- */
memset( (void *)tA, 0xA, tASize + tRSize + sizeof(unsigned long) );
/* --- free the block --- */
InternalDeAllocate( (void *)tA );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void Allocator::BeforeAllocate( int iLine, const char *pFileName )
{
if ( GetDbgStream() )
*GetDbgStream() << "Alloc: " << pFileName << ":" << iLine << " ";
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void Allocator::BeforeDeAllocate( int iLine, const char *pFileName )
{
if ( GetDbgStream() )
*GetDbgStream() << "Free : " << pFileName << ":" << iLine << " ";
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void Allocator::Mark( int iLine, const char *pFileName )
{
if ( GetDbgStream() )
*GetDbgStream() << "Mark : " << pFileName << ":" << iLine << endl;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Not thread safe
\*____________________________________________________________________________*/
void Allocator::AddToList( AllocInfo *pAlloc )
{
for(int i=0; i<iPtr; i++)
{
if ( !aAllocInfo[i] )
{
aAllocInfo[i]=pAlloc->ulIndex; return;
};
};
if ( iPtr<NUM_ALLOC_INFO )
{
aAllocInfo[iPtr++]=pAlloc->ulIndex; return;
};
if ( GetDbgStream() )
{
*GetDbgStream() << "ERROR, Array to track allocations has run \
out of space" << endl;
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Not thread safe
\*____________________________________________________________________________*/
void Allocator::DeleteFromList( AllocInfo *pAlloc )
{
unsigned long ulI=pAlloc->ulIndex;
for(int i=0; i<iPtr; i++)
{
if ( aAllocInfo[i]==ulI )
{
aAllocInfo[i]=0; return;
};
};
/* --- not found --- */
if ( GetDbgStream() )
*GetDbgStream() << "ERROR, cannot find cross reference ";
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Not thread safe
\*____________________________________________________________________________*/
void Allocator::InternalWriteOutList( ostream *pOs )
{
if ( !pOs ) return;
*pOs << endl
<< "========================================================"
<< endl << "Allocator<" << (void *)this << ">, "
<< "unfree'd allocations" << endl
<< "========================================================"
<< endl;
for(int i=0; i<iPtr; i++)
{
if ( aAllocInfo[i] )
{
*pOs << "C.Ref. " << aAllocInfo[i] << endl;
};
};
*pOs << endl
<< "========================================================"
<< endl
<< "========================================================"
<< endl;
*pOs << endl
<< "========================================================"
<< endl << "Peak Memory usage: " << tMaximumAllocatedMemory
<< " bytes."
<< endl
<< "========================================================"
<< endl;
}
#endif /* defined(USE_ALLOCATOR_DEBUG) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -