2000-09-18 16:07:07 +00:00
|
|
|
/*************************************************************************
|
|
|
|
*
|
|
|
|
* $RCSfile: mempool.cxx,v $
|
|
|
|
*
|
2005-04-13 11:12:46 +00:00
|
|
|
* $Revision: 1.4 $
|
2000-09-18 16:07:07 +00:00
|
|
|
*
|
2005-04-13 11:12:46 +00:00
|
|
|
* last change: $Author: obo $ $Date: 2005-04-13 12:12:46 $
|
2000-09-18 16:07:07 +00:00
|
|
|
*
|
|
|
|
* The Contents of this file are made available subject to the terms of
|
|
|
|
* either of the following licenses
|
|
|
|
*
|
|
|
|
* - GNU Lesser General Public License Version 2.1
|
|
|
|
* - Sun Industry Standards Source License Version 1.1
|
|
|
|
*
|
|
|
|
* Sun Microsystems Inc., October, 2000
|
|
|
|
*
|
|
|
|
* GNU Lesser General Public License Version 2.1
|
|
|
|
* =============================================
|
|
|
|
* Copyright 2000 by Sun Microsystems, Inc.
|
|
|
|
* 901 San Antonio Road, Palo Alto, CA 94303, USA
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License version 2.1, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Sun Industry Standards Source License Version 1.1
|
|
|
|
* =================================================
|
|
|
|
* The contents of this file are subject to the Sun Industry Standards
|
|
|
|
* Source License Version 1.1 (the "License"); You may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of the
|
|
|
|
* License at http://www.openoffice.org/license.html.
|
|
|
|
*
|
|
|
|
* Software provided under this License is provided on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
|
|
|
|
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
|
|
|
|
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
|
|
|
|
* See the License for the specific provisions governing your rights and
|
|
|
|
* obligations concerning the Software.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
|
|
|
|
*
|
|
|
|
* Copyright: 2000 by Sun Microsystems, Inc.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s): _______________________________________
|
|
|
|
*
|
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
#include "mempool.hxx"
|
2000-09-18 16:07:07 +00:00
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
#include "debug.hxx"
|
2000-09-18 16:07:07 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
2004-06-17 12:12:06 +00:00
|
|
|
#if ( SAL_TYPES_ALIGNMENT8 > 4 )
|
|
|
|
#define MEMPOOL_ALIGNMENT SAL_TYPES_ALIGNMENT8
|
2000-09-18 16:07:07 +00:00
|
|
|
#else
|
|
|
|
#define MEMPOOL_ALIGNMENT 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// -----------------
|
|
|
|
// - FixedMemBlock -
|
|
|
|
// -----------------
|
|
|
|
|
|
|
|
struct FixedMemBlock
|
|
|
|
{
|
2002-05-21 19:30:47 +00:00
|
|
|
/** Representation.
|
|
|
|
*/
|
2000-09-18 16:07:07 +00:00
|
|
|
USHORT nSize;
|
|
|
|
USHORT nFree;
|
|
|
|
USHORT nFirst;
|
|
|
|
USHORT nDummyAlign1;
|
|
|
|
FixedMemBlock* pNext;
|
2004-06-17 12:12:06 +00:00
|
|
|
#if (SAL_TYPES_SIZEOFPOINTER == 4) && (SAL_TYPES_ALIGNMENT8 == 8)
|
2000-09-18 16:07:07 +00:00
|
|
|
void* pDummyAlign2;
|
|
|
|
#endif
|
|
|
|
char aData[1];
|
2002-05-21 19:30:47 +00:00
|
|
|
|
|
|
|
/** Allocation.
|
|
|
|
*/
|
|
|
|
static void* operator new (size_t, USHORT nTypes, USHORT nTypeSize)
|
|
|
|
{
|
|
|
|
return ::operator new (sizeof(FixedMemBlock) + nTypes * nTypeSize);
|
|
|
|
}
|
|
|
|
static void operator delete (void *p, size_t)
|
|
|
|
{
|
|
|
|
::operator delete (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Construction.
|
|
|
|
*/
|
|
|
|
FixedMemBlock (USHORT nTypes = 1, USHORT nTypeSize = 0);
|
|
|
|
~FixedMemBlock() {}
|
2000-09-18 16:07:07 +00:00
|
|
|
};
|
|
|
|
|
2002-05-21 19:30:47 +00:00
|
|
|
FixedMemBlock::FixedMemBlock (USHORT nTypes, USHORT nTypeSize)
|
|
|
|
: nSize (nTypes * nTypeSize),
|
|
|
|
nFree (nTypes - 1),
|
|
|
|
nFirst (1),
|
|
|
|
pNext (0)
|
|
|
|
{
|
|
|
|
char * pData = aData;
|
|
|
|
for (USHORT i = 1; i < nTypes; i++)
|
|
|
|
{
|
|
|
|
*reinterpret_cast<USHORT*>(pData) = i;
|
|
|
|
pData += nTypeSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* FixedMemPool_Impl.
|
|
|
|
|*
|
|
|
|
*************************************************************************/
|
|
|
|
struct FixedMemPool_Impl
|
|
|
|
{
|
|
|
|
/** Representation.
|
|
|
|
*/
|
|
|
|
FixedMemBlock* m_pFirst;
|
|
|
|
USHORT m_nTypeSize;
|
|
|
|
USHORT m_nInitSize;
|
|
|
|
USHORT m_nGrowSize;
|
|
|
|
|
|
|
|
/** Construction.
|
|
|
|
*/
|
|
|
|
FixedMemPool_Impl (USHORT nTypeSize, USHORT nInitSize, USHORT nGrowSize);
|
|
|
|
~FixedMemPool_Impl();
|
|
|
|
|
|
|
|
/** Operation.
|
|
|
|
*/
|
|
|
|
FixedMemBlock * newInitBlock() const
|
|
|
|
{
|
|
|
|
return new(m_nInitSize, m_nTypeSize) FixedMemBlock(m_nInitSize, m_nTypeSize);
|
|
|
|
}
|
|
|
|
FixedMemBlock * newGrowBlock() const
|
|
|
|
{
|
|
|
|
return new(m_nGrowSize, m_nTypeSize) FixedMemBlock(m_nGrowSize, m_nTypeSize);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FixedMemPool_Impl::FixedMemPool_Impl (
|
|
|
|
USHORT nTypeSize, USHORT nInitSize, USHORT nGrowSize)
|
|
|
|
: m_pFirst (0), m_nInitSize (nInitSize), m_nGrowSize (nGrowSize)
|
|
|
|
{
|
|
|
|
if ( nTypeSize > 4 )
|
|
|
|
m_nTypeSize = (nTypeSize + (MEMPOOL_ALIGNMENT-1)) & ~(MEMPOOL_ALIGNMENT-1);
|
|
|
|
else if ( nTypeSize <= 2 )
|
|
|
|
m_nTypeSize = 2;
|
|
|
|
else
|
|
|
|
m_nTypeSize = 4;
|
|
|
|
|
|
|
|
DBG_ASSERT( (ULONG)nTypeSize*nInitSize <= USHRT_MAX,
|
|
|
|
"FixedMemPool: TypeSize*nInitSize > USHRT_MAX" );
|
|
|
|
DBG_ASSERT( (ULONG)nTypeSize*nGrowSize <= USHRT_MAX,
|
|
|
|
"FixedMemPool: TypeSize*GrowSize > USHRT_MAX" );
|
|
|
|
}
|
|
|
|
|
|
|
|
FixedMemPool_Impl::~FixedMemPool_Impl()
|
|
|
|
{
|
|
|
|
FixedMemBlock* pBlock = m_pFirst;
|
|
|
|
while ( pBlock )
|
|
|
|
{
|
|
|
|
FixedMemBlock* pTemp = pBlock;
|
|
|
|
pBlock = pBlock->pNext;
|
|
|
|
delete pTemp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-18 16:07:07 +00:00
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* ImpDbgPoolTest()
|
|
|
|
|*
|
|
|
|
|* Beschreibung MEMPOOL.SDW
|
|
|
|
|* Ersterstellung TH 30.11.94
|
|
|
|
|* Letzte Aenderung TH 30.11.94
|
|
|
|
|*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#ifdef DBG_UTIL
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
static void ImpDbgPoolTest( FixedMemPool_Impl * pMemPool )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
|
|
|
DbgData* pData = DbgGetData();
|
|
|
|
|
|
|
|
if ( !pData )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( !(pData->nTestFlags & (DBG_TEST_MEM_OVERWRITE | DBG_TEST_MEM_OVERWRITEFREE)) )
|
|
|
|
return;
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
FixedMemBlock* pFirst = pMemPool->m_pFirst;
|
2000-09-18 16:07:07 +00:00
|
|
|
FixedMemBlock* pBlock = pFirst;
|
|
|
|
while ( pBlock )
|
|
|
|
{
|
|
|
|
if ( pBlock->nFree )
|
|
|
|
{
|
|
|
|
USHORT i = pBlock->nFree;
|
|
|
|
USHORT n = pBlock->nFirst;
|
|
|
|
char* pData = pBlock->aData;
|
|
|
|
while ( i )
|
|
|
|
{
|
2005-04-13 11:12:46 +00:00
|
|
|
if ( !(n < (pBlock->nSize/pMemPool->m_nTypeSize)) )
|
2000-09-18 16:07:07 +00:00
|
|
|
DbgError( "MemPool: Memory Overwrite" );
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
char* pNext = pData+(n*pMemPool->m_nTypeSize);
|
2000-09-18 16:07:07 +00:00
|
|
|
n = *((USHORT*)pNext);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pBlock = pBlock->pNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* FixedMemPool::FixedMemPool()
|
|
|
|
|*
|
|
|
|
|* Beschreibung MEMPOOL.SDW
|
|
|
|
|* Ersterstellung TH 02.09.94
|
|
|
|
|* Letzte Aenderung TH 02.09.94
|
|
|
|
|*
|
|
|
|
*************************************************************************/
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
FixedMemPool::FixedMemPool (
|
|
|
|
USHORT _nTypeSize, USHORT _nInitSize, USHORT _nGrowSize )
|
|
|
|
: m_pImpl (new FixedMemPool_Impl (_nTypeSize, _nInitSize, _nGrowSize))
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* FixedMemPool::~FixedMemPool()
|
|
|
|
|*
|
|
|
|
|* Beschreibung MEMPOOL.SDW
|
|
|
|
|* Ersterstellung TH 02.09.94
|
|
|
|
|* Letzte Aenderung TH 02.09.94
|
|
|
|
|*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
FixedMemPool::~FixedMemPool()
|
|
|
|
{
|
2005-04-13 11:12:46 +00:00
|
|
|
delete m_pImpl;
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* FixedMemPool::Alloc()
|
|
|
|
|*
|
|
|
|
|* Beschreibung MEMPOOL.SDW
|
|
|
|
|* Ersterstellung TH 02.09.94
|
|
|
|
|* Letzte Aenderung TH 02.09.94
|
|
|
|
|*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
void* FixedMemPool::Alloc()
|
|
|
|
{
|
|
|
|
#ifdef DBG_UTIL
|
2005-04-13 11:12:46 +00:00
|
|
|
ImpDbgPoolTest( m_pImpl );
|
2000-09-18 16:07:07 +00:00
|
|
|
#endif
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
if ( !m_pImpl->m_pFirst )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
2005-04-13 11:12:46 +00:00
|
|
|
m_pImpl->m_pFirst = m_pImpl->newInitBlock();
|
|
|
|
if ( !m_pImpl->m_pFirst )
|
2000-09-18 16:07:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
return (void*)(m_pImpl->m_pFirst->aData);
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
FixedMemBlock* pBlock = m_pImpl->m_pFirst;
|
2000-09-18 16:07:07 +00:00
|
|
|
while ( pBlock && !pBlock->nFree )
|
|
|
|
pBlock = pBlock->pNext;
|
|
|
|
|
|
|
|
if ( pBlock )
|
|
|
|
{
|
2005-04-13 11:12:46 +00:00
|
|
|
char* pFree = pBlock->aData+(pBlock->nFirst*m_pImpl->m_nTypeSize);
|
2000-09-18 16:07:07 +00:00
|
|
|
pBlock->nFirst = *((USHORT*)pFree); // UMR, wenn letzter freier Block, ist OK
|
|
|
|
pBlock->nFree--;
|
|
|
|
return (void*)pFree;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-04-13 11:12:46 +00:00
|
|
|
if ( !m_pImpl->m_nGrowSize )
|
2000-09-18 16:07:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
pBlock = m_pImpl->newGrowBlock();
|
2000-09-18 16:07:07 +00:00
|
|
|
if ( !pBlock )
|
|
|
|
return NULL;
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
pBlock->pNext = m_pImpl->m_pFirst->pNext;
|
|
|
|
m_pImpl->m_pFirst->pNext = pBlock;
|
2000-09-18 16:07:07 +00:00
|
|
|
|
|
|
|
return (void*)(pBlock->aData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
|*
|
|
|
|
|* FixedMemPool::Free()
|
|
|
|
|*
|
|
|
|
|* Beschreibung MEMPOOL.SDW
|
|
|
|
|* Ersterstellung TH 02.09.94
|
|
|
|
|* Letzte Aenderung TH 02.09.94
|
|
|
|
|*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
void FixedMemPool::Free( void* pFree )
|
|
|
|
{
|
|
|
|
if ( !pFree )
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef DBG_UTIL
|
2005-04-13 11:12:46 +00:00
|
|
|
ImpDbgPoolTest( m_pImpl );
|
2000-09-18 16:07:07 +00:00
|
|
|
#endif
|
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
FixedMemBlock* pBlock = m_pImpl->m_pFirst;
|
2000-09-18 16:07:07 +00:00
|
|
|
FixedMemBlock* pPrev = NULL;
|
|
|
|
while ( ((ULONG)pBlock->aData > (ULONG)pFree) ||
|
|
|
|
((ULONG)pFree >= ((ULONG)pBlock->aData+pBlock->nSize)) )
|
|
|
|
{
|
|
|
|
pPrev = pBlock;
|
|
|
|
pBlock = pBlock->pNext;
|
|
|
|
|
|
|
|
#ifdef DBG_UTIL
|
|
|
|
DBG_ASSERT( pBlock, "FixedMemPool - Delete: Wrong Pointer" );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
pBlock->nFree++;
|
|
|
|
*((USHORT*)pFree) = pBlock->nFirst;
|
2005-04-13 11:12:46 +00:00
|
|
|
pBlock->nFirst = (USHORT)(((ULONG)pFree-(ULONG)(pBlock->aData)) / m_pImpl->m_nTypeSize);
|
2000-09-18 16:07:07 +00:00
|
|
|
|
2005-04-13 11:12:46 +00:00
|
|
|
if ( pPrev && (pBlock->nFree*m_pImpl->m_nTypeSize == pBlock->nSize) )
|
2000-09-18 16:07:07 +00:00
|
|
|
{
|
|
|
|
pPrev->pNext = pBlock->pNext;
|
|
|
|
delete pBlock;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( pPrev )
|
|
|
|
{
|
|
|
|
pPrev->pNext = pBlock->pNext;
|
2005-04-13 11:12:46 +00:00
|
|
|
pBlock->pNext = m_pImpl->m_pFirst->pNext;
|
|
|
|
m_pImpl->m_pFirst->pNext = pBlock;
|
2000-09-18 16:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|