webengine/osswebengine/MemoryManager/Src/FastAllocator.cpp
changeset 1 7c90e6132015
parent 0 dd21522fd290
child 26 cb62a4f66ebe
equal deleted inserted replaced
0:dd21522fd290 1:7c90e6132015
    13 *
    13 *
    14 * Description:  
    14 * Description:  
    15 *
    15 *
    16 *
    16 *
    17 */
    17 */
       
    18 
    18 #include "FastAllocator.h"
    19 #include "FastAllocator.h"
    19 #include "MemoryManager.h"
    20 #include "MemoryManager.h"
    20 #include "MemoryPool.h"
    21 #include "MemoryPool.h"
    21 
    22 #include <e32std.h>
       
    23 #include <e32debug.h>
       
    24 
       
    25 
       
    26 #ifdef TRACK_ALLOCATIONS
       
    27 TAny* GetClientPtrFromRealPtr(TAny* aRealPtr)
       
    28 	{
       
    29 	return (TAny*)(((TUint8*)aRealPtr) + sizeof(THeapTrace));
       
    30 	}
       
    31 TAny* GetRealPtrFromClientPtr(TAny* aClientPtr)
       
    32 	{
       
    33 	return (TAny*)(((TUint8*)aClientPtr) - sizeof(THeapTrace));
       
    34 	}
       
    35 
       
    36 void IncreaseSizeInPlace(TInt& aSize)
       
    37 	{
       
    38 	aSize += sizeof(THeapTrace);
       
    39 	}
       
    40 void DecreaseSizeInPlace(TInt& aSize)
       
    41 	{
       
    42 	aSize -= sizeof(THeapTrace);
       
    43 	}
       
    44 
       
    45 TInt GetSizeFromRealPtr(TAny* aRealPtr)
       
    46 	{
       
    47 	return ((THeapTrace*)aRealPtr)->requestedSize;
       
    48 	}
       
    49 
       
    50 void SetSizeWithRealPtr(TAny *aRealPtr, TInt aSize)
       
    51 	{
       
    52 	((THeapTrace*)aRealPtr)->requestedSize = aSize;
       
    53 	}
       
    54 
       
    55 TUint32 GetCellIdWithRealPtr(TAny* aRealPtr)
       
    56 	{
       
    57 	return((THeapTrace*)aRealPtr)->cellId;
       
    58 	}
       
    59 
       
    60 void SetCellIdWithRealPtr(TAny* aRealPtr, TUint32 aCellId)
       
    61 	{
       
    62 	((THeapTrace*)aRealPtr)->cellId = aCellId;
       
    63 	}
       
    64 void IncrementAndSetCellIdWithRealPtr(TAny* aRealPtr, TUint32* aCellId)
       
    65 	{
       
    66 	((THeapTrace*)aRealPtr)->cellId = (*aCellId)++;
       
    67 	}
       
    68 
       
    69 
       
    70 TAny* TraceAlloc(CMemoryPool* aPool, TInt* aSize, TUint32* aCellId)
       
    71 	{
       
    72 	// add space at the start of the data area for a TInt tracking requested size
       
    73 	TInt requestedSize = *aSize;
       
    74 	IncreaseSizeInPlace(*aSize);
       
    75 	TAny *ptr = aPool->Allocate( *aSize );
       
    76     SetSizeWithRealPtr(ptr, requestedSize);
       
    77     IncrementAndSetCellIdWithRealPtr(ptr, aCellId);
       
    78     
       
    79     RDebug::Print(_L("MEM: a,%d,,%d"), GetCellIdWithRealPtr(ptr), requestedSize);
       
    80 	
       
    81     // somewhere to add a debugger to catch every 1024 allocations
       
    82     if((*aCellId & 0x0FFF) == 0)
       
    83     	{
       
    84     	aCellId = aCellId;
       
    85     	}
       
    86     
       
    87     return GetClientPtrFromRealPtr(ptr);
       
    88 	}
       
    89 
       
    90 void TraceFree(TAny **aPtr)
       
    91 	{
       
    92 	if(aPtr && *aPtr)
       
    93 		{
       
    94 		*aPtr = GetRealPtrFromClientPtr(*aPtr);
       
    95 	    RDebug::Print(_L("MEM: f,%d,%d,"), GetCellIdWithRealPtr(*aPtr), GetSizeFromRealPtr(*aPtr));
       
    96 		}
       
    97 	}
       
    98 
       
    99 TBool TracePreRealloc(TAny** aPtr, TInt *aSize, TUint32& aOriginalCellId)
       
   100 	{
       
   101 	TInt requestedSize = *aSize;
       
   102 	TBool issueNewCellId = EFalse;
       
   103 	aOriginalCellId=0;
       
   104 	if(aPtr && *aPtr)
       
   105 		{
       
   106 		*aPtr = GetRealPtrFromClientPtr(*aPtr);
       
   107 		aOriginalCellId = GetCellIdWithRealPtr(*aPtr);
       
   108 	    RDebug::Print(_L("MEM: r,%d,%d,%d"), GetCellIdWithRealPtr(*aPtr), GetSizeFromRealPtr(*aPtr), requestedSize);
       
   109 		}
       
   110 	else
       
   111 		{
       
   112 		issueNewCellId = ETrue;
       
   113 		}
       
   114 	IncreaseSizeInPlace(*aSize);
       
   115 	
       
   116 	return issueNewCellId;
       
   117 	}
       
   118 
       
   119 void TracePostRealloc(TAny** p, TInt aRequestedSize, TUint32* aNewCellId, TUint32 aOriginalCellId, TBool aIssueNewCellId)
       
   120 	{
       
   121     if(p && *p)
       
   122     	{
       
   123     	SetSizeWithRealPtr(*p, aRequestedSize);
       
   124     	if(aIssueNewCellId)
       
   125     		IncrementAndSetCellIdWithRealPtr(*p, aNewCellId);
       
   126     	else
       
   127     		SetCellIdWithRealPtr(*p, aOriginalCellId);
       
   128     	*p = GetClientPtrFromRealPtr(*p);
       
   129     	}
       
   130 	}
       
   131 #endif
    22 EXPORT_C RFastAllocator::RFastAllocator(CFastMemoryPool* aPool) : iHeap( User::Heap() ), iPool( aPool )
   132 EXPORT_C RFastAllocator::RFastAllocator(CFastMemoryPool* aPool) : iHeap( User::Heap() ), iPool( aPool )
    23 {
   133 {
    24     iHeapBase = (TUint32)iHeap.Base() & 0xffff0000;
   134     iHeapBase = (TUint32)iHeap.Base() & 0xffff0000;
       
   135 #ifdef TRACK_ALLOCATIONS
       
   136     iNextCellId = 0;
       
   137 #endif
    25 }
   138 }
    26 
   139 
    27 EXPORT_C RFastAllocator::~RFastAllocator()
   140 EXPORT_C RFastAllocator::~RFastAllocator()
    28 {
   141 {
    29 }
   142 }
    30 
   143 
    31 
   144 
    32 EXPORT_C TAny* RFastAllocator::Alloc(TInt aSize)
   145 EXPORT_C TAny* RFastAllocator::Alloc(TInt aSize)
    33 {
   146 {
       
   147 #ifdef TRACK_ALLOCATIONS
       
   148 	return TraceAlloc(iPool, &aSize, &iNextCellId);
       
   149 #else
    34     return iPool->Allocate( aSize );
   150     return iPool->Allocate( aSize );
       
   151 #endif
    35 }
   152 }
    36 
   153 
    37 EXPORT_C void RFastAllocator::Free(TAny* aPtr)
   154 EXPORT_C void RFastAllocator::Free(TAny* aPtr)
    38 {
   155 {
       
   156 #ifdef TRACK_ALLOCATIONS
       
   157 	TraceFree(&aPtr);
       
   158 #endif	
    39     // make sure we go to the right memory pool
   159     // make sure we go to the right memory pool
    40     if( IsLocatedInHeap( aPtr ) )
   160     if( IsLocatedInHeap( aPtr ) )
    41         iHeap.Free( aPtr );
   161         iHeap.Free( aPtr );
    42     else
   162     else
    43         iPool->Free( aPtr );
   163         iPool->Free( aPtr );
    44 }
   164 }
    45 
   165 
    46 EXPORT_C TAny* RFastAllocator::ReAlloc(TAny* aPtr, TInt aSize, TInt aMode)
   166 EXPORT_C TAny* RFastAllocator::ReAlloc(TAny* aPtr, TInt aSize, TInt aMode)
    47 {
   167 {
       
   168 #ifdef TRACK_ALLOCATIONS
       
   169 	TUint32 originalCellId;
       
   170 	TBool issueNewCellId = TracePreRealloc(&aPtr, &aSize, originalCellId);
       
   171 #endif
    48     TAny* p(NULL);
   172     TAny* p(NULL);
    49     TInt originalSize(0);
   173     TInt originalSize(0);
    50     
   174     
    51     // check the right memory pool
   175     // check the right memory pool
    52     if( IsLocatedInHeap( aPtr ) )
   176     if( IsLocatedInHeap( aPtr ) )
    73          (aMode == EAllowMoveOnShrink && originalSize >= aSize && p != aPtr) )
   197          (aMode == EAllowMoveOnShrink && originalSize >= aSize && p != aPtr) )
    74         {
   198         {
    75         Free(p);
   199         Free(p);
    76         return NULL;
   200         return NULL;
    77         }
   201         }
    78 
   202 #ifdef TRACK_ALLOCATIONS
       
   203     TracePostRealloc(&p, aSize, &iNextCellId, originalCellId, issueNewCellId);
       
   204 #endif
    79     return p;
   205     return p;
    80 }
   206 }
    81 
   207 
    82 EXPORT_C TInt RFastAllocator::AllocLen(const TAny* aCell) const
   208 EXPORT_C TInt RFastAllocator::AllocLen(const TAny* aCell) const
    83 {
   209 {
       
   210 #ifdef TRACK_ALLOCATIONS
       
   211 	if(aCell)
       
   212 		aCell = GetRealPtrFromClientPtr((TAny*)aCell);
       
   213 	TInt size = iPool->MemorySize( (void*)(aCell) );
       
   214 	DecreaseSizeInPlace(size);
       
   215 	return size;
       
   216 #else
    84     return iPool->MemorySize( (void*)(aCell) );
   217     return iPool->MemorySize( (void*)(aCell) );
       
   218 #endif
    85 }
   219 }
    86 
   220 
    87 EXPORT_C TInt RFastAllocator::Compress()
   221 EXPORT_C TInt RFastAllocator::Compress()
    88 {
   222 {
    89     // DO NOTHING
   223     // DO NOTHING