os/kernelhwsrv/kernel/eka/debug/crashMonitor/src/scmbytestreamutil.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\debug\crashMonitor\src\scmbytestreamutil.cpp
sl@0
    15
// some utility classes for writing data to flash buffer
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
/**
sl@0
    20
 @file
sl@0
    21
 @internalTechnology
sl@0
    22
*/
sl@0
    23
sl@0
    24
sl@0
    25
#include "scmbytestreamutil.h"
sl@0
    26
#include "scmtrace.h"
sl@0
    27
sl@0
    28
sl@0
    29
sl@0
    30
namespace Debug 
sl@0
    31
	{
sl@0
    32
	/**
sl@0
    33
	 * TByteStreamBase Constructor 
sl@0
    34
	 * @param aBuffer - pointer to buffer that this utility will use
sl@0
    35
	 */	
sl@0
    36
	TByteStreamBase::TByteStreamBase(TUint8* aBuffer) 
sl@0
    37
		: iBuffer(aBuffer)
sl@0
    38
		, iPos(0) 
sl@0
    39
		{
sl@0
    40
		}
sl@0
    41
			
sl@0
    42
	/**
sl@0
    43
	 * SetPosition
sl@0
    44
	 * @param aBuffer - Sets iPos
sl@0
    45
	 * @return void
sl@0
    46
	 */	
sl@0
    47
	void TByteStreamBase::SetPosition(TInt aPos)
sl@0
    48
		{
sl@0
    49
		iPos = aPos;
sl@0
    50
		}
sl@0
    51
	
sl@0
    52
	/**
sl@0
    53
	 * CurrentPosition
sl@0
    54
	 * @param aBuffer - Returns the current value of iPos
sl@0
    55
	 * @return Tint
sl@0
    56
	 */	
sl@0
    57
	TInt TByteStreamBase::CurrentPosition() const
sl@0
    58
		{
sl@0
    59
		return iPos;
sl@0
    60
		}
sl@0
    61
	
sl@0
    62
	/**
sl@0
    63
	 * TByteStreamReader Constructor 
sl@0
    64
	 * @param aBuffer - pointer to buffer that this utility will use
sl@0
    65
	 */	
sl@0
    66
	TByteStreamReader::TByteStreamReader(TUint8* aBuffer) 
sl@0
    67
		: TByteStreamBase(aBuffer)
sl@0
    68
		{	
sl@0
    69
		}
sl@0
    70
	
sl@0
    71
	
sl@0
    72
	/**
sl@0
    73
	 * Constructor for TByteStreamWriter
sl@0
    74
	 * @param aBuffer buffer for writing
sl@0
    75
	 * @param aPhysEnabled whether or not physical writing to another media is enabled
sl@0
    76
	 */
sl@0
    77
	TByteStreamWriter::TByteStreamWriter(TUint8* aBuffer, TBool aPhysEnabled) 
sl@0
    78
		: TByteStreamBase(aBuffer)
sl@0
    79
		, iPhysEnabled(aPhysEnabled)
sl@0
    80
		, iBytesWritten(0)
sl@0
    81
		{			
sl@0
    82
		}
sl@0
    83
sl@0
    84
	/**
sl@0
    85
	 * Writes a byte to the buffer
sl@0
    86
	 * @param aValue Byte to write
sl@0
    87
	 */
sl@0
    88
	void TByteStreamWriter::WriteByte(TUint8 aValue)
sl@0
    89
		{
sl@0
    90
		if(iBuffer)
sl@0
    91
			{
sl@0
    92
			iBuffer[iPos++] = aValue;
sl@0
    93
			++iBytesWritten;
sl@0
    94
			}
sl@0
    95
		}
sl@0
    96
	
sl@0
    97
	/**
sl@0
    98
	 * Resets the byte counter back to zero
sl@0
    99
	 */
sl@0
   100
	void TByteStreamWriter::ResetBytesWritten()
sl@0
   101
		{
sl@0
   102
		iBytesWritten = 0;
sl@0
   103
		}
sl@0
   104
	
sl@0
   105
	/**
sl@0
   106
	 * TCachedByteStreamWriter Constructor 
sl@0
   107
	 * @param aBuffer - pointer to buffer that this utility will use
sl@0
   108
	 * @param aCacheSize - suggested length of cache to use if greater than EMaxCache
sl@0
   109
	 * 					cache length of EMaxCache will be used
sl@0
   110
	 */
sl@0
   111
	TCachedByteStreamWriter::TCachedByteStreamWriter(TUint8* aCacheBuffer, TInt aCacheSize, TBool aPhysEnabled) 
sl@0
   112
		: TByteStreamWriter(NULL, aPhysEnabled)
sl@0
   113
		, iCacheSize(aCacheSize)
sl@0
   114
		, iCacheBuffer(aCacheBuffer)
sl@0
   115
		, iPhysicalWriter(NULL)
sl@0
   116
		{
sl@0
   117
		}
sl@0
   118
	
sl@0
   119
	
sl@0
   120
	/**
sl@0
   121
	 * FlushCache 
sl@0
   122
	 * Writes the contents of the cache to buffer amd/or to physical writer implementation
sl@0
   123
	 * if one is currently set
sl@0
   124
	 * @return void
sl@0
   125
	 */	
sl@0
   126
	TInt TCachedByteStreamWriter::FlushCache()
sl@0
   127
		{			
sl@0
   128
		TInt padCount = iCacheSize - iPos;
sl@0
   129
		if(padCount > 0)
sl@0
   130
			{		
sl@0
   131
			for(TInt i=0;i<padCount;i++)
sl@0
   132
				{
sl@0
   133
				iCacheBuffer[iPos++] = 0;
sl@0
   134
				}		
sl@0
   135
			}
sl@0
   136
				
sl@0
   137
		if(iPhysEnabled)
sl@0
   138
			{
sl@0
   139
			if(iPhysicalWriter) // do we have a writer to send the cache data to
sl@0
   140
				{
sl@0
   141
				iPhysicalWriter->DoPhysicalWrite(iCacheBuffer, iBytesWritten, iPos);
sl@0
   142
				}
sl@0
   143
			}
sl@0
   144
		
sl@0
   145
		iPos = 0;
sl@0
   146
		return KErrNone;
sl@0
   147
		}
sl@0
   148
	
sl@0
   149
	/**
sl@0
   150
	 * Writes a byte to the cached buffer
sl@0
   151
	 * @param aValue Byte to write
sl@0
   152
	 */
sl@0
   153
	void TCachedByteStreamWriter::WriteByte(TUint8 aValue)
sl@0
   154
		{
sl@0
   155
		if(iPos == iCacheSize)
sl@0
   156
			{
sl@0
   157
			FlushCache();
sl@0
   158
			}		
sl@0
   159
		iCacheBuffer[iPos++] = aValue;	
sl@0
   160
		++iBytesWritten;
sl@0
   161
		}
sl@0
   162
	
sl@0
   163
	/**
sl@0
   164
	 * CurrentPosition
sl@0
   165
	 * @param aBuffer -  need to return the position in buffer plus the write pos into cache
sl@0
   166
	 * @return Tint
sl@0
   167
	 */	
sl@0
   168
	TInt TCachedByteStreamWriter::CurrentPosition() const
sl@0
   169
		{
sl@0
   170
		return iBytesWritten;	
sl@0
   171
		}
sl@0
   172
sl@0
   173
	/**
sl@0
   174
	 * SetWriterImpl
sl@0
   175
	 * @param aPhysicalWriter - sets the physical writer implementation to be used when the cache is flushed
sl@0
   176
	 * 							pass NULL to remove a previous writer implementation
sl@0
   177
	 * @return void
sl@0
   178
	 */	
sl@0
   179
	void TCachedByteStreamWriter::SetWriterImpl(MPhysicalWriterImpl* aPhysicalWriter)
sl@0
   180
		{
sl@0
   181
		iPhysicalWriter = aPhysicalWriter;
sl@0
   182
		}	
sl@0
   183
	}
sl@0
   184
sl@0
   185
//eof
sl@0
   186