sl@0: // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of the License "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: // 
sl@0: 
sl@0: #include <kernel/kernel.h>
sl@0: #include "d_buffer_manager.h"
sl@0: 
sl@0: using namespace Debug;
sl@0: 
sl@0: // Global buffer manager
sl@0: DBufferManager TheDBufferManager;
sl@0: 
sl@0: DBufferManager::DBufferManager()
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: DBufferManager::~DBufferManager()
sl@0: 	{
sl@0: 	for(TInt i=0; i<iBuffers.Count(); i++)
sl@0: 		{
sl@0: 		if(iBuffers[i].iSize>0 && iBuffers[i].iValue)
sl@0: 			{
sl@0: 			Kern::Free((TAny*)iBuffers[i].iValue);
sl@0: 			}
sl@0: 		}
sl@0: 	
sl@0: 	iBuffers.Close();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0:  * Creates a buffer and adds it to the buffer manager
sl@0:  * @param aBufferDetails Contains the size and tag ID of the buffer to be created and the location
sl@0:  *						in which the resulting buffer is created
sl@0:  * @return One of the System wide error codes
sl@0:  */
sl@0: TInt DBufferManager::CreateBuffer(TTag& aBufferDetails)
sl@0: 	{
sl@0: 	if(aBufferDetails.iType != ETagTypePointer)
sl@0: 		{
sl@0: 		return KErrArgument;
sl@0: 		}
sl@0: 	if(aBufferDetails.iSize == 0)
sl@0: 		{
sl@0: 		return KErrArgument;
sl@0: 		}
sl@0: 
sl@0: 	for(TInt i=0; i<iBuffers.Count(); i++)
sl@0: 		{
sl@0: 		if(iBuffers[i].iTagId == aBufferDetails.iTagId)
sl@0: 			{
sl@0: 			return KErrAlreadyExists;
sl@0: 			}
sl@0: 		}
sl@0: 	
sl@0: 	TAny* buffer = Kern::AllocZ(aBufferDetails.iSize);
sl@0: 	if(!buffer)
sl@0: 		{
sl@0: 		return KErrNoMemory;
sl@0: 		}
sl@0: 	
sl@0: 	aBufferDetails.iValue = (TUint32)buffer;
sl@0: 	if(KErrNone != iBuffers.Append(aBufferDetails))
sl@0: 		{
sl@0: 		Kern::Free(buffer);
sl@0: 		return KErrNoMemory;
sl@0: 		}
sl@0: 	
sl@0: 	return KErrNone;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0:  * Retrieves buffer details for a given tag
sl@0:  * @param aTag The tag for which to get buffer details
sl@0:  * @return One of the System wide error codes
sl@0:  */
sl@0: TInt DBufferManager::GetBufferDetails(const TBufferType aBufferType, TTag& aTag) const
sl@0: 	{
sl@0: 	for(TInt i=0; i<iBuffers.Count(); i++)
sl@0: 		{
sl@0: 		if(iBuffers[i].iTagId == aBufferType)
sl@0: 			{
sl@0: 			aTag = iBuffers[i];
sl@0: 			return KErrNone;
sl@0: 			}
sl@0: 		}
sl@0: 	
sl@0: 	aTag.iTagId = aBufferType;
sl@0: 	aTag.iType = ETagTypePointer;
sl@0: 	aTag.iSize = 0;
sl@0: 	aTag.iValue = NULL;
sl@0: 
sl@0: 	return KErrNotFound;
sl@0: 	}
sl@0: