memana/analyzetoolclient/commandlineengine/internal/src/catallocs.cpp
changeset 0 f0f2b8682603
equal deleted inserted replaced
-1:000000000000 0:f0f2b8682603
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 #include "../inc/ATCommonDefines.h"
       
    18 #include "../inc/catallocs.h"
       
    19 
       
    20 void CATAllocs::Alloc( const string& sAllocString )
       
    21 {
       
    22 	LOG_LOW_FUNC_ENTRY("CATAllocs::Alloc");
       
    23 	string sAlloc( sAllocString );
       
    24 	CATAlloc alloc;
       
    25 	string sAddress = GetStringUntilNextSpace( sAlloc, true );
       
    26 	alloc.m_sTime = GetStringUntilNextSpace( sAlloc, true );
       
    27 	alloc.m_sSize = GetStringUntilNextSpace( sAlloc, true );
       
    28 	alloc.m_vCallStack.insert( pair<unsigned long,string>(1, sAlloc) );
       
    29 	// Add allocation
       
    30 	pair< map<string,CATAlloc>::iterator, bool> ret;
       
    31 	ret = m_vAllocs.insert( pair<string, CATAlloc>( sAddress, alloc ) );
       
    32 	if( ret.second == false )
       
    33 	{
       
    34 		// Allocation to this memory address was already added.
       
    35 		LOG_STRING( "CATAllocs: Allocating same address again, address: " << sAddress );
       
    36 	}
       
    37 }
       
    38 
       
    39 void CATAllocs::AllocH( const string& sAllocHString )
       
    40 {
       
    41 	LOG_LOW_FUNC_ENTRY("CATAllocs::AllocH");
       
    42 	string sAllocH( sAllocHString );
       
    43 	// Parse alloc & create new allocation.
       
    44 	CATAlloc alloc;
       
    45 	string sAddress = GetStringUntilNextSpace( sAllocH, true );
       
    46 	alloc.m_sTime = GetStringUntilNextSpace( sAllocH, true );
       
    47 	alloc.m_sSize = GetStringUntilNextSpace( sAllocH, true );
       
    48 	alloc.m_iCSCount = _httoi( string( GetStringUntilNextSpace( sAllocH, true ) ).c_str() );
       
    49 	// Insert call stack fragment as "first" 1 because we are header.
       
    50 	if ( alloc.m_iCSCount > 0 )
       
    51 		alloc.m_vCallStack.insert( pair<unsigned long,string>( 0, sAllocH ) );
       
    52 	// Add allocation
       
    53 	pair< map<string,CATAlloc>::iterator, bool> ret;
       
    54 	ret = m_vAllocs.insert( pair<string, CATAlloc>( sAddress, alloc ) );
       
    55 	if( ret.second == false )
       
    56 	{
       
    57 		// Allocation to this memory address was already added.
       
    58 		LOG_STRING( "CATAllocs: Allocating same address again, address: " << sAddress );
       
    59 	}
       
    60 }
       
    61 
       
    62 void CATAllocs::AllocF( const string& sAllocFString )
       
    63 {
       
    64 	LOG_LOW_FUNC_ENTRY("CATAllocs::AllocF");
       
    65 	string sAllocF( sAllocFString );
       
    66 	string sAddress = GetStringUntilNextSpace( sAllocF, true );
       
    67 	string sTime = GetStringUntilNextSpace( sAllocF, true );
       
    68 	unsigned long iNumber = _httoi( string( GetStringUntilNextSpace( sAllocF, true ) ).c_str() );
       
    69 	string sCallSstack = sAllocF;
       
    70 	// Find correct allocation into which add call stack fragment.
       
    71 	map<string, CATAlloc>::iterator it;
       
    72 	it = m_vAllocs.find( sAddress );
       
    73 	// TODO: If cannot find, create new in cache.
       
    74 	if ( it == m_vAllocs.end() )
       
    75 	{
       
    76 		LOG_STRING( "CATAllocs: Allocate fragment without header: " << sAddress );
       
    77 		return;
       
    78 	}
       
    79 	pair< map<unsigned long,string>::iterator, bool> ret;
       
    80 	// Add call stack to it.
       
    81 	ret = it->second.m_vCallStack.insert( pair<unsigned long,string>( iNumber, sCallSstack ) );
       
    82 	if( ret.second == false )
       
    83 	{
       
    84 		LOG_STRING( "CATAllocs: Same allocation fragment again: " << sAddress );
       
    85 	}
       
    86 }
       
    87 
       
    88 // Free message.
       
    89 void CATAllocs::Free( const string& sFreeString )
       
    90 {
       
    91 	LOG_LOW_FUNC_ENTRY("CATAllocs::Free");
       
    92 	string sFree( sFreeString );
       
    93 	string sKey = GetStringUntilNextSpace( sFree );
       
    94 	// Find related allocation.
       
    95 	map<string, CATAlloc>::iterator it;
       
    96 	it = m_vAllocs.find( sKey );
       
    97 	if ( it == m_vAllocs.end() )
       
    98 	{
       
    99 		LOG_STRING( "CATAllocs: Free message which has no alloc pair: " << sKey );
       
   100 		return;
       
   101 	}
       
   102 	// Delete it.
       
   103 	m_vAllocs.erase( it );
       
   104 }
       
   105 
       
   106 // FreeH.
       
   107 void CATAllocs::FreeH( const string& sFreeH )
       
   108 {
       
   109 	LOG_LOW_FUNC_ENTRY("CATAllocs::FreeH");
       
   110 	// Current implementation does not use call stack of
       
   111 	// free message to anything.
       
   112 	string sFree( sFreeH );
       
   113 	string sKey = GetStringUntilNextSpace( sFree );
       
   114 	// Time stamp. (not used currently)
       
   115 	// Call stack count. (not used currently)
       
   116 	// Call stack data. (not used currently)
       
   117 
       
   118 	// Find related allocation.
       
   119 	map<string, CATAlloc>::iterator it;
       
   120 	it = m_vAllocs.find( sKey );
       
   121 	if ( it == m_vAllocs.end() )
       
   122 	{
       
   123 		LOG_STRING( "CATAllocs: FreeH message which has no alloc pair: " << sKey );
       
   124 		return;
       
   125 	}
       
   126 	// Delete it.
       
   127 	m_vAllocs.erase( it );
       
   128 }
       
   129 
       
   130 // FreeF.
       
   131 void CATAllocs::FreeF( const string& /* sFreeF */ )
       
   132 {
       
   133 	LOG_LOW_FUNC_ENTRY("CATAllocs::FreeF");
       
   134 	// Fragments are currently ignored.
       
   135 	// Memory address. (not used currently)
       
   136 	// Time stamp. (not used currently)
       
   137 	// Packet number. (not used currently)
       
   138 	// Call stack data. (not used currently)
       
   139 }
       
   140 
       
   141 // Get "leak" list ordered by allocation time.
       
   142 void CATAllocs::GetLeakList( vector<string>& vLeakList )
       
   143 {
       
   144 	LOG_LOW_FUNC_ENTRY("CATAllocs::GetLeakList");
       
   145 
       
   146 	// Create multimap where key is timestamp.
       
   147 	// Add allocations there so they will go ascending order.
       
   148 	multimap<string,string> mmap;
       
   149 	multimap<string,string>::iterator itMmap;
       
   150 	
       
   151 	map<string, CATAlloc>::iterator it;
       
   152 	for( it = m_vAllocs.begin(); it != m_vAllocs.end() ; it++ )
       
   153 	{
       
   154 		string sTime = it->second.m_sTime;
       
   155 		
       
   156 		string sLine = it->first;
       
   157 		sLine.append(" ");
       
   158 		sLine.append( it->second.GetAllocString() );
       
   159 
       
   160 		mmap.insert( pair<string,string>( sTime, sLine ) );
       
   161 	}
       
   162 	
       
   163 	// Iterate list to parameter vector.
       
   164 	vLeakList.clear();
       
   165 	for ( itMmap = mmap.begin(); itMmap != mmap.end(); itMmap++ )
       
   166 		vLeakList.push_back( itMmap->second );
       
   167 }
       
   168 
       
   169 // Clear alloc data.
       
   170 void CATAllocs::ClearAllocs( void )
       
   171 {
       
   172 	LOG_LOW_FUNC_ENTRY("CATAllocs::ClearAllocs");
       
   173 	m_vAllocs.clear();
       
   174 }
       
   175 
       
   176 //EOF