usbmgmt/usbmgr/device/classdrivers/ncm/classimplementation/ncmpktdrv/pktdrv/src/ncmbuffermanager.cpp
branchRCL_3
changeset 15 f92a4f87e424
equal deleted inserted replaced
14:d3e8e7d462dd 15:f92a4f87e424
       
     1 /*
       
     2 * Copyright (c) 2010 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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20 @file
       
    21 @internalComponent
       
    22 */
       
    23 
       
    24 
       
    25 #include "ncmbuffermanager.h"
       
    26 #include "OstTraceDefinitions.h"
       
    27 #ifdef OST_TRACE_COMPILER_IN_USE
       
    28 #include "ncmbuffermanagerTraces.h"
       
    29 #endif
       
    30 
       
    31 
       
    32 
       
    33 
       
    34 // if there are KCongestionCount buffers will notify networking to stop sending
       
    35 // if there are at least KUnCongestionCount, will notify networking to start sending
       
    36 const TInt KCongestionCount = 1;
       
    37 const TInt KUnCongestionCount = 3;
       
    38 
       
    39 
       
    40 
       
    41 // ======== MEMBER FUNCTIONS ========
       
    42 //
       
    43 
       
    44 CNcmBufferManager* CNcmBufferManager::NewL()
       
    45     {
       
    46     OstTraceFunctionEntry0( CNCMBUFFERMANAGER_NEWL_ENTRY );
       
    47     CNcmBufferManager *self = new (ELeave) CNcmBufferManager();
       
    48     OstTraceFunctionExit0( CNCMBUFFERMANAGER_NEWL_EXIT );
       
    49     return self;
       
    50     }
       
    51 
       
    52 CNcmBufferManager::~CNcmBufferManager()
       
    53     {
       
    54     OstTraceFunctionEntry1( CNCMBUFFERMANAGER_CNCMBUFFERMANAGER_ENTRY, this );
       
    55     iFreeQueue.Reset();
       
    56     OstTraceFunctionExit1( CNCMBUFFERMANAGER_CNCMBUFFERMANAGER_EXIT, this );
       
    57     }
       
    58 
       
    59 //
       
    60 // get a buffer block from manager
       
    61 //
       
    62 TInt CNcmBufferManager::GetBuffer(TNcmBuffer& aBuffer)
       
    63     {
       
    64 	OstTraceFunctionEntry1( CNCMBUFFERMANAGER_GETBUFFER_ENTRY, this );
       
    65 	
       
    66     TInt count = iFreeQueue.Count();
       
    67     TInt ret = KErrNone;
       
    68 
       
    69     if (count == 0)
       
    70         {
       
    71         OstTraceFunctionExit1( CNCMBUFFERMANAGER_GETBUFFER_EXIT, this );
       
    72         return KErrNoBuffer;
       
    73         }
       
    74     else if (count == KCongestionCount)
       
    75         {
       
    76         iIsCongestion = ETrue;
       
    77         ret = KErrCongestion;
       
    78         }
       
    79     aBuffer = iFreeQueue[0];
       
    80     iFreeQueue.Remove(0);
       
    81     OstTraceFunctionExit1( CNCMBUFFERMANAGER_GETBUFFER_EXIT_DUP1, this );
       
    82     return ret;
       
    83     }
       
    84 
       
    85 //
       
    86 // free a buffer block to manager
       
    87 //
       
    88 void CNcmBufferManager::FreeBuffer(const TNcmBuffer& aBuffer)
       
    89     {
       
    90     OstTraceFunctionEntry1( CNCMBUFFERMANAGER_FREEBUFFER_ENTRY, this );
       
    91     iFreeQueue.Append(aBuffer);  
       
    92     int count = iFreeQueue.Count();
       
    93     iFreeQueue[count-1].iLen = 0;
       
    94     if (count == KUnCongestionCount)
       
    95         {
       
    96         iIsCongestion = EFalse;
       
    97         }
       
    98     OstTraceFunctionExit1( CNCMBUFFERMANAGER_FREEBUFFER_EXIT, this );
       
    99     }
       
   100 
       
   101 //
       
   102 // set the whole buffer area to create buffer blocks
       
   103 //
       
   104 void CNcmBufferManager::InitBufferArea(TAny* aBuf, TInt aLength)
       
   105     {
       
   106     OstTrace1( TRACE_NORMAL, CNCMBUFFERMANAGER_INITBUFFERAREA, "CNcmBufferManager::InitBufferArea aLength=%d", aLength );    
       
   107     iBuf = (TUint8*)aBuf;
       
   108     iLen = aLength;
       
   109     }
       
   110 
       
   111 //
       
   112 // set the size of a buffer block, create buffer blocks on the buffer area
       
   113 //
       
   114 TInt CNcmBufferManager::SetBufferCellSize(TInt aSize)
       
   115     {
       
   116     OstTraceFunctionEntry1( CNCMBUFFERMANAGER_SETBUFFERCELLSIZE_ENTRY, this );
       
   117 
       
   118 //    buffer block size must be aligned with KAlignSize to make the each buffer start align with KAlignSize 
       
   119     iCellSize = (aSize+iAlignSize-1)&~(iAlignSize-1);
       
   120     OstTraceExt2( TRACE_NORMAL, CNCMBUFFERMANAGER_SETBUFFERCELLSIZE, "CNcmBufferManager::SetBufferCellSize aSize=%d, iCellSize=%d", aSize, iCellSize );
       
   121 
       
   122 //   buffer area got from share chunk LDD may not align, make it align to KAlignSize first
       
   123     TUint32 buf = (TUint)iBuf;
       
   124     buf = (buf+iAlignSize-1)&~(iAlignSize-1);
       
   125     TUint8* alignbuf = (TUint8*)buf;
       
   126     TUint32 offset = buf - (TUint)iBuf;
       
   127     
       
   128     int count = (iLen-offset)/iCellSize;
       
   129     iFreeQueue.Reset();
       
   130     if (count < KUnCongestionCount)
       
   131         {        
       
   132         OstTrace1( TRACE_FATAL, CNCMBUFFERMANAGER_SETBUFFERCELLSIZE1, "the buffer cell size is too big and create too less buffers %d", count );
       
   133         OstTraceFunctionExit1( CNCMBUFFERMANAGER_SETBUFFERCELLSIZE_EXIT, this );
       
   134         return KErrUnknown;
       
   135         }
       
   136 
       
   137     TNcmBuffer ncmbuf;
       
   138     for (TInt i=0 ; i<count ; i++)
       
   139         {
       
   140         ncmbuf.iPtr = alignbuf+i*iCellSize;
       
   141         ncmbuf.iLen = 0;
       
   142         ncmbuf.iMaxLength = iCellSize;
       
   143         iFreeQueue.Append(ncmbuf);
       
   144         }
       
   145     iIsCongestion = EFalse;
       
   146     OstTraceFunctionExit1( CNCMBUFFERMANAGER_SETBUFFERCELLSIZE_EXIT_DUP1, this );
       
   147     return KErrNone;
       
   148     }
       
   149 
       
   150 CNcmBufferManager::CNcmBufferManager()
       
   151     {
       
   152     iIsCongestion = EFalse;
       
   153     }
       
   154 
       
   155 //
       
   156 // if there is not enough buffer blocks, return ETrue and will start flow control in networking
       
   157 //
       
   158 
       
   159 TBool CNcmBufferManager::IsCongestion()
       
   160     {
       
   161     OstTrace1( TRACE_NORMAL, CNCMBUFFERMANAGER_ISCONGESTION, "CNcmBufferManager::FreeBufferCount %d", iFreeQueue.Count() );
       
   162     return iIsCongestion;
       
   163     }
       
   164 
       
   165 TInt CNcmBufferManager::RequiredBufferCount()
       
   166     {
       
   167     return KUnCongestionCount;
       
   168     }
       
   169 
       
   170