mpx/commonframework/common/src/mpxdataarray.cpp
changeset 0 a2952bb97e68
child 19 51035f0751c2
equal deleted inserted replaced
-1:000000000000 0:a2952bb97e68
       
     1 /*
       
     2 * Copyright (c) 2006 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 array of variable size elements on global chunk
       
    15 *
       
    16 */
       
    17 
       
    18 #include <mpxlog.h>
       
    19 #include "mpxdataarray.h"
       
    20 #include "mpxheapmanager.h"
       
    21 #include "mpxdata.h"
       
    22 
       
    23 
       
    24 #define ITEM(x,y) RMPXHeapManager::Ptr<RMPXDataItem>(x,y)
       
    25 #define OFFSET(x,y) RMPXHeapManager::Offset(x,y)
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // RMPXDataItem::Reset
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 void RMPXDataItem::Reset(TUint aClientHandle)
       
    32     {
       
    33     MPX_ASSERT(aClientHandle);
       
    34     if (iBufLen)
       
    35         {
       
    36         // If the buffer contains a media/media array, then we need
       
    37         // to release its reference (could end up being recursive)
       
    38         //
       
    39         TAny* data=Buf(aClientHandle);
       
    40         MMPXData* d=MMPXData::Data(aClientHandle,data,iBufLen);
       
    41         if(d)
       
    42             {
       
    43             d->Release(aClientHandle);
       
    44             }
       
    45         // Now free the actual buffer
       
    46         RMPXHeapManager& m=RMPXHeapManager::HeapManager(aClientHandle);
       
    47         iUid=0;
       
    48         iBufLen=0;
       
    49         }
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // RMPXDataItem::Copy
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void RMPXDataItem::Copy(TUint aClientHandle,TAny* aSrc,TInt aSize)
       
    57     {
       
    58     MPX_ASSERT(aSrc&&aClientHandle);
       
    59     //
       
    60     Reset(aClientHandle);
       
    61     RMPXHeapManager& m=RMPXHeapManager::HeapManager(aClientHandle);
       
    62     //Set uid for this item
       
    63     iUid=m.IncrementCounter();
       
    64     if (aSize) // Could be zero sized, e.g. zero-length descriptor
       
    65         {
       
    66         (void)Mem::Copy((TAny*)((TUint8*)this+iBufOffset),aSrc,aSize);
       
    67         iBufLen=aSize;
       
    68         //
       
    69         // Need to increment ref count if adding a 'sleeping' media/media array object
       
    70         //
       
    71         TAny* data=Buf(aClientHandle);
       
    72         MMPXData* d=MMPXData::Data(aClientHandle,data,iBufLen);
       
    73         if(d)
       
    74             {
       
    75             d->AddRef(aClientHandle);
       
    76             }
       
    77         }
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // RMPXDataItem::Data
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 TAny* RMPXDataItem::Buf(TUint aClientHandle)
       
    85     {
       
    86     ASSERT(aClientHandle);
       
    87     return iBufLen?(TAny*)((TUint8*)this+iBufOffset):NULL;
       
    88     }
       
    89     
       
    90 // -----------------------------------------------------------------------------
       
    91 // RMPXDataItem::Insert
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void RMPXDataItem::Insert(
       
    95     TUint aClientHandle,
       
    96     RMPXDataItem* aPrevious,
       
    97     RMPXDataItem* aNext)
       
    98     {
       
    99     MPX_ASSERT(aClientHandle);
       
   100     //
       
   101     iPreviousOffset=aPrevious?OFFSET(aClientHandle,aPrevious):0;
       
   102     iNextOffset=aNext?OFFSET(aClientHandle,aNext):0;
       
   103     TInt thisOffset=OFFSET(aClientHandle,this);
       
   104     if (aPrevious)
       
   105         {
       
   106         aPrevious->iNextOffset=thisOffset;
       
   107         }
       
   108     if (aNext)
       
   109         {
       
   110         aNext->iPreviousOffset=thisOffset;
       
   111         }
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // RMPXDataItem::Insert
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void RMPXDataItem::Insert(
       
   119     TUint aClientHandle,
       
   120     TInt aPreviousOffset,
       
   121     TInt aNextOffset)
       
   122     {
       
   123     MPX_ASSERT(aClientHandle);
       
   124     iPreviousOffset=aPreviousOffset;
       
   125     iNextOffset=aNextOffset;
       
   126     TInt thisOffset=OFFSET(aClientHandle,this);
       
   127     if (aPreviousOffset)
       
   128         {
       
   129         RMPXDataItem* previous=ITEM(aClientHandle,aPreviousOffset);
       
   130         previous->iNextOffset=thisOffset;
       
   131         }
       
   132     if (aNextOffset)
       
   133         {
       
   134         RMPXDataItem* next=ITEM(aClientHandle,aNextOffset);
       
   135         next->iPreviousOffset=thisOffset;
       
   136         }
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // RMPXDataItem::Set
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 void RMPXDataItem::Set(TUint aClientHandle,RMPXDataItem* aOldItem)
       
   144     {
       
   145     MPX_ASSERT(aOldItem&&aClientHandle);
       
   146     //
       
   147     iPreviousOffset=aOldItem->iPreviousOffset;
       
   148     iNextOffset=aOldItem->iNextOffset;
       
   149     TInt thisOffset=OFFSET(aClientHandle,this);
       
   150     if (iPreviousOffset)
       
   151         {
       
   152         RMPXDataItem* previous=ITEM(aClientHandle,iPreviousOffset);
       
   153         previous->iNextOffset=thisOffset;
       
   154         }
       
   155     if (iNextOffset)
       
   156         {
       
   157         RMPXDataItem* next=ITEM(aClientHandle,iNextOffset);
       
   158         next->iPreviousOffset=thisOffset;
       
   159         }
       
   160     
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // RMPXDataItem::Set
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 void RMPXDataItem::Set(TUint aClientHandle,TInt aOldItemOffset)
       
   168     {
       
   169     RMPXDataItem* oldItem=ITEM(aClientHandle,aOldItemOffset);
       
   170     Set(aClientHandle,oldItem);
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // RMPXDataItem::Remove
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void RMPXDataItem::Remove(TUint aClientHandle)
       
   178     {
       
   179     MPX_ASSERT(aClientHandle);
       
   180     if (iPreviousOffset)
       
   181         {
       
   182         RMPXDataItem* previous=ITEM(aClientHandle,iPreviousOffset);
       
   183         previous->iNextOffset=iNextOffset;
       
   184         }
       
   185     if (iNextOffset)
       
   186         {
       
   187         RMPXDataItem* next=ITEM(aClientHandle,iNextOffset);
       
   188         next->iPreviousOffset=iPreviousOffset;
       
   189         }
       
   190     iPreviousOffset=0;
       
   191     iNextOffset=0;
       
   192     }
       
   193 
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // RMPXDataItemArray::RMPXDataItemArray
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 RMPXDataItemArray::RMPXDataItemArray()
       
   200     : iFirstItemOffset(0),iLastItemOffset(0),iNumItems(0),iPos(0),iIndex(0)
       
   201     {
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // RMPXDataItemArray::Close
       
   206 // -----------------------------------------------------------------------------
       
   207 //    
       
   208 void RMPXDataItemArray::Close(TUint aClientHandle)
       
   209     { 
       
   210     MPX_ASSERT(aClientHandle); 
       
   211     Reset(aClientHandle);
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // RMPXDataItemArray::DataItem
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 RMPXDataItem* RMPXDataItemArray::DataItem(TUint aClientHandle,TInt aIndex)
       
   219     {
       
   220     MPX_ASSERT(aClientHandle&&aIndex>=0&&aIndex<iNumItems);
       
   221     //
       
   222     RMPXDataItem* dataItem=NULL;
       
   223     while(!dataItem)
       
   224         {
       
   225         //
       
   226         // Start looking from the current position: this is an optimisation
       
   227         // since the client is likely to either iterate from this position,
       
   228         // or make another query from this item. iPos and iIndex MUST 
       
   229         // correspond
       
   230         //
       
   231         RMPXDataItem* item=ITEM(aClientHandle,iPos);
       
   232         if (iIndex==aIndex)
       
   233             {
       
   234             dataItem=item;
       
   235             }
       
   236         else if (iIndex>aIndex)
       
   237             {
       
   238             iIndex--;
       
   239             iPos=item->PreviousOffset();
       
   240             }
       
   241         else // if (iIndex<aIndex)
       
   242             {
       
   243             iIndex++;
       
   244             iPos=item->NextOffset();
       
   245             }
       
   246         }
       
   247     MPX_ASSERT(dataItem && (TInt)dataItem > (TInt)&RMPXHeapManager::HeapManager(aClientHandle));
       
   248     return dataItem;
       
   249     }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // RMPXDataItemArray::Delete
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void RMPXDataItemArray::Delete(TUint aClientHandle,RMPXDataItem* aItem)
       
   256     {
       
   257     MPX_ASSERT(aClientHandle&&aItem);
       
   258     aItem->Close(aClientHandle);  // Release objects in the buffer
       
   259     TInt r=RMPXHeapManager::HeapManager(aClientHandle).Free(aClientHandle,aItem);
       
   260     MPX_ASSERT(r==KErrNone);
       
   261     (void)r;
       
   262     }
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // RMPXDataItemArray::Append
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 void RMPXDataItemArray::Append(TUint aClientHandle,RMPXDataItem& aItem)
       
   269     {
       
   270     MPX_ASSERT(aClientHandle);
       
   271     //
       
   272     aItem.Insert(aClientHandle,iLastItemOffset,0);
       
   273     iLastItemOffset=OFFSET(aClientHandle,&aItem);
       
   274     if (iNumItems++==0)
       
   275         {
       
   276         iFirstItemOffset=iLastItemOffset;
       
   277         }
       
   278     iPos=iFirstItemOffset;
       
   279     iIndex=0;
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // RMPXDataItemArray::Set
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 void RMPXDataItemArray::Set(TUint aClientHandle,RMPXDataItem& aItem,TInt aIndex)
       
   287     {
       
   288     MPX_ASSERT(aClientHandle&&aIndex>=0&&aIndex<iNumItems);
       
   289     //
       
   290     RMPXDataItem* oldItem=DataItem(aClientHandle,aIndex);
       
   291     aItem.Set(aClientHandle,oldItem);
       
   292     TInt oldItemOffset=OFFSET(aClientHandle,oldItem); 
       
   293     TInt itemOffset=OFFSET(aClientHandle,&aItem); 
       
   294     if (oldItemOffset==iLastItemOffset) 
       
   295         {
       
   296         iLastItemOffset=itemOffset;
       
   297         }
       
   298      if (oldItemOffset==iFirstItemOffset) 
       
   299         {
       
   300         iFirstItemOffset=itemOffset;
       
   301         } 
       
   302     Delete(aClientHandle,oldItem);
       
   303     iPos=iFirstItemOffset;
       
   304     iIndex=0;
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // RMPXDataItemArray::Insert
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 void RMPXDataItemArray::Insert(TUint aClientHandle,RMPXDataItem& aItem,TInt aIndex)
       
   312     {
       
   313     MPX_ASSERT(aClientHandle&&aIndex>=0&&aIndex<iNumItems);
       
   314     //
       
   315     RMPXDataItem* itemBefore=aIndex==0?NULL:DataItem(aClientHandle,aIndex-1);
       
   316     RMPXDataItem* itemAfter=aIndex<iNumItems?DataItem(aClientHandle,aIndex):NULL;
       
   317     aItem.Insert(aClientHandle,itemBefore,itemAfter);
       
   318     TInt itemOffset=OFFSET(aClientHandle,&aItem);
       
   319     if (!itemAfter) // this is the last one
       
   320         {
       
   321         iLastItemOffset=itemOffset;
       
   322         }
       
   323     if (!itemBefore||iNumItems==0)  // this is the first or the only one
       
   324         {
       
   325         iFirstItemOffset=itemOffset;
       
   326         } 
       
   327     iPos=iFirstItemOffset;
       
   328     iIndex=0;
       
   329     ++iNumItems;
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // RMPXDataItemArray::Remove
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 void RMPXDataItemArray::Remove(TUint aClientHandle,TInt aIndex)
       
   337     {
       
   338     MPX_ASSERT(aClientHandle&&aIndex>=0&&aIndex<iNumItems);
       
   339     //
       
   340     RMPXDataItem* item=DataItem(aClientHandle,aIndex);
       
   341     TInt itemOffset=OFFSET(aClientHandle,item);
       
   342     if (iFirstItemOffset==itemOffset)
       
   343         {
       
   344         iFirstItemOffset=item->NextOffset();
       
   345         }
       
   346     if (iLastItemOffset==itemOffset)
       
   347         {
       
   348         iLastItemOffset=item->PreviousOffset();
       
   349         }   
       
   350     item->Remove(aClientHandle);
       
   351     Delete(aClientHandle,item);
       
   352     iPos=iFirstItemOffset; 
       
   353     iIndex=0; 
       
   354     --iNumItems;
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // RMPXDataItemArray::Reset
       
   359 // -----------------------------------------------------------------------------
       
   360 //
       
   361 void RMPXDataItemArray::Reset(TUint aClientHandle)
       
   362     {
       
   363     if (iFirstItemOffset)
       
   364         {
       
   365         RMPXDataItem* item=ITEM(aClientHandle,iFirstItemOffset);
       
   366         while(item)
       
   367             {
       
   368             Delete(aClientHandle,item);
       
   369             item=item->NextOffset()?ITEM(aClientHandle,item->NextOffset()):NULL;
       
   370             }
       
   371         }
       
   372     // Reset members
       
   373     iFirstItemOffset=0;
       
   374     iLastItemOffset=0;
       
   375     iNumItems=0;
       
   376     iPos=0;
       
   377     iIndex=0; 
       
   378     }
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // RMPXDataItemArray::Find
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 TInt RMPXDataItemArray::Find(TUint aClientHandle,const RMPXDataItem& aItem)
       
   385     {
       
   386     TInt r=KErrNotFound;
       
   387     if (iFirstItemOffset)
       
   388         {
       
   389         TInt index=KErrNotFound;
       
   390         RMPXDataItem* item=ITEM(aClientHandle,iFirstItemOffset);
       
   391         while(item)
       
   392             {  
       
   393             index++;
       
   394             TUint8* d1 = reinterpret_cast<TUint8*>(item->Buf(aClientHandle));
       
   395             TUint8* d2 = 
       
   396              reinterpret_cast<TUint8*>(const_cast<RMPXDataItem*>(&aItem)->
       
   397                                                             Buf(aClientHandle));
       
   398             if (Mem::Compare(d1, item->Size(), d2, aItem.Size()) == 0)
       
   399                 {
       
   400                 r=index;
       
   401                 break;
       
   402                 }
       
   403             item=item->NextOffset()?ITEM(aClientHandle,item->NextOffset()):NULL;
       
   404             }
       
   405         }  
       
   406     return r;
       
   407     }
       
   408 // End of file