backupandrestore/backupengine/src/sbheapwrapper.cpp
changeset 0 d0791faffa3f
child 15 f85613f12947
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // CHeapWrapper implementation
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #include "sbheapwrapper.h"
       
    24 #include <connect/panic.h>
       
    25 
       
    26 namespace conn
       
    27 	{
       
    28 	CHeapWrapper* CHeapWrapper::NewL()
       
    29 	/**
       
    30 	Symbian 1st phase constructor
       
    31 	
       
    32 	@return pointer to a newly created CHeapWrapper object
       
    33 	*/
       
    34 		{
       
    35 		CHeapWrapper* self = new (ELeave) CHeapWrapper();
       
    36 		
       
    37 		return self;
       
    38 		}
       
    39 	
       
    40 	TPtrC8& CHeapWrapper::ReadBufferL(const RChunk& aChunk)
       
    41 	/**
       
    42 	Getter for the GSH descriptor
       
    43 	
       
    44 	@param aChunk The RChunk to interrogate for the heap information
       
    45 	@return reference to the descriptor stored on the global shared heap
       
    46 	*/
       
    47 		{
       
    48 		if (Header(aChunk).LockedFlag())
       
    49 			{
       
    50 			User::Leave(KErrInUse);
       
    51 			}
       
    52 
       
    53 		CleanReadBuffer();
       
    54 			
       
    55 		iReadBuf = new (ELeave) TPtrC8(Buffer(aChunk));
       
    56 
       
    57 		return *iReadBuf;
       
    58 		}
       
    59 		
       
    60 	TPtr8& CHeapWrapper::WriteBufferL(const RChunk& aChunk)
       
    61 	/**
       
    62 	Lock the heap and return a reference to the descriptor on the GSH
       
    63 	
       
    64 	@param aChunk The RChunk to interrogate for the heap information
       
    65 	@return reference to the descriptor stored on the global shared heap
       
    66 	*/
       
    67 		{
       
    68 		if (Header(aChunk).LockedFlag())
       
    69 			{
       
    70 			User::Leave(KErrInUse);
       
    71 			}
       
    72 
       
    73 		CleanReadBuffer();
       
    74 
       
    75 		TPtr8& des = Buffer(aChunk);
       
    76 		
       
    77 		// Blank the descriptor as it's a write buffer
       
    78 		des.Zero();
       
    79 		
       
    80 		return des;
       
    81 		}
       
    82 		
       
    83 	THeapWrapperHeader& CHeapWrapper::Header(const RChunk& aChunk) const
       
    84 	/**
       
    85 	Getter for the protocol structure
       
    86 	
       
    87 	@param aChunk The RChunk to interrogate for the heap information
       
    88 	@return Pointer to the header structure containing heap protocol information
       
    89 	*/
       
    90 		{
       
    91 		return *reinterpret_cast<THeapWrapperHeader*>(aChunk.Base() + KHeaderOffset);
       
    92 		}
       
    93 		
       
    94 	TInt CHeapWrapper::ResetHeap(const RChunk& aChunk)
       
    95 	/**
       
    96 	Reset all of the protocol data in the stack if we're the first users
       
    97 	
       
    98 	@param aChunk The RChunk to interrogate for the heap information
       
    99 	@return KErrNone always.
       
   100 	*/
       
   101 		{
       
   102 		TInt err = KErrNone;
       
   103 		TUint8* heapPtr = aChunk.Base();
       
   104 		
       
   105 		new (&Header(aChunk)) THeapWrapperHeader;
       
   106 		
       
   107 		THeapWrapperHeader& header = Header(aChunk);
       
   108 		header.iFinished = ETrue;
       
   109 		header.SetLockedFlag(EFalse);
       
   110 		
       
   111 		// placement new a new descriptor on the heap to initialise it
       
   112 		new (heapPtr + KDescriptorOffset) TPtr8(heapPtr + KDataOffset, 0, aChunk.Size() - KDataOffset);
       
   113 
       
   114 		return err;
       
   115 		}
       
   116 
       
   117 	TPtr8& CHeapWrapper::Buffer(const RChunk& aChunk)
       
   118  	/**
       
   119  	@param aChunk The RChunk to interrogate for the heap information
       
   120  	@return reference to the descriptor stored on the global shared heap
       
   121  	*/
       
   122   		{
       
   123   		TUint8* heapPtr = aChunk.Base();
       
   124 		
       
   125 		// Set the descriptor
       
   126 		TPtr8* descriptor = reinterpret_cast<TPtr8*>(heapPtr + KDescriptorOffset);
       
   127 		const TInt length = descriptor->Length();
       
   128 		const TInt maxLength = descriptor->MaxLength();
       
   129 		descriptor->Set(heapPtr + KDataOffset, length, maxLength);
       
   130 
       
   131 		return *descriptor;
       
   132 		}
       
   133 		
       
   134 	void CHeapWrapper::CleanReadBuffer()
       
   135 	/**
       
   136 	Delete and NULL the read buffer if necessary
       
   137 	*/
       
   138 		{
       
   139 		delete iReadBuf;
       
   140 		iReadBuf = NULL;
       
   141 		}
       
   142 		
       
   143 	CHeapWrapper::~CHeapWrapper()
       
   144 		{
       
   145 		CleanReadBuffer();
       
   146 		}
       
   147 	}