sysperfana/heapanalyser/Libraries/Engine/HeapLib/Array/HeapCellArrayBase.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.Collections;
       
    41 using System.Collections.Generic;
       
    42 using HeapLib.Cells;
       
    43 
       
    44 namespace HeapLib.Array
       
    45 {
       
    46 	public abstract class HeapCellArrayBase : IEnumerable<HeapCell>
       
    47 	{
       
    48 		#region Constructors & destructor
       
    49 		public HeapCellArrayBase()
       
    50         {
       
    51 		}
       
    52 		#endregion
       
    53 
       
    54 		#region API - abstract
       
    55         public abstract void Clear();
       
    56 
       
    57         public abstract void Add( HeapCell aCell );
       
    58 
       
    59         public abstract void Remove( HeapCell aCell );
       
    60 
       
    61         public abstract int CellIndex( HeapCell aCell );
       
    62         #endregion
       
    63 
       
    64         #region API
       
    65         public bool Contains( HeapCell aCell )
       
    66         {
       
    67             bool exists = CellByAddress( aCell.Address ) != null;
       
    68             return exists;
       
    69         }
       
    70 
       
    71         public HeapCell CellByAddress( uint aAddress )
       
    72 		{
       
    73 			int index = 0;
       
    74             HeapCell ret = CellByAddress( aAddress, out index );
       
    75             return ret;
       
    76 		}
       
    77 
       
    78         public HeapCell CellByExactAddress( uint aAddress )
       
    79 		{
       
    80             int index = 0;
       
    81             HeapCell ret = CellByExactAddress( aAddress, out index );
       
    82             return ret;
       
    83         }
       
    84 
       
    85         public virtual HeapCell CellByAddress( uint aAddress, out int aIndex )
       
    86         {
       
    87             HeapCell ret = null;
       
    88             aIndex = -1;
       
    89             //
       
    90             int i = 0;
       
    91             foreach( HeapCell cell in this )
       
    92             {
       
    93                 HeapCell.TRegion region = cell.RegionForAddress( aAddress );
       
    94                 //
       
    95                 if	( region == HeapCell.TRegion.EHeader || region == HeapCell.TRegion.EPayload )
       
    96                 {
       
    97                     aIndex = i;
       
    98                     ret = cell;
       
    99                     break;
       
   100                 }
       
   101                 else
       
   102                 {
       
   103                     i++;
       
   104                 }
       
   105             }
       
   106             //
       
   107             return ret;
       
   108         }
       
   109 
       
   110         public virtual HeapCell CellByExactAddress( uint aAddress, out int aIndex )
       
   111         {
       
   112             aIndex  = -1;
       
   113             HeapCell dummyCell = new HeapCell();
       
   114             dummyCell.Address = aAddress;
       
   115             //
       
   116             HeapCell ret = null;
       
   117             int index = CellIndex( dummyCell );
       
   118             //
       
   119             if	( index >= 0 && index < Count )
       
   120             {
       
   121                 ret = this[ index ];
       
   122                 aIndex = index;
       
   123             }
       
   124             //
       
   125             return ret;
       
   126         }
       
   127 
       
   128 		public HeapCell CellByAllocationNumber( uint aNumber )
       
   129 		{
       
   130             int index;
       
   131             return CellByAllocationNumber( aNumber, out index );
       
   132 		}
       
   133 
       
   134         public HeapCell CellByAllocationNumber( uint aNumber, out int aIndex )
       
   135         {
       
   136             HeapCell ret = null;
       
   137             //
       
   138             aIndex = -1;
       
   139             int index = 0;
       
   140             foreach( HeapCell cell in this )
       
   141             {
       
   142                 if	( cell.Type == HeapCell.TType.EAllocated && cell.AllocationNumber == aNumber )
       
   143                 {
       
   144                     ret = cell;
       
   145                     aIndex = index;
       
   146                     break;
       
   147                 }
       
   148                 ++index;
       
   149             }
       
   150             //
       
   151             return ret;
       
   152         }
       
   153 
       
   154         public HeapCell CellByAllocationNumberIndexed( uint aNumber, int aDelta )
       
   155         {
       
   156             if  ( iAllocMap == null )
       
   157             {
       
   158                 iAllocMap = new HeapCellArrayAllocationNumberMap( this );
       
   159             }
       
   160             //
       
   161             HeapCell cell = null;
       
   162             uint nextAllocNum = (uint) ( aNumber + aDelta );
       
   163             //
       
   164             while( cell == null && iAllocMap.InRange( nextAllocNum ) )
       
   165             {
       
   166                 cell = iAllocMap[ nextAllocNum ];
       
   167                 nextAllocNum = (uint) ( nextAllocNum + aDelta );
       
   168             }
       
   169             //
       
   170             return cell;
       
   171         }
       
   172 
       
   173         public bool AllocationNumberInRange( uint aNumber )
       
   174         {
       
   175             if  ( iAllocMap == null )
       
   176             {
       
   177                 iAllocMap = new HeapCellArrayAllocationNumberMap( this );
       
   178             }
       
   179             //
       
   180             return iAllocMap.InRange( aNumber );
       
   181         }
       
   182 
       
   183         public IEnumerator<HeapCell> CreateEnumerator()
       
   184         {
       
   185             return new HeapCellArrayEnumerator( this );
       
   186         }
       
   187 
       
   188         public void Copy( HeapCellArray aArray )
       
   189         {
       
   190             foreach ( HeapCell cell in aArray )
       
   191             {
       
   192                 Add( cell );
       
   193             }
       
   194         }
       
   195         #endregion
       
   196 
       
   197 		#region Properties - abstract
       
   198         public abstract int Count
       
   199         {
       
   200             get;
       
   201         }
       
   202 
       
   203 		public abstract HeapCell this[ int aIndex ]
       
   204 		{
       
   205             get;
       
   206 		}
       
   207 		#endregion
       
   208 
       
   209         #region As CSV & Text
       
   210         public override string ToString()
       
   211         {
       
   212             StringBuilder ret = new StringBuilder();
       
   213 
       
   214             // Create entries
       
   215             int index = 0;
       
   216             foreach( HeapCell cell in this )
       
   217             {
       
   218                 // INDEX
       
   219                 ret.Append( "[" + index.ToString("d6") + "]  " );
       
   220 
       
   221                 // TYPE
       
   222                 ret.Append( cell.TypeString );
       
   223                 if	( cell.Type == HeapCell.TType.EFree )
       
   224                 {
       
   225                     ret.Append( "     " ); // to make the same length as "Allocated" we need 5 extra spaces
       
   226                 }
       
   227                 ret.Append( "  " );
       
   228 
       
   229                 // ADDRESS
       
   230                 ret.Append( "@ 0x" + cell.Address.ToString("x8") );
       
   231                 ret.Append( "  " );
       
   232 
       
   233                 // LENGTH
       
   234                 ret.Append( "Len: " + cell.Length.ToString( "########" ) );
       
   235                 ret.Append( ", " );
       
   236 
       
   237                 // PAYLOAD LENGTH
       
   238                 ret.Append( "PayL: " + cell.PayloadLength.ToString( "########" ) );
       
   239                 ret.Append( ", " );
       
   240 
       
   241                 // NESTING LEVEL
       
   242                 ret.Append( "Nest: " + cell.NestingLevel.ToString( "########" ) );
       
   243                 ret.Append( ", " );
       
   244 
       
   245                 // ALLOCATION NUMBER
       
   246                 ret.Append( "Allo#: " + cell.AllocationNumber.ToString( "########" ) );
       
   247                 ret.Append( ", " );
       
   248 
       
   249                 // VTABLE ADDRESS
       
   250                 ret.Append( "VT: 0x" + cell.PossibleVTableAddress.ToString("x8") );
       
   251                 ret.Append( ", " );
       
   252 
       
   253                 // VTABLE SYMBOL
       
   254                 ret.Append( "{ " + cell.SymbolString + " }" );
       
   255                 ret.Append( System.Environment.NewLine );
       
   256 
       
   257                 ++index;
       
   258             }
       
   259             //
       
   260             return ret.ToString();
       
   261         }
       
   262 
       
   263         public string ToCSV()
       
   264         {
       
   265             StringBuilder ret = new StringBuilder();
       
   266 			
       
   267             // Create header
       
   268             ret.Append( "Index,Type,Address,Length,Payload Length,Payload Length (inc  linked cells),Recursive (linked) Payload Length,Nesting Level,Allocation Number,VTable Address,VTable Symbol" );
       
   269             ret.Append( System.Environment.NewLine );
       
   270 
       
   271             // Create entries
       
   272             int index = 0;
       
   273             foreach( HeapCell cell in this )
       
   274             {
       
   275                 // INDEX
       
   276                 ret.Append( index.ToString("d6") );
       
   277                 ret.Append( "," );
       
   278 
       
   279                 // TYPE
       
   280                 ret.Append( cell.TypeString );
       
   281                 ret.Append( "," );
       
   282 
       
   283                 // ADDRESS
       
   284                 ret.Append( "0x" + cell.Address.ToString("x8") );
       
   285                 ret.Append( "," );
       
   286 
       
   287                 // LENGTH
       
   288                 ret.Append( cell.Length );
       
   289                 ret.Append( "," );
       
   290 
       
   291                 // PAYLOAD LENGTH
       
   292                 ret.Append( cell.PayloadLength );
       
   293                 ret.Append( "," );
       
   294 
       
   295                 // PAYLOAD LENGTH INCLUDING LINKED CELLS
       
   296                 ret.Append( cell.PayloadLengthIncludingLinkedCells );
       
   297                 ret.Append( "," );
       
   298 
       
   299                 // RECURSIVE LINKED PAYLOAD LENGTH
       
   300                 ret.Append( cell.CombinedLinkedCellPayloadLengths );
       
   301                 ret.Append( "," );
       
   302 
       
   303                 // NESTING LEVEL
       
   304                 ret.Append( cell.NestingLevel );
       
   305                 ret.Append( "," );
       
   306 
       
   307                 // ALLOCATION NUMBER
       
   308                 ret.Append( cell.AllocationNumber );
       
   309                 ret.Append( "," );
       
   310 
       
   311                 // VTABLE ADDRESS
       
   312                 ret.Append( "0x" + cell.PossibleVTableAddress.ToString("x8") );
       
   313                 ret.Append( "," );
       
   314 
       
   315                 // VTABLE SYMBOL
       
   316                 ret.Append( cell.SymbolString );
       
   317 
       
   318                 ret.Append( System.Environment.NewLine );
       
   319 
       
   320                 ++index;
       
   321             }
       
   322             //
       
   323             return ret.ToString();
       
   324         }
       
   325         #endregion
       
   326 
       
   327 		#region IEnumerable Members
       
   328         IEnumerator IEnumerable.GetEnumerator()
       
   329 		{
       
   330 			return new HeapCellArrayEnumerator( this );
       
   331 		}
       
   332 
       
   333         IEnumerator<HeapCell> IEnumerable<HeapCell>.GetEnumerator()
       
   334         {
       
   335             return CreateEnumerator();
       
   336         }
       
   337         #endregion
       
   338 
       
   339 		#region Data members
       
   340         private HeapCellArrayAllocationNumberMap iAllocMap = null;
       
   341 		#endregion
       
   342 	}
       
   343 }