Pyrogenesis
13997
|
cache of file contents with support for zero-copy IO. More...
#include <file_cache.h>
Classes | |
class | Impl |
Public Member Functions | |
FileCache (size_t size) | |
shared_ptr< u8 > | Reserve (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< Impl > | impl |
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.
Definition at line 48 of file file_cache.h.
FileCache::FileCache | ( | size_t | size | ) |
size | maximum amount [bytes] of memory to use for the cache. (managed as a virtual memory region that's committed on-demand) |
Definition at line 230 of file file_cache.cpp.
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().
data | |
size | |
pathname | key that will be used to Retrieve file contents. |
cost | is the expected cost of retrieving the file again and influences how/when it is evicted from the cache. |
Definition at line 240 of file file_cache.cpp.
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.
Definition at line 245 of file file_cache.cpp.
shared_ptr< u8 > FileCache::Reserve | ( | size_t | size | ) |
Reserve a chunk of the cache's memory region.
size | required number of bytes (more may be allocated due to alignment and/or internal fragmentation) |
it is expected that this data will be Add()-ed once its IO completes.
Definition at line 235 of file file_cache.cpp.
Attempt to retrieve a file's contents from the file cache.
Definition at line 250 of file file_cache.cpp.
|
private |
Definition at line 104 of file file_cache.h.