webengine/osswebengine/MemoryManager/Src/FastAllocator.cpp
changeset 1 7c90e6132015
parent 0 dd21522fd290
child 26 cb62a4f66ebe
--- a/webengine/osswebengine/MemoryManager/Src/FastAllocator.cpp	Mon Mar 30 12:54:55 2009 +0300
+++ b/webengine/osswebengine/MemoryManager/Src/FastAllocator.cpp	Fri May 08 08:25:06 2009 +0300
@@ -15,13 +15,126 @@
 *
 *
 */
+
 #include "FastAllocator.h"
 #include "MemoryManager.h"
 #include "MemoryPool.h"
+#include <e32std.h>
+#include <e32debug.h>
 
+
+#ifdef TRACK_ALLOCATIONS
+TAny* GetClientPtrFromRealPtr(TAny* aRealPtr)
+	{
+	return (TAny*)(((TUint8*)aRealPtr) + sizeof(THeapTrace));
+	}
+TAny* GetRealPtrFromClientPtr(TAny* aClientPtr)
+	{
+	return (TAny*)(((TUint8*)aClientPtr) - sizeof(THeapTrace));
+	}
+
+void IncreaseSizeInPlace(TInt& aSize)
+	{
+	aSize += sizeof(THeapTrace);
+	}
+void DecreaseSizeInPlace(TInt& aSize)
+	{
+	aSize -= sizeof(THeapTrace);
+	}
+
+TInt GetSizeFromRealPtr(TAny* aRealPtr)
+	{
+	return ((THeapTrace*)aRealPtr)->requestedSize;
+	}
+
+void SetSizeWithRealPtr(TAny *aRealPtr, TInt aSize)
+	{
+	((THeapTrace*)aRealPtr)->requestedSize = aSize;
+	}
+
+TUint32 GetCellIdWithRealPtr(TAny* aRealPtr)
+	{
+	return((THeapTrace*)aRealPtr)->cellId;
+	}
+
+void SetCellIdWithRealPtr(TAny* aRealPtr, TUint32 aCellId)
+	{
+	((THeapTrace*)aRealPtr)->cellId = aCellId;
+	}
+void IncrementAndSetCellIdWithRealPtr(TAny* aRealPtr, TUint32* aCellId)
+	{
+	((THeapTrace*)aRealPtr)->cellId = (*aCellId)++;
+	}
+
+
+TAny* TraceAlloc(CMemoryPool* aPool, TInt* aSize, TUint32* aCellId)
+	{
+	// add space at the start of the data area for a TInt tracking requested size
+	TInt requestedSize = *aSize;
+	IncreaseSizeInPlace(*aSize);
+	TAny *ptr = aPool->Allocate( *aSize );
+    SetSizeWithRealPtr(ptr, requestedSize);
+    IncrementAndSetCellIdWithRealPtr(ptr, aCellId);
+    
+    RDebug::Print(_L("MEM: a,%d,,%d"), GetCellIdWithRealPtr(ptr), requestedSize);
+	
+    // somewhere to add a debugger to catch every 1024 allocations
+    if((*aCellId & 0x0FFF) == 0)
+    	{
+    	aCellId = aCellId;
+    	}
+    
+    return GetClientPtrFromRealPtr(ptr);
+	}
+
+void TraceFree(TAny **aPtr)
+	{
+	if(aPtr && *aPtr)
+		{
+		*aPtr = GetRealPtrFromClientPtr(*aPtr);
+	    RDebug::Print(_L("MEM: f,%d,%d,"), GetCellIdWithRealPtr(*aPtr), GetSizeFromRealPtr(*aPtr));
+		}
+	}
+
+TBool TracePreRealloc(TAny** aPtr, TInt *aSize, TUint32& aOriginalCellId)
+	{
+	TInt requestedSize = *aSize;
+	TBool issueNewCellId = EFalse;
+	aOriginalCellId=0;
+	if(aPtr && *aPtr)
+		{
+		*aPtr = GetRealPtrFromClientPtr(*aPtr);
+		aOriginalCellId = GetCellIdWithRealPtr(*aPtr);
+	    RDebug::Print(_L("MEM: r,%d,%d,%d"), GetCellIdWithRealPtr(*aPtr), GetSizeFromRealPtr(*aPtr), requestedSize);
+		}
+	else
+		{
+		issueNewCellId = ETrue;
+		}
+	IncreaseSizeInPlace(*aSize);
+	
+	return issueNewCellId;
+	}
+
+void TracePostRealloc(TAny** p, TInt aRequestedSize, TUint32* aNewCellId, TUint32 aOriginalCellId, TBool aIssueNewCellId)
+	{
+    if(p && *p)
+    	{
+    	SetSizeWithRealPtr(*p, aRequestedSize);
+    	if(aIssueNewCellId)
+    		IncrementAndSetCellIdWithRealPtr(*p, aNewCellId);
+    	else
+    		SetCellIdWithRealPtr(*p, aOriginalCellId);
+    	*p = GetClientPtrFromRealPtr(*p);
+    	}
+	}
+#endif
 EXPORT_C RFastAllocator::RFastAllocator(CFastMemoryPool* aPool) : iHeap( User::Heap() ), iPool( aPool )
 {
     iHeapBase = (TUint32)iHeap.Base() & 0xffff0000;
+#ifdef TRACK_ALLOCATIONS
+    iNextCellId = 0;
+#endif
 }
 
 EXPORT_C RFastAllocator::~RFastAllocator()
@@ -31,11 +144,18 @@
 
 EXPORT_C TAny* RFastAllocator::Alloc(TInt aSize)
 {
+#ifdef TRACK_ALLOCATIONS
+	return TraceAlloc(iPool, &aSize, &iNextCellId);
+#else
     return iPool->Allocate( aSize );
+#endif
 }
 
 EXPORT_C void RFastAllocator::Free(TAny* aPtr)
 {
+#ifdef TRACK_ALLOCATIONS
+	TraceFree(&aPtr);
+#endif	
     // make sure we go to the right memory pool
     if( IsLocatedInHeap( aPtr ) )
         iHeap.Free( aPtr );
@@ -45,6 +165,10 @@
 
 EXPORT_C TAny* RFastAllocator::ReAlloc(TAny* aPtr, TInt aSize, TInt aMode)
 {
+#ifdef TRACK_ALLOCATIONS
+	TUint32 originalCellId;
+	TBool issueNewCellId = TracePreRealloc(&aPtr, &aSize, originalCellId);
+#endif
     TAny* p(NULL);
     TInt originalSize(0);
     
@@ -75,13 +199,23 @@
         Free(p);
         return NULL;
         }
-
+#ifdef TRACK_ALLOCATIONS
+    TracePostRealloc(&p, aSize, &iNextCellId, originalCellId, issueNewCellId);
+#endif
     return p;
 }
 
 EXPORT_C TInt RFastAllocator::AllocLen(const TAny* aCell) const
 {
+#ifdef TRACK_ALLOCATIONS
+	if(aCell)
+		aCell = GetRealPtrFromClientPtr((TAny*)aCell);
+	TInt size = iPool->MemorySize( (void*)(aCell) );
+	DecreaseSizeInPlace(size);
+	return size;
+#else
     return iPool->MemorySize( (void*)(aCell) );
+#endif
 }
 
 EXPORT_C TInt RFastAllocator::Compress()