sysperfana/heapanalyser/Libraries/Engine/HeapLib/Reconstructor/RHeap/Extractor/RawItemQueue.cs
changeset 8 15296fd0af4a
equal deleted inserted replaced
7:8e12a575a9b5 8:15296fd0af4a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * Redistribution and use in source and binary forms, with or without
       
     6 * modification, are permitted provided that the following conditions are met:
       
     7 *
       
     8 * - Redistributions of source code must retain the above copyright notice,
       
     9 *   this list of conditions and the following disclaimer.
       
    10 * - Redistributions in binary form must reproduce the above copyright notice,
       
    11 *   this list of conditions and the following disclaimer in the documentation
       
    12 *   and/or other materials provided with the distribution.
       
    13 * - Neither the name of Nokia Corporation nor the names of its contributors
       
    14 *   may be used to endorse or promote products derived from this software
       
    15 *   without specific prior written permission.
       
    16 *
       
    17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
       
    21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    27 * POSSIBILITY OF SUCH DAMAGE.
       
    28 * 
       
    29 * Initial Contributors:
       
    30 * Nokia Corporation - initial contribution.
       
    31 *
       
    32 * Contributors:
       
    33 *
       
    34 * Description: 
       
    35 *
       
    36 */
       
    37 
       
    38 using System;
       
    39 using System.Text;
       
    40 using System.Text.RegularExpressions;
       
    41 using System.Threading;
       
    42 using System.Collections;
       
    43 using System.Collections.Generic;
       
    44 using SymbianUtils;
       
    45 using SymbianUtils.RawItems;
       
    46 
       
    47 namespace HeapLib.Reconstructor.RHeap.Extractor
       
    48 {
       
    49     internal class RawItemQueue
       
    50     {
       
    51         #region Constructors & destructor
       
    52         public RawItemQueue()
       
    53         {
       
    54             iItems = new List<RawItem>( 10 * 1000 );
       
    55         }
       
    56         #endregion
       
    57 
       
    58         #region API
       
    59         public void Add( byte[] aData, uint aAddress )
       
    60         {
       
    61             int length = aData.Length;
       
    62             if ( length % 4 != 0 )
       
    63             {
       
    64                 throw new ArgumentException( "Data is not a multiple of 4 bytes" );
       
    65             }
       
    66 
       
    67             for ( int i = 0; i < length; i += 4 )
       
    68             {
       
    69                 // Make helper
       
    70                 RawItemPrecursor prec = new RawItemPrecursor();
       
    71                 prec.Bytes[ 3 ] = aData[ i + 0 ];
       
    72                 prec.Bytes[ 2 ] = aData[ i + 1 ];
       
    73                 prec.Bytes[ 1 ] = aData[ i + 2 ];
       
    74                 prec.Bytes[ 0 ] = aData[ i + 3 ];
       
    75                 prec.ConvertBytesToCharacters();
       
    76 
       
    77                 // Make final item
       
    78                 RawItem ret = new RawItem();
       
    79                 ret.Address = (uint) ( aAddress + i );
       
    80                 ret.OriginalData = prec.ByteValue;
       
    81                 ret.Data = prec.ByteValueReversed;
       
    82                 ret.CharacterisedData = prec.CharValue;
       
    83                 //
       
    84                 iItems.Add( ret );
       
    85             }
       
    86         }
       
    87 
       
    88         public void ReEnqueueItem( RawItem aItem )
       
    89         {
       
    90             iItems.Insert( 0, aItem );
       
    91         }
       
    92 
       
    93         public RawItem DequeueHeadItem()
       
    94         {
       
    95             RawItem head = iItems[ 0 ];
       
    96             iItems.RemoveAt( 0 );
       
    97             return head;
       
    98         }
       
    99         #endregion
       
   100 
       
   101         #region Properties
       
   102         public int Count
       
   103         {
       
   104             get { return iItems.Count; }
       
   105         }
       
   106         #endregion
       
   107 
       
   108         #region Internal methods
       
   109         private static RawItemPrecursor CreatePrecursor( Queue<Capture> aDataQ, Queue<char> aCharQ )
       
   110         {
       
   111             System.Diagnostics.Debug.Assert( aDataQ.Count >= 4 && aCharQ.Count >= 4 );
       
   112             //
       
   113             RawItemPrecursor ret = new RawItemPrecursor();
       
   114 
       
   115             // Get data
       
   116             ret.Bytes[ 3 ] = ConvertCaptureToByteValue( aDataQ.Dequeue() );
       
   117             ret.Bytes[ 2 ] = ConvertCaptureToByteValue( aDataQ.Dequeue() );
       
   118             ret.Bytes[ 1 ] = ConvertCaptureToByteValue( aDataQ.Dequeue() );
       
   119             ret.Bytes[ 0 ] = ConvertCaptureToByteValue( aDataQ.Dequeue() );
       
   120             
       
   121             // Get characters
       
   122             ret.Chars[ 0 ] = aCharQ.Dequeue();
       
   123             ret.Chars[ 1 ] = aCharQ.Dequeue();
       
   124             ret.Chars[ 2 ] = aCharQ.Dequeue();
       
   125             ret.Chars[ 3 ] = aCharQ.Dequeue();
       
   126             
       
   127             return ret;
       
   128         }
       
   129 
       
   130         private static uint ConvertCaptureToByteValue( Capture aCapture )
       
   131         {
       
   132             uint ret = System.Convert.ToUInt32( aCapture.Value.Trim(), KBaseHex );
       
   133             return ret;
       
   134         }
       
   135 
       
   136         private static uint GetValueFromByteArray( uint[] aArray )
       
   137         {
       
   138             uint ret = aArray[ 0 ];
       
   139             //
       
   140             ret += ( aArray[ 1 ] << 8 );
       
   141             ret += ( aArray[ 2 ] << 16 );
       
   142             ret += ( aArray[ 3 ] << 24 );
       
   143             //
       
   144             return ret;
       
   145         }
       
   146 
       
   147         private static uint GetReversedValueFromByteArray( uint[] aArray )
       
   148         {
       
   149             // Get original bytes
       
   150             uint b1 = aArray[ 0 ];
       
   151             uint b2 = aArray[ 1 ];
       
   152             uint b3 = aArray[ 2 ];
       
   153             uint b4 = aArray[ 3 ];
       
   154 
       
   155             // Build reversed value
       
   156             uint ret = b4;
       
   157             ret += ( b3 << 8 );
       
   158             ret += ( b2 << 16 );
       
   159             ret += ( b1 << 24 );
       
   160             //
       
   161             return ret;
       
   162         }
       
   163 
       
   164         private static string GetCharacterisedDataFromCharacterQueue( Queue<char> aCharQ )
       
   165         {
       
   166             char[] chars = { '.', '.', '.', '.' };
       
   167             //
       
   168             if ( aCharQ.Count >= 4 )
       
   169             {
       
   170                 char c1 = aCharQ.Dequeue();
       
   171                 char c2 = aCharQ.Dequeue();
       
   172                 char c3 = aCharQ.Dequeue();
       
   173                 char c4 = aCharQ.Dequeue();
       
   174                 //
       
   175                 chars[ 0 ] = c1;
       
   176                 chars[ 1 ] = c2;
       
   177                 chars[ 2 ] = c3;
       
   178                 chars[ 3 ] = c4;
       
   179             }
       
   180 
       
   181             // It's very dumb that this is the only way to get a char array into a string.
       
   182             StringBuilder ret = new StringBuilder();
       
   183             foreach( char c in chars )
       
   184             {
       
   185                 ret.Append( c );
       
   186             }
       
   187             return ret.ToString();
       
   188         }
       
   189 
       
   190         private static RawItem CreateRawItem( uint aAddress, Queue<Capture> aDataQ, Queue<char> aCharQ )
       
   191         {
       
   192             RawItemPrecursor precusor = CreatePrecursor( aDataQ, aCharQ );
       
   193             //
       
   194             RawItem ret = new RawItem();
       
   195             ret.Address = aAddress;
       
   196             ret.OriginalData = precusor.ByteValue;
       
   197             ret.Data = precusor.ByteValueReversed;
       
   198             ret.CharacterisedData = precusor.CharValueReversed;
       
   199             //
       
   200             return ret;
       
   201         }
       
   202         #endregion
       
   203 
       
   204         #region Internal constants
       
   205         private const int KBaseHex = 16;
       
   206         #endregion
       
   207 
       
   208         #region Data members
       
   209         private readonly List<RawItem> iItems;
       
   210         #endregion
       
   211     }
       
   212 }