systemswstubs/examplecommonisc/IscDataTransmissionBase/src/IscBufferQueue.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 DIscBufferQueue class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include <IscDefinitions.h>
       
    23 #include "IscBufferQueue.h"
       
    24 #include "IscQueue.h"
       
    25 #include "IscTrace.h"
       
    26 
       
    27 #ifdef __WINS__
       
    28 #include <windows.h>
       
    29 #endif
       
    30 
       
    31 // EXTERNAL DATA STRUCTURES
       
    32 #ifdef __WINS__
       
    33 extern CRITICAL_SECTION g_IscDTBCriticalSection;
       
    34 #endif
       
    35 
       
    36 // EXTERNAL FUNCTION PROTOTYPES  
       
    37 
       
    38 // CONSTANTS
       
    39 const TInt KIscInterruptLevelTwo( 2 );
       
    40 
       
    41 // MACROS
       
    42 
       
    43 // LOCAL CONSTANTS AND MACROS
       
    44 
       
    45 // MODULE DATA STRUCTURES
       
    46 
       
    47 // LOCAL FUNCTION PROTOTYPES
       
    48 
       
    49 // FORWARD DECLARATIONS
       
    50 
       
    51 // ============================ MEMBER FUNCTIONS ===============================
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // DIscBufferQueue::DIscBufferQueue
       
    55 // C++ default constructor can NOT contain any code, that
       
    56 // might leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 DIscBufferQueue::DIscBufferQueue()
       
    60     :   iCount( NULL ),
       
    61         iBuffers( NULL ),
       
    62         iBuffersQueue( NULL )
       
    63     {
       
    64     }
       
    65     
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // DIscBufferQueue::Construct
       
    69 // Symbian 2nd phase constructor can leave.
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 TInt DIscBufferQueue::Construct( 
       
    73     TUint16 aSize, 
       
    74     TUint16 aCount, 
       
    75     TUint8* &aCurrentAddress )
       
    76     {
       
    77     C_TRACE( ( _T( "DIscBufferQueue::Construct(0x%x, 0x%x, 0x%x) 0x%x, 0x%x" ), aSize, aCount, &aCurrentAddress, iBuffers, iBuffersQueue  ) );
       
    78     
       
    79     TInt r = KErrNone;
       
    80     if ( aSize == 0 || aCount == 0 )
       
    81         return KErrArgument;
       
    82     
       
    83     iCount = aCount;
       
    84     iBuffers = new TUint32*[aCount];
       
    85     ASSERT_RESET_ALWAYS( iBuffers, "ISCDataTransmissionBase",EIscMemoryAllocationFailure );
       
    86     iBuffersQueue = new DIscQueue( iBuffers, aCount );
       
    87     ASSERT_RESET_ALWAYS( iBuffersQueue, "ISCDataTransmissionBase",EIscMemoryAllocationFailure );
       
    88     
       
    89     for ( TInt i = 0; i < aCount; i++ )
       
    90         {   
       
    91         TPtr8*  pTmp;        
       
    92 #ifndef __WINS__
       
    93         pTmp = new TPtr8 ( ( TUint8* )aCurrentAddress, aSize );
       
    94         ASSERT_RESET_ALWAYS( pTmp, "ISCDataTransmissionBase",EIscMemoryAllocationFailure );
       
    95 #else
       
    96         TAny* ptr = Kern::Alloc( aSize );
       
    97         ASSERT_RESET_ALWAYS( ptr, "ISCDataTransmissionBase",EIscMemoryAllocationFailure );
       
    98         pTmp = new TPtr8( ( TUint8* )ptr, 0, aSize );
       
    99         ASSERT_RESET_ALWAYS( pTmp, "ISCDataTransmissionBase",EIscMemoryAllocationFailure );
       
   100 #endif // __WINS__
       
   101 
       
   102         // Put the buffer into RX resource queue.        
       
   103         r = iBuffersQueue->Add( pTmp );
       
   104         
       
   105         aCurrentAddress += aSize;
       
   106         }
       
   107     
       
   108     C_TRACE( ( _T( "DIscBufferQueue::Construct - return 0x%x" ),r ) );
       
   109 
       
   110     return r;
       
   111     }
       
   112 
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // DIscBufferQueue::New
       
   116 // Construct a new queue
       
   117 // ( other items were commented in a header ).
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 DIscBufferQueue* DIscBufferQueue::New( 
       
   121     TUint16 aSize, 
       
   122     TUint16 aCount, 
       
   123     TUint8*& aCurrentAddress )
       
   124     {
       
   125     C_TRACE( ( _T( "DIscBufferQueue::New(0x%x, 0x%x, 0x%x)" ), aSize, aCount, aCurrentAddress) );
       
   126 
       
   127     DIscBufferQueue* self = new DIscBufferQueue();
       
   128     ASSERT_RESET_ALWAYS( self, "ISCDataTransmissionBase",EIscMemoryAllocationFailure );
       
   129     if ( self->Construct( aSize, aCount, aCurrentAddress ) != KErrNone )
       
   130         {
       
   131         delete self;
       
   132         self = NULL;
       
   133         }
       
   134     else
       
   135         {
       
   136         }
       
   137 
       
   138     C_TRACE( ( _T( "DIscBufferQueue::New - return 0x%x" ),self ) );
       
   139     return self;
       
   140     }
       
   141 
       
   142     
       
   143 // -----------------------------------------------------------------------------
       
   144 // DIscBufferQueue::~DIscBufferQueue
       
   145 // Destructor
       
   146 // ( other items were commented in a header ).
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 DIscBufferQueue::~DIscBufferQueue()
       
   150     {
       
   151     C_TRACE( ( _T( "DIscBufferQueue::~DIscBufferQueue()" ) ) );
       
   152     if ( iBuffersQueue )
       
   153         {
       
   154         /* release all buffers */
       
   155         for ( TInt i = 0; i < iBuffersQueue->Count(); i++ )
       
   156             {
       
   157             // destroy allocated buffer
       
   158             TPtr8* delBuf = ( TPtr8* )iBuffersQueue->RemoveFirst();
       
   159             
       
   160             if ( delBuf )
       
   161                 {
       
   162 #ifdef __WINS__
       
   163                 Kern::Free( ( TAny* )delBuf->Ptr() );
       
   164                 delete delBuf;
       
   165 #else
       
   166                 delete delBuf;
       
   167 #endif
       
   168                 }
       
   169             else
       
   170                 {
       
   171                 }
       
   172             }
       
   173         delete iBuffersQueue;
       
   174         delete []iBuffers;
       
   175         }
       
   176     C_TRACE( ( _T( "DIscBufferQueue::~DIscBufferQueue - return void" ) ) );
       
   177     }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // DIscBufferQueue::Reserve
       
   181 // Reserves first element from the queue
       
   182 // ( other items were commented in a header ).
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 TDes8* DIscBufferQueue::Reserve()
       
   186     {
       
   187 
       
   188     if ( iBuffersQueue )
       
   189         {
       
   190         TDes8* temp =  ( TDes8* )iBuffersQueue->RemoveFirst();
       
   191         if ( temp )
       
   192             {
       
   193             TInt irqLevel = DisableIrqs();
       
   194             iCount--;
       
   195             RestoreIrqs( irqLevel );
       
   196             }
       
   197         return temp;
       
   198         }
       
   199     else
       
   200         {
       
   201         return NULL;
       
   202         }
       
   203 
       
   204     }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // DIscBufferQueue::Release
       
   208 // Releases element from the queue
       
   209 // ( other items were commented in a header ).
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 void DIscBufferQueue::Release( 
       
   213     TDes8* aPtr )
       
   214     {
       
   215      
       
   216     if ( iBuffersQueue )
       
   217         {
       
   218         aPtr->Zero();
       
   219         TInt err = iBuffersQueue->Add( aPtr );
       
   220         ASSERT_RESET_ALWAYS( err == KErrNone, "ISCDataTransmissionBase",EIscBufferAllocationFailure );
       
   221         TInt irqLevel = DisableIrqs();
       
   222         iCount++;
       
   223         RestoreIrqs( irqLevel );
       
   224         }
       
   225 
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // DIscBufferQueue::DisableIrqs
       
   230 // Function to disable interrupts
       
   231 // ( other items were commented in a header ).
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 TInt DIscBufferQueue::DisableIrqs()
       
   235     {
       
   236 #ifndef __WINS__
       
   237     return NKern::DisableInterrupts( KIscInterruptLevelTwo );    
       
   238 #else //__WINS__
       
   239     EnterCriticalSection( &g_IscDTBCriticalSection );
       
   240     return KErrNone;
       
   241 #endif//__WINS__
       
   242     }
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // DIscBufferQueue::RestoreIrqs
       
   246 // Function to restore interrupts
       
   247 // ( other items were commented in a header ).
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 
       
   251 #ifndef __WINS__
       
   252 void DIscBufferQueue::RestoreIrqs( 
       
   253     TInt aLevel )
       
   254     {
       
   255     NKern::RestoreInterrupts( aLevel );
       
   256 #else //__WINS__
       
   257 void DIscBufferQueue::RestoreIrqs( 
       
   258     TInt )
       
   259     {
       
   260     LeaveCriticalSection( &g_IscDTBCriticalSection );
       
   261 #endif//__WINS__
       
   262     }
       
   263 //  End of File