⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eb_cachemanager.h

📁 这是法国Kaleido公司提供了一个手机mmi设计平台
💻 H
字号:
/***************************************************************************
CacheManager.h  -  An generic cache manager for Kaleido
----------------
begin                : Tue Feb 07 2006
copyright            : (C) 2003-2006 by DigitalAirways
email                : info@digitalairways.com
***************************************************************************/

/*
* Copyright (c) 2005-2006 DigitalAirways, sarl. All Rights Reserved.
*
* This software is the confidential and proprietary information of
* DigitalAirways, sarl. ("Confidential Information").  You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with DigitalAirways.
* A copy of this license is included in the licence.txt file included
* on this software package.
*
*
***************************************************************************
*
*/


#ifndef __CACHEMANAGER__
#define __CACHEMANAGER__


#include "EB_Utils.h"


#define CACHEDOBJECT_STATUS_EMPTY		0x00000000
#define CACHEDOBJECT_STATUS_KEYSTRING	0x00000010

typedef void (*objectDestructor)(void* object);


/*
* name: is owned by the cachedObject that must free it when it is not used anymore
* key:  if (status & CACHEDOBJECT_STATUS_KEYSTRING), "key" is a string that is owned 
*   by the cachedObject that must free it when it is not used anymore.
*       if !(status & CACHEDOBJECT_STATUS_KEYSTRING), "key" is a long
*/
class KREBDLIBS_API CachedObject {
private:
	unsigned int   status ;
	char*          key;
	void*          data;
	unsigned int   size;      // Size of the stored object
	unsigned int   extraSize; // Size of the data referenced by the object (explicitely provided, may be false...)
	KALEIDO_TIME_TYPE  lastAccess;
	unsigned int   references ;
	objectDestructor dtor ;
private:
	/*
	*
	*/
	void initContent();
public:
	DEFINE_NEW(CachedObject);
	DEFINE_DELETE(CachedObject);
	CachedObject();
	~CachedObject();
	void* getData();
	/*
	*
	*/
	void flushContent();
	/*
	* The ownership of newData is transferred to the object
	* A new reference on the new content is automatically created.
	*/
	void setContent(KALEIDO_TIME_TYPE  newLastAccess, int newKey, void* newData, int newSize, int newExtraSize, objectDestructor newDtor);
	/*
	* The ownership of the string newKey is not transferred to the object that duplicates its content.
	* The ownership of newData is transferred to the object
	* A new reference on the new content is automatically created.
	*/
	void setContent(KALEIDO_TIME_TYPE  newLastAccess, char* newKey, void* newData, int newSize, int newExtraSize, objectDestructor newDtor);
	/*
	*
	*/
	boolean hasSameKey(int newKey);
	/*
	*
	*/
	boolean hasSameKey(char* newKey);
	/*
	* Return the number of references on the object, or -1 if the objet has no content.
	* If the returned value is >=0, newLastAccess is updated.
	*/
	int getReferences(KALEIDO_TIME_TYPE  *newLastAccess=NULL);
	/*
	* Return the size of this object, or 0 if the object has currently no content.
	*/
	int getSize(boolean withExtraSize);
	/*
	* Return a pointer on the current content and update newSize if it is provided.
	*/
	void* createReference(KALEIDO_TIME_TYPE  newLastAccess, int *newSize=NULL);
	/*
	* Return the number of references.
	*/
	int releaseReference(KALEIDO_TIME_TYPE  newLastAccess, boolean flushIfUnreferenced);
#ifdef DEV_DEBUG
	/*
	* 
	*/
	void dump(KALEIDO_TIME_TYPE  newLastAccess, int index);
#endif // def DEV_DEBUG
};


class KREBDLIBS_API ObjectPool {
private:
	CachedObject** objects;
	int objectNum ; // Number of slots in this pool
	int cacheMaxSize;  // Maximum size usable by the pool
	int cacheUsedSize; // Size currently used by the pool
	int cacheRefSize;  // Size currently referenced by the pool
	int cacheUnstoredNum;  // Number of objects that have not been stored because there was not enough free room
	int cacheUnstoredSize; // Size of objects that have not been stored because there was not enough free room
private:
	/*
	* This function tries to find (and return) a free or
	* a recyclable object.
	*/
	CachedObject* findFreeSlot(void);
	/*
	* This function returns the oldest unreferenced non-empty slot 
	* or NULL if it does not exist.
	*/
	CachedObject* getOldestSlot(void);
	/*
	* This function find a slot to store a new object whose
	* size is defined.
	*/
	CachedObject* findFreeObject(int sizeToFind);
	/*
	*
	*/
	CachedObject* findObject(int key);
	/*
	*
	*/
	CachedObject* findObject(char* key);
	/*
	*
	*/
	CachedObject* findObjectByData(void* data);
	/*
	*
	*/
	int countUsedSlots(void);
public:
	DEFINE_NEW(ObjectPool);
	DEFINE_DELETE(ObjectPool);
	ObjectPool(int newNum, int newSize=0);
	~ObjectPool();
	//
	void* getObjectByIndex(int index);
	/*
	* Try to find the key in the pool. If it is found, a pointer on the data bloc is returned
	* and an additional reference on the object is created.
	* If it is not found, it returns NULL.
	*/
	void* getFromPool(KALEIDO_TIME_TYPE currentTime, int key  , int *size=NULL);
	void* getFromPool(KALEIDO_TIME_TYPE currentTime, char* key, int *size=NULL);
	/*
	* Try to find the key or data in the pool. If it is found, a reference on the object is removed.
	* Return the number of current references on the same key, or -1 if the objet is  not found.
	*/
	int releaseFromPool(KALEIDO_TIME_TYPE currentTime      , CachedObject* cached, boolean flushIfUnreferenced);
	int releaseFromPool(KALEIDO_TIME_TYPE currentTime      , int key             , boolean flushIfUnreferenced);
	int releaseFromPool(KALEIDO_TIME_TYPE currentTime      , char* key           , boolean flushIfUnreferenced);
	int releaseFromPoolByData(KALEIDO_TIME_TYPE currentTime, void* data          , boolean flushIfUnreferenced);
	/*
	* Store the data in the pool. 
	* WARNING: we don't check that the key is not already stored in the pool.
	* Return true if and only if the system has been able to store the data.
	*/
	boolean pushInPool(KALEIDO_TIME_TYPE  newLastAccess, int newKey  , void* newData, int newSize, int newExtraSize, objectDestructor newDtor=NULL);
	boolean pushInPool(KALEIDO_TIME_TYPE  newLastAccess, char* newKey, void* newData, int newSize, int newExtraSize, objectDestructor newDtor=NULL);
	/*
	*
	*/
	void setMaxSize(int newCacheMaxSize);
	int getMaxSize(void)  { return cacheMaxSize; }
	int getUsedSize(void) { return cacheUsedSize; }
	int getRefSize(void)  { return cacheRefSize; }
	/*
	* This function flushes all the unreferenced objects.
	*/
	void pack(void);
#ifdef DEV_DEBUG
	/*
	* 
	*/
	void dump(KALEIDO_TIME_TYPE  newLastAccess);
#endif // def DEV_DEBUG
} ;



#endif // ndef __CACHEMANAGER__

⌨️ 快捷键说明

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