servicediscoveryandcontrol/pnp/test/upnp/chunkmgr/src/cmemchunk.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 // Copyright (c) 2008-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 // @file
       
    15 // @internalComponent
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <e32base.h>
       
    20 #include "cchunkallocator.h"
       
    21 #include "rmemcell.h"
       
    22 #include "cmemchunk.h"
       
    23 
       
    24 #define MASK 0x03
       
    25 #define WORD_ALIGN(x) ( ( ( (x) + (MASK) )& ~MASK ) ) // aligns 'x' to the nearest word size
       
    26 #define IS_WORD_ALIGNED(x) ( ( (TUint32)(x) & MASK ) == 0 )
       
    27 
       
    28 
       
    29 CMemChunk* CMemChunk::New ( CChunkAllocator& aAllocator, RChunkPool& aChunkPool, TInt aNumBlocks , TInt aBlockSize )
       
    30 	{
       
    31 	// allocates chunk on shared Heap using ::new operator
       
    32 	
       
    33 	// round of RMemCell & aBlockSize to the nearest word size.
       
    34 	TInt memCellSize = WORD_ALIGN ( sizeof ( RMemCell ) );
       
    35 	TInt blockSize = WORD_ALIGN ( aBlockSize );
       
    36 	
       
    37 	CMemChunk* chunk = new ( aNumBlocks * ( memCellSize + blockSize ), aAllocator ) CMemChunk ( aAllocator, aNumBlocks );
       
    38 	
       
    39 	// In the complete memory allocated on shared heap; first bytes are of CMemChunk,
       
    40 	// then start the memory required for RMemCell * NumBlocks and at the end contains
       
    41 	// the actual data buffer.
       
    42 	
       
    43 	if ( chunk != NULL )
       
    44 		{
       
    45 		// memory required for CMemChunk and for its parameters storage
       
    46 		TUint8* chunkSize = ( ( TUint8* ) chunk ) + sizeof ( CMemChunk );
       
    47 	 	__ASSERT_DEBUG ( IS_WORD_ALIGNED ( chunkSize ), User::Invariant () ); // TODO Proper panic code
       
    48 		
       
    49 	 	// memory required for RMemCell and its parameters storage
       
    50 		TUint8* dataBufStart = chunkSize + ( aNumBlocks * memCellSize );
       
    51 		
       
    52 		// now place each RMemCell and its databuffer in the address specified
       
    53 		for ( TInt i = 0; i < aNumBlocks ; i++ )
       
    54 			{
       
    55 			// address assigned for RMemCell 'i'
       
    56 			RMemCell* hdr = ( RMemCell* ) ( chunkSize + i * memCellSize );
       
    57 			
       
    58 			// overloaded ::new, that makes in-place allocation of MemCell at specified address
       
    59 			( void ) new ( hdr ) RMemCell ( &aChunkPool, dataBufStart + ( i * aBlockSize ), aBlockSize );
       
    60 			
       
    61 			// completed construction of a RMemCell.
       
    62 			chunk->iQueue.Append ( hdr );
       
    63 			}
       
    64 		}
       
    65 	
       
    66 	return chunk;
       
    67 	}
       
    68 
       
    69 CMemChunk::CMemChunk ( CChunkAllocator& aAllocator, TInt aNumBlocks )
       
    70 	: iChunkAllocator ( aAllocator ), iNumBlocks ( aNumBlocks )
       
    71 	{
       
    72 	}
       
    73 
       
    74 CMemChunk::~CMemChunk ()
       
    75 	{	
       
    76 	}
       
    77 
       
    78 TAny* CMemChunk::operator new ( TUint aSize, TInt aExtra, CChunkAllocator& aAllocator )
       
    79 	{
       
    80 	return aAllocator.Alloc ( aSize + aExtra );
       
    81 	}
       
    82 
       
    83 void CMemChunk::operator delete ( TAny* aPtr, TInt /*aExtra*/, CChunkAllocator& /*aAllocator*/ )
       
    84 	{
       
    85 	CMemChunk::operator delete ( aPtr );
       
    86 	}
       
    87 
       
    88 void CMemChunk::operator delete ( TAny* aPtr )
       
    89 	{
       
    90 	if ( aPtr )
       
    91 		{
       
    92 		( ( CMemChunk* ) aPtr )->iChunkAllocator.Free ( aPtr );
       
    93 		}
       
    94 	}
       
    95 
       
    96 void RMemCellList::Append ( RMemCell* aPtr )
       
    97 	{
       
    98 	if ( IsEmpty () )	
       
    99 		{
       
   100 		iNext = iLast = aPtr;
       
   101 		}
       
   102 	else
       
   103 		{
       
   104 		iLast->Link ( aPtr );
       
   105 		iLast = aPtr;
       
   106 		}
       
   107 	}
       
   108 	
       
   109 void RMemCellList::Append ( RMemCellList& aList )
       
   110 	{
       
   111 	if ( aList.IsEmpty () )
       
   112 		{
       
   113 		return;
       
   114 		}
       
   115 	
       
   116 	if ( this->IsEmpty () )
       
   117 		{
       
   118 		iNext = aList.First ();
       
   119 		iLast = aList.Last ();		
       
   120 		}
       
   121 	else
       
   122 		{
       
   123 		iLast->Link ( aList.First () );
       
   124 		iLast = aList.Last ();
       
   125 		}
       
   126 	aList.Init ();
       
   127 	}
       
   128 	
       
   129 TInt RMemCellList::Transfer ( RMemCellList& aList, TInt aSize )
       
   130 	{
       
   131 	__ASSERT_DEBUG ( aSize >= 0, User::Invariant () ); //TODO
       
   132 	TInt transferedBytes = 0;
       
   133 	
       
   134 	if ( !IsEmpty () )
       
   135 		{
       
   136 		RMemCell* first;
       
   137 		RMemCell* next;
       
   138 		RMemCell* last;
       
   139 		
       
   140 		// catch hold of first link
       
   141 		first = iNext;
       
   142 		next = first;
       
   143 		last = first;
       
   144 		
       
   145 		while ( aSize >= 0 && next != NULL )
       
   146 			{
       
   147 			transferedBytes += next->Size ();
       
   148 			aSize -= next->Size ();
       
   149 			
       
   150 			last = next;
       
   151 			
       
   152 			next = next->Next ();
       
   153 			
       
   154 			if ( aSize == 0 )
       
   155 				{
       
   156 				break;
       
   157 				}
       
   158 			}
       
   159 		last->SetLength(aSize + last->Size());
       
   160 		
       
   161 		if ( next == NULL ) // all the blocks are transfferred
       
   162 			{
       
   163 			Init ();
       
   164 			}
       
   165 		else 				// still left with more blocks, so adjust the start & End links
       
   166 			{
       
   167 			last->Link ( NULL );
       
   168 			iNext = next;
       
   169 			}
       
   170 		
       
   171 		// now the last link is ready to use
       
   172 		RMemCellList newList ( first, last );
       
   173 		aList.Append ( newList );
       
   174 		}
       
   175 	
       
   176 	return transferedBytes;
       
   177 	}
       
   178 
       
   179