dmitryh 2004/08/11 06:03:59
Modified: c/src/xalanc/Include XalanMemMgrAutoPtr.hpp
Added: c/src/xalanc/Include XalanMemMngArrayAllocate.hpp
XalanMemoryManagement.hpp
Log:
Experimental implementation of the memory management utilities
Revision Changes Path
1.3 +219 -16 xml-xalan/c/src/xalanc/Include/XalanMemMgrAutoPtr.hpp
Index: XalanMemMgrAutoPtr.hpp
===================================================================
RCS file: /home/cvs/xml-xalan/c/src/xalanc/Include/XalanMemMgrAutoPtr.hpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- XalanMemMgrAutoPtr.hpp 21 Jul 2004 17:53:42 -0000 1.2
+++ XalanMemMgrAutoPtr.hpp 11 Aug 2004 13:03:59 -0000 1.3
@@ -23,15 +23,14 @@
-#include <xercesc/framework/MemoryManager.hpp>
-
-
-
+#include <xalanc/Include/XalanMemoryManagement.hpp>
#include <cstddef>
#include <cassert>
+#include <utility>
+
XALAN_CPP_NAMESPACE_BEGIN
@@ -41,12 +40,12 @@
// We're using our own auto_ptr-like class due to wide
// variations amongst the varous platforms we have to
// support
-template<class Type>
+template< class Type,
+ bool toCallDestructor >
class XalanMemMgrAutoPtr
{
public:
- typedef XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager
MemoryManagerType;
typedef XALAN_STD_QUALIFIER pair<MemoryManagerType*, Type*>
AutoPtrPairType;
class MemMgrAutoPtrData : public AutoPtrPairType
@@ -78,22 +77,25 @@
invariants();
if ( isInitilized() )
- {
- second->~Type();
+ {
+ if ( toCallDestructor )
+ {
+ second->~Type();
+ }
first->deallocate(second);
}
}
void
- reset( MemoryManagerType* _m_memoryManager ,
- Type* _m_dataPointer )
+ reset( MemoryManagerType* m_memoryManager ,
+ Type* m_dataPointer )
{
invariants();
- first = _m_memoryManager;
+ first = m_memoryManager;
- second = _m_dataPointer;
+ second = m_dataPointer;
invariants();
}
@@ -120,14 +122,13 @@
{
}
- XalanMemMgrAutoPtr(const XalanMemMgrAutoPtr<Type>& theSource) :
+ XalanMemMgrAutoPtr(const XalanMemMgrAutoPtr<Type,toCallDestructor>&
theSource) :
m_pointerInfo(((XalanMemMgrAutoPtr<Type>&)theSource).release())
{
}
-
- XalanMemMgrAutoPtr<Type>&
- operator=(XalanMemMgrAutoPtr<Type>& theRHS)
+ XalanMemMgrAutoPtr<Type,toCallDestructor>&
+ operator=(XalanMemMgrAutoPtr<Type,toCallDestructor>& theRHS)
{
if (this != &theRHS)
{
@@ -186,6 +187,208 @@
// data member
MemMgrAutoPtrData m_pointerInfo;
+};
+
+
+
+
+template< class Type>
+class XalanMemMgrAutoPtrArray
+{
+public:
+
+ typedef unsigned int size_type;
+
+ class MemMgrAutoPtrArrayData
+ {
+ public:
+ MemMgrAutoPtrArrayData():
+ m_memoryManager(0),
+ m_dataArray(0),
+ m_size(0)
+ {
+ }
+
+ MemMgrAutoPtrArrayData(
+ MemoryManagerType* memoryManager,
+ Type* dataPointer,
+ size_type size):
+ m_memoryManager(memoryManager),
+ m_dataArray(dataPointer),
+ m_size(size)
+ {
+ invariants();
+ }
+
+
+ bool
+ isInitilized()const
+ {
+ return ( (m_memoryManager != 0) && (m_dataArray != 0)
&& (m_size != 0) )? true : false;
+ }
+
+ void
+ deallocate()
+ {
+ invariants();
+
+ if ( isInitilized() )
+ {
+ assert ( m_dataArray != 0 );
+
+ Type* runPtr = m_dataArray;
+
+ for ( size_type i = 0; i < m_size ; ++i )
+ {
+ runPtr->~Type();
+ }
+ XalanMemoryManagement::deallocate (
m_dataArray, m_memoryManager );
+ }
+ }
+
+ void
+ reset( MemoryManagerType* memoryManager ,
+ Type* dataPointer,
+ size_type size)
+ {
+ invariants();
+
+ m_memoryManager = memoryManager;
+
+ m_dataArray = dataPointer;
+
+ m_size = size;
+
+ invariants();
+ }
+
+
+ MemoryManagerType* m_memoryManager;
+ Type* m_dataArray;
+ size_type m_size;
+
+ private:
+ void
+ invariants()const
+ {
+ assert( isInitilized() ||
+ ( (m_memoryManager == 0) &&
(m_dataArray ==0) && (m_size == 0)) );
+ }
+
+
+
+
+ };
+
+ XalanMemMgrAutoPtrArray(
+ MemoryManagerType* theManager,
+ Type* ptr,
+ size_type size) :
+ m_pointerInfo(theManager, ptr, size)
+ {
+ }
+
+ XalanMemMgrAutoPtrArray() :
+ m_pointerInfo()
+ {
+ }
+
+ XalanMemMgrAutoPtrArray(const XalanMemMgrAutoPtrArray<Type>&
theSource) :
+ m_pointerInfo(((XalanMemMgrAutoPtr<Type>&)theSource).release())
+ {
+ }
+
+
+ XalanMemMgrAutoPtrArray<Type>&
+ operator=(XalanMemMgrAutoPtrArray<Type>& theRHS)
+ {
+ if (this != &theRHS)
+ {
+ m_pointerInfo.deallocate();
+
+ m_pointerInfo = theRHS.release();
+ }
+
+ return *this;
+ }
+
+ ~XalanMemMgrAutoPtrArray()
+ {
+ m_pointerInfo.deallocate();
+ }
+
+ Type&
+ operator*() const
+ {
+ return *m_pointerInfo.m_dataArray;
+ }
+
+ Type*
+ operator->() const
+ {
+ return m_pointerInfo.m_dataArray;
+ }
+
+ Type*
+ get() const
+ {
+ return m_pointerInfo.m_dataArray;
+ }
+
+ size_type
+ getSize()const
+ {
+ return m_pointerInfo.m_size;
+ }
+
+ XalanMemMgrAutoPtrArray<Type>&
+ operator++ ()
+ {
+ ++m_pointerInfo.m_size;
+
+ return *this;
+ }
+
+ XalanMemMgrAutoPtrArray<Type>
+ operator++ (int)
+ {
+ XalanMemMgrAutoPtrArray<Type> temp = *this;
+ ++*this;
+
+ return temp;
+ }
+
+ MemMgrAutoPtrArrayData
+ release()
+ {
+ MemMgrAutoPtrArrayData tmp = m_pointerInfo;
+
+ m_pointerInfo.reset( 0 , 0 , 0);
+
+ return MemMgrAutoPtrArrayData(tmp);
+ }
+
+ void
+ reset( MemoryManagerType* theManager = 0,
+ Type* thePointer = 0 ,
+ size_type size = 0)
+ {
+ m_pointerInfo.deallocate();
+
+ m_pointerInfo.reset ( theManager , thePointer , size );
+ }
+
+ Type&
+ operator[](size_type index) const
+ {
+ return m_pointerInfo.m_dataArray[index];
+ }
+
+private:
+
+
+ // data member
+ MemMgrAutoPtrArrayData m_pointerInfo;
};
1.1
xml-xalan/c/src/xalanc/Include/XalanMemMngArrayAllocate.hpp
Index: XalanMemMngArrayAllocate.hpp
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if !defined(XALANMEMMGRARRAYALLOCATE_HEADER_GUARD_1357924680)
#define XALANMEMMGRARRAYALLOCATE_HEADER_GUARD_1357924680
// Base include file. Must be first.
#include <xalanc/PlatformSupport/PlatformSupportDefinitions.hpp>
#include <xalanc/Include/XalanMemMgrAutoPtr.hpp>
XALAN_CPP_NAMESPACE_BEGIN
template <class Type>
class XalanMemMngArrayAllocate
{
public:
static Type*
allocate( size_t size,
MemoryManagerType* memoryManager)
{
XalanMemMgrAutoPtrArray<Type> theGuard ( memoryManager,
(Type*)XalanMemoryManagement::allocate(
sizeof(Type)*size , memoryManager ),
0 );
size_t allocated = 0;
for ( Type* runPtr = theGuard.get() ; allocated < size ; ++
allocated )
{
new ( runPtr + allocated ) Type();
++theGuard;
}
Type* theResult = theGuard.get();
theGuard.release();
return theResult;
}
static void
deallocate ( Type* ptr,
size_t size,
MemoryManagerType* memoryManager)
{
assert ( ptr != 0 );
Type* runPtr = ptr;
for ( size_t i = 0; i < size ; ++i )
{
runPtr->~Type();
}
XalanMemoryManagement::deallocate ( ptr, memoryManager );
}
};
XALAN_CPP_NAMESPACE_END
#endif // if !defined(XALANMEMMGRARRAYALLOCATE_HEADER_GUARD_1357924680)
1.1 xml-xalan/c/src/xalanc/Include/XalanMemoryManagement.hpp
Index: XalanMemoryManagement.hpp
===================================================================
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if !defined(XALANMEMORYMANAGEMENT_HEADER_GUARD_1357924680)
#define XALANMEMORYMANAGEMENT_HEADER_GUARD_1357924680
// Base include file. Must be first.
#include <xalanc/Include/PlatformDefinitions.hpp>
#include <xercesc/framework/MemoryManager.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <cassert>
XALAN_CPP_NAMESPACE_BEGIN
typedef XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager
MemoryManagerType;
class XalanMemoryManagement
{
public:
static void*
allocate( size_t size,
MemoryManagerType* memoryManager )
{
void* theResult = 0;
if ( memoryManager == 0 )
{
theResult = ::operator new ( size );
}
else
{
theResult = memoryManager->allocate( size );
}
return theResult;
}
static void
deallocate( void* pDataPointer,
MemoryManagerType* memoryManager )
{
if ( memoryManager == 0 )
{
::operator delete ( pDataPointer );
}
else
{
memoryManager->deallocate( pDataPointer );
}
}
static MemoryManagerType*
chooseMemMgrForXerces( MemoryManagerType* memoryManager )
{
if ( memoryManager == 0 )
{
XALAN_USING_XERCES(XMLPlatformUtils)
return XMLPlatformUtils::fgMemoryManager;
}
else
{
return memoryManager;
}
}
};
XALAN_CPP_NAMESPACE_END
#endif // XALANMEMORYMANAGEMENT_HEADER_GUARD_1357924680
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]