chunkres.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 2,018 行 · 第 1/4 页

CPP
2,018
字号
/* ***** BEGIN LICENSE BLOCK *****
 * Source last modified: $Id: chunkres.cpp,v 1.14.32.4 2004/07/09 01:44:51 hubbe Exp $
 * 
 * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
 * 
 * The contents of this file, and the files included with this file,
 * are subject to the current version of the RealNetworks Public
 * Source License (the "RPSL") available at
 * http://www.helixcommunity.org/content/rpsl unless you have licensed
 * the file under the current version of the RealNetworks Community
 * Source License (the "RCSL") available at
 * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
 * will apply. You may also obtain the license terms directly from
 * RealNetworks.  You may not use this file except in compliance with
 * the RPSL or, if you have a valid RCSL with RealNetworks applicable
 * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
 * the rights, obligations and limitations governing use of the
 * contents of the file.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL") in which case the provisions of the GPL are applicable
 * instead of those above. If you wish to allow use of your version of
 * this file only under the terms of the GPL, and not to allow others
 * to use your version of this file under the terms of either the RPSL
 * or RCSL, indicate your decision by deleting the provisions above
 * and replace them with the notice and other provisions required by
 * the GPL. If you do not delete the provisions above, a recipient may
 * use your version of this file under the terms of any one of the
 * RPSL, the RCSL or the GPL.
 * 
 * This file is part of the Helix DNA Technology. RealNetworks is the
 * developer of the Original Code and owns the copyrights in the
 * portions it created.
 * 
 * This file, and the files included with this file, is distributed
 * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
 * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
 * ENJOYMENT OR NON-INFRINGEMENT.
 * 
 * Technology Compatibility Kit Test Suite(s) Location:
 *    http://www.helixcommunity.org/content/tck
 * 
 * Contributor(s):
 * 
 * ***** END LICENSE BLOCK ***** */

#include "hxslist.h"
#include "chunkres.h"
#include "hlxclib/stdlib.h"		// needed for MAX_PATH
#include "hlxclib/stdio.h"		// for fopen(), etc.
#include "hlxclib/limits.h"		// for INT_MAX, etc.

#include "hlxclib/fcntl.h"	// for O_CREAT, etc.
#include "hlxclib/io.h"
#include "hlxclib/windows.h"

#include "chxdataf.h"	// cross platform file object

#ifdef _MACINTOSH
#ifdef _MAC_MACHO
#include <unistd.h> // for unlink call
#else
#include <unix.h>		// for unlink call
#undef _UNIX	//defined in unixmac.h
#endif
#endif

#ifdef _UNIX
#include <unistd.h> 	// for unlink
#ifndef _MAX_PATH
#define _MAX_PATH		256
#endif
#endif

#ifdef _SYMBIAN
#include <unistd.h> //for unlink
#endif

#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyResMgr::OpenResource()
//
//	Purpose:
//
//		Opens an existing resource or creates a new resource.
//
//	Parameters:
//
//		CChunkyRes** ppChunkyRes
//		Memory location that will be filled in on output with the pointer
//		to the opened CChunkRes object.
//
//		const char* pResName
//		Unique name of the resource to open or create.
//
//	Return:
//
//		HX_RESULT
//		Possible errors include: TBD.
//
HX_RESULT CChunkyResMgr::OpenResource(CChunkyRes** ppChunkyRes, const char* pResName)
{
    HX_RESULT theErr = HXR_OK;

    HX_ASSERT(ppChunkyRes && pResName);
    
    void* pData;
    if (m_OpenResources.Lookup(pResName, pData))
    {
	*ppChunkyRes = (CChunkyRes*)pData;
    }
    else if (m_ClosedResources.Lookup(pResName, pData))
    {
	*ppChunkyRes = (CChunkyRes*)pData;
	HX_VERIFY(m_ClosedResources.RemoveKey(pResName));
	m_OpenResources.SetAt(pResName, pData);

	RemoveFromLRU(pResName);
    }
    else
    {
	*ppChunkyRes = new CChunkyRes;
	if (*ppChunkyRes)
	{
	    m_OpenResources.SetAt(pResName, (void*)*ppChunkyRes);
	}
	else
	{
	    theErr = HXR_OUTOFMEMORY;
	}
    }

    return theErr;
}

/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyResMgr::CloseResource()
//
//	Purpose:
//
//		Closes an existing resource. Closed resources may be discarded.
//
//	Parameters:
//
//		CChunkyRes* pChunkyRes
//		Pointer to a previously opened CChunkRes object.
//
//	Return:
//
//		HX_RESULT
//		Possible errors include: TBD.
//
HX_RESULT CChunkyResMgr::CloseResource(CChunkyRes* pChunkyRes)
{
    HX_RESULT theErr = HXR_FAIL;

    POSITION pPos = m_OpenResources.GetStartPosition();
    while (pPos)
    {
	CHXString key;
	void* pData;
	m_OpenResources.GetNextAssoc(pPos, key, pData);

	if (pData == (void*)pChunkyRes)
	{
	    HX_VERIFY(m_OpenResources.RemoveKey(key));

	    m_ClosedResources.SetAt(key, pData);

	    HX_ASSERT(!m_LRUResources.FindString(key));
	    m_LRUResources.AddTailString(key);

	    theErr = HXR_OK;
	}
    }

    if (theErr == HXR_OK)
    {
	DiscardDiskData();
    }

    return theErr;
}


/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyResMgr::CloseResource()
//
//	Purpose:
//
//		Closes an existing resource. Closed resources may be discarded.
//
//	Parameters:
//
//		const char* pResName
//		Unique name of a previously opened resource.
//
//	Return:
//
//		HX_RESULT
//		Possible errors include: TBD.
//
HX_RESULT CChunkyResMgr::CloseResource(const char* pResName)
{
    HX_RESULT theErr = HXR_FAIL;

    void* pData;
    if (m_OpenResources.Lookup(pResName, pData))
    {
	HX_VERIFY(m_OpenResources.RemoveKey(pResName));

	m_ClosedResources.SetAt(pResName, pData);

	HX_ASSERT(!m_LRUResources.FindString(pResName));
	m_LRUResources.AddTailString(pResName);

	theErr = HXR_OK;
    }

    if (theErr == HXR_OK)
    {
	DiscardDiskData();
    }

    return theErr;
}

/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyResMgr::DiscardResource()
//
//	Purpose:
//
//		Discards a resource. Closed resources may be discarded.
//
//	Parameters:
//
//		const char* pResName
//		Unique name of a previously opened resource.
//
//	Return:
//
//		HX_RESULT
//		Possible errors include: TBD.
//
HX_RESULT CChunkyResMgr::DiscardResource(const char* pResName)
{
    HX_RESULT theErr = HXR_FAIL;

    void* pData;
    if (m_OpenResources.Lookup(pResName, pData))
    {
	HX_VERIFY(m_OpenResources.RemoveKey(pResName));

	CChunkyRes* pRes = (CChunkyRes*)pData;

	delete pRes;

	theErr = HXR_OK;
    }

    if (m_ClosedResources.Lookup(pResName, pData))
    {
	HX_VERIFY(m_ClosedResources.RemoveKey(pResName));

	RemoveFromLRU(pResName);

	CChunkyRes* pRes = (CChunkyRes*)pData;

	delete pRes;

	theErr = HXR_OK;
    }

    return theErr;
}


/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyResMgr::FindResource()
//
//	Purpose:
//
//		Looks to see if the resource exists.
//
//	Parameters:
//
//		const char* pResName
//		Unique name of a previously opened resource.
//
//	Return:
//
//		HX_RESULT
HX_RESULT
CChunkyResMgr::FindResource(const char* pResName)
{
    void* pData;
    if (m_OpenResources.Lookup(pResName, pData) ||
	m_ClosedResources.Lookup(pResName, pData))
    {
	return HXR_OK;
    }

    return HXR_FAIL;
}


/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyResMgr::SetDiskUsageThreshold()
//
//	Purpose:
//
//		Sets the Disk Usage threshold for the chunky resource manager.
//		If closed resources amount to more than the threshold, then they
//		will be discarded.
//
//	Parameters:
//
//		ULONG32 diskUsage
//		Disk usage in bytes which will be allowed for closed resources.
//
//	Return:
//
//		None.
//
void CChunkyResMgr::SetDiskUsageThreshold(ULONG32 diskUsage)
{
    m_ulDiskUsage = diskUsage;

    DiscardDiskData();
}


void CChunkyResMgr::DiscardDiskData()
{
    void* pData = NULL;
    CChunkyRes* pRes = NULL;
    ULONG32 ulTotal = 0;

    // Count the total disk usage
    POSITION pPos = m_ClosedResources.GetStartPosition();
    while (pPos)
    {
	CHXString key;
	m_ClosedResources.GetNextAssoc(pPos, key, pData);

	HX_ASSERT(pData);
	pRes = (CChunkyRes*)pData;

	ulTotal += pRes->GetDiskUsage();
    }

    // Trim as much as we need until we're under the disk usage threshold.
    pPos = m_LRUResources.GetHeadPosition();
    while (pPos && ulTotal > m_ulDiskUsage)
    {
	CHXString* pResName = m_LRUResources.GetNext(pPos);

	HX_ASSERT(pResName);
	if (m_ClosedResources.Lookup(*pResName, pData))
	{
	    HX_ASSERT(pData);
	    pRes = (CChunkyRes*)pData;

	    ULONG32 ulSize = pRes->GetDiskUsage();
	    if (ulSize)
	    {
		HX_ASSERT(ulSize <= ulTotal);
		ulTotal -= ulSize;

		m_ClosedResources.RemoveKey(*pResName);

		RemoveFromLRU(*pResName);

		delete pRes;
	    }
	}
    }
}


void CChunkyResMgr::RemoveFromLRU(const char* pResName)
{
    POSITION pPos = m_LRUResources.GetHeadPosition();
    POSITION pPrev; 
    while (pPos)
    {
	pPrev = pPos;
	CHXString* pStr = m_LRUResources.GetNext(pPos);
	if (!strcmp(*pStr, pResName))
	{
	    m_LRUResources.RemoveAt(pPrev);
	}
    }
}




/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyResMgr::CChunkyResMgr()
//
//	Purpose:
//
//		Construtor for chunky resource manager.
//
//	Parameters:
//
//		None.
//
//	Return:
//
//		N/A
//
CChunkyResMgr::CChunkyResMgr()
    : m_ulDiskUsage(0)
{
}


/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyResMgr::~CChunkyResMgr()
//
//	Purpose:
//
//		Destructor for chunky resource manager.
//
//	Parameters:
//
//		None.
//
//	Return:
//
//		N/A
//
CChunkyResMgr::~CChunkyResMgr()
{
    CHXString	key;
    CChunkyRes* pRes;
    POSITION	p;
    
    p = m_OpenResources.GetStartPosition();
    while (p)
    {
	m_OpenResources.GetNextAssoc(p, key, (void*&)pRes);
	HX_DELETE(pRes);
    }

    p = m_ClosedResources.GetStartPosition();
    while (p)
    {
	m_ClosedResources.GetNextAssoc(p, key, (void*&)pRes);
	HX_DELETE(pRes);
    }

}


/////////////////////////////////////////////////////////////////////////////
//
//	Method:
//
//		CChunkyRes::DiscardRange()
//
//	Purpose:
//
//		Discards the specified range of the file.
//
//	Parameters:
//
//		The location and length of the range to be discarded.
//
//	Return:
//

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?