wim/WimServer/src/WimMemMgmt.cpp
changeset 0 164170e6151a
child 5 3b17fc5c9564
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002-2009 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:  Memory management function for WimServer to handle WIMI memory
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include    "WimMemMgmt.h"
       
    21 #include    "Wimi.h"        // WIMI definitions
       
    22 #include    "WimConsts.h"
       
    23 #include    "WimTrace.h"
       
    24 #include    "WimCleanup.h"
       
    25 
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CWimMemMgmt::CWimMemMgmt
       
    31 // C++ default constructor can NOT contain any code, that
       
    32 // might leave.
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CWimMemMgmt::CWimMemMgmt()
       
    36     {
       
    37     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::CWimMemMgmt | Begin"));
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CWimMemMgmt::ConstructL
       
    42 // Symbian 2nd phase constructor can leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 void CWimMemMgmt::ConstructL()
       
    46     {
       
    47     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::ConstructL | Begin"));
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CWimMemMgmt::NewL
       
    52 // Two-phased constructor.
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 CWimMemMgmt* CWimMemMgmt::NewL()
       
    56     {
       
    57     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::NewL | Begin"));
       
    58     CWimMemMgmt* self = new( ELeave ) CWimMemMgmt;
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64     
       
    65 // Destructor
       
    66 CWimMemMgmt::~CWimMemMgmt()
       
    67     {
       
    68     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::~CWimMemMgmt | Begin"));
       
    69 
       
    70     TInt refsCount = iRefs.Count();
       
    71     for( TInt index = 0; index < refsCount; ++index )
       
    72         {
       
    73         TWimiAllocRef* allocRef = iRefs[ index ];
       
    74         if( allocRef )
       
    75             {
       
    76             if( allocRef->refType == EWimiRefpt )
       
    77                 {
       
    78                 _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::~CWimMemMgmt | -ref 0x%08x"), allocRef->ref);
       
    79                 free_WIMI_Ref_t( allocRef->ref );
       
    80                 }
       
    81             else
       
    82                 {
       
    83                 _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::~CWimMemMgmt | -refLst 0x%08x"), allocRef->refLst);
       
    84                 free_WIMI_RefList_t( allocRef->refLst );
       
    85                 }
       
    86             }
       
    87         }
       
    88     iRefs.ResetAndDestroy();
       
    89 
       
    90     if( iWimRef )
       
    91         {
       
    92         _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::~CWimMemMgmt, -ref 0x%08x"), iWimRef);
       
    93         free_WIMI_Ref_t( iWimRef );
       
    94         }
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CWimMemMgmt::SetWIMRef
       
    99 // Sets active memory reference to iWimRef member variable.
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CWimMemMgmt::SetWIMRef( WIMI_Ref_pt aWimRef )
       
   103     {
       
   104     __ASSERT_DEBUG( iWimRef == NULL, User::Invariant() );
       
   105     _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::SetWIMRef, +ref 0x%08x"), aWimRef);
       
   106     iWimRef = aWimRef;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CWimMemMgmt::GetWIMRef
       
   111 // Fetches active reference.
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 WIMI_Ref_pt CWimMemMgmt::WimRef()
       
   115     {
       
   116     _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::GetWIMRef, iWimRef 0x%08x"), iWimRef);
       
   117     return iWimRef;
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CWimMemMgmt::AppendWIMRefLstL
       
   122 // Creates and appends a new WIMI_Alloc_Ref item to iRefs array.
       
   123 // -----------------------------------------------------------------------------
       
   124 //
       
   125 void CWimMemMgmt::AppendWIMRefLstL( WIMI_RefList_t aRefLst )
       
   126     {
       
   127     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::AppendWIMRefLstL | Begin"));
       
   128     CleanupPushWimRefListL( aRefLst ); // take ownership first
       
   129     TWimiAllocRef* allocRef = new( ELeave ) TWimiAllocRef;
       
   130     CleanupStack::PushL( allocRef );
       
   131     allocRef->refLst = aRefLst;
       
   132     allocRef->refType = EWimiRefListt;
       
   133     iRefs.AppendL( allocRef );
       
   134     _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::AppendWIMRefLstL, +refLst 0x%08x"), aRefLst);
       
   135     CleanupStack::Pop( 2, aRefLst );    // allocRef, aRefLst
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CWimMemMgmt::AppendWIMRefL
       
   140 // Creates and appends a new WIMI_Alloc_Ref item to iRefs array.
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 void CWimMemMgmt::AppendWIMRefL( WIMI_Ref_pt aRef )
       
   144     {
       
   145     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::AppendWIMRefL | Begin"));
       
   146     CleanupPushWimRefL( aRef );    // take ownership first
       
   147     TWimiAllocRef* allocRef = new( ELeave ) TWimiAllocRef;
       
   148     CleanupStack::PushL( allocRef );
       
   149     allocRef->ref = aRef;
       
   150     allocRef->refType = EWimiRefpt;
       
   151     iRefs.AppendL( allocRef );
       
   152     _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::AppendWIMRefL, +ref 0x%08x"), aRef);
       
   153     CleanupStack::Pop( 2, aRef );   // allocRef, aRef
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CWimMemMgmt::FreeRef
       
   158 // Removes reference from the list and releases allocated memory.
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void CWimMemMgmt::FreeRef( WIMI_Ref_pt aRef )
       
   162     {
       
   163     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::FreeRef | Begin"));
       
   164     TInt count = iRefs.Count();
       
   165     for( TInt index = 0; index < count; index++ )
       
   166         {
       
   167         TWimiAllocRef* allocRef = iRefs[ index ];
       
   168         if ( allocRef && ( allocRef->ref == aRef ) && ( allocRef->refType == EWimiRefpt ) )
       
   169             {
       
   170 			_WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::FreeRef, -ref 0x%08x"), aRef);
       
   171             iRefs.Remove( index );
       
   172             iRefs.Compress();
       
   173             free_WIMI_Ref_t( aRef );
       
   174             delete allocRef;
       
   175             break;
       
   176             }
       
   177         }
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CWimMemMgmt::FreeWIMRefs
       
   182 // Frees the reference(s) pointed by a client.
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CWimMemMgmt::FreeWIMRefs( const RMessage2& aMessage )
       
   186     {
       
   187     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::FreeWIMRefsL | Begin"));
       
   188     TUint wimCount = aMessage.Int1(); 
       
   189     WIMI_Ref_pt ref = reinterpret_cast< WIMI_Ref_pt >( aMessage.Int0() );
       
   190     _WIMTRACE3(_L("WIM | WIMServer | CWimMemMgmt::FreeWIMRefsL | ref 0x%08x, wimCount %d"),
       
   191             ref, wimCount);
       
   192 
       
   193     for( TUint8 wimIndex = 0; wimIndex < wimCount; wimIndex++ )
       
   194         {
       
   195         _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::FreeWIMRefsL | index=%d"), wimIndex);
       
   196         FreeRef( ref );     // TODO: fix, now deallocating the same ref in each loop?
       
   197         }
       
   198 
       
   199     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::FreeWIMRefsL | End"));
       
   200     aMessage.Complete( KErrNone );
       
   201     }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CWimMemMgmt::FreeRefLst
       
   205 // Frees the list of reference(s) pointed by a client.
       
   206 // -----------------------------------------------------------------------------
       
   207 //
       
   208 void CWimMemMgmt::FreeRefLst( const RMessage2& aMessage )
       
   209     {
       
   210     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::FreeRefLst | Begin"));
       
   211     WIMI_Ref_pt* refList = ( WIMI_Ref_pt* )aMessage.Int0();
       
   212     TInt count = iRefs.Count();
       
   213     for( TUint8 index = 0; index < count; index++ )
       
   214         {
       
   215         TWimiAllocRef* allocRef = iRefs[ index ];
       
   216         if ( allocRef && ( allocRef->refLst == refList ) && ( allocRef->refType == EWimiRefListt ))
       
   217             {
       
   218             _WIMTRACE2(_L("WIM | WIMServer | CWimMemMgmt::FreeRefLst, -refList 0x%08x"), refList);
       
   219             iRefs.Remove( index );
       
   220             iRefs.Compress();
       
   221             free_WIMI_RefList_t( refList );
       
   222             delete allocRef;
       
   223             aMessage.Complete( KErrNone );
       
   224             return;
       
   225             }
       
   226         }
       
   227     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::FreeRefLst | not found") );
       
   228     aMessage.Complete( KErrNotFound );              
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CWimMemMgmt::ValidateWIMRefL
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 TBool CWimMemMgmt::ValidateWIMRefL( WIMI_Ref_pt aRef )
       
   236     {
       
   237     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::ValidateWIMRefL | Begin"));
       
   238     TInt count = iRefs.Count();
       
   239     for( TUint8 index = 0; index < count; index++ )
       
   240         {
       
   241         TWimiAllocRef* allocRef = iRefs[ index ];
       
   242         if( allocRef->ref == aRef )
       
   243             {
       
   244             _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::ValidateWIMRefL, valid"));
       
   245             return ETrue;
       
   246             }
       
   247         }
       
   248     _WIMTRACE(_L("WIM | WIMServer | CWimMemMgmt::ValidateWIMRefL, invalid"));
       
   249     return EFalse;
       
   250     }
       
   251 
       
   252 //  End of File