analyzetool/dynamicmemoryhook/src/analyzetoolallocator.cpp
branchRCL_3
changeset 59 8ad140f3dd41
parent 49 7fdc9a71d314
--- a/analyzetool/dynamicmemoryhook/src/analyzetoolallocator.cpp	Wed Sep 15 13:53:27 2010 +0300
+++ b/analyzetool/dynamicmemoryhook/src/analyzetoolallocator.cpp	Wed Oct 13 16:17:58 2010 +0300
@@ -37,25 +37,27 @@
 // might leave.
 // -----------------------------------------------------------------------------
 //
-RAnalyzeToolAllocator::RAnalyzeToolAllocator( TBool aNotFirst,
+RAnalyzeToolAllocator::RAnalyzeToolAllocator( TBool aNotFirst, 
+                                              RATStorageServer& aStorageServer, 
                                               RArray<TCodeblock>& aCodeblocks, 
                                               RMutex& aMutex,
                                               TUint aProcessId,
                                               RAnalyzeTool& aAnalyzeTool,
+                                              TBool aStorageServerOpen,
                                               TUint32 aLogOption,
                                               TUint32 aAllocCallStackSize,
-                                              TUint32 aFreeCallStackSize,
-                                              RATFileLog& aLogFile ) :
+                                              TUint32 aFreeCallStackSize ) :
     RAnalyzeToolMemoryAllocator( aNotFirst ),
+    iStorageServer( aStorageServer ), 
     iCodeblocks( aCodeblocks ), 
     iMutex( aMutex ),
     iProcessId( aProcessId ),
     iThreadArray( KATMaxCallstackLength ),
     iAnalyzeTool( aAnalyzeTool ),
+    iStorageServerOpen( aStorageServerOpen ),
     iLogOption( aLogOption ),
     iAllocMaxCallStack( aAllocCallStackSize ),
-    iFreeMaxCallStack( aFreeCallStackSize ),
-    iLogFile ( aLogFile )
+    iFreeMaxCallStack( aFreeCallStackSize )
     {
     LOGSTR1( "ATMH RAnalyzeToolAllocator::RAnalyzeToolAllocator()" );
     
@@ -69,16 +71,6 @@
         LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
         error = iThreadArray.Append( TThreadStack( RThread().Id(), 
                              params().iStackAddress + params().iStackSize ) );
-
-        if ( iLogOption == EATLogToTraceFast )
-        	{
-            // log thread added
-            ATFastLogThreadStarted( aProcessId, RThread().Id().operator TUint() ); 
-        	}
-        else if ( iLogOption == EATLogToFile )
-        	{
-            iLogFile.ATFileLogThreadStarted( RThread().Id().operator TUint() );
-        	}
         }
     
     __ASSERT_ALWAYS( KErrNone == error, AssertPanic( ECantAppendToTheArray ) ); 
@@ -93,20 +85,6 @@
     {
     LOGSTR1( "ATMH RAnalyzeToolAllocator::~RAnalyzeToolAllocator()" );
     
-    for( TInt i=0; i<iThreadArray.Count(); i++)
-    	{
-        // log thread removed
-        if ( iLogOption == EATLogToTraceFast )
-        	{
-            ATFastLogThreadEnded( RProcess().Id().operator TUint(), RThread().Id().operator TUint() ); 
-            }
-        else if ( iLogOption == EATLogToFile )
-        	{
-            iLogFile.ATFileLogThreadEnded( RThread().Id().operator TUint() );
-        	}
-         }
-    
-    
     // Close the thread array 
     iThreadArray.Close();
     }
@@ -156,67 +134,88 @@
     // Acquire the mutex
     iMutex.Wait();
     
-    // get thread ID
-    TUint threadId = RThread().Id();
     // Alloc memory from the original allocator
     TAny* p = iAllocator->Alloc( aSize );
     
     LOGSTR3( "ATMH RAnalyzeToolAllocator::Alloc() - aSize: %i, address: %x", 
              aSize,  (TUint32) p );
     
-	// Reset the callstack
-	iCallStack.Reset();
-
-	// Find the current thread callstack start address
-	TUint32 stackstart( 0 );
-	TBool found( FindCurrentThreadStack( stackstart ) );
-	LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
-	
-	TUint32 _sp;
-	__asm
-		{
-		mov [_sp], esp
-		}
-	
-	// Get codeblocks count
-	TInt blocksCount( iCodeblocks.Count() );
-	TInt error( KErrNone );
-	TUint arrayCounter = 0;
-	
-	for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
-		{
-		TUint32 addr = (TUint32) *( (TUint32*) i );
-		if ( ! IsAddressLoadedCode( addr ) )
-			continue;
-		for ( TInt j = 0; j < blocksCount; j++ )
-			{
-			if ( iCodeblocks[j].CheckAddress( addr ) )
-				{
-				// To avoid recursive call to ReAlloc specifying granularity
-				// Add address to the callstack
-				iCallStack[arrayCounter] = ( addr );
-				arrayCounter++;
-				break;
-				}
-			}
-		if ( arrayCounter == KATMaxCallstackLength ||
-			 arrayCounter == iAllocMaxCallStack )
-			{
-			LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
-			break;
-			}
-		}
-	// Log the memory allocation information
-	if ( iLogOption == EATLogToTraceFast )
-		{
-		// Using fast mode.
-		ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize, threadId );
-		}
-	else if ( iLogOption == EATLogToFile )
-		{
-		iLogFile.ATFileLogMemoryAllocated( (TUint32) p, iCallStack, aSize, threadId );
-		}
-   
+    // Don't collect call stack and log data
+    // if storage server not open or logging mode not fast.
+    if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
+        {
+        // Reset the callstack
+        iCallStack.Reset();
+    
+        // Find the current thread callstack start address
+        TUint32 stackstart( 0 );
+        TBool found( FindCurrentThreadStack( stackstart ) );
+        LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
+        
+        TUint32 _sp;
+        __asm
+            {
+            mov [_sp], esp
+            }
+        
+        // Get codeblocks count
+        TInt blocksCount( iCodeblocks.Count() );
+        TInt error( KErrNone );
+        TUint arrayCounter = 0;
+        
+        for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
+            {
+            TUint32 addr = (TUint32) *( (TUint32*) i );
+            if ( ! IsAddressLoadedCode( addr ) )
+                continue;
+            for ( TInt j = 0; j < blocksCount; j++ )
+                {
+                if ( iCodeblocks[j].CheckAddress( addr ) )
+                    {
+                    // To avoid recursive call to ReAlloc specifying granularity
+                    // Add address to the callstack
+                    iCallStack[arrayCounter] = ( addr );
+                    arrayCounter++;
+                    break;
+                    }
+                }
+            if ( arrayCounter == KATMaxCallstackLength ||
+                 arrayCounter == iAllocMaxCallStack )
+                {
+                LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
+                break;
+                }
+            }
+        // Log the memory allocation information
+        if ( iLogOption == EATLogToTraceFast )
+            {
+            // Using fast mode.
+            ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize );
+            }
+        else
+            {
+            // Using storage server.
+            error = iStorageServer.LogMemoryAllocated( (TUint32) p,
+                                                       iCallStack,
+                                                       aSize );
+            if ( KErrNone != error )
+                {
+                LOGSTR2( "ATMH LogMemoryAllocated error %i", error );
+                switch ( error )
+                    {
+                    case KErrNoMemory:
+                    LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - KErrNoMemory case"  );
+                    if ( iStorageServerOpen )
+                        {
+                        iStorageServerOpen = EFalse;
+                        LOGSTR1( "ATMH RAnalyzeToolMainAllocator::Alloc() - close iStorageServer"  );
+                        iStorageServer.Close();
+                        }
+                    break;
+                    }
+                }
+            }
+        }
     // Release the mutex
     iMutex.Signal();
     
@@ -236,59 +235,79 @@
     // Acquire the mutex
     iMutex.Wait();
     
-    // get thread ID
-    TUint threadId = RThread().Id();
-        
     // Alloc memory from the original allocator
     TAny* p = iAllocator->Alloc( aSize );
+    
+    // Don't collect call stack and log data
+    // if storage server not open or logging mode not fast.
+    if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
+        {
+        // Reset the callstack
+        iCallStack.Reset(); 
         
-
-	// Reset the callstack
-	iCallStack.Reset(); 
-	
-	// Find the current thread callstack start address
-	TUint32 stackstart( 0 );
-	TBool found( FindCurrentThreadStack( stackstart ) );
-	LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
-	
-	// Get codeblocks count
-	TInt blocksCount( iCodeblocks.Count() );
-	TUint arrayCounter = 0;
-	
-	for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
-		{
-		TUint32 addr = (TUint32) *( (TUint32*) i );
-		if ( ! IsAddressLoadedCode( addr ) )
-			continue;
-		for ( TInt j = 0; j < blocksCount; j++ )
-			{
-			if ( iCodeblocks[j].CheckAddress( addr ) )
-				{
-				// To avoid recursive call to ReAlloc specifying granularity
-				// Add address to the callstack
-				iCallStack[arrayCounter] = ( addr );
-				arrayCounter++;
-				break;
-				}
-			}
-		if ( arrayCounter == KATMaxCallstackLength ||
-			 arrayCounter == iAllocMaxCallStack )
-			{
-			LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
-			break;
-			}
-		}
-	// Log the memory allocation information
-	if ( iLogOption == EATLogToTraceFast )
-		{
-		// Fast mode.
-		ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize, threadId );
-		} 
-	else if ( iLogOption == EATLogToFile )
-		{
-	    iLogFile.ATFileLogMemoryAllocated( (TUint32) p, iCallStack, aSize, threadId );
-		}
-
+        // Find the current thread callstack start address
+        TUint32 stackstart( 0 );
+        TBool found( FindCurrentThreadStack( stackstart ) );
+        LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
+        
+        // Get codeblocks count
+        TInt blocksCount( iCodeblocks.Count() );
+        TInt error( KErrNone );
+        TUint arrayCounter = 0;
+        
+        for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
+            {
+            TUint32 addr = (TUint32) *( (TUint32*) i );
+            if ( ! IsAddressLoadedCode( addr ) )
+                continue;
+            for ( TInt j = 0; j < blocksCount; j++ )
+                {
+                if ( iCodeblocks[j].CheckAddress( addr ) )
+                    {
+                    // To avoid recursive call to ReAlloc specifying granularity
+                    // Add address to the callstack
+                    iCallStack[arrayCounter] = ( addr );
+                    arrayCounter++;
+                    break;
+                    }
+                }
+            if ( arrayCounter == KATMaxCallstackLength ||
+                 arrayCounter == iAllocMaxCallStack )
+                {
+                LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
+                break;
+                }
+            }
+        // Log the memory allocation information
+        if ( iLogOption == EATLogToTraceFast )
+            {
+            // Fast mode.
+            ATFastLogMemoryAllocated( iProcessId, (TUint32) p, iCallStack, aSize );
+            }
+        else
+            {
+            // Using storage server.
+            error = iStorageServer.LogMemoryAllocated( (TUint32) p, 
+                                                        iCallStack, 
+                                                        aSize );
+            if ( KErrNone != error )
+                {
+                LOGSTR2( "ATMH LogMemoryAllocated error %i", error );
+                switch ( error )
+                    {
+                    case KErrNoMemory:
+                    LOGSTR1( "ATMH RAnalyzeToolAllocator::Alloc() - KErrNoMemory case"  );
+                    if ( iStorageServerOpen )
+                        {
+                        iStorageServerOpen = EFalse;
+                        LOGSTR1( "ATMH RAnalyzeToolAllocator::Alloc() - close iStorageServer"  );
+                        iStorageServer.Close();
+                        }
+                    break;
+                    }
+                }
+            }
+        }
  
     // Release the mutex
     iMutex.Signal(); 
@@ -310,68 +329,76 @@
     // Acquire the mutex
     iMutex.Wait();
     
-    // get thread ID
-    TUint threadId = RThread().Id();
-    
-	// Reset the callstack
-	iFreeCallStack.Reset();
-	
-	if ( iFreeMaxCallStack > 0 )
-		{
-		// Find the current thread callstack start address
-		TUint32 stackstart( 0 );
-		TBool found( FindCurrentThreadStack( stackstart ) );
-		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
-		TUint32 _sp;
-		
-		#ifdef __WINS__
-			__asm
-				{
-				mov [_sp], esp
-				}
-		#else
-			_sp = __current_sp();
-		#endif
-		
-		// Get codeblocks count
-		TInt blocksCount( iCodeblocks.Count() );
-		TUint arrayCounter = 0;
-	
-		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
-			{
-			TUint32 addr = (TUint32) *( (TUint32*) i );
-			if ( ! IsAddressLoadedCode( addr ) )
-				continue;
-			for ( TInt j = 0; j < blocksCount; j++ )
-				{
-				if ( iCodeblocks[j].CheckAddress( addr ) )
-					{
-					// To avoid recursive call to ReAlloc specifying granularity
-					// Add address to the callstack
-					iFreeCallStack[arrayCounter] = addr;
-					arrayCounter++;
-					break;
-					}
-				}
-			if ( arrayCounter == KATMaxFreeCallstackLength ||
-				 arrayCounter == iFreeMaxCallStack )
-				{
-				break;
-				}
-			}
-		LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter );
-		}
-	
-	// Log freed memory.
-	if ( iLogOption == EATLogToTraceFast )
-		{
-		// Using fast mode.
-		ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack, threadId );
-		} 
-	else if ( iLogOption == EATLogToFile )
-		{
-		iLogFile.ATFileLogMemoryFreed( (TUint32) aPtr, iFreeCallStack, threadId );
-		}
+    // Don't collect or log data if storage server not open or logging mode not fast.
+    if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
+        {
+        // Reset the callstack
+        iFreeCallStack.Reset();
+        
+        // Check if trace logging mode because free call stack is not used in other log options.
+        if ( (iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast )
+                && iFreeMaxCallStack > 0 )
+            {
+            // Find the current thread callstack start address
+            TUint32 stackstart( 0 );
+            TBool found( FindCurrentThreadStack( stackstart ) );
+            LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
+            TUint32 _sp;
+            
+            #ifdef __WINS__
+                __asm
+                    {
+                    mov [_sp], esp
+                    }
+            #else
+                _sp = __current_sp();
+            #endif
+            
+            // Get codeblocks count
+            TInt blocksCount( iCodeblocks.Count() );
+            TUint arrayCounter = 0;
+        
+            for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
+                {
+                TUint32 addr = (TUint32) *( (TUint32*) i );
+                if ( ! IsAddressLoadedCode( addr ) )
+                    continue;
+                for ( TInt j = 0; j < blocksCount; j++ )
+                    {
+                    if ( iCodeblocks[j].CheckAddress( addr ) )
+                        {
+                        // To avoid recursive call to ReAlloc specifying granularity
+                        // Add address to the callstack
+                        iFreeCallStack[arrayCounter] = addr;
+                        arrayCounter++;
+                        break;
+                        }
+                    }
+                if ( arrayCounter == KATMaxFreeCallstackLength ||
+                     arrayCounter == iFreeMaxCallStack )
+                    {
+                    break;
+                    }
+                }
+            LOGSTR2( "ATMH > iFreeCallStack count ( %i )", arrayCounter );
+            }
+        
+        // Log freed memory.
+        if ( iLogOption == EATLogToTraceFast )
+            {
+            // Using fast mode.
+            ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
+            }
+        else
+            {
+            // Using storage server.
+            TInt err( iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack ) );
+            if ( err != KErrNone )
+                {
+                LOGSTR2( "ATMH > LogMemoryFreed err( %i )", err );
+                }
+            }
+        }
     
     // Free the memory using original allocator
     iAllocator->Free( aPtr ); 
@@ -413,15 +440,6 @@
             LOGSTR2( "ATMH Thread stack size:    %x", params().iStackSize );
             iThreadArray.Append( TThreadStack( RThread().Id(), 
                     params().iStackAddress + params().iStackSize ) );
-            if ( iLogOption == EATLogToTraceFast )
-            	{
-                // log thread added
-                ATFastLogThreadStarted( RProcess().Id().operator TUint(), RThread().Id().operator TUint()); 
-            	}
-            else if ( iLogOption == EATLogToFile )
-				{
-				iLogFile.ATFileLogThreadStarted( RThread().Id().operator TUint() );
-				}
             }
         }
     
@@ -458,16 +476,6 @@
             {
             // Remove the thread
             iThreadArray.Remove( i );
-            if ( iLogOption == EATLogToTraceFast )
-            	{
-                // log thread removed
-                ATFastLogThreadEnded( RProcess().Id().operator TUint(), RThread().Id().operator TUint() ); 
-            	}
-            else if ( iLogOption == EATLogToFile )
-				{
-				iLogFile.ATFileLogThreadEnded( RThread().Id().operator TUint() );
-				}
-            
             break;
             }
         }
@@ -489,9 +497,6 @@
 
     // Acquire the mutex
     iMutex.Wait();
-    
-    // get thread ID
-    TUint threadId = RThread().Id();
 
     // Realloc the memory using original allocator
     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
@@ -504,61 +509,117 @@
         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aSize: %i, aMode: %i", 
                 aSize, aMode );
 
-
-		// Reset the callstack
-		iReCallStack.Reset(); 
+        // Don't collect or log data if storage server not open or logging mode fast.
+        if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
+            {
+            // Reset the callstack
+            iReCallStack.Reset(); 
 
-		// Find the current thread callstack start address
-		TUint32 stackstart( 0 ); 
-		TBool found( FindCurrentThreadStack( stackstart ) );
-		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
-		
-		// Get current sp
-		TUint32 _sp( 0 );
-		__asm
-			{
-			mov [_sp], esp
-			}
-		
-		// Get codeblocks count
-		TInt blocksCount( iCodeblocks.Count() );
-		TInt error( KErrNone );
-		TUint arrayCounter = 0;
-		
-		for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
-			{
-			TUint32 addr = (TUint32) *( (TUint32*) i );
-			if ( ! IsAddressLoadedCode( addr ) )
-				continue;
-			for ( TInt j = 0; j < blocksCount; j++ )
-				{
-				if ( iCodeblocks[j].CheckAddress( addr ) )
-					{
-					// To avoid recursive call to ReAlloc specifying granularity
-					// Add address to the callstack
-					iReCallStack[arrayCounter] = addr;
-					arrayCounter++;
-					break;
-					}
-				}
-			if ( arrayCounter == KATMaxCallstackLength || 
-				 arrayCounter == iAllocMaxCallStack )
-				{
-				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
-				break;
-				}
-			}
-
-		if ( iLogOption == EATLogToTraceFast )
-			{
-			// Using fast logging mode.
-			ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
-			}
-		else if ( iLogOption == EATLogToFile )
-			{
-			iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
-			}	
-		}
+            // Find the current thread callstack start address
+            TUint32 stackstart( 0 ); 
+            TBool found( FindCurrentThreadStack( stackstart ) );
+            LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
+            
+            // Get current sp
+            TUint32 _sp( 0 );
+            __asm
+                {
+                mov [_sp], esp
+                }
+            
+            // Get codeblocks count
+            TInt blocksCount( iCodeblocks.Count() );
+            TInt error( KErrNone );
+            TUint arrayCounter = 0;
+            
+            for ( TUint32 i = _sp; i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
+                {
+                TUint32 addr = (TUint32) *( (TUint32*) i );
+                if ( ! IsAddressLoadedCode( addr ) )
+                    continue;
+                for ( TInt j = 0; j < blocksCount; j++ )
+                    {
+                    if ( iCodeblocks[j].CheckAddress( addr ) )
+                        {
+                        // To avoid recursive call to ReAlloc specifying granularity
+                        // Add address to the callstack
+                        iReCallStack[arrayCounter] = addr;
+                        arrayCounter++;
+                        break;
+                        }
+                    }
+                if ( arrayCounter == KATMaxCallstackLength || 
+                     arrayCounter == iAllocMaxCallStack )
+                    {
+                    LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
+                    break;
+                    }
+                }
+            
+            // No need to report free if the aPtr was NULL
+            if ( aPtr != NULL )
+                {
+                // Reset the free callstack
+                iFreeCallStack.Reset();
+                
+                // if trace logging mode(s) we also log call stack in free.
+                if ( ( iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast )
+                        && iFreeMaxCallStack > 0 )
+                    {
+                    for ( TInt i = 0; i < arrayCounter; i++ )
+                        {
+                        if ( i == KATMaxFreeCallstackLength || i == iFreeMaxCallStack )
+                            {
+                            break;
+                            }
+                        iFreeCallStack[i] = iReCallStack[i];
+                        }
+                    }
+                
+                // Try to remove old address from the storage server's
+                // leak array. If found it's removed from the array because system frees
+                // old address directly in the RHeap in ReAlloc case.
+                if ( iLogOption == EATLogToTraceFast )
+                    {
+                    ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
+                    }
+                else
+                    {
+                    iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack );
+                    }
+                }
+            
+            // Log the memory allocation information
+            if ( iLogOption == EATLogToTraceFast )
+                {
+                // Using fast mode.
+                ATFastLogMemoryAllocated( iProcessId, (TUint32) ptr, iFreeCallStack, aSize);
+                }
+            else
+                {
+                // Using storage server.
+                error = iStorageServer.LogMemoryAllocated( (TUint32) ptr, 
+                                                            iReCallStack, 
+                                                            aSize );
+                if ( KErrNone != error )
+                    {
+                    LOGSTR2( "ATMH LogMemoryAllocated ReAlloc error %i", error );
+                    switch ( error )
+                        {
+                        case KErrNoMemory:
+                        LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc() - KErrNoMemory case"  );
+                        if ( iStorageServerOpen )
+                            {
+                            iStorageServerOpen = EFalse;
+                            LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc() - close iStorageServer"  );
+                            iStorageServer.Close();
+                            }
+                        break;
+                        }
+                    }
+                }
+            }
+        }
     
     // Release the mutex
     iMutex.Signal();
@@ -580,9 +641,6 @@
 
     // Acquire the mutex
     iMutex.Wait();
-    
-    // get thread ID
-    TUint threadId = RThread().Id();
 
     // Realloc the memory using original allocator
     TAny* ptr = iAllocator->ReAlloc( aPtr, aSize, aMode );
@@ -595,55 +653,110 @@
         LOGSTR3( "ATMH RAnalyzeToolAllocator::ReAlloc() - aSize: %i, aMode: %i", 
                 aSize, aMode );
 
-		// Reset the callstack
-		iReCallStack.Reset(); 
-
-		// Find the current thread callstack start address
-		TUint32 stackstart( 0 ); 
-		TBool found( FindCurrentThreadStack( stackstart ) );
-		LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
-		
-		// Get codeblocks count
-		TInt blocksCount( iCodeblocks.Count() );
+        // Don't collect or log data if storage server not open or logging mode fast.
+        if ( iStorageServerOpen || iLogOption == EATLogToTraceFast )
+            {
+            // Reset the callstack
+            iReCallStack.Reset(); 
 
-		TUint arrayCounter = 0;
-		
-		for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
-			{
-			TUint32 addr = (TUint32) *( (TUint32*) i );
-			if ( ! IsAddressLoadedCode( addr ) )
-				continue;
-			for ( TInt j = 0; j < blocksCount; j++ )
-				{
-				if ( iCodeblocks[j].CheckAddress( addr ) )
-					{
-					// To avoid recursive call to ReAlloc specifying granularity
-					// Add address to the callstack
-					iReCallStack[arrayCounter] = ( addr );
-					arrayCounter++;
-					break;
-					}
-				}
-			if ( arrayCounter == KATMaxCallstackLength || 
-				 arrayCounter == iAllocMaxCallStack )
-				{
-				LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
-				break;
-				}
-			}
-
-		if ( iLogOption == EATLogToTraceFast )
-			{
-			// Using fast logging mode.
-			ATFastLogMemoryReallocated( iProcessId, (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
-			}
-		else if ( iLogOption == EATLogToFile )
-			{
-			iLogFile.ATFileLogMemoryReallocated( (TUint32) aPtr, (TUint32) ptr, iReCallStack, aSize, threadId );
-			}
-		
-		
-		}
+            // Find the current thread callstack start address
+            TUint32 stackstart( 0 ); 
+            TBool found( FindCurrentThreadStack( stackstart ) );
+            LOGSTR3( "ATMH > stackstart: %x , found = %i", stackstart, found );
+            
+            // Get codeblocks count
+            TInt blocksCount( iCodeblocks.Count() );
+            TInt error( KErrNone );
+            TUint arrayCounter = 0;
+            
+            for ( TUint32 i = __current_sp(); i < stackstart; i = i + KAddressLength )//lint !e1055 !e526 !e628 !e348
+                {
+                TUint32 addr = (TUint32) *( (TUint32*) i );
+                if ( ! IsAddressLoadedCode( addr ) )
+                    continue;
+                for ( TInt j = 0; j < blocksCount; j++ )
+                    {
+                    if ( iCodeblocks[j].CheckAddress( addr ) )
+                        {
+                        // To avoid recursive call to ReAlloc specifying granularity
+                        // Add address to the callstack
+                        iReCallStack[arrayCounter] = ( addr );
+                        arrayCounter++;
+                        break;
+                        }
+                    }
+                if ( arrayCounter == KATMaxCallstackLength || 
+                     arrayCounter == iAllocMaxCallStack )
+                    {
+                    LOGSTR2( "ATMH > Wanted CallStack items ready( %i )", arrayCounter );
+                    break;
+                    }
+                }
+            
+            // No need to report free if the aPtr was NULL
+            if ( aPtr != NULL )
+                {
+                // Reset the free callstack
+                iFreeCallStack.Reset();
+                
+                // if trace logging mode(s) we also log call stack with free.
+                if ( ( iLogOption == EATUseDefault || iLogOption == EATLogToTrace || iLogOption == EATLogToTraceFast )
+                        && iFreeMaxCallStack > 0 )
+                    {
+                    for ( TInt i = 0; i < arrayCounter; i++ )
+                        {
+                        if ( i == iFreeCallStack.Count() )
+                            {
+                            break;
+                            }
+                        iFreeCallStack[i] = iReCallStack[i];
+                        }
+                    }
+                
+                // Try to remove old address from the storage server's
+                // leak array. If found it's removed from the array because system frees
+                // old address directly in the RHeap in ReAlloc case.
+                if ( iLogOption == EATLogToTraceFast )
+                    {
+                    ATFastLogMemoryFreed( iProcessId, (TUint32) aPtr, iFreeCallStack );
+                    }
+                else
+                    {
+                    iStorageServer.LogMemoryFreed( (TUint32) aPtr, iFreeCallStack );
+                    }
+                }
+        
+            // Log the memory allocation information
+            if ( iLogOption == EATLogToTraceFast )
+                {
+                // Using fast mode.
+                ATFastLogMemoryAllocated( iProcessId, (TUint32) ptr, iReCallStack, aSize );
+                }
+            else
+                {
+                // Using storage server.
+                error = iStorageServer.LogMemoryAllocated( (TUint32) ptr, 
+                                                            iReCallStack, 
+                                                            aSize );
+                if ( KErrNone != error )
+                    {
+                    LOGSTR2( "ATMH LogMemoryAllocated ReAlloc error %i", error );
+                    switch ( error )
+                        {
+                        case KErrNoMemory:
+                        LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc() - KErrNoMemory case"  );
+                        if ( iStorageServerOpen )
+                            {
+                            iStorageServerOpen = EFalse;
+                            LOGSTR1( "ATMH RAnalyzeToolAllocator::ReAlloc() - close iStorageServer"  );
+                            iStorageServer.Close();
+                            }
+                        break;
+                        }
+                    }
+                }
+            }
+        }
 
     // Release the mutex
     iMutex.Signal();