Pyrogenesis  trunk
Classes | Public Member Functions | Private Attributes | List of all members
FileCache Class Reference

cache of file contents with support for zero-copy IO. More...

#include <file_cache.h>

Collaboration diagram for FileCache:
Collaboration graph
[legend]

Classes

class  Impl
 

Public Member Functions

 FileCache (size_t size)
 
shared_ptr< u8Reserve (size_t size)
 Reserve a chunk of the cache's memory region. More...
 
void Add (const VfsPath &pathname, const shared_ptr< u8 > &data, size_t size, size_t cost=1)
 Add a file's contents to the cache. More...
 
void Remove (const VfsPath &pathname)
 Remove a file's contents from the cache (if it exists). More...
 
bool Retrieve (const VfsPath &pathname, shared_ptr< u8 > &data, size_t &size)
 Attempt to retrieve a file's contents from the file cache. More...
 

Private Attributes

shared_ptr< Implimpl
 

Detailed Description

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, eviction policies) are enough to warrant separate implementations.

Constructor & Destructor Documentation

FileCache::FileCache ( size_t  size)
Parameters
sizemaximum amount [bytes] of memory to use for the cache. (managed as a virtual memory region that's committed on-demand)

Member Function Documentation

void FileCache::Add ( const VfsPath pathname,
const shared_ptr< u8 > &  data,
size_t  size,
size_t  cost = 1 
)

Add a file's contents to the cache.

The cache will be able to satisfy subsequent Retrieve() calls by returning this data; if CONFIG2_CACHE_READ_ONLY, 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().

Parameters
data
size
pathnamekey that will be used to Retrieve file contents.
costis the expected cost of retrieving the file again and influences how/when it is evicted from the cache.
void FileCache::Remove ( const VfsPath pathname)

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.

shared_ptr< u8 > FileCache::Reserve ( size_t  size)

Reserve a chunk of the cache's memory region.

Parameters
sizerequired number of bytes (more may be allocated due to alignment and/or internal fragmentation)
Returns
memory suitably aligned for IO; never fails.

it is expected that this data will be Add()-ed once its IO completes.

bool FileCache::Retrieve ( const VfsPath pathname,
shared_ptr< u8 > &  data,
size_t &  size 
)

Attempt to retrieve a file's contents from the file cache.

Returns
whether the contents were successfully retrieved; if so, data references the read-only file contents.

Member Data Documentation

shared_ptr<Impl> FileCache::impl
private

The documentation for this class was generated from the following files: