Team Ai
Datasetpublic

hschumann2/TempleOS-Source-Code

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