crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbolLib/CodeSeg/CodeSegDefinitionCollection.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     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 using System;
       
    18 using System.Collections;
       
    19 using System.Collections.Generic;
       
    20 
       
    21 namespace SymbolLib.CodeSegDef
       
    22 {
       
    23 	public class CodeSegDefinitionCollection : IEnumerable<CodeSegDefinition>
       
    24 	{
       
    25 		#region Constructors & destructor
       
    26 		public CodeSegDefinitionCollection()
       
    27 			: this( 10 )
       
    28 		{
       
    29 		}
       
    30 
       
    31 		public CodeSegDefinitionCollection( int aGranularity )
       
    32 		{
       
    33 			iEntries = new List<CodeSegDefinition>( aGranularity );
       
    34 		}
       
    35 		#endregion
       
    36 
       
    37 		#region API
       
    38 		public void Reset()
       
    39 		{
       
    40 			iEntries.Clear();
       
    41 		}
       
    42 
       
    43 		public void Add( CodeSegDefinition aNewEntry )
       
    44 		{
       
    45             if ( aNewEntry.ImageFileName.Length == 0 )
       
    46             {
       
    47                 throw new ArgumentException( "Invalid code seg definition entry" );
       
    48             }
       
    49 
       
    50             // Our check-for-exists predicate
       
    51             Predicate<CodeSegDefinition> existsPredicate = delegate( CodeSegDefinition aEntry )
       
    52             {
       
    53                 return aEntry.ImageFileName.ToLower() == aNewEntry.ImageFileName.ToLower();
       
    54             };
       
    55 
       
    56             // Check whether there is already an entry that matches
       
    57             if ( !iEntries.Exists( existsPredicate ) )
       
    58             {
       
    59                 iEntries.Add( aNewEntry );
       
    60             }
       
    61             else
       
    62             {
       
    63 #if TRACE_RESOLVER
       
    64                System.Diagnostics.Debug.WriteLine( "IGNORING DUPE CodeSegDefinition: " + aNewEntry.ToString() );
       
    65 #endif
       
    66             }
       
    67 		}
       
    68 
       
    69         public void Remove( CodeSegDefinition aRemoveEntry )
       
    70         {
       
    71             if ( aRemoveEntry.ImageFileName.Length == 0 )
       
    72             {
       
    73                 throw new ArgumentException( "Invalid code seg definition entry" );
       
    74             }
       
    75 
       
    76             // Our check-for-match predicate
       
    77             Predicate<CodeSegDefinition> matchPredicate = delegate( CodeSegDefinition aEntry )
       
    78             {
       
    79                 return aEntry.ImageFileName.ToLower() == aRemoveEntry.ImageFileName.ToLower();
       
    80             };
       
    81 
       
    82             // Remove any matching entries
       
    83             iEntries.RemoveAll( matchPredicate );
       
    84         }
       
    85 
       
    86         public int IndexOf( CodeSegDefinition aEntry )
       
    87         {
       
    88             int index = -1;
       
    89             int i = 0;
       
    90             //
       
    91             foreach ( CodeSegDefinition entry in iEntries )
       
    92             {
       
    93                 if ( entry.EnvironmentFileNameAndPath.ToLower() == aEntry.EnvironmentFileNameAndPath.ToLower() )
       
    94                 {
       
    95                     index = i;
       
    96                     break;
       
    97                 }
       
    98                 ++i;
       
    99             }
       
   100             //
       
   101             return index;
       
   102         }
       
   103 
       
   104 		public void SortByFileName()
       
   105 		{
       
   106 			iEntries.Sort( new CodeSegDefinitionCollectionCompareByFileName() );
       
   107 		}
       
   108 
       
   109 		public void SortByAddress()
       
   110 		{
       
   111 			iEntries.Sort( new CodeSegDefinitionCollectionCompareByAddress() );
       
   112 		}
       
   113 
       
   114         public CodeSegDefinition DefinitionByAddress( long aAddress )
       
   115         {
       
   116             CodeSegDefinition ret = null;
       
   117             //
       
   118             foreach ( CodeSegDefinition entry in iEntries )
       
   119             {
       
   120                 if ( entry.AddressRange.Contains( aAddress ) )
       
   121                 {
       
   122                     ret = entry;
       
   123                     break;
       
   124                 }
       
   125             }
       
   126             //
       
   127             return ret;
       
   128         }
       
   129 
       
   130 		public CodeSegDefinition FindByCodeSegmentFileNameAndPath( string aImageCodeSegmentFileNameAndPath )
       
   131 		{
       
   132 			CodeSegDefinition temp = new CodeSegDefinition( string.Empty, aImageCodeSegmentFileNameAndPath );
       
   133 			int index = iEntries.IndexOf( temp );
       
   134 
       
   135 			int x = 0;
       
   136 			if	( x != 0 )
       
   137 			{
       
   138 				int count = iEntries.Count;
       
   139 				for( int i=0; i<count; i++ )
       
   140 				{
       
   141 					CodeSegDefinition e = this[ i ];
       
   142 #if TRACE_RESOLVER
       
   143 					System.Diagnostics.Debug.WriteLine( "Entry = ENV[ " + e.EnvironmentFileName + " => " + e.EnvironmentFileNameAndPath + " ] IMG[ " + e.ImageFileName + " => " + e.ImageFileNameAndPath + " ]" );
       
   144 #endif
       
   145 				}
       
   146 			}
       
   147 
       
   148 			if	( index >= 0 && index < Count )
       
   149 			{
       
   150 				temp = this[ index ];
       
   151 			}
       
   152 			else
       
   153 			{
       
   154 				// Not found
       
   155 				temp = null;
       
   156 			}
       
   157 			//
       
   158 			return temp;
       
   159 		}
       
   160 
       
   161         public void MergeInto( CodeSegDefinitionCollection aItems )
       
   162         {
       
   163             foreach ( CodeSegDefinition entry in aItems )
       
   164             {
       
   165                 Add( entry );
       
   166             }
       
   167         }
       
   168 		#endregion
       
   169 
       
   170 		#region Properties
       
   171 		public int Count
       
   172 		{
       
   173 			get { return iEntries.Count; }
       
   174 		}
       
   175 
       
   176 		public CodeSegDefinition this[ int aIndex ]
       
   177 		{
       
   178 			get { return iEntries[ aIndex ]; }
       
   179 		}
       
   180 		#endregion
       
   181 
       
   182         #region IEnumerable Members
       
   183         public IEnumerator GetEnumerator()
       
   184 		{
       
   185 			return new CodeSegDefinitionCollectionEnumerator( this );
       
   186 		}
       
   187 
       
   188         IEnumerator<CodeSegDefinition> IEnumerable<CodeSegDefinition>.GetEnumerator()
       
   189         {
       
   190             return new CodeSegDefinitionCollectionEnumerator( this );
       
   191         }
       
   192 		#endregion
       
   193 
       
   194 		#region Data members
       
   195 		private readonly List<CodeSegDefinition> iEntries;
       
   196 		#endregion
       
   197     }
       
   198 }