commsfwutils/commsbufs/version1/mbufmgr/src/MB_BUF.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(Buffer)
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalComponent
       
    21 */
       
    22 
       
    23 #include <es_mbman.h>
       
    24 
       
    25 #include "MBufPoolChain.h"
       
    26 #include "MBufPoolManager.h"
       
    27 
       
    28 //
       
    29 // MBUF
       
    30 //
       
    31 
       
    32 
       
    33 TAny *RMBuf::operator new(TUint /*aSize*/, RMBuf* aPtr)
       
    34 /**
       
    35 Provide MBuf with a new in-place operator
       
    36 */
       
    37 	{
       
    38 	__ASSERT_DEBUG(aPtr, CMBufManager::Panic(EMBuf_BadNewInPlace));
       
    39 	return aPtr;
       
    40 	}
       
    41 
       
    42 
       
    43 void RMBuf::operator delete(TAny* /*aPtr*/)
       
    44 /**
       
    45 In place delete
       
    46 */
       
    47 	{
       
    48 	}
       
    49 
       
    50 
       
    51 void RMBuf::operator delete(TAny* /*aPtr*/, RMBuf*)
       
    52 /**
       
    53 extra delete operator for exception unwinding in Code Warrior
       
    54 */
       
    55 	{
       
    56 	}
       
    57 
       
    58 
       
    59 RMBuf::RMBuf(TUint8* aBuffer, RMBufPoolChain* aPoolChain, TInt aSize)
       
    60 	: iBuffer(aBuffer), iSize(aSize), iPoolChain(aPoolChain)
       
    61 /**
       
    62 MBuf constructor initialiser
       
    63 @param aBuffer A buffer
       
    64 @param aPoolChain The poolchain that contains the MBuf
       
    65 @param aSize Size
       
    66 */
       
    67 	{
       
    68 	Init(EMBufFree);
       
    69 	}
       
    70 
       
    71 
       
    72 EXPORT_C void RMBuf::Init(TMBufType aType)
       
    73 /**
       
    74 Initialises MBuf's members
       
    75 @param aType A MBuf type
       
    76 */
       
    77 	{
       
    78 	iType = aType;
       
    79 	iLength = iSize;
       
    80 	iOffset = 0;
       
    81 	iNext = NULL;
       
    82 	iRefCount = 0;
       
    83 	iNextPkt.Init();
       
    84 	}
       
    85 
       
    86 
       
    87 // allocates memory for a RMBuf object, return a pointer to the RMBuf object memory
       
    88 // - the size of the mbuf to be allocatd will depend on the available pool chains, eg. 128, 1514, 9000, etc
       
    89 EXPORT_C RMBuf* RMBuf::AllocL()
       
    90  	{
       
    91 	return CMBufManager::Context()->AllocL(KMBufSmallSize);		// default size is specified within CMufManager implementation, guaranteed to be only 1 mbuf
       
    92 	}
       
    93 EXPORT_C RMBuf* RMBuf::AllocL(TUint aSize)
       
    94  	{
       
    95 	RMBuf* mBuf = CMBufManager::Context()->Alloc(aSize, aSize);	// min. size constraint to ensure only 1 mbuf is allocated
       
    96 	if (!mBuf)
       
    97 		{
       
    98 		User::Leave(KErrNoMBufs);
       
    99 		}
       
   100 	return mBuf;
       
   101 	}
       
   102 EXPORT_C RMBuf* RMBuf::Alloc()
       
   103  	{
       
   104 	return CMBufManager::Context()->Alloc(KMBufSmallSize);		// default size is specified within CMufManager implementation, guaranteed to be only 1 mbuf
       
   105 	}
       
   106 EXPORT_C RMBuf* RMBuf::Alloc(TUint aSize)
       
   107  	{
       
   108 	return CMBufManager::Context()->Alloc(aSize, aSize);	// min. size constraint to ensure only 1 mbuf is allocated
       
   109 	}
       
   110 
       
   111 // overloading for TLS
       
   112 EXPORT_C RMBuf* RMBuf::AllocL(RMBufAllocator& aRMBufAllocator)
       
   113  	{
       
   114 	return aRMBufAllocator.MBufManager().AllocL(KMBufSmallSize);		// default size is specified within CMufManager implementation, guaranteed to be only 1 mbuf
       
   115 	}
       
   116 EXPORT_C RMBuf* RMBuf::AllocL(TUint aSize, RMBufAllocator& aRMBufAllocator)
       
   117  	{
       
   118 	RMBuf *mBuf = aRMBufAllocator.MBufManager().Alloc(aSize, aSize);	// min. size constraint to ensure only 1 mbuf is allocated
       
   119 	if (!mBuf)
       
   120 		{
       
   121 		User::Leave(KErrNoMBufs);
       
   122 		}
       
   123 	return mBuf;
       
   124 	}
       
   125 EXPORT_C RMBuf* RMBuf::Alloc(RMBufAllocator& aRMBufAllocator)
       
   126 	{
       
   127 	return aRMBufAllocator.MBufManager().Alloc(KMBufSmallSize);		// default size is specified within CMufManager implementation, guaranteed to be only 1 mbuf
       
   128 	}
       
   129 EXPORT_C RMBuf* RMBuf::Alloc(TUint aSize, RMBufAllocator& aRMBufAllocator)
       
   130 	{
       
   131 	return aRMBufAllocator.MBufManager().Alloc(aSize, aSize);	// min. size constraint to ensure only 1 mbuf is allocated
       
   132 	}
       
   133 
       
   134 EXPORT_C void RMBuf::Free()
       
   135 /**
       
   136 Frees the allocated memory for the RMBuf object
       
   137 */
       
   138 	{
       
   139 
       
   140 	// There is currently no support for reference counting, this check is to
       
   141 	// ensure that someone hasn't mistakenly tried to use the iRefCount member.
       
   142 	// The iRefCount member was introduced to allow reference counting to be
       
   143 	// implemented at a later without another BC break having to be raised.
       
   144 	__ASSERT_DEBUG(iRefCount==0, CMBufManager::Panic(EMBuf_InvalidRefCount));
       
   145 	__ASSERT_DEBUG(iPoolChain != NULL, CMBufManager::Panic(EMBuf_NoPoolChain));
       
   146 	((RMBufPoolChain*)iPoolChain)->MBufPoolManager().Free(this, EFalse);
       
   147 	}
       
   148 
       
   149 
       
   150 EXPORT_C RMBuf* RMBuf::Last()
       
   151 /**
       
   152 Last MBuf in a chain.
       
   153 Not really supposed to be in RMBuf,
       
   154 but putting it here allows the code to
       
   155 be shared by RMBufChain and RMBufQ
       
   156 @return A pointer to the last MBuf in the chain
       
   157 */
       
   158 {
       
   159 	RMBuf* p = NULL;
       
   160 	TMBufIter m(this);
       
   161 
       
   162 	while (m.More())
       
   163 		{
       
   164 		p = m;
       
   165 		m++;
       
   166 		}
       
   167 	return p;
       
   168 	}
       
   169 
       
   170 EXPORT_C void RMBuf::__DbgPut(TUint8 aVal, TInt aOffset)
       
   171 {
       
   172 	__ASSERT_ALWAYS(aOffset<(iOffset+iLength), CMBufManager::Panic(EMBuf_BadOffset));
       
   173 	*(Ptr()+aOffset) = aVal;
       
   174 }
       
   175 
       
   176 EXPORT_C TUint8 RMBuf::__DbgGet(TInt aOffset) const
       
   177 {
       
   178 	__ASSERT_ALWAYS(aOffset<(iOffset+iLength), CMBufManager::Panic(EMBuf_BadOffset));
       
   179 	return *(Ptr()+aOffset);
       
   180 }