2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
10 # $Id: cache.c 911 2005-03-14 21:02:17Z oopo $
11 # APA cache manipulation routines
20 int cacheInit(u32 size
)
25 cacheSize
=size
; // save size ;)
26 if((header
=(apa_header
*)allocMem(size
*sizeof(apa_header
)))){
27 cacheBuf
=allocMem((size
+1)*sizeof(apa_cache
));
33 // setup cache header...
34 memset(cacheBuf
, 0, (size
+1)*sizeof(apa_cache
));
35 cacheBuf
->next
=cacheBuf
;
36 cacheBuf
->tail
=cacheBuf
;
37 for(i
=1; i
<size
+1;i
++, header
++){
38 cacheBuf
[i
].header
=header
;
39 cacheBuf
[i
].device
=-1;
40 cacheLink(cacheBuf
->tail
, &cacheBuf
[i
]);
45 void cacheLink(apa_cache
*clink
, apa_cache
*cnew
)
48 cnew
->next
=clink
->next
;
49 clink
->next
->tail
=cnew
;
53 apa_cache
*cacheUnLink(apa_cache
*clink
)
55 clink
->tail
->next
=clink
->next
;
56 clink
->next
->tail
=clink
->tail
;
60 int cacheTransfer(apa_cache
*clink
, int type
)
64 err
=apaWriteHeader(clink
->device
, clink
->header
, clink
->sector
);
66 err
=apaReadHeader(clink
->device
, clink
->header
, clink
->sector
);
70 dprintf1("ps2hdd: Error: disk err %d on device %ld, sector %ld, type %d\n",
71 err
, clink
->device
, clink
->sector
, type
);
72 if(type
==0)// save any read error's..
73 apaSaveError(clink
->device
, clink
->header
, APA_SECTOR_SECTOR_ERROR
, clink
->sector
);
75 clink
->flags
&=~CACHE_FLAG_DIRTY
;
79 void cacheFlushDirty(apa_cache
*clink
)
81 if(clink
->flags
&CACHE_FLAG_DIRTY
)
82 cacheTransfer(clink
, THEADER_MODE_WRITE
);
85 int cacheFlushAllDirty(u32 device
)
89 for(i
=1;i
<cacheSize
+1;i
++){
90 if((cacheBuf
[i
].flags
& CACHE_FLAG_DIRTY
) && cacheBuf
[i
].device
==device
)
91 journalWrite(&cacheBuf
[i
]);
95 for(i
=1;i
<cacheSize
+1;i
++){
96 if((cacheBuf
[i
].flags
& CACHE_FLAG_DIRTY
) && cacheBuf
[i
].device
==device
)
97 cacheTransfer(&cacheBuf
[i
], THEADER_MODE_WRITE
);
99 return journalReset(device
);
102 apa_cache
*cacheGetHeader(u32 device
, u32 sector
, u32 mode
, int *result
)
104 apa_cache
*clink
=NULL
;
108 for(i
=1;i
<cacheSize
+1;i
++){
109 if(cacheBuf
[i
].sector
==sector
&&
110 cacheBuf
[i
].device
==device
){
116 // cached ver was found :)
118 clink
=cacheUnLink(clink
);
122 if((cacheBuf
->tail
==cacheBuf
) &&
123 (cacheBuf
->tail
==cacheBuf
->tail
->next
)){
124 dprintf1("ps2hdd: error: free buffer empty\n");
128 clink
=cacheBuf
->next
;
129 if(clink
->flags
& CACHE_FLAG_DIRTY
)
130 dprintf1("ps2hdd: error: dirty buffer allocated\n");
133 clink
->device
=device
;
134 clink
->sector
=sector
;
135 clink
=cacheUnLink(clink
);
144 if((*result
=cacheTransfer(clink
, THEADER_MODE_READ
))<0){
147 cacheLink(cacheBuf
, clink
);
154 void cacheAdd(apa_cache
*clink
)
157 dprintf1("ps2hdd: Error: null buffer returned\n");
161 dprintf1("ps2hdd: Error: unused cache returned\n");
164 if(clink
->flags
& CACHE_FLAG_DIRTY
)
165 dprintf1("ps2hdd: Error: dirty buffer returned\n");
168 cacheLink(cacheBuf
->tail
, clink
);
172 apa_cache
*cacheGetFree()
176 if((cacheBuf
->tail
==cacheBuf
) &&
177 (cacheBuf
->tail
==cacheBuf
->tail
->next
)){
178 dprintf1("ps2hdd: Error: free buffer empty\n");
181 cnext
=cacheBuf
->next
;
182 if(cnext
->flags
& CACHE_FLAG_DIRTY
)
183 dprintf1("ps2hdd: Error: dirty buffer allocated\n");
186 cnext
->device
=(u32
)-1;
188 return cacheUnLink(cnext
);