systemswstubs/examplecommonisc/IscDataTransmissionBase/src/IscBufferAllocator.cpp
changeset 0 0ce1b5ce9557
equal deleted inserted replaced
-1:000000000000 0:0ce1b5ce9557
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Implementation of DIscBufferAllocator class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <kernel.h>
       
    22 #include <platform.h>
       
    23 #include <kern_priv.h>
       
    24 
       
    25 #include "IscBufferAllocator.h"
       
    26 #include "IscBufferQueue.h"
       
    27 #include "IscTrace.h"
       
    28 
       
    29 // EXTERNAL DATA STRUCTURES
       
    30 
       
    31 // EXTERNAL FUNCTION PROTOTYPES  
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 // MACROS
       
    36 
       
    37 // LOCAL CONSTANTS AND MACROS
       
    38 
       
    39 // MODULE DATA STRUCTURES
       
    40 
       
    41 // LOCAL FUNCTION PROTOTYPES
       
    42 
       
    43 // FORWARD DECLARATIONS
       
    44 
       
    45 // ============================ MEMBER FUNCTIONS ===============================
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // DIscBufferAllocator::DIscBufferAllocator
       
    49 // C++ default constructor can NOT contain any code, that
       
    50 // might leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 DIscBufferAllocator::DIscBufferAllocator( TIscBufferEntry* aBufferConfig )
       
    54     : iBuffers( NULL )
       
    55 #ifndef __WINS__
       
    56     , iBufferChunk( NULL )
       
    57     , iCurrentAddress( NULL )
       
    58 #endif
       
    59     {
       
    60     for ( TUint16 i = 0; i < KIscBufferAmount; i++ )
       
    61         {
       
    62         iBufferConfig[i].size  = aBufferConfig[i].size;
       
    63         iBufferConfig[i].count = aBufferConfig[i].count;
       
    64         }
       
    65     
       
    66     C_TRACE( ( _T( "DIscBufferAllocator::DIscBufferAllocator" ) ) );
       
    67     }
       
    68 
       
    69 // Destructor
       
    70 DIscBufferAllocator::~DIscBufferAllocator()
       
    71     {
       
    72     // delete iBuffers & chunk
       
    73     for ( TUint8 i = 0; i < KIscBufferAmount; i++ )
       
    74         {
       
    75         delete iBuffers[i];
       
    76         iBuffers[i] = NULL;
       
    77         }
       
    78     delete [] iBuffers;
       
    79     iBuffers=NULL;
       
    80 
       
    81 #ifndef __WINS__
       
    82     delete iBufferChunk;
       
    83     iBufferChunk = NULL;
       
    84     // free ram pages..
       
    85 #endif
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // DIscBufferAllocator::AllocBuffers
       
    90 // Allocate buffers according the multiplexer
       
    91 // ( other items were commented in a header ).
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 TInt DIscBufferAllocator::AllocBuffers()
       
    95     {
       
    96     C_TRACE( ( _T( "DIscBufferAllocator::AllocBuffers()" ) ) );
       
    97     TUint8 i = 0;
       
    98 #ifndef __WINS__
       
    99     TInt memoryNeeded = 0;
       
   100     for ( i = 0; i < KIscBufferAmount; i++ )
       
   101         {
       
   102         if ( iBufferConfig[i].size > 0 && iBufferConfig[i].count> 0 )
       
   103             {
       
   104             memoryNeeded += ( iBufferConfig[i].size * iBufferConfig[i].count );
       
   105             }        
       
   106         }    
       
   107     C_TRACE( ( _T( "DIscBufferAllocator::AllocBuffers memory needed %d" ), memoryNeeded ) );
       
   108 
       
   109     // Check if buffers are allocated from ISC
       
   110     if ( memoryNeeded > 0 )
       
   111         {
       
   112         AllocPhMemory( memoryNeeded );
       
   113         }
       
   114     else
       
   115         {
       
   116         return KErrNone;
       
   117         }
       
   118 
       
   119     TUint8* address = ( TUint8* )iCurrentAddress;
       
   120 #else
       
   121     TUint8* address = NULL;
       
   122 #endif //WINS
       
   123 
       
   124    
       
   125         
       
   126     iBuffers = new DIscBufferQueue*[ KIscBufferAmount ];
       
   127     ASSERT_RESET_ALWAYS( iBuffers, "ISCDataTransmissionBase",EIscMemoryAllocationFailure );
       
   128     
       
   129     for ( i = 0; i < KIscBufferAmount; i++ )
       
   130         {
       
   131         if ( iBufferConfig[i].size > 0 && iBufferConfig[i].count > 0 )
       
   132             {
       
   133             iBuffers[i] = DIscBufferQueue::New( iBufferConfig[i].size, iBufferConfig[i].count, address );
       
   134             }
       
   135         }
       
   136 
       
   137     return KErrNone;
       
   138 
       
   139     }
       
   140 
       
   141 #ifndef __WINS__
       
   142 // -----------------------------------------------------------------------------
       
   143 // DIscBufferAllocator::AllocPhMemory
       
   144 // Physical buffer allocations
       
   145 // ( other items were commented in a header ).
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 void DIscBufferAllocator::AllocPhMemory( TInt aMemoryNeeded )
       
   149     {
       
   150     C_TRACE( ( _T( "DIscBufferAllocator::AllocPhMemory(0x%x)" ), aMemoryNeeded ) );
       
   151 
       
   152     TPhysAddr physAddr = 0;
       
   153     NKern::ThreadEnterCS();
       
   154     TInt r = Epoc::AllocPhysicalRam( aMemoryNeeded, physAddr );
       
   155     ASSERT_RESET_ALWAYS( r == KErrNone, "IscDataTransmissionBase",EIscMemoryAllocationFailure );
       
   156     r = DPlatChunkHw::New( iBufferChunk, physAddr, aMemoryNeeded, EMapAttrSupRw );
       
   157     NKern::ThreadLeaveCS();
       
   158     ASSERT_RESET_ALWAYS( r == KErrNone, "IscDataTransmissionBase",EIscMemoryAllocationFailure );
       
   159     iCurrentAddress = iBufferChunk->LinearAddress();
       
   160 
       
   161     }
       
   162 #endif //WINS
       
   163 
       
   164 // -----------------------------------------------------------------------------
       
   165 // DIscBufferAllocator::ReserveMemoryBlock
       
   166 // Reserves pre-allocated memory block
       
   167 // ( other items were commented in a header ).
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void DIscBufferAllocator::ReserveMemoryBlock( 
       
   171     TDes8*& aPtr, 
       
   172     TUint16 aSize )
       
   173     {
       
   174     E_TRACE( ( _T( "IBA:Res (0x%x)" ), aSize ) );
       
   175     
       
   176     if ( iBuffers )
       
   177         {
       
   178         for ( TUint8 i = 0; i < KIscBufferAmount;i++ )
       
   179             {
       
   180             if ( aSize <= iBufferConfig[i].size && iBufferConfig[i].count > 0 )
       
   181                 {
       
   182                 if ( iBuffers[i] )
       
   183                     {
       
   184                     if ( !iBuffers[i]->Empty() )
       
   185                         {
       
   186                         aPtr = ( TPtr8* )iBuffers[i]->Reserve();
       
   187                         if ( aPtr )
       
   188                             {
       
   189                             aPtr->SetLength( 0 );
       
   190                             E_TRACE( ( _T( "IBA:Res 0x%x %d" ), aPtr, i ) );
       
   191                             return;
       
   192                             }
       
   193                         }
       
   194                     }
       
   195                 }
       
   196             }
       
   197         }
       
   198     // If Memory block is not reserved, set NULL to aPtr    
       
   199     aPtr = NULL;
       
   200     return;    
       
   201     }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // DIscBufferAllocator::ReleaseMemoryBlock
       
   205 // Releases memory block allocated with ReserveMemoryBlock
       
   206 // ( other items were commented in a header ).
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 void DIscBufferAllocator::ReleaseMemoryBlock( 
       
   210     TDes8* aPtr )
       
   211     {
       
   212     E_TRACE( ( _T( "IBA:Rel 0x%x" ), aPtr ) );
       
   213     if ( !aPtr )
       
   214         {
       
   215         TRACE_ASSERT_ALWAYS;
       
   216         }
       
   217     else if ( KIscBufferAmount != 0 && iBuffers )
       
   218         {
       
   219         for ( TUint8 i = 0; i < KIscBufferAmount; i++ )
       
   220             {
       
   221             if ( iBufferConfig[i].size == ( ( TDes8* )aPtr )->MaxLength() )
       
   222                 {
       
   223                 iBuffers[i]->Release( aPtr );
       
   224                 }
       
   225             }
       
   226         }   
       
   227     }
       
   228 
       
   229 //  End of File