File size: 3,543 Bytes
806d4b5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
U0 DskCacheInit(I64 size_in_U8s)
{
CCacheBlk *tmpc;
I64 i,cnt;
while (LBts(&sys_semas[SEMA_DSK_CACHE],0))
Yield;
Free(blkdev.cache_ctrl);
Free(blkdev.cache_base);
Free(blkdev.cache_hash_table);
if (size_in_U8s<0x2000) {
blkdev.cache_ctrl=NULL;
blkdev.cache_base=NULL;
blkdev.cache_hash_table=NULL;
} else {
blkdev.cache_ctrl=ACAlloc(offset(CCacheBlk.body));
blkdev.cache_base=AMAlloc(size_in_U8s);
QueInit(blkdev.cache_ctrl);
cnt=MSize(blkdev.cache_base)/sizeof(CCacheBlk);
blkdev.cache_size=cnt*BLK_SIZE;
for (i=0;i<cnt;i++) {
tmpc=blkdev.cache_base+i;
QueIns(tmpc,blkdev.cache_ctrl->last_lru);
tmpc->next_hash=tmpc->last_hash=tmpc;
tmpc->dv=NULL;
tmpc->blk=0;
}
blkdev.cache_hash_table=AMAlloc(DSK_CACHE_HASH_SIZE*sizeof(U8 *)*2);
for (i=0;i<DSK_CACHE_HASH_SIZE;i++) {
tmpc=blkdev.cache_hash_table(U8 *)+i*sizeof(U8 *)*2
-offset(CCacheBlk.next_hash);
tmpc->next_hash=tmpc->last_hash=tmpc;
}
}
LBtr(&sys_semas[SEMA_DSK_CACHE],0);
}
I64 DskCacheHash(I64 blk)
{
I64 i=blk & (DSK_CACHE_HASH_SIZE-1);
return blkdev.cache_hash_table(U8 *)+i<<4-offset(CCacheBlk.next_hash);
}
U0 DskCacheQueRem(CCacheBlk *tmpc)
{
QueRem(tmpc);
tmpc->next_hash->last_hash=tmpc->last_hash;
tmpc->last_hash->next_hash=tmpc->next_hash;
}
U0 DskCacheQueIns(CCacheBlk *tmpc)
{
CCacheBlk *tmp_n,*tmp_l;
QueIns(tmpc,blkdev.cache_ctrl->last_lru);
tmp_l=DskCacheHash(tmpc->blk);
tmp_n=tmp_l->next_hash;
tmpc->last_hash=tmp_l;
tmpc->next_hash=tmp_n;
tmp_l->next_hash=tmp_n->last_hash=tmpc;
}
CCacheBlk *DskCacheFind(CDrv *dv,I64 blk)
{
CCacheBlk *tmpc,*tmpc1=DskCacheHash(blk);
tmpc=tmpc1->next_hash;
while (tmpc!=tmpc1) {
if (tmpc->dv==dv && tmpc->blk==blk)
return tmpc;
tmpc=tmpc->next_hash;
}
return NULL;
}
U0 DskCacheAdd(CDrv *dv,U8 *buf, I64 blk, I64 cnt)
{
CCacheBlk *tmpc;
if (blkdev.cache_base) {
while (LBts(&sys_semas[SEMA_DSK_CACHE],0))
Yield;
while (cnt-->0) {
if (!(tmpc=DskCacheFind(dv,blk)))
tmpc=blkdev.cache_ctrl->next_lru;
DskCacheQueRem(tmpc);
MemCpy(&tmpc->body,buf,BLK_SIZE);
tmpc->dv=dv;
tmpc->blk=blk;
DskCacheQueIns(tmpc);
blk++;
buf+=BLK_SIZE;
}
LBtr(&sys_semas[SEMA_DSK_CACHE],0);
}
}
U0 DskCacheInvalidate2(CDrv *dv)
{
CCacheBlk *tmpc,*tmpc1;
if (blkdev.cache_base) {
while (LBts(&sys_semas[SEMA_DSK_CACHE],0))
Yield;
tmpc=blkdev.cache_ctrl->last_lru;
while (tmpc!=blkdev.cache_ctrl) {
tmpc1=tmpc->last_lru;
if (tmpc->dv==dv) {
DskCacheQueRem(tmpc);
tmpc->dv=NULL;
tmpc->blk=0;
tmpc->next_hash=tmpc->last_hash=tmpc;
QueIns(tmpc,blkdev.cache_ctrl->last_lru);
}
tmpc=tmpc1;
}
LBtr(&sys_semas[SEMA_DSK_CACHE],0);
}
}
U0 RCache(CDrv *dv,U8 **_buf, I64 *_blk, I64 *_cnt)
{
CCacheBlk *tmpc;
if (blkdev.cache_base) {
while (LBts(&sys_semas[SEMA_DSK_CACHE],0))
Yield;
//fetch leading blks from cache
while (*_cnt>0) {
if (tmpc=DskCacheFind(dv,*_blk)) {
MemCpy(*_buf,&tmpc->body,BLK_SIZE);
*_cnt-=1;
*_buf+=BLK_SIZE;
*_blk+=1;
} else
break;
}
//fetch trailing blks from cache
while (*_cnt>0) {
if (tmpc=DskCacheFind(dv,*_blk+*_cnt-1)) {
MemCpy(*_buf+(*_cnt-1)<<BLK_SIZE_BITS,&tmpc->body,BLK_SIZE);
*_cnt-=1;
} else
break;
}
LBtr(&sys_semas[SEMA_DSK_CACHE],0);
}
}
|