crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianSymbolLib/Plugins/SLPluginSymbol/Data/SymbolFileData.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2004-2008 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 
       
    18 using System;
       
    19 using System.IO;
       
    20 using System.Collections.Generic;
       
    21 using System.Text;
       
    22 using System.Threading;
       
    23 using SymbianStructuresLib.Debug.Symbols;
       
    24 using SymbianStructuresLib.Debug.Symbols.Interfaces;
       
    25 using SymbianSymbolLib.SourceManagement.Source;
       
    26 using SymbianSymbolLib.SourceManagement.Provisioning;
       
    27 using SymbianUtils;
       
    28 using SymbianUtils.Tracer;
       
    29 using SymbianUtils.FileTypes;
       
    30 using SymbianUtils.Threading;
       
    31 using SLPluginSymbol.Source;
       
    32 
       
    33 namespace SLPluginSymbol.Data
       
    34 {
       
    35     internal class SymbolFileData : DisposableObject, IEnumerable<SymbolFileSegment>
       
    36     {
       
    37         #region Delegates & events
       
    38         public delegate void DataPreparedHandler( SymbolFileData aData );
       
    39         public event DataPreparedHandler DataPrepared = delegate { };
       
    40         #endregion
       
    41 
       
    42         #region Constructors
       
    43         public SymbolFileData( SymbolSource aSource )
       
    44 		{
       
    45             iSource = aSource;
       
    46             iData = File.ReadAllBytes( aSource.URI );
       
    47         }
       
    48         #endregion
       
    49 
       
    50         #region API
       
    51         public void Split( int aSegmentCount )
       
    52         {
       
    53             int chunkSize = (int) iData.Length / aSegmentCount;
       
    54             int blockPosStart = 0;
       
    55             int length = 0;
       
    56             //
       
    57             for ( int i = 0; i < aSegmentCount; i++ )
       
    58             {
       
    59                 int pos = 0;
       
    60                 int blockPosEnd = Math.Min( iData.Length - 1, blockPosStart + chunkSize );
       
    61                 while ( pos >= 0 )
       
    62                 {
       
    63                     pos = Array.IndexOf( iData, KPattern[ 0 ], blockPosEnd );
       
    64                     if ( pos > 0 )
       
    65                     {
       
    66                         if ( pos + 8 >= iData.Length )
       
    67                         {
       
    68                             break;
       
    69                         }
       
    70                         else if ( pos + 8 < iData.Length && iData[ pos + 7 ] == KPattern[ 7 ] )
       
    71                         {
       
    72                             bool isMatch = CompareByteArrays( KPattern, iData, pos );
       
    73                             if ( isMatch )
       
    74                             {
       
    75                                 length = pos - blockPosStart;
       
    76                                 //System.Diagnostics.Debug.WriteLine( string.Format( "Block {0:d2} @ 0x{1:x8}, length: {2:d8}", i, blockPosStart, length ) );
       
    77                                 //
       
    78                                 iSegments.Add( new SymbolFileSegment( this, blockPosStart, length ) );
       
    79                                 blockPosStart = pos;
       
    80                                 break;
       
    81                             }
       
    82                             else
       
    83                             {
       
    84                                 // Didn't find a match, move forwards
       
    85                                 blockPosEnd = pos + 1;
       
    86                             }
       
    87                         }
       
    88                         else
       
    89                         {
       
    90                             // Didn't find a match, move forwards
       
    91                             blockPosEnd = pos + 1;
       
    92                         }
       
    93                     }
       
    94                     else
       
    95                     {
       
    96                         // Searched to end of file and didn't find another block, so just create
       
    97                         // a new reader for everything that remains.
       
    98                         length = iData.Length - blockPosStart;
       
    99                         //System.Diagnostics.Debug.WriteLine( string.Format( "Block {0:d2} @ 0x{1:x8}, length: {2:d8}", i, blockPosStart, length ) );
       
   100                         iSegments.Add( new SymbolFileSegment( this, blockPosStart, length ) );
       
   101                         //
       
   102                         break;
       
   103                     }
       
   104                 }
       
   105             }
       
   106 
       
   107             iCollectionIdentifier = new MultiThreadedProcessor<SymbolFileSegment>( this );
       
   108         }
       
   109 
       
   110         public void FindCollections()
       
   111         {
       
   112             System.Diagnostics.Debug.Assert( iCollectionIdentifier != null );
       
   113             iCollectionIdentifier.ProcessItem += new MultiThreadedProcessor<SymbolFileSegment>.ItemProcessor( CollectionIdentifier_ProcessItem );
       
   114             iCollectionIdentifier.EventHandler += new MultiThreadedProcessor<SymbolFileSegment>.ProcessorEventHandler( CollectionIdentifier_EventHandler );
       
   115             iCollectionIdentifier.Start( TSynchronicity.EAsynchronous );
       
   116         }
       
   117         #endregion
       
   118 
       
   119         #region Properties
       
   120         public SymbolSource Source
       
   121         {
       
   122             get { return iSource; }
       
   123         }
       
   124         #endregion
       
   125 
       
   126         #region Operators
       
   127         public static implicit operator byte[]( SymbolFileData aData )
       
   128         {
       
   129             return aData.iData;
       
   130         }
       
   131         #endregion
       
   132 
       
   133         #region Internal constants
       
   134         private static readonly byte[] KPattern = new byte[] { (byte) 'F', (byte) 'r', (byte) 'o', (byte) 'm', (byte) ' ', (byte) ' ', (byte) ' ', (byte) ' ' };
       
   135         #endregion
       
   136 
       
   137         #region Event handlers
       
   138         private void CollectionIdentifier_EventHandler( MultiThreadedProcessor<SymbolFileSegment>.TEvent aEvent )
       
   139         {
       
   140             switch ( aEvent )
       
   141             {
       
   142             default:
       
   143                 break;
       
   144             case MultiThreadedProcessor<SymbolFileSegment>.TEvent.EEventStarting:
       
   145                 break;
       
   146             case MultiThreadedProcessor<SymbolFileSegment>.TEvent.EEventCompleted:
       
   147                 iCollectionIdentifier.ProcessItem -= new MultiThreadedProcessor<SymbolFileSegment>.ItemProcessor( CollectionIdentifier_ProcessItem );
       
   148                 iCollectionIdentifier.EventHandler -= new MultiThreadedProcessor<SymbolFileSegment>.ProcessorEventHandler( CollectionIdentifier_EventHandler );
       
   149                 iCollectionIdentifier.Dispose();
       
   150                 iCollectionIdentifier = null;
       
   151                 DataPrepared( this );
       
   152                 break;
       
   153             }
       
   154         }
       
   155 
       
   156         private void CollectionIdentifier_ProcessItem( SymbolFileSegment aItem )
       
   157         {
       
   158             aItem.PerformInitialCollectionIdentification();
       
   159         }
       
   160         #endregion
       
   161 
       
   162         #region Internal methods
       
   163         private static bool CompareByteArrays( byte[] aSearchFor, byte[] aSearchIn, int aStartPos )
       
   164         {
       
   165             bool areEqual = true;
       
   166             //
       
   167             for ( int i = 0; i < aSearchFor.Length; i++ )
       
   168             {
       
   169                 byte c = aSearchFor[ i ];
       
   170 
       
   171                 int bigBufferIndex = aStartPos + i;
       
   172                 if ( bigBufferIndex > aSearchIn.Length )
       
   173                 {
       
   174                     // We ran out of data
       
   175                     areEqual = false;
       
   176                     break;
       
   177                 }
       
   178                 else
       
   179                 {
       
   180                     byte b = aSearchIn[ bigBufferIndex ];
       
   181                     if ( b != c )
       
   182                     {
       
   183                         areEqual = false;
       
   184                         break;
       
   185                     }
       
   186                 }
       
   187             }
       
   188             //
       
   189             return areEqual;
       
   190         }
       
   191         #endregion
       
   192         
       
   193         #region From IEnumerable<SymbolFileSegment>
       
   194         public IEnumerator<SymbolFileSegment> GetEnumerator()
       
   195         {
       
   196             foreach( SymbolFileSegment segment in iSegments )
       
   197             {
       
   198                 yield return segment;
       
   199             }
       
   200         }
       
   201 
       
   202         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       
   203         {
       
   204             foreach ( SymbolFileSegment segment in iSegments )
       
   205             {
       
   206                 yield return segment;
       
   207             }
       
   208         }
       
   209         #endregion
       
   210 
       
   211         #region From DisposableObject
       
   212         protected override void CleanupManagedResources()
       
   213         {
       
   214             try
       
   215             {
       
   216                 base.CleanupManagedResources();
       
   217             }
       
   218             finally
       
   219             {
       
   220                 foreach ( SymbolFileSegment seg in iSegments )
       
   221                 {
       
   222                     seg.Dispose();
       
   223                 }
       
   224                 //
       
   225                 if ( iCollectionIdentifier != null )
       
   226                 {
       
   227                     iCollectionIdentifier.Dispose();
       
   228                     iCollectionIdentifier = null;
       
   229                 }
       
   230                 //
       
   231                 iSegments.Clear();
       
   232                 iData = null;
       
   233             }
       
   234         }
       
   235         #endregion
       
   236 
       
   237         #region Data members
       
   238         private readonly SymbolSource iSource;
       
   239         private byte[] iData = null;
       
   240         private List<SymbolFileSegment> iSegments = new List<SymbolFileSegment>();
       
   241         private MultiThreadedProcessor<SymbolFileSegment> iCollectionIdentifier;
       
   242         #endregion
       
   243     }
       
   244 }