templeos-info/public/Wb/Kernel/BlkDev/DskCache.HC.HTML

177 lines
17 KiB
Plaintext
Raw Normal View History

2024-03-24 21:24:44 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DskCacheInit</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> size_in_U8s)
<a name="l2"></a>{
<a name="l3"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc;
<a name="l4"></a> </span><span class=cF9>I64</span><span class=cF0> i,cnt;
<a name="l5"></a>
<a name="l6"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DSK_CACHE</span><span class=cF0>],0</span><span class=cF7>)</span><span class=cF0>)
<a name="l7"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l8"></a> </span><span class=cF5>Free</span><span class=cF0>(</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl);
<a name="l9"></a> </span><span class=cF5>Free</span><span class=cF0>(</span><span class=cFB>blkdev</span><span class=cF0>.cache_base);
<a name="l10"></a> </span><span class=cF5>Free</span><span class=cF0>(</span><span class=cFB>blkdev</span><span class=cF0>.cache_hash_table);
<a name="l11"></a> </span><span class=cF1>if</span><span class=cF0> (size_in_U8s&lt;0x2000) </span><span class=cF7>{</span><span class=cF0>
<a name="l12"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l13"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_base=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l14"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_hash_table=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l15"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l16"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl=</span><span class=cF5>ACAlloc</span><span class=cF0>(</span><span class=cF1>offset</span><span class=cF7>(</span><span class=cF9>CCacheBlk</span><span class=cF0>.body</span><span class=cF7>)</span><span class=cF0>);
<a name="l17"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_base=</span><span class=cF5>AMAlloc</span><span class=cF0>(size_in_U8s);
<a name="l18"></a> </span><span class=cF5>QueInit</span><span class=cF0>(</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl);
<a name="l19"></a>
<a name="l20"></a> cnt=</span><span class=cF5>MSize</span><span class=cF0>(</span><span class=cFB>blkdev</span><span class=cF0>.cache_base)/</span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0>);
<a name="l21"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_size=cnt*</span><span class=cF3>BLK_SIZE</span><span class=cF0>;
<a name="l22"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;cnt;i++) {
<a name="l23"></a> tmpc=</span><span class=cFB>blkdev</span><span class=cF0>.cache_base+i;
<a name="l24"></a> </span><span class=cF5>QueIns</span><span class=cF0>(tmpc,</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;last_lru);
<a name="l25"></a> tmpc-&gt;next_hash=tmpc-&gt;last_hash=tmpc;
<a name="l26"></a> tmpc-&gt;dv=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l27"></a> tmpc-&gt;blk=0;
<a name="l28"></a> }
<a name="l29"></a>
<a name="l30"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_hash_table=</span><span class=cF5>AMAlloc</span><span class=cF0>(</span><span class=cF3>DSK_CACHE_HASH_SIZE</span><span class=cF0>*</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF7>)</span><span class=cF0>*2);
<a name="l31"></a> </span><span class=cF1>for</span><span class=cF0> (i=0;i&lt;</span><span class=cF3>DSK_CACHE_HASH_SIZE</span><span class=cF0>;i++) {
<a name="l32"></a> tmpc=</span><span class=cFB>blkdev</span><span class=cF0>.cache_hash_table(</span><span class=cF1>U8</span><span class=cF0> *)+i*</span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *)*2
<a name="l33"></a> -</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0>.next_hash);
<a name="l34"></a> tmpc-&gt;next_hash=tmpc-&gt;last_hash=tmpc;
<a name="l35"></a> }
<a name="l36"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l37"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DSK_CACHE</span><span class=cF0>],0);
<a name="l38"></a>}
<a name="l39"></a>
<a name="l40"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFD>DskCacheHash</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> blk)
<a name="l41"></a>{
<a name="l42"></a> </span><span class=cF9>I64</span><span class=cF0> i=blk &amp; (</span><span class=cF3>DSK_CACHE_HASH_SIZE</span><span class=cF0>-1);
<a name="l43"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cFB>blkdev</span><span class=cF0>.cache_hash_table(</span><span class=cF1>U8</span><span class=cF0> *)+i&lt;&lt;4-</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0>.next_hash);
<a name="l44"></a>}
<a name="l45"></a>
<a name="l46"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>DskCacheQueRem</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc)
<a name="l47"></a>{
<a name="l48"></a> </span><span class=cF5>QueRem</span><span class=cF0>(tmpc);
<a name="l49"></a> tmpc-&gt;next_hash-&gt;last_hash=tmpc-&gt;last_hash;
<a name="l50"></a> tmpc-&gt;last_hash-&gt;next_hash=tmpc-&gt;next_hash;
<a name="l51"></a>}
<a name="l52"></a>
<a name="l53"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>DskCacheQueIns</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc)
<a name="l54"></a>{
<a name="l55"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmp_n,*tmp_l;
<a name="l56"></a> </span><span class=cF5>QueIns</span><span class=cF0>(tmpc,</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;last_lru);
<a name="l57"></a> tmp_l=</span><span class=cFD>DskCacheHash</span><span class=cF0>(tmpc-&gt;blk);
<a name="l58"></a> tmp_n=tmp_l-&gt;next_hash;
<a name="l59"></a> tmpc-&gt;last_hash=tmp_l;
<a name="l60"></a> tmpc-&gt;next_hash=tmp_n;
<a name="l61"></a> tmp_l-&gt;next_hash=tmp_n-&gt;last_hash=tmpc;
<a name="l62"></a>}
<a name="l63"></a>
<a name="l64"></a></span><span class=cF9>CCacheBlk</span><span class=cF0> *</span><span class=cFD>DskCacheFind</span><span class=cF0>(</span><span class=cF9>CDrv</span><span class=cF0> *dv,</span><span class=cF9>I64</span><span class=cF0> blk)
<a name="l65"></a>{
<a name="l66"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc,*tmpc1=</span><span class=cFD>DskCacheHash</span><span class=cF0>(blk);
<a name="l67"></a> tmpc=tmpc1-&gt;next_hash;
<a name="l68"></a> </span><span class=cF1>while</span><span class=cF0> (tmpc!=tmpc1) </span><span class=cF7>{</span><span class=cF0>
<a name="l69"></a> </span><span class=cF1>if</span><span class=cF0> (tmpc-&gt;dv==dv &amp;&amp; tmpc-&gt;blk==blk)
<a name="l70"></a> </span><span class=cF1>return</span><span class=cF0> tmpc;
<a name="l71"></a> tmpc=tmpc-&gt;next_hash;
<a name="l72"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l73"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l74"></a>}
<a name="l75"></a>
<a name="l76"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>DskCacheAdd</span><span class=cF0>(</span><span class=cF9>CDrv</span><span class=cF0> *dv,</span><span class=cF1>U8</span><span class=cF0> *buf, </span><span class=cF9>I64</span><span class=cF0> blk, </span><span class=cF9>I64</span><span class=cF0> cnt)
<a name="l77"></a>{
<a name="l78"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc;
<a name="l79"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>blkdev</span><span class=cF0>.cache_base) </span><span class=cF7>{</span><span class=cF0>
<a name="l80"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DSK_CACHE</span><span class=cF0>],0</span><span class=cF7>)</span><span class=cF0>)
<a name="l81"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l82"></a> </span><span class=cF1>while</span><span class=cF0> (cnt--&gt;0) {
<a name="l83"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>tmpc=</span><span class=cFD>DskCacheFind</span><span class=cF0>(dv,blk)</span><span class=cF7>)</span><span class=cF0>)
<a name="l84"></a> tmpc=</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;next_lru;
<a name="l85"></a> </span><span class=cFD>DskCacheQueRem</span><span class=cF0>(tmpc);
<a name="l86"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(&amp;tmpc-&gt;body,buf,</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l87"></a> tmpc-&gt;dv=dv;
<a name="l88"></a> tmpc-&gt;blk=blk;
<a name="l89"></a> </span><span class=cFD>DskCacheQueIns</span><span class=cF0>(tmpc);
<a name="l90"></a> blk++;
<a name="l91"></a> buf+=</span><span class=cF3>BLK_SIZE</span><span class=cF0>;
<a name="l92"></a> }
<a name="l93"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DSK_CACHE</span><span class=cF0>],0);
<a name="l94"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l95"></a>}
<a name="l96"></a>
<a name="l97"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>DskCacheInvalidate2</span><span class=cF0>(</span><span class=cF9>CDrv</span><span class=cF0> *dv)
<a name="l98"></a>{
<a name="l99"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc,*tmpc1;
<a name="l100"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>blkdev</span><span class=cF0>.cache_base) </span><span class=cF7>{</span><span class=cF0>
<a name="l101"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DSK_CACHE</span><span class=cF0>],0</span><span class=cF7>)</span><span class=cF0>)
<a name="l102"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l103"></a> tmpc=</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;last_lru;
<a name="l104"></a> </span><span class=cF1>while</span><span class=cF0> (tmpc!=</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl) {
<a name="l105"></a> tmpc1=tmpc-&gt;last_lru;
<a name="l106"></a> </span><span class=cF1>if</span><span class=cF0> (tmpc-&gt;dv==dv) </span><span class=cF7>{</span><span class=cF0>
<a name="l107"></a> </span><span class=cFD>DskCacheQueRem</span><span class=cF0>(tmpc);
<a name="l108"></a> tmpc-&gt;dv=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l109"></a> tmpc-&gt;blk=0;
<a name="l110"></a> tmpc-&gt;next_hash=tmpc-&gt;last_hash=tmpc;
<a name="l111"></a> </span><span class=cF5>QueIns</span><span class=cF0>(tmpc,</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;last_lru);
<a name="l112"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l113"></a> tmpc=tmpc1;
<a name="l114"></a> }
<a name="l115"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DSK_CACHE</span><span class=cF0>],0);
<a name="l116"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l117"></a>}
<a name="l118"></a>
<a name="l119"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>RCache</span><span class=cF0>(</span><span class=cF9>CDrv</span><span class=cF0> *dv,</span><span class=cF1>U8</span><span class=cF0> **_buf, </span><span class=cF9>I64</span><span class=cF0> *_blk, </span><span class=cF9>I64</span><span class=cF0> *_cnt)
<a name="l120"></a>{
<a name="l121"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc;
<a name="l122"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>blkdev</span><span class=cF0>.cache_base) </span><span class=cF7>{</span><span class=cF0>
<a name="l123"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DSK_CACHE</span><span class=cF0>],0</span><span class=cF7>)</span><span class=cF0>)
<a name="l124"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l125"></a></span><span class=cF2>//fetch leading blks from cache</span><span class=cF0>
<a name="l126"></a> </span><span class=cF1>while</span><span class=cF0> (*_cnt&gt;0) {
<a name="l127"></a> </span><span class=cF1>if</span><span class=cF0> (tmpc=</span><span class=cFD>DskCacheFind</span><span class=cF7>(</span><span class=cF0>dv,*_blk</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l128"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(*_buf,&amp;tmpc-&gt;body,</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l129"></a> *_cnt-=1;
<a name="l130"></a> *_buf+=</span><span class=cF3>BLK_SIZE</span><span class=cF0>;
<a name="l131"></a> *_blk+=1;
<a name="l132"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0>
<a name="l133"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l134"></a> }
<a name="l135"></a></span><span class=cF2>//fetch trailing blks from cache</span><span class=cF0>
<a name="l136"></a> </span><span class=cF1>while</span><span class=cF0> (*_cnt&gt;0) {
<a name="l137"></a> </span><span class=cF1>if</span><span class=cF0> (tmpc=</span><span class=cFD>DskCacheFind</span><span class=cF7>(</span><span class=cF0>dv,*_blk+*_cnt-1</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l138"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(*_buf+</span><span class=cF7>(</span><span class=cF0>*_cnt-1</span><span class=cF7>)</span><span class=cF0>&lt;&lt;</span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>,&amp;tmpc-&gt;body,</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l139"></a> *_cnt-=1;
<a name="l140"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>else</span><span class=cF0>
<a name="l141"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l142"></a> }
<a name="l143"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;</span><span class=cFB>sys_semas</span><span class=cF0>[</span><span class=cF3>SEMA_DSK_CACHE</span><span class=cF0>],0);
<a name="l144"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l145"></a>}
</span></pre></body>
</html>