2
* cache.cpp - simple floppy/cd cache for Win32
4
* Basilisk II (C) 1997-2005 Christian Bauer
6
* Windows platform specific code copyright (C) Lauri Pesonen
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
Note that this is particularly silly cache code
25
and doesn't even use hash buckets. It is sufficient
26
for floppies and maybe emulated cd's but that's it.
40
static char THIS_FILE[] = __FILE__;
43
void cache_clear( cachetype *cptr )
47
memset( cptr->LRU, 0, NBLOCKS * sizeof(int) );
51
static int init( cachetype *cptr, int sector_size )
54
cptr->sector_size = sector_size;
55
cptr->blocks = (char *)VirtualAlloc(
56
NULL, NBLOCKS*sector_size,
57
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
58
cptr->block = (int *)VirtualAlloc(
59
NULL, NBLOCKS*sizeof(int),
60
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
61
cptr->LRU = (DWORD *)VirtualAlloc(
62
NULL, NBLOCKS*sizeof(DWORD),
63
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
64
return(cptr->blocks != NULL);
67
static void final( cachetype *cptr )
70
VirtualFree( cptr->blocks, 0, MEM_RELEASE );
74
VirtualFree( cptr->block, 0, MEM_RELEASE );
78
VirtualFree( cptr->LRU, 0, MEM_RELEASE );
84
void cache_init( cachetype *cptr )
89
void cache_final( cachetype *cptr )
97
static int in_cache( cachetype *cptr, int block )
100
for(i=cptr->res_count-1; i>=0; i--) {
101
if(cptr->block[i] == block) return(i);
106
static int get_LRU( cachetype *cptr )
109
DWORD mtime = cptr->LRU[0];
111
for(i=1; i<NBLOCKS; i++) {
112
if(cptr->LRU[i] < mtime) {
113
mtime = cptr->LRU[i];
120
void cache_put( cachetype *cptr, int block, char *buf, int ss )
125
if(!init(cptr,ss)) return;
128
inx = in_cache( cptr, block );
130
if(cptr->res_count == NBLOCKS) {
131
inx = get_LRU( cptr );
133
inx = cptr->res_count++;
135
cptr->block[inx] = block;
137
cptr->LRU[inx] = GetTickCount();
138
memcpy( cptr->blocks + inx * ss, buf, ss );
141
int cache_get( cachetype *cptr, int block, char *buf )
145
if(!cptr->inited) return(0);
147
inx = in_cache( cptr, block );
149
memcpy( buf, cptr->blocks + inx * cptr->sector_size, cptr->sector_size );
156
void cache_remove( cachetype *cptr, int block, int ss )
161
if(!init(cptr,ss)) return;
164
inx = in_cache( cptr, block );
166
if(cptr->res_count > 1) {
167
from = cptr->res_count-1;
168
cptr->block[inx] = cptr->block[from];
169
cptr->LRU[inx] = cptr->LRU[from];
171
cptr->blocks + inx * cptr->sector_size,
172
cptr->blocks + from * cptr->sector_size,