memspy/Driver/Kernel/Source/MemSpyDriverStreamWriter.cpp
changeset 0 a03f92240627
child 27 6ab44b3687b2
child 30 86a2e675b80a
equal deleted inserted replaced
-1:000000000000 0:a03f92240627
       
     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 
       
    18 #include "MemSpyDriverStreamWriter.h"
       
    19 
       
    20 // User includes
       
    21 #include "MemSpyDriverUtils.h"
       
    22 #include "MemSpyDriverXferBuffer.h"
       
    23 
       
    24 
       
    25 
       
    26 RMemSpyMemStreamWriter::RMemSpyMemStreamWriter()
       
    27 :   iBuffer( NULL ), iCurrent( NULL ), iMax( 0 ), iMark( NULL ), iCommitted( 0 )
       
    28     {
       
    29     }
       
    30 
       
    31 
       
    32 RMemSpyMemStreamWriter::RMemSpyMemStreamWriter( DMemSpyDriverXferBuffer& aBuffer )
       
    33 :   iBuffer( &aBuffer ), iMark( NULL ), iCommitted( 0 )
       
    34     {
       
    35     iCurrent = iBuffer->Ptr();
       
    36     iMax = iCurrent + iBuffer->Size();
       
    37     TRACE( Kern::Printf("RMemSpyMemStreamWriter::RMemSpyMemStreamWriter() - current: 0x%08x, max: 0x%08x, size: %d", iCurrent, iMax, iBuffer->Size() ) );
       
    38     }
       
    39 
       
    40 
       
    41 void RMemSpyMemStreamWriter::Close()
       
    42     {
       
    43     iBuffer = NULL;
       
    44     iMark = NULL;
       
    45     }
       
    46 
       
    47 
       
    48 TInt RMemSpyMemStreamWriter::WriteAndClose( TDes8* aDestinationPointer )
       
    49     {
       
    50     __ASSERT_ALWAYS( IsOpen(), MemSpyDriverUtils::Fault( __LINE__ ) );
       
    51     //
       
    52     const TInt ret = iBuffer->WriteToClient( aDestinationPointer, iCommitted );
       
    53     Close();
       
    54     //
       
    55     return ret;
       
    56     }
       
    57 
       
    58 
       
    59 TInt32* RMemSpyMemStreamWriter::WriteInt32( TInt32 aValue )
       
    60     {
       
    61     TInt32* ret = NULL;
       
    62     //
       
    63     __ASSERT_ALWAYS( IsOpen(), MemSpyDriverUtils::Fault( __LINE__ ) );
       
    64     if  ( !IsFull() )
       
    65         {
       
    66         ret = (TInt32*) iCurrent;
       
    67         *ret = aValue;
       
    68         IncrementPos( sizeof( TInt32 ) );
       
    69         }
       
    70     else
       
    71         {
       
    72         TRACE( Kern::Printf( "RMemSpyMemStreamWriter::WriteInt32() - asked to write: 0x%08x from fn: 0x%08x BUT AM FULL", aValue, __return_address() ) );
       
    73         }
       
    74     //
       
    75     return ret;
       
    76     }
       
    77 
       
    78 
       
    79 TUint32* RMemSpyMemStreamWriter::WriteUint32( TUint32 aValue )
       
    80     {
       
    81     TUint32* ret = NULL;
       
    82     //
       
    83     __ASSERT_ALWAYS( IsOpen(), MemSpyDriverUtils::Fault( __LINE__ ) );
       
    84     if  ( !IsFull() )
       
    85         {
       
    86         ret = (TUint32*) iCurrent;
       
    87         *ret = aValue;
       
    88         IncrementPos( sizeof( TUint32 ) );
       
    89         }
       
    90     else
       
    91         {
       
    92         TRACE( Kern::Printf( "RMemSpyMemStreamWriter::WriteUint32() - asked to write: 0x%08x from fn: 0x%08x BUT AM FULL", aValue, __return_address() ) );
       
    93         }
       
    94     //
       
    95     return ret;
       
    96     }
       
    97 
       
    98 
       
    99 TUint8* RMemSpyMemStreamWriter::Current() const
       
   100     {
       
   101     __ASSERT_ALWAYS( IsOpen(), MemSpyDriverUtils::Fault( __LINE__ ) );
       
   102     return iCurrent;
       
   103     }
       
   104 
       
   105 
       
   106 void RMemSpyMemStreamWriter::Seek( TInt aPosition )
       
   107     {
       
   108     __ASSERT_ALWAYS( IsOpen(), MemSpyDriverUtils::Fault( __LINE__ ) );
       
   109     TUint8* pos = iBuffer->Ptr() + aPosition;
       
   110     __ASSERT_ALWAYS( pos < iMax, MemSpyDriverUtils::Fault( __LINE__ ) );
       
   111     iCurrent = pos;
       
   112     }
       
   113 
       
   114 
       
   115 TUint32 RMemSpyMemStreamWriter::Remaining() const
       
   116     {
       
   117     __ASSERT_ALWAYS( IsOpen(), MemSpyDriverUtils::Fault( __LINE__ ) );
       
   118     const TUint32 max = reinterpret_cast< TUint32 >( iMax );
       
   119     const TUint32 cur = reinterpret_cast< TUint32 >( iCurrent );
       
   120     TRACE( Kern::Printf("RMemSpyMemStreamWriter::Remaining() - current: 0x%08x, max: 0x%08x, ret: %d", cur, max, ( max - cur ) ) );
       
   121     return ( max - cur );
       
   122     }
       
   123 
       
   124 
       
   125 void RMemSpyMemStreamWriter::MarkSet()
       
   126     {
       
   127     __ASSERT_ALWAYS( iMark == NULL, MemSpyDriverUtils::Fault( __LINE__ ) );
       
   128     iMark = iCurrent;
       
   129     }
       
   130 
       
   131 
       
   132 void RMemSpyMemStreamWriter::MarkResume()
       
   133     {
       
   134     __ASSERT_ALWAYS( iMark != NULL, MemSpyDriverUtils::Fault( __LINE__ ) );
       
   135     iCurrent = iMark;
       
   136     iMark = NULL;
       
   137     }
       
   138 
       
   139 
       
   140 TBool RMemSpyMemStreamWriter::IsFull() const
       
   141     {
       
   142     __ASSERT_ALWAYS( IsOpen(), MemSpyDriverUtils::Fault( __LINE__ ) );
       
   143     const TBool ret = ( iCurrent >= iMax );
       
   144     return ret;
       
   145     }
       
   146 
       
   147 
       
   148 TBool RMemSpyMemStreamWriter::IsOpen() const
       
   149     {
       
   150     const TBool ret = ( iBuffer && iCurrent && iMax );
       
   151     return ret;
       
   152     }
       
   153 
       
   154 
       
   155 RMemSpyMemStreamWriter& RMemSpyMemStreamWriter::operator=( const RMemSpyMemStreamWriter& aCopy )
       
   156     {
       
   157     iBuffer = aCopy.iBuffer;
       
   158     iMark = aCopy.iMark;
       
   159     iCommitted = aCopy.iCommitted;
       
   160     iCurrent = aCopy.iCurrent;
       
   161     iMax = aCopy.iMax;
       
   162     //
       
   163     return *this;
       
   164     }
       
   165 
       
   166 
       
   167 void RMemSpyMemStreamWriter::IncrementPos( TInt aAmount )
       
   168     {
       
   169     __ASSERT_ALWAYS( IsOpen(), MemSpyDriverUtils::Fault( __LINE__ ) );
       
   170     if  ( iCurrent + aAmount > iMax )
       
   171         {
       
   172         Kern::Printf("RMemSpyMemStreamWriter::IncrementPos() - OVERFLOW - current: 0x%08x, max: 0x%08x, aAmount: %d", iCurrent, iMax, aAmount );
       
   173         }
       
   174 
       
   175     iCurrent += aAmount;
       
   176     iCommitted += aAmount;
       
   177     }
       
   178 
       
   179 
       
   180