commsfwutils/commsbufs/mbufmgr/src/mb_que.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 1997-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 // Buffer Manager for Protocols (MBuf and Packet Queues)
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <es_mbuf.h>
       
    19 
       
    20 //
       
    21 // MBUF QUEUE
       
    22 //
       
    23 
       
    24 __IMPLEMENT_CLEANUP(RMBufQ, Free)
       
    25 
       
    26 
       
    27 EXPORT_C RMBufQ::RMBufQ(RMBuf* aChain)
       
    28 /**
       
    29 Constructor initializes the members
       
    30 */
       
    31  	{
       
    32 	iNext = aChain;
       
    33 	iLast = aChain->Last(); // Safe even if aChain==NULL
       
    34 	}
       
    35 
       
    36 
       
    37 EXPORT_C void RMBufQ::Init()
       
    38 /**
       
    39 initializes the members
       
    40 */
       
    41 	{
       
    42 	RCommsBufQ::Init();
       
    43 	}
       
    44 
       
    45 
       
    46 EXPORT_C void RMBufQ::Assign(RMBufQ& aQueue)
       
    47 /**
       
    48 Assign this queue to a MBuf queue
       
    49 @param aQueue to queue
       
    50 */
       
    51 	{
       
    52 	RCommsBufQ::Assign(aQueue);
       
    53 	}
       
    54 
       
    55 
       
    56 EXPORT_C void RMBufQ::Assign(RMBufChain& aChain)
       
    57 /**
       
    58 Assign this a MBuf chain to this queue
       
    59 @param aChain the chain
       
    60 */
       
    61  	{
       
    62  	RCommsBufQ::Assign(aChain);
       
    63 	}
       
    64 
       
    65 
       
    66 EXPORT_C void RMBufQ::Free()
       
    67 /**
       
    68 Frees the queue making it empty
       
    69 */
       
    70 	{
       
    71 	RCommsBufQ::Free();
       
    72 	}
       
    73 
       
    74 
       
    75 EXPORT_C void RMBufQ::Append(RMBuf* aBuf)
       
    76 /**
       
    77 Appends a MBuf to the queue
       
    78 @param aBuf the buffer to be prepended
       
    79 */
       
    80  	{
       
    81  	RCommsBufQ::Append(aBuf);
       
    82 	}
       
    83 
       
    84 
       
    85 EXPORT_C void RMBufQ::Prepend(RMBuf* aBuf)
       
    86 /**
       
    87 Prepends one MBuf to this queue. aBuf must not point to any further Mbufs.
       
    88 @param aBuf the buffer to be appended
       
    89 */
       
    90 	{
       
    91 	RCommsBufQ::Prepend(aBuf);
       
    92 	}
       
    93 
       
    94 
       
    95 EXPORT_C void RMBufQ::Append(RMBufQ& aQueue)
       
    96 /**
       
    97 Appends a MBuf queue to this queue
       
    98 @param aQueue the queue to be appended
       
    99 */
       
   100  	{
       
   101  	RCommsBufQ::Append(aQueue);
       
   102 	}
       
   103 
       
   104 
       
   105 EXPORT_C void RMBufQ::Append(RMBufChain& aChain)
       
   106 /**
       
   107 Appends a MBuf chain to this queue
       
   108 @param aChain the chain to be appended
       
   109 */
       
   110  	{
       
   111  	RCommsBufQ::Append(aChain);
       
   112 	}
       
   113 
       
   114 
       
   115 EXPORT_C void RMBufQ::Prepend(RMBufChain& aChain)
       
   116 /**
       
   117 Prepends a MBuf chain to this queue
       
   118 @param aChain the chain to be prepended
       
   119 */
       
   120  	{
       
   121  	RCommsBufQ::Prepend(aChain);
       
   122 	}
       
   123 
       
   124 
       
   125 EXPORT_C void RMBufQ::Prepend(RMBufQ& aQueue)
       
   126 /**
       
   127 Prepends a MBuf queue to this queue
       
   128 @param aQueue the queue to be prepended
       
   129 */ 
       
   130  	{
       
   131  	RCommsBufQ::Prepend(aQueue);
       
   132 	}
       
   133 
       
   134 
       
   135 EXPORT_C RMBuf* RMBufQ::Remove()
       
   136 /**
       
   137 Removes the first MBuf from the queue
       
   138 @return the MBuf
       
   139 */
       
   140 	{
       
   141 	return static_cast<RMBuf*>(RCommsBufQ::Remove());
       
   142 	}
       
   143 
       
   144 EXPORT_C TInt RMBufQ::Transfer(RMBufQ& aQueue, TInt aSize, TInt aBufSize)
       
   145 /**
       
   146 For the benefit of the MBuf allocator
       
   147 Grab as many buffer as possible, as quickly as possible.
       
   148 @param aQueue the queue
       
   149 @param aSize the size
       
   150 @return aSize - buffer_space_transfered
       
   151 */
       
   152 	{
       
   153 	TInt bufCount;
       
   154 	return RCommsBufQ::Transfer(aQueue, aSize, aBufSize, bufCount);
       
   155 	}
       
   156 
       
   157 EXPORT_C RMBuf* RMBufQ::RemoveLast()
       
   158 /**
       
   159 Removes and returns the last RMBuf from the queu
       
   160  */
       
   161     {
       
   162     return static_cast<RMBuf*>(RCommsBufQ::RemoveLast());
       
   163     }
       
   164 
       
   165 //
       
   166 // MBUF PACKET QUEUE
       
   167 //
       
   168 
       
   169 __IMPLEMENT_CLEANUP(RMBufPktQ, Free)
       
   170 
       
   171 
       
   172 EXPORT_C void RMBufPktQ::Init()
       
   173 /**
       
   174 Initializes the the members
       
   175 */
       
   176 	{
       
   177 	iNext.Init();
       
   178 	iLast.Init();
       
   179 	}
       
   180 
       
   181 
       
   182 EXPORT_C void RMBufPktQ::Free()
       
   183 /**
       
   184 Frees the RMBuf Paket Queue, delets all objects
       
   185 */
       
   186 	{
       
   187 	RMBufChain chain;
       
   188 	while (Remove(chain))
       
   189 		chain.Free();
       
   190 	}
       
   191 
       
   192 
       
   193 EXPORT_C void RMBufPktQ::Append(RMBufChain& aChain)
       
   194 /**
       
   195 Appends a MBuf chain to the queue
       
   196 @param aCHain A MBuf Chain
       
   197 */
       
   198  	{
       
   199 	if (IsEmpty())
       
   200 		{
       
   201 		iNext = aChain;
       
   202 		iLast = aChain;
       
   203 		}
       
   204 	else
       
   205 		{
       
   206 		iLast.Link(aChain);
       
   207 		iLast = aChain;
       
   208 		}
       
   209 	aChain.Init();
       
   210 	}
       
   211 
       
   212 
       
   213 EXPORT_C void RMBufPktQ::Append(RMBufPktQ& aQueue)
       
   214 /**
       
   215 Appends a queue to the queue
       
   216 @param aQueue a BMuf Paket Queue
       
   217 */
       
   218  	{
       
   219 	if (aQueue.IsEmpty())
       
   220 		return;
       
   221 
       
   222 	if (IsEmpty())
       
   223 		{
       
   224 		iNext = aQueue.iNext;
       
   225 		iLast = aQueue.iLast;
       
   226 		}
       
   227 	else
       
   228 		{
       
   229 		iLast.Link(aQueue.iNext);
       
   230 		iLast = aQueue.iLast;
       
   231 		}
       
   232 	aQueue.Init();
       
   233 	}
       
   234 
       
   235 
       
   236 EXPORT_C void RMBufPktQ::Prepend(RMBufChain& aChain)
       
   237 /**
       
   238 Prepends a MBuf chain to the queue
       
   239 @param aCHain A MBuf Chain
       
   240 */
       
   241  	{
       
   242 	if (IsEmpty())
       
   243 		{
       
   244 		iNext = aChain;
       
   245 		iLast = aChain;
       
   246 		}
       
   247 	else
       
   248 		{
       
   249 		aChain.Link(iNext);
       
   250 		iNext = aChain;
       
   251 		}
       
   252 	aChain.Init();
       
   253 	}
       
   254 
       
   255 
       
   256 EXPORT_C TBool RMBufPktQ::Remove(RMBufChain& aChain)
       
   257 /**
       
   258 Prepends a queue to the queue
       
   259 @param aQueue a BMuf Packet Queue
       
   260 */
       
   261 	{
       
   262 	if (IsEmpty())
       
   263 		return EFalse;
       
   264 
       
   265 	aChain = iNext;
       
   266 
       
   267 	if (iNext = iNext.Next(), iNext.IsEmpty())
       
   268 		iLast.Init();
       
   269 
       
   270 	aChain.Unlink();
       
   271 	return ETrue;
       
   272 	}
       
   273 
       
   274 
       
   275 void RMBufPktQ::Insert(RMBufChain& aNew, RMBufChain& aPrev)
       
   276 /**
       
   277 Inserts a MBuf chain next to a member in the queue
       
   278 @param aNew A MBuf chain to be inserted
       
   279 @param aPrev the member (a RMBufChain object) of the chain next to which the chain in inserted into
       
   280 */
       
   281 	{
       
   282 	if (aPrev.IsEmpty())
       
   283 		Prepend(aNew);
       
   284 	else if (aPrev.Next().IsEmpty())
       
   285  		Append(aNew);
       
   286 	else
       
   287 		{
       
   288 		RMBufChain tmp;
       
   289 		tmp.Assign(aNew);
       
   290 		tmp.Link(aPrev.Next());
       
   291 		aPrev.Link(tmp);
       
   292 		}
       
   293 	}
       
   294 
       
   295 void RMBufPktQ::Remove(RMBufChain& aNew, RMBufChain& aPrev)
       
   296 /**
       
   297 Removes the part after a member from the queue
       
   298 @param aNew the resulted removed part
       
   299 @param aPrevious the memeber (a RMBufChain object) of the chain the part of which is removed from the queue
       
   300 */
       
   301 	{
       
   302 	if (aPrev.IsEmpty())
       
   303 		(void)Remove(aNew);
       
   304 	else
       
   305 		{
       
   306 		aNew.Assign(aPrev.Next());
       
   307 		aPrev.Link(aNew.Next());
       
   308 		aNew.Unlink();
       
   309 		}
       
   310 	}
       
   311 
       
   312 EXPORT_C void TMBufPktQIter::Insert(RMBufChain& aNewChain)
       
   313 /**
       
   314 Inserts a chain into the queue to the part after current position
       
   315 @param aNewChain the chain to be inserted
       
   316 */
       
   317 	{
       
   318 	iQueue->Insert(aNewChain, iPrev);
       
   319 	TidyAfterUpdate();
       
   320 	}
       
   321 
       
   322 EXPORT_C void TMBufPktQIter::Remove(RMBufChain& aNewChain)
       
   323 /**
       
   324 Removes the part after the current position
       
   325 @param aNewChain the resulted removed queue
       
   326 */
       
   327 	{
       
   328 	iQueue->Remove(aNewChain, iPrev);
       
   329 	TidyAfterUpdate();
       
   330 	}
       
   331 
       
   332 EXPORT_C const RMBufChain& TMBufPktQIter::operator ++(TInt)
       
   333 /**
       
   334 Operator ++
       
   335 move to the next position in the queue
       
   336 */
       
   337 	{
       
   338 	if (iPrev = iCurrent, !iPrev.IsEmpty())
       
   339 		iCurrent = iPrev.Next();
       
   340 	return iPrev;
       
   341 	}
       
   342 
       
   343 EXPORT_C void TMBufPktQIter::TidyAfterUpdate()
       
   344 /**
       
   345 Update the current position, after update
       
   346 if the current position becomes empty after update - the current position will
       
   347 be the first in the queue, else the next in the queue
       
   348 */
       
   349 	{
       
   350 	if (iPrev.IsEmpty())
       
   351 		SetToFirst();
       
   352 	else
       
   353 		iCurrent = iPrev.Next();
       
   354 	}