#ifndef __CACHE_HPP #define __CACHE_HPP #define CACHEBLOCKSIZE 512 #define CACHEBLOCKS 100 class CacheBlockOwner; class CacheManager; typedef unsigned long Tag; enum Bool {False, True}; typedef unsigned long TimeStamp; class CacheBlock { Bool used; Bool dirty; CacheBlockOwner *owner; Tag tag; int useLockCount; int readLockCount; int writeLockCount; void *memory; friend class CacheManager; TimeStamp firstRead,lastRead,firstWrite,lastWrite; unsigned long readCount,writeCount; unsigned long dropPriority; CacheBlock() { used=False; } public: void writeStamp(); void readStamp(); void * lockWriteMem(Bool autostamp=True); void unlockWriteMem(Bool autostamp=True); const void * lockReadMem(Bool autostamp=True); void unlockReadMem(Bool autostamp=True); }; struct cw_request { Tag tag; unsigned long priority; const void *memptr; Bool written; unsigned long reserved; }; struct cr_request { Tag tag; unsigned long priority; void *memptr; Bool read; unsigned long reserved; }; class CacheBlockOwner { private: virtual int cache_read(cr_request *r, unsigned requests) =0; virtual int cache_write(cw_request *r, unsigned requests, unsigned minwrite) =0; friend class CacheManager; }; class CacheManager { public: CacheManager(); enum alloc_where { alloc_free, alloc_used, alloc_dirty }; CacheBlock * alloc(CacheBlockOwner *owner, Tag tag, alloc_where where); CacheBlock * find(CacheBlockOwner *owner, Tag tag, Bool readIn=True, alloc_where where=alloc_dirty); struct find_request { Tag tag; CacheBlock *cb; Bool readIn; alloc_where where; }; void find(CacheBlockOwner *owner, find_request *r, unsigned requests); void unlock(CacheBlock *cb); void flush(CacheBlock *cb); void flush(CacheBlockOwner *owner, Tag tag); void flush(CacheBlockOwner *owner); void free(CacheBlock *cb); void free(CacheBlockOwner *owner, Tag tag); void free(CacheBlockOwner *owner); private: char mempool[CACHEBLOCKS][CACHEBLOCKSIZE]; CacheBlock cacheBlock[CACHEBLOCKS]; CacheBlock *selectBlock(alloc_where where); void prioritizeBlocks(); }; #endif