commsfwutils/commsbufs/src/commsbufq.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "es_commsbuf_internal.h"
       
    17 
       
    18 __IMPLEMENT_CLEANUP(RCommsBufQ, Free)
       
    19 
       
    20 
       
    21 EXPORT_C RCommsBufQ::RCommsBufQ(RCommsBuf* aChain)
       
    22  	{
       
    23 	iNext = aChain;
       
    24 	iLast = RCommsBufChain(aChain).Last(); // Safe even if aChain==NULL
       
    25 	}
       
    26 
       
    27 
       
    28 EXPORT_C void RCommsBufQ::Init()
       
    29 /**
       
    30 initializes the members
       
    31 */
       
    32 	{
       
    33 	iNext = NULL;
       
    34 	iLast = NULL;
       
    35 	}
       
    36 
       
    37 
       
    38 EXPORT_C void RCommsBufQ::Assign(RCommsBufQ& aQueue)
       
    39 /**
       
    40 Assign this queue to a RCommsBuf queue
       
    41 @param aQueue to queue
       
    42 */
       
    43 	{
       
    44 	*this = aQueue;
       
    45 	aQueue.Init();
       
    46 	}
       
    47 
       
    48 
       
    49 EXPORT_C void RCommsBufQ::Assign(RCommsBufChain& aChain)
       
    50 /**
       
    51 Assign this a RCommsBuf chain to this queue
       
    52 @param aChain the chain
       
    53 */
       
    54  	{
       
    55 	iNext = aChain.First();
       
    56 	iLast = aChain.Last();
       
    57 	aChain.iNext = NULL;
       
    58 	}
       
    59 
       
    60 
       
    61 EXPORT_C void RCommsBufQ::Free()
       
    62 /**
       
    63 Frees the queue making it empty
       
    64 */
       
    65 	{
       
    66 	if(iNext)
       
    67 		iNext->Free();
       
    68 	Init();
       
    69 	}
       
    70 
       
    71 
       
    72 EXPORT_C void RCommsBufQ::Append(RCommsBuf* aBuf)
       
    73 /**
       
    74 Appends a RCommsBuf to the queue
       
    75 @param aBuf the buffer to be prepended
       
    76 */
       
    77  	{
       
    78 	if (IsEmpty())
       
    79 		iNext = iLast = aBuf;
       
    80 	else
       
    81 		{
       
    82 		iLast->SetNext(aBuf);
       
    83 		iLast = aBuf;
       
    84 		}
       
    85 	}
       
    86 
       
    87 
       
    88 EXPORT_C void RCommsBufQ::Prepend(RCommsBuf* aBuf)
       
    89 /**
       
    90 Prepends one RCommsBuf to this queue. aBuf must not point to any further CommsBufs.
       
    91 @param aBuf the buffer to be appended
       
    92 */
       
    93 	{
       
    94 
       
    95     __ASSERT_DEBUG(aBuf->Next()==NULL, CommsBuf::Panic(EMBuf_CannotPrependChainedMBuf));
       
    96 	if (IsEmpty())
       
    97 		{
       
    98 		iNext = aBuf;
       
    99 		iLast = aBuf;
       
   100 		aBuf->SetNext(NULL);	// break the mbuf chain (if any)
       
   101 		}
       
   102 	else
       
   103 		{
       
   104 		aBuf->SetNext(iNext);	// alter the mbuf chain (if any)
       
   105 		iNext = aBuf;
       
   106 		}
       
   107 	}
       
   108 
       
   109 
       
   110 EXPORT_C void RCommsBufQ::Append(RCommsBufQ& aQueue)
       
   111 /**
       
   112 Appends a RCommsBuf queue to this queue
       
   113 @param aQueue the queue to be appended
       
   114 */
       
   115  	{
       
   116 	if (aQueue.IsEmpty())
       
   117 		return;
       
   118 
       
   119 	if (IsEmpty())
       
   120 		{
       
   121 		// src queue is reset, thus this operation is an append & move
       
   122 		*this = aQueue;
       
   123 		aQueue.Init();      // trs; why zero the src queue? implies a move instead of just append.  ideally this should be fixed, but kept as is to avoid a functional break
       
   124 		}
       
   125 	else
       
   126 		{
       
   127 		// src queue is not altered, thus this operation is only an append (ie. no move)
       
   128 		iLast->SetNext(aQueue.iNext);
       
   129 		iLast = aQueue.iLast;
       
   130 		}
       
   131 	}
       
   132 
       
   133 
       
   134 EXPORT_C void RCommsBufQ::Append(RCommsBufChain& aChain)
       
   135 /**
       
   136 Appends a RCommsBuf chain to this queue
       
   137 @param aChain the chain to be appended
       
   138 */
       
   139  	{
       
   140 	if (aChain.IsEmpty())
       
   141 		return;
       
   142 
       
   143 	if (IsEmpty())
       
   144 		iNext = aChain.First();
       
   145 	else
       
   146 		iLast->SetNext(aChain.First());
       
   147 	iLast = aChain.Last();
       
   148 	aChain.iNext = NULL;
       
   149 	}
       
   150 
       
   151 
       
   152 EXPORT_C void RCommsBufQ::Prepend(RCommsBufChain& aChain)
       
   153 /**
       
   154 Prepends a RCommsBuf chain to this queue
       
   155 @param aChain the chain to be prepended
       
   156 */
       
   157  	{
       
   158 	if (aChain.IsEmpty())
       
   159 		return;
       
   160 
       
   161 	if (IsEmpty())
       
   162 		iLast = aChain.Last();
       
   163 	else
       
   164 		aChain.Last()->SetNext(iNext);
       
   165 	iNext = aChain.First();
       
   166 	aChain.iNext = NULL;
       
   167 	}
       
   168 
       
   169 
       
   170 EXPORT_C void RCommsBufQ::Prepend(RCommsBufQ& aQueue)
       
   171 /**
       
   172 Prepends a RCommsBuf queue to this queue
       
   173 @param aQueue the queue to be prepended
       
   174 */
       
   175  	{
       
   176 	if (aQueue.IsEmpty())
       
   177 		return;
       
   178 
       
   179 	if (IsEmpty())
       
   180 		Assign(aQueue);
       
   181 	else
       
   182 		{
       
   183 		aQueue.iLast->SetNext(iNext);
       
   184 		iNext = aQueue.iNext;
       
   185 		}
       
   186 	}
       
   187 
       
   188 
       
   189 EXPORT_C RCommsBuf* RCommsBufQ::Remove()
       
   190 /**
       
   191 Removes the first RCommsBuf from the queue
       
   192 @return the MBuf
       
   193 */
       
   194 	{
       
   195 	RCommsBuf* m;
       
   196 
       
   197 	if (IsEmpty())
       
   198 		return NULL;
       
   199 
       
   200 	m = iNext;
       
   201 
       
   202 	if (iNext = m->Next(), iNext==NULL)
       
   203 		iLast = NULL;
       
   204 
       
   205 	m->SetNext(NULL);
       
   206 
       
   207 	return m;
       
   208 	}
       
   209 
       
   210 EXPORT_C TInt RCommsBufQ::Transfer(RCommsBufQ& aQueue, TInt aSize, TInt aBufSize, TInt& aCount)
       
   211     {
       
   212     __ASSERT_DEBUG(aSize >= 0, CommsBuf::Panic(EMBuf_BadBufferSize));
       
   213 
       
   214     TInt transfered = 0;
       
   215     aCount = 0;
       
   216     
       
   217     if (!IsEmpty())
       
   218         {
       
   219         RCommsBuf* first;
       
   220         RCommsBuf* next;
       
   221         RCommsBuf* last;
       
   222     
       
   223         first = iNext;
       
   224         next = first;
       
   225         last = first;
       
   226     
       
   227         do 
       
   228             {
       
   229             ++aCount;
       
   230             transfered += aBufSize;
       
   231             aSize -= aBufSize;
       
   232             last = next;
       
   233             next = next->Next();
       
   234             } while (aSize > 0 && next != NULL);
       
   235             
       
   236         if (next)
       
   237             {
       
   238             last->SetNext(NULL);
       
   239             iNext = next;
       
   240             }
       
   241         else
       
   242             {
       
   243             iNext = NULL;
       
   244             iLast = NULL;
       
   245             }
       
   246 
       
   247         RCommsBufQ q(first, last);
       
   248         
       
   249         if (aSize < 0)
       
   250             {
       
   251             q.Last()->AdjustDataEnd(aSize);
       
   252             }
       
   253         aQueue.Append(q);
       
   254         }
       
   255     return transfered;
       
   256     }
       
   257 
       
   258 EXPORT_C RCommsBuf* RCommsBufQ::RemoveLast()
       
   259     {
       
   260     RCommsBuf* current = iNext;
       
   261     if(iNext == iLast)
       
   262         {
       
   263         iNext = iLast = NULL;
       
   264         return current;
       
   265         }
       
   266 
       
   267     while(current->Next() != iLast)
       
   268         {
       
   269         current = current->Next();
       
   270         }
       
   271     RCommsBuf* last = iLast;
       
   272     current->SetNext(NULL);
       
   273     iLast = current;
       
   274     return last;
       
   275     }
       
   276