Pyrogenesis  trunk
file_cache.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * cache of file contents (supports zero-copy IO)
25  */
26 
27 #ifndef INCLUDED_FILE_CACHE
28 #define INCLUDED_FILE_CACHE
29 
30 #include "lib/file/vfs/vfs_path.h"
31 
32 /**
33  * cache of file contents with support for zero-copy IO.
34  * this works by reserving a region of the cache, using it as the IO buffer,
35  * and returning the memory directly to users. optional write-protection
36  * via MMU ensures that the shared contents aren't inadvertently changed.
37  *
38  * (unique copies of) VFS pathnames are used as lookup key and owner tag.
39  *
40  * to ensure efficient operation and prevent fragmentation, only one
41  * reference should be active at a time. in other words, read a file,
42  * process it, and only then start reading the next file.
43  *
44  * rationale: this is rather similar to BlockCache; however, the differences
45  * (Reserve's size parameter, eviction policies) are enough to warrant
46  * separate implementations.
47  **/
48 class FileCache
49 {
50 public:
51  /**
52  * @param size maximum amount [bytes] of memory to use for the cache.
53  * (managed as a virtual memory region that's committed on-demand)
54  **/
55  FileCache(size_t size);
56 
57  /**
58  * Reserve a chunk of the cache's memory region.
59  *
60  * @param size required number of bytes (more may be allocated due to
61  * alignment and/or internal fragmentation)
62  * @return memory suitably aligned for IO; never fails.
63  *
64  * it is expected that this data will be Add()-ed once its IO completes.
65  **/
66  shared_ptr<u8> Reserve(size_t size);
67 
68  /**
69  * Add a file's contents to the cache.
70  *
71  * The cache will be able to satisfy subsequent Retrieve() calls by
72  * returning this data; if CONFIG2_CACHE_READ_ONLY, the buffer is made
73  * read-only. If need be and no references are currently attached to it,
74  * the memory can also be commandeered by Reserve().
75  *
76  * @param data
77  * @param size
78  * @param pathname key that will be used to Retrieve file contents.
79  * @param cost is the expected cost of retrieving the file again and
80  * influences how/when it is evicted from the cache.
81  **/
82  void Add(const VfsPath& pathname, const shared_ptr<u8>& data, size_t size, size_t cost = 1);
83 
84  /**
85  * Remove a file's contents from the cache (if it exists).
86  *
87  * this ensures subsequent reads of the files see the current, presumably
88  * recently changed, contents of the file.
89  *
90  * this would typically be called in response to a notification that a
91  * file has changed.
92  **/
93  void Remove(const VfsPath& pathname);
94 
95  /**
96  * Attempt to retrieve a file's contents from the file cache.
97  *
98  * @return whether the contents were successfully retrieved; if so,
99  * data references the read-only file contents.
100  **/
101  bool Retrieve(const VfsPath& pathname, shared_ptr<u8>& data, size_t& size);
102 
103 private:
104  class Impl;
105  shared_ptr<Impl> impl;
106 };
107 
108 #endif // #ifndef INCLUDED_FILE_CACHE
Definition: file_cache.cpp:157
bool Retrieve(const VfsPath &pathname, shared_ptr< u8 > &data, size_t &size)
Attempt to retrieve a file&#39;s contents from the file cache.
Definition: file_cache.cpp:250
cache of file contents with support for zero-copy IO.
Definition: file_cache.h:48
shared_ptr< Impl > impl
Definition: file_cache.h:104
Definition: path.h:77
void Remove(const VfsPath &pathname)
Remove a file&#39;s contents from the cache (if it exists).
Definition: file_cache.cpp:245
shared_ptr< u8 > Reserve(size_t size)
Reserve a chunk of the cache&#39;s memory region.
Definition: file_cache.cpp:235
void Add(const VfsPath &pathname, const shared_ptr< u8 > &data, size_t size, size_t cost=1)
Add a file&#39;s contents to the cache.
Definition: file_cache.cpp:240
FileCache(size_t size)
Definition: file_cache.cpp:230