crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianStructuresLib/Debug/Code/Code/CodeCollectionList.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.IO;
       
    19 using System.Text;
       
    20 using System.Collections.Generic;
       
    21 using SymbianUtils;
       
    22 using SymbianStructuresLib.Arm;
       
    23 using SymbianStructuresLib.Arm.Instructions;
       
    24 using SymbianStructuresLib.Debug.Common.FileName;
       
    25 using SymbianStructuresLib.CodeSegments;
       
    26 
       
    27 namespace SymbianStructuresLib.Debug.Code
       
    28 {
       
    29     public class CodeCollectionList : IEnumerable<CodeCollection>
       
    30     {
       
    31         #region Constructors
       
    32         public CodeCollectionList()
       
    33         {
       
    34         }
       
    35         #endregion
       
    36 
       
    37         #region API
       
    38         public void Clear()
       
    39         {
       
    40             lock ( iCollections )
       
    41             {
       
    42                 iCollections.Clear();
       
    43             }
       
    44         }
       
    45 
       
    46         public void Add( CodeCollection aCollection )
       
    47         {
       
    48             lock ( iCollections )
       
    49             {
       
    50                 if ( !Contains( aCollection ) )
       
    51                 {
       
    52                     aCollection.ParentList = this;
       
    53                     iCollections.Add( aCollection );
       
    54                 }
       
    55                 else
       
    56                 {
       
    57                     throw new ArgumentException( string.Format( "Collection \'{0}\' already exists", aCollection.FileName.FileNameInHost ) );
       
    58                 }
       
    59             }
       
    60         }
       
    61 
       
    62         public void Remove( CodeCollection aCollection )
       
    63         {
       
    64             Predicate<CodeCollection> predicate = delegate( CodeCollection collection )
       
    65             {
       
    66                 return collection.Equals( aCollection );
       
    67             };
       
    68             //
       
    69             lock ( iCollections )
       
    70             {
       
    71                 iCollections.RemoveAll( predicate );
       
    72             }
       
    73         }
       
    74 
       
    75         public void RemoveUntagged()
       
    76         {
       
    77             Predicate<CodeCollection> predicate = delegate( CodeCollection collection )
       
    78             {
       
    79                 return collection.Tagged == false;
       
    80             };
       
    81             //
       
    82             lock ( iCollections )
       
    83             {
       
    84                 iCollections.RemoveAll( predicate );
       
    85             }
       
    86         }
       
    87 
       
    88         public bool Contains( uint aAddress )
       
    89         {
       
    90             Predicate<CodeCollection> predicate = delegate( CodeCollection collection )
       
    91             {
       
    92                 return collection.Contains( aAddress );
       
    93             };
       
    94             //
       
    95             bool ret = false;
       
    96             //
       
    97             lock ( iCollections )
       
    98             {
       
    99                 CodeCollection found = iCollections.Find( predicate );
       
   100                 if ( found != null )
       
   101                 {
       
   102                     // Implement last-access optimisation
       
   103                     MakeHeadOfMRU( found );
       
   104                     ret = true;
       
   105                 }
       
   106             }
       
   107             //
       
   108             return ret;
       
   109         }
       
   110 
       
   111         public bool Contains( CodeCollection aCollection )
       
   112         {
       
   113             Predicate<CodeCollection> predicate = delegate( CodeCollection collection )
       
   114             {
       
   115                 return collection.Equals( aCollection );
       
   116             };
       
   117             //
       
   118             bool ret = false;
       
   119             //
       
   120             lock ( iCollections )
       
   121             {
       
   122                 ret = iCollections.Find( predicate ) != null;
       
   123             }
       
   124             //
       
   125             return ret;
       
   126         }
       
   127 
       
   128         public bool GetInstructions( uint aAddress, TArmInstructionSet aInstructionSet, int aCount, out IArmInstruction[] aInstructions )
       
   129         {
       
   130             Predicate<CodeCollection> predicate = delegate( CodeCollection collection )
       
   131             {
       
   132                 return collection.Contains( aAddress );
       
   133             };
       
   134             //
       
   135             bool ret = false;
       
   136             //
       
   137             lock ( iCollections )
       
   138             {
       
   139                 CodeCollection found = iCollections.Find( predicate );
       
   140                 if ( found != null )
       
   141                 {
       
   142                     // Implement last-access optimisation
       
   143                     ret = found.GetInstructions( aAddress, aInstructionSet, aCount, out aInstructions );
       
   144                 }
       
   145                 else
       
   146                 {
       
   147                     aInstructions = new IArmInstruction[ 0 ];
       
   148                 }
       
   149             }
       
   150             //
       
   151             return ret;
       
   152         }
       
   153         #endregion
       
   154 
       
   155         #region Properties
       
   156         public int Count
       
   157         {
       
   158             get
       
   159             {
       
   160                 lock ( iCollections )
       
   161                 {
       
   162                     return iCollections.Count;
       
   163                 }
       
   164             }
       
   165         }
       
   166 
       
   167         public bool IsEmpty
       
   168         {
       
   169             get { return Count == 0; } 
       
   170         }
       
   171 
       
   172         public bool Tagged
       
   173         {
       
   174             set
       
   175             {
       
   176                 lock ( this )
       
   177                 {
       
   178                     Action<CodeCollection> action = delegate( CodeCollection collection )
       
   179                     {
       
   180                         collection.Tagged = value;
       
   181                     };
       
   182                     iCollections.ForEach( action );
       
   183                 }
       
   184             }
       
   185         }
       
   186 
       
   187         public object Tag
       
   188         {
       
   189             get { return iTag; }
       
   190             set
       
   191             {
       
   192                 iTag = value;
       
   193             }
       
   194         }
       
   195 
       
   196         public CodeCollection this[ int aIndex ]
       
   197         {
       
   198             get
       
   199             {
       
   200                 lock ( iCollections )
       
   201                 {
       
   202                     return iCollections[ aIndex ];
       
   203                 }
       
   204             }
       
   205         }
       
   206 
       
   207         public CodeCollection this[ PlatformFileName aFileName ]
       
   208         {
       
   209             get
       
   210             {
       
   211                 Predicate<CodeCollection> predicate = delegate( CodeCollection collection )
       
   212                 {
       
   213                     bool same = collection.FileName.Equals( aFileName );
       
   214                     return same;
       
   215                 };
       
   216                 //
       
   217                 CodeCollection ret = null;
       
   218                 //
       
   219                 lock ( iCollections )
       
   220                 {
       
   221                     ret = iCollections.Find( predicate );
       
   222                 }
       
   223                 //
       
   224                 return ret;
       
   225             }
       
   226         }
       
   227 
       
   228         public CodeCollection this[ CodeSegDefinition aCodeSegment ]
       
   229         {
       
   230             get
       
   231             {
       
   232                 Predicate<CodeCollection> predicate = delegate( CodeCollection collection )
       
   233                 {
       
   234                     bool same = collection.IsMatchingCodeSegment( aCodeSegment );
       
   235                     return same;
       
   236                 };
       
   237                 //
       
   238                 CodeCollection ret = null;
       
   239                 //
       
   240                 lock ( iCollections )
       
   241                 {
       
   242                     ret = iCollections.Find( predicate );
       
   243                 }
       
   244                 //
       
   245                 return ret;
       
   246             }
       
   247         }
       
   248         #endregion
       
   249 
       
   250         #region From IEnumerable<CodeCollection>
       
   251         IEnumerator<CodeCollection> IEnumerable<CodeCollection>.GetEnumerator()
       
   252         {
       
   253             foreach ( CodeCollection col in iCollections )
       
   254             {
       
   255                 yield return col;
       
   256             }
       
   257         }
       
   258 
       
   259         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       
   260         {
       
   261             foreach ( CodeCollection col in iCollections )
       
   262             {
       
   263                 yield return col;
       
   264             }
       
   265         }
       
   266         #endregion
       
   267 
       
   268         #region Internal methods
       
   269         private void MakeHeadOfMRU( CodeCollection aCollection )
       
   270         {
       
   271             lock ( iCollections )
       
   272             {
       
   273                 // Implement last-access optimisation
       
   274                 int pos = iCollections.IndexOf( aCollection );
       
   275                 iCollections.RemoveAt( pos );
       
   276                 iCollections.Insert( 0, aCollection );
       
   277             }
       
   278         }
       
   279         #endregion
       
   280 
       
   281         #region Data members
       
   282         private object iTag = null;
       
   283         private List<CodeCollection> iCollections = new List<CodeCollection>();
       
   284         #endregion
       
   285     }
       
   286 }