|
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, const string& aTimeString ) |
|
40 { |
|
41 LOG_LOW_FUNC_ENTRY("CATAllocs::AllocH"); |
|
42 string sAllocH( sAllocHString ); |
|
43 // Parse alloc & create new allocation. |
|
44 CATAlloc alloc; |
|
45 alloc.m_sTime = aTimeString; |
|
46 string sAddress = GetStringUntilNextSpace( sAllocH, true ); |
|
47 alloc.m_sSize = GetStringUntilNextSpace( sAllocH, true ); |
|
48 alloc.m_iThreadId = GetStringUntilNextSpace ( sAllocH, true ); |
|
49 alloc.m_iCSCount = _httoi( string( GetStringUntilNextSpace( sAllocH, true ) ).c_str() ); |
|
50 // Insert call stack fragment as "first" 1 because we are header. |
|
51 if ( alloc.m_iCSCount > 0 ) |
|
52 alloc.m_vCallStack.insert( pair<unsigned long,string>( 0, sAllocH ) ); |
|
53 // Add allocation |
|
54 pair< map<string,CATAlloc>::iterator, bool> ret; |
|
55 ret = m_vAllocs.insert( pair<string, CATAlloc>( sAddress, alloc ) ); |
|
56 if( ret.second == false ) |
|
57 { |
|
58 // Allocation to this memory address was already added. |
|
59 LOG_STRING( "CATAllocs: Allocating same address again, address: " << sAddress ); |
|
60 } |
|
61 } |
|
62 |
|
63 void CATAllocs::AllocF( const string& sAllocFString, const string& aTimeString ) |
|
64 { |
|
65 LOG_LOW_FUNC_ENTRY("CATAllocs::AllocF"); |
|
66 string sAllocF( sAllocFString ); |
|
67 string sAddress = GetStringUntilNextSpace( sAllocF, true ); |
|
68 string sTime = aTimeString; |
|
69 unsigned long iNumber = _httoi( string( GetStringUntilNextSpace( sAllocF, true ) ).c_str() ); |
|
70 string sCallSstack = sAllocF; |
|
71 // Find correct allocation into which add call stack fragment. |
|
72 map<string, CATAlloc>::iterator it; |
|
73 it = m_vAllocs.find( sAddress ); |
|
74 // TODO: If cannot find, create new in cache. |
|
75 if ( it == m_vAllocs.end() ) |
|
76 { |
|
77 LOG_STRING( "CATAllocs: Allocate fragment without header: " << sAddress ); |
|
78 return; |
|
79 } |
|
80 pair< map<unsigned long,string>::iterator, bool> ret; |
|
81 // Add call stack to it. |
|
82 ret = it->second.m_vCallStack.insert( pair<unsigned long,string>( iNumber, sCallSstack ) ); |
|
83 if( ret.second == false ) |
|
84 { |
|
85 LOG_STRING( "CATAllocs: Same allocation fragment again: " << sAddress ); |
|
86 } |
|
87 } |
|
88 |
|
89 // ReallocH. |
|
90 void CATAllocs::ReallocH( const string& sReallocHString, const string& aTimeString ) |
|
91 { |
|
92 LOG_LOW_FUNC_ENTRY("CATAllocs::ReallocH"); |
|
93 string sAllocH( sReallocHString ); |
|
94 // Parse alloc & create new allocation. |
|
95 CATAlloc alloc; |
|
96 string sAddress = GetStringUntilNextSpace( sAllocH, true ); |
|
97 alloc.m_sTime = aTimeString; |
|
98 alloc.m_sSize = GetStringUntilNextSpace( sAllocH, true ); |
|
99 alloc.m_iCSCount = _httoi( string( GetStringUntilNextSpace( sAllocH, true ) ).c_str() ); |
|
100 // Insert call stack fragment as "first" 1 because we are header. |
|
101 if ( alloc.m_iCSCount > 0 ) |
|
102 alloc.m_vCallStack.insert( pair<unsigned long,string>( 0, sAllocH ) ); |
|
103 // Add allocation |
|
104 pair< map<string,CATAlloc>::iterator, bool> ret; |
|
105 ret = m_vAllocs.insert( pair<string, CATAlloc>( sAddress, alloc ) ); |
|
106 if( ret.second == false ) |
|
107 { |
|
108 // Allocation to this memory address was already added. |
|
109 LOG_STRING( "CATAllocs: Allocating same address again, address: " << sAddress ); |
|
110 } |
|
111 } |
|
112 |
|
113 // ReallocF. |
|
114 void CATAllocs::ReallocF( const string& sReallocFString, const string& aTimeString ) |
|
115 { |
|
116 LOG_LOW_FUNC_ENTRY("CATAllocs::ReallocF"); |
|
117 string sAllocF( sReallocFString ); |
|
118 string sAddress = GetStringUntilNextSpace( sAllocF, true ); |
|
119 string sTime = aTimeString; |
|
120 unsigned long iNumber = _httoi( string( GetStringUntilNextSpace( sAllocF, true ) ).c_str() ); |
|
121 string sCallSstack = sAllocF; |
|
122 // Find correct allocation into which add call stack fragment. |
|
123 map<string, CATAlloc>::iterator it; |
|
124 it = m_vAllocs.find( sAddress ); |
|
125 // TODO: If cannot find, create new in cache. |
|
126 if ( it == m_vAllocs.end() ) |
|
127 { |
|
128 LOG_STRING( "CATAllocs: Allocate fragment without header: " << sAddress ); |
|
129 return; |
|
130 } |
|
131 pair< map<unsigned long,string>::iterator, bool> ret; |
|
132 // Add call stack to it. |
|
133 ret = it->second.m_vCallStack.insert( pair<unsigned long,string>( iNumber, sCallSstack ) ); |
|
134 if( ret.second == false ) |
|
135 { |
|
136 LOG_STRING( "CATAllocs: Same allocation fragment again: " << sAddress ); |
|
137 } |
|
138 } |
|
139 |
|
140 // Free message. |
|
141 void CATAllocs::Free( const string& sFreeString ) |
|
142 { |
|
143 LOG_LOW_FUNC_ENTRY("CATAllocs::Free"); |
|
144 string sFree( sFreeString ); |
|
145 string sKey = GetStringUntilNextSpace( sFree ); |
|
146 // Find related allocation. |
|
147 map<string, CATAlloc>::iterator it; |
|
148 it = m_vAllocs.find( sKey ); |
|
149 if ( it == m_vAllocs.end() ) |
|
150 { |
|
151 LOG_STRING( "CATAllocs: Free message which has no alloc pair: " << sKey ); |
|
152 return; |
|
153 } |
|
154 // Delete it. |
|
155 m_vAllocs.erase( it ); |
|
156 } |
|
157 |
|
158 // FreeH. |
|
159 void CATAllocs::FreeH( const string& sFreeH, const string& aTimeString ) |
|
160 { |
|
161 LOG_LOW_FUNC_ENTRY("CATAllocs::FreeH"); |
|
162 // Current implementation does not use call stack of |
|
163 // free message to anything. |
|
164 string sFree( sFreeH ); |
|
165 string sKey = GetStringUntilNextSpace( sFree ); |
|
166 // Time stamp. (not used currently) |
|
167 // Thread ID. (not used currently) |
|
168 // Call stack count. (not used currently) |
|
169 // Call stack data. (not used currently) |
|
170 |
|
171 // Find related allocation. |
|
172 map<string, CATAlloc>::iterator it; |
|
173 it = m_vAllocs.find( sKey ); |
|
174 if ( it == m_vAllocs.end() ) |
|
175 { |
|
176 LOG_STRING( "CATAllocs: FreeH message which has no alloc pair: " << sKey ); |
|
177 return; |
|
178 } |
|
179 // Delete it. |
|
180 m_vAllocs.erase( it ); |
|
181 } |
|
182 |
|
183 // FreeF. |
|
184 void CATAllocs::FreeF( const string& /* sFreeF */, const string& /* aTimeString */ ) |
|
185 { |
|
186 LOG_LOW_FUNC_ENTRY("CATAllocs::FreeF"); |
|
187 // Fragments are currently ignored. |
|
188 // Memory address. (not used currently) |
|
189 // Time stamp. (not used currently) |
|
190 // Packet number. (not used currently) |
|
191 // Call stack data. (not used currently) |
|
192 } |
|
193 |
|
194 // Get "leak" list ordered by allocation time. |
|
195 void CATAllocs::GetLeakList( vector<string>& vLeakList ) |
|
196 { |
|
197 LOG_LOW_FUNC_ENTRY("CATAllocs::GetLeakList"); |
|
198 |
|
199 // Create multimap where key is timestamp. |
|
200 // Add allocations there so they will go ascending order. |
|
201 multimap<string,string> mmap; |
|
202 multimap<string,string>::iterator itMmap; |
|
203 |
|
204 map<string, CATAlloc>::iterator it; |
|
205 for( it = m_vAllocs.begin(); it != m_vAllocs.end() ; it++ ) |
|
206 { |
|
207 string sTime = it->second.m_sTime; |
|
208 |
|
209 string sLine = it->first; |
|
210 sLine.append(" "); |
|
211 sLine.append( it->second.GetAllocString() ); |
|
212 |
|
213 mmap.insert( pair<string,string>( sTime, sLine ) ); |
|
214 } |
|
215 |
|
216 // Iterate list to parameter vector. |
|
217 vLeakList.clear(); |
|
218 for ( itMmap = mmap.begin(); itMmap != mmap.end(); itMmap++ ) |
|
219 vLeakList.push_back( itMmap->second ); |
|
220 } |
|
221 |
|
222 // Clear alloc data. |
|
223 void CATAllocs::ClearAllocs( void ) |
|
224 { |
|
225 LOG_LOW_FUNC_ENTRY("CATAllocs::ClearAllocs"); |
|
226 m_vAllocs.clear(); |
|
227 } |
|
228 |
|
229 //EOF |