RetroArch
cache.h
Go to the documentation of this file.
1 /*
2  cache.h
3  The cache is not visible to the user. It should be flushed
4  when any file is closed or changes are made to the filesystem.
5 
6  This cache implements a least-used-page replacement policy. This will
7  distribute sectors evenly over the pages, so if less than the maximum
8  pages are used at once, they should all eventually remain in the cache.
9  This also has the benefit of throwing out old sectors, so as not to keep
10  too many stale pages around.
11 
12  Copyright (c) 2006 Michael "Chishm" Chisholm
13 
14  Redistribution and use in source and binary forms, with or without modification,
15  are permitted provided that the following conditions are met:
16 
17  1. Redistributions of source code must retain the above copyright notice,
18  this list of conditions and the following disclaimer.
19  2. Redistributions in binary form must reproduce the above copyright notice,
20  this list of conditions and the following disclaimer in the documentation and/or
21  other materials provided with the distribution.
22  3. The name of the author may not be used to endorse or promote products derived
23  from this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 
36 #ifndef _CACHE_H
37 #define _CACHE_H
38 
39 #include "common.h"
40 #include "disc.h"
41 
42 typedef struct {
43  sec_t sector;
44  unsigned int count;
45  unsigned int last_access;
46  bool dirty;
47  uint8_t* cache;
48 } CACHE_ENTRY;
49 
50 typedef struct {
51  const DISC_INTERFACE* disc;
52  sec_t endOfPartition;
53  unsigned int numberOfPages;
54  unsigned int sectorsPerPage;
55  unsigned int bytesPerSector;
56  CACHE_ENTRY* cacheEntries;
57 } CACHE;
58 
59 /*
60 Read data from a sector in the cache
61 If the sector is not in the cache, it will be swapped in
62 offset is the position to start reading from
63 size is the amount of data to read
64 Precondition: offset + size <= BYTES_PER_READ
65 */
66 bool _FAT_cache_readPartialSector (CACHE* cache, void* buffer, sec_t sector, unsigned int offset, size_t size);
67 
68 bool _FAT_cache_readLittleEndianValue (CACHE* cache, uint32_t *value, sec_t sector, unsigned int offset, int num_bytes);
69 
70 /*
71 Write data to a sector in the cache
72 If the sector is not in the cache, it will be swapped in.
73 When the sector is swapped out, the data will be written to the disc
74 offset is the position to start writing to
75 size is the amount of data to write
76 Precondition: offset + size <= BYTES_PER_READ
77 */
78 bool _FAT_cache_writePartialSector (CACHE* cache, const void* buffer, sec_t sector, unsigned int offset, size_t size);
79 
80 bool _FAT_cache_writeLittleEndianValue (CACHE* cache, const uint32_t value, sec_t sector, unsigned int offset, int num_bytes);
81 
82 /*
83 Write data to a sector in the cache, zeroing the sector first
84 If the sector is not in the cache, it will be swapped in.
85 When the sector is swapped out, the data will be written to the disc
86 offset is the position to start writing to
87 size is the amount of data to write
88 Precondition: offset + size <= BYTES_PER_READ
89 */
90 bool _FAT_cache_eraseWritePartialSector (CACHE* cache, const void* buffer, sec_t sector, unsigned int offset, size_t size);
91 
92 /*
93 Read several sectors from the cache
94 */
95 bool _FAT_cache_readSectors (CACHE* cache, sec_t sector, sec_t numSectors, void* buffer);
96 
97 /*
98 Read a full sector from the cache
99 */
100 static inline bool _FAT_cache_readSector (CACHE* cache, void* buffer, sec_t sector) {
101  return _FAT_cache_readPartialSector (cache, buffer, sector, 0, cache->bytesPerSector);
102 }
103 
104 /*
105 Write a full sector to the cache
106 */
107 static inline bool _FAT_cache_writeSector (CACHE* cache, const void* buffer, sec_t sector) {
108  return _FAT_cache_writePartialSector (cache, buffer, sector, 0, cache->bytesPerSector);
109 }
110 
111 bool _FAT_cache_writeSectors (CACHE* cache, sec_t sector, sec_t numSectors, const void* buffer);
112 
113 /*
114 Write any dirty sectors back to disc and clear out the contents of the cache
115 */
116 bool _FAT_cache_flush (CACHE* cache);
117 
118 /*
119 Clear out the contents of the cache without writing any dirty sectors first
120 */
121 void _FAT_cache_invalidate (CACHE* cache);
122 
123 CACHE* _FAT_cache_constructor (unsigned int numberOfPages, unsigned int sectorsPerPage, const DISC_INTERFACE* discInterface, sec_t endOfPartition, unsigned int bytesPerSector);
124 
125 void _FAT_cache_destructor (CACHE* cache);
126 
127 #endif // _CACHE_H
128 
unsigned char uint8_t
Definition: stdint.h:124
GLsizeiptr size
Definition: glext.h:6559
bool _FAT_cache_writeLittleEndianValue(CACHE *cache, const uint32_t value, sec_t sector, unsigned int offset, int num_bytes)
Definition: cache.c:241
bool _FAT_cache_readSectors(CACHE *cache, sec_t sector, sec_t numSectors, void *buffer)
Definition: cache.c:161
Definition: cache.h:50
GLuint GLuint GLsizei count
Definition: glext.h:6292
bool _FAT_cache_readLittleEndianValue(CACHE *cache, uint32_t *value, sec_t sector, unsigned int offset, int num_bytes)
Definition: cache.c:206
bool _FAT_cache_eraseWritePartialSector(CACHE *cache, const void *buffer, sec_t sector, unsigned int offset, size_t size)
Definition: cache.c:259
Definition: iosuhax_disc_interface.h:52
Definition: cache.h:42
CACHE * _FAT_cache_constructor(unsigned int numberOfPages, unsigned int sectorsPerPage, const DISC_INTERFACE *discInterface, sec_t endOfPartition, unsigned int bytesPerSector)
Definition: cache.c:49
static bool _FAT_cache_readSector(CACHE *cache, void *buffer, sec_t sector)
Definition: cache.h:100
uint32_t sec_t
Definition: iosuhax_disc_interface.h:40
bool _FAT_cache_writePartialSector(CACHE *cache, const void *buffer, sec_t sector, unsigned int offset, size_t size)
Definition: cache.c:224
Definition: video4linux2.c:51
GLsizei const GLfloat * value
Definition: glext.h:6709
unsigned int bytesPerSector
Definition: cache.h:55
bool _FAT_cache_readPartialSector(CACHE *cache, void *buffer, sec_t sector, unsigned int offset, size_t size)
Definition: cache.c:190
void _FAT_cache_invalidate(CACHE *cache)
Definition: cache.c:325
unsigned int uint32_t
Definition: stdint.h:126
GLintptr offset
Definition: glext.h:6560
bool _FAT_cache_writeSectors(CACHE *cache, sec_t sector, sec_t numSectors, const void *buffer)
Definition: cache.c:278
void _FAT_cache_destructor(CACHE *cache)
Definition: cache.c:93
static bool _FAT_cache_writeSector(CACHE *cache, const void *buffer, sec_t sector)
Definition: cache.h:107
bool _FAT_cache_flush(CACHE *cache)
Definition: cache.c:308