mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-12 17:55:55 +00:00
317f98a6c0
archive: pretty much done except for trace and archive_builder IO: io_manager still needs to be fixed/revised (especially WRT buffer and alignment management) posix: done vfs: need to complete vfs_mount and tie it in to vfs.cpp dir_util: needs further revisions (VFS interface changed since then) This was SVN commit r5475.
92 lines
3.2 KiB
C++
92 lines
3.2 KiB
C++
/**
|
|
* =========================================================================
|
|
* File : file_cache.h
|
|
* Project : 0 A.D.
|
|
* Description : cache of file contents (supports zero-copy IO)
|
|
* =========================================================================
|
|
*/
|
|
|
|
// license: GPL; see lib/license.txt
|
|
|
|
#ifndef INCLUDED_FILE_CACHE
|
|
#define INCLUDED_FILE_CACHE
|
|
|
|
typedef boost::shared_ptr<const u8> FileCacheData;
|
|
|
|
/**
|
|
* cache of file contents with support for zero-copy IO.
|
|
* this works by reserving a region of the cache, using it as the IO buffer,
|
|
* and returning the memory directly to users. optional write-protection
|
|
* via MMU ensures that the shared contents aren't inadvertently changed.
|
|
*
|
|
* (unique copies of) VFS pathnames are used as lookup key and owner tag.
|
|
*
|
|
* to ensure efficient operation and prevent fragmentation, only one
|
|
* reference should be active at a time. in other words, read a file,
|
|
* process it, and only then start reading the next file.
|
|
*
|
|
* rationale: this is rather similar to BlockCache; however, the differences
|
|
* (Reserve's size parameter, Add vs. MarkComplete, different eviction
|
|
* policies) are enough to warrant separate implementations.
|
|
**/
|
|
class FileCache
|
|
{
|
|
public:
|
|
/**
|
|
* @param size maximum amount [bytes] of memory to use for the cache.
|
|
* (managed as a virtual memory region that's committed on-demand)
|
|
**/
|
|
FileCache(size_t size);
|
|
|
|
/**
|
|
* Reserve a chunk of the cache's memory region.
|
|
*
|
|
* @param vfsPathname pathname of the file that is to be read; this is
|
|
* the key that will be used to Retrieve the file contents.
|
|
* @param size required number of bytes (more may be allocated due to
|
|
* alignment and/or internal fragmentation)
|
|
* @return memory suitably aligned for IO; never fails.
|
|
*
|
|
* it is expected that this data will be Add()-ed once its IO completes.
|
|
**/
|
|
FileCacheData Reserve(const char* vfsPathname, size_t size);
|
|
|
|
/**
|
|
* Add a file's contents to the cache.
|
|
*
|
|
* the cache will be able to satisfy subsequent Retrieve() calls by
|
|
* returning this data; if CONFIG_READ_ONLY_CACHE, the buffer is made
|
|
* read-only. if need be and no references are currently attached to it,
|
|
* the memory can also be commandeered by Reserve().
|
|
*
|
|
* @param cost is the expected cost of retrieving the file again and
|
|
* influences how/when it is evicted from the cache.
|
|
**/
|
|
void Add(const char* vfsPathname, FileCacheData data, size_t size, uint cost = 1);
|
|
|
|
/**
|
|
* Remove a file's contents from the cache (if it exists).
|
|
*
|
|
* this ensures subsequent reads of the files see the current, presumably
|
|
* recently changed, contents of the file.
|
|
*
|
|
* this would typically be called in response to a notification that a
|
|
* file has changed.
|
|
**/
|
|
void Remove(const char* vfsPathname);
|
|
|
|
/**
|
|
* Attempt to retrieve a file's contents from the file cache.
|
|
*
|
|
* @return whether the contents were successfully retrieved; if so,
|
|
* data references the read-only file contents.
|
|
**/
|
|
bool Retrieve(const char* vfsPathname, FileCacheData& data, size_t& size);
|
|
|
|
private:
|
|
class Impl;
|
|
boost::shared_ptr<Impl> impl;
|
|
};
|
|
|
|
#endif // #ifndef INCLUDED_FILE_CACHE
|