ZealOS/docs/Kernel/BlkDev/DiskCache.CC.html
2021-09-21 23:45:45 -04:00

197 lines
19 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V1.01">
<style type="text/css">
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DiskCacheInit</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, count;
<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_DISK_CACHE</span><span class=cF0>], </span><span class=cFE>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; </span><span class=cFE>0x2000</span><span class=cF0>)
<a name="l12"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l13"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l14"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_base = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l15"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_hash_table = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l16"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l17"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l18"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l19"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl = </span><span class=cF5>SysCAlloc</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="l20"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_base = </span><span class=cF5>SysMAlloc</span><span class=cF0>(size_in_U8s);
<a name="l21"></a> </span><span class=cF5>QueueInit</span><span class=cF0>(</span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl);
<a name="l22"></a>
<a name="l23"></a> count = </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="l24"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_size = count * </span><span class=cF3>BLK_SIZE</span><span class=cF0>;
<a name="l25"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; count; i++)
<a name="l26"></a> {
<a name="l27"></a> tmpc = </span><span class=cFB>blkdev</span><span class=cF0>.cache_base+i;
<a name="l28"></a> </span><span class=cF5>QueueInsert</span><span class=cF0>(tmpc, </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;last_lru);
<a name="l29"></a> tmpc-&gt;next_hash = tmpc-&gt;last_hash = tmpc;
<a name="l30"></a> tmpc-&gt;drive = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l31"></a> tmpc-&gt;blk = </span><span class=cFE>0</span><span class=cF0>;
<a name="l32"></a> }
<a name="l33"></a>
<a name="l34"></a> </span><span class=cFB>blkdev</span><span class=cF0>.cache_hash_table = </span><span class=cF5>SysMAlloc</span><span class=cF0>(</span><span class=cF3>DISK_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> * </span><span class=cFE>2</span><span class=cF0>);
<a name="l35"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; </span><span class=cF3>DISK_CACHE_HASH_SIZE</span><span class=cF0>; i++)
<a name="l36"></a> {
<a name="l37"></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> *) * </span><span class=cFE>2</span><span class=cF0> - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0>.next_hash);
<a name="l38"></a> tmpc-&gt;next_hash = tmpc-&gt;last_hash = tmpc;
<a name="l39"></a> }
<a name="l40"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l41"></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_DISK_CACHE</span><span class=cF0>], </span><span class=cFE>0</span><span class=cF0>);
<a name="l42"></a>}
<a name="l43"></a>
<a name="l44"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFD>DiskCacheHash</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> blk)
<a name="l45"></a>{
<a name="l46"></a> </span><span class=cF9>I64</span><span class=cF0> i = blk &amp; (</span><span class=cF3>DISK_CACHE_HASH_SIZE</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>);
<a name="l47"></a>
<a name="l48"></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; </span><span class=cFE>4</span><span class=cF0> - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0>.next_hash);
<a name="l49"></a>}
<a name="l50"></a>
<a name="l51"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>DiskCacheQueueRemove</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc)
<a name="l52"></a>{
<a name="l53"></a> </span><span class=cF5>QueueRemove</span><span class=cF0>(tmpc);
<a name="l54"></a> tmpc-&gt;next_hash-&gt;last_hash = tmpc-&gt;last_hash;
<a name="l55"></a> tmpc-&gt;last_hash-&gt;next_hash = tmpc-&gt;next_hash;
<a name="l56"></a>}
<a name="l57"></a>
<a name="l58"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>DiskCacheQueueIns</span><span class=cF0>(</span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc)
<a name="l59"></a>{
<a name="l60"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmp_n, *tmp_l;
<a name="l61"></a>
<a name="l62"></a> </span><span class=cF5>QueueInsert</span><span class=cF0>(tmpc, </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;last_lru);
<a name="l63"></a> tmp_l = </span><span class=cFD>DiskCacheHash</span><span class=cF0>(tmpc-&gt;blk);
<a name="l64"></a> tmp_n = tmp_l-&gt;next_hash;
<a name="l65"></a> tmpc-&gt;last_hash = tmp_l;
<a name="l66"></a> tmpc-&gt;next_hash = tmp_n;
<a name="l67"></a> tmp_l-&gt;next_hash = tmp_n-&gt;last_hash = tmpc;
<a name="l68"></a>}
<a name="l69"></a>
<a name="l70"></a></span><span class=cF9>CCacheBlk</span><span class=cF0> *</span><span class=cFD>DiskCacheFind</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF9>I64</span><span class=cF0> blk)
<a name="l71"></a>{
<a name="l72"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc, *tmpc1 = </span><span class=cFD>DiskCacheHash</span><span class=cF0>(blk);
<a name="l73"></a>
<a name="l74"></a> tmpc = tmpc1-&gt;next_hash;
<a name="l75"></a> </span><span class=cF1>while</span><span class=cF0> (tmpc != tmpc1)
<a name="l76"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l77"></a> </span><span class=cF1>if</span><span class=cF0> (tmpc-&gt;drive == drive &amp;&amp; tmpc-&gt;blk == blk)
<a name="l78"></a> </span><span class=cF1>return</span><span class=cF0> tmpc;
<a name="l79"></a> tmpc = tmpc-&gt;next_hash;
<a name="l80"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l81"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l82"></a>}
<a name="l83"></a>
<a name="l84"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>DiskCacheAdd</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </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> count)
<a name="l85"></a>{
<a name="l86"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc;
<a name="l87"></a>
<a name="l88"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>blkdev</span><span class=cF0>.cache_base)
<a name="l89"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l90"></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_DISK_CACHE</span><span class=cF0>], </span><span class=cFE>0</span><span class=cF7>)</span><span class=cF0>)
<a name="l91"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l92"></a> </span><span class=cF1>while</span><span class=cF0> (count-- &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l93"></a> {
<a name="l94"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>tmpc = </span><span class=cFD>DiskCacheFind</span><span class=cF0>(drive, blk)</span><span class=cF7>)</span><span class=cF0>)
<a name="l95"></a> tmpc = </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;next_lru;
<a name="l96"></a> </span><span class=cFD>DiskCacheQueueRemove</span><span class=cF0>(tmpc);
<a name="l97"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(&amp;tmpc-&gt;body, buf, </span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l98"></a> tmpc-&gt;drive = drive;
<a name="l99"></a> tmpc-&gt;blk = blk;
<a name="l100"></a> </span><span class=cFD>DiskCacheQueueIns</span><span class=cF0>(tmpc);
<a name="l101"></a> blk++;
<a name="l102"></a> buf += </span><span class=cF3>BLK_SIZE</span><span class=cF0>;
<a name="l103"></a> }
<a name="l104"></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_DISK_CACHE</span><span class=cF0>], </span><span class=cFE>0</span><span class=cF0>);
<a name="l105"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l106"></a>}
<a name="l107"></a>
<a name="l108"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>DiskCacheInvalidate2</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive)
<a name="l109"></a>{
<a name="l110"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc, *tmpc1;
<a name="l111"></a>
<a name="l112"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>blkdev</span><span class=cF0>.cache_base)
<a name="l113"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l114"></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_DISK_CACHE</span><span class=cF0>], </span><span class=cFE>0</span><span class=cF7>)</span><span class=cF0>)
<a name="l115"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l116"></a> tmpc = </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;last_lru;
<a name="l117"></a> </span><span class=cF1>while</span><span class=cF0> (tmpc != </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl)
<a name="l118"></a> {
<a name="l119"></a> tmpc1 = tmpc-&gt;last_lru;
<a name="l120"></a> </span><span class=cF1>if</span><span class=cF0> (tmpc-&gt;drive == drive)
<a name="l121"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l122"></a> </span><span class=cFD>DiskCacheQueueRemove</span><span class=cF0>(tmpc);
<a name="l123"></a> tmpc-&gt;drive = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l124"></a> tmpc-&gt;blk = </span><span class=cFE>0</span><span class=cF0>;
<a name="l125"></a> tmpc-&gt;next_hash = tmpc-&gt;last_hash = tmpc;
<a name="l126"></a> </span><span class=cF5>QueueInsert</span><span class=cF0>(tmpc, </span><span class=cFB>blkdev</span><span class=cF0>.cache_ctrl-&gt;last_lru);
<a name="l127"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l128"></a> tmpc = tmpc1;
<a name="l129"></a> }
<a name="l130"></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_DISK_CACHE</span><span class=cF0>], </span><span class=cFE>0</span><span class=cF0>);
<a name="l131"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l132"></a>}
<a name="l133"></a>
<a name="l134"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>RCache</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </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> *_count)
<a name="l135"></a>{
<a name="l136"></a> </span><span class=cF9>CCacheBlk</span><span class=cF0> *tmpc;
<a name="l137"></a>
<a name="l138"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFB>blkdev</span><span class=cF0>.cache_base)
<a name="l139"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l140"></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_DISK_CACHE</span><span class=cF0>], </span><span class=cFE>0</span><span class=cF7>)</span><span class=cF0>)
<a name="l141"></a> </span><span class=cF5>Yield</span><span class=cF0>;
<a name="l142"></a></span><span class=cF2>//fetch leading blks from cache</span><span class=cF0>
<a name="l143"></a> </span><span class=cF1>while</span><span class=cF0> (*_count &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l144"></a> {
<a name="l145"></a> </span><span class=cF1>if</span><span class=cF0> (tmpc = </span><span class=cFD>DiskCacheFind</span><span class=cF7>(</span><span class=cF0>drive, *_blk</span><span class=cF7>)</span><span class=cF0>)
<a name="l146"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l147"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(*_buf, &amp;tmpc-&gt;body, </span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l148"></a> *_count -= </span><span class=cFE>1</span><span class=cF0>;
<a name="l149"></a> *_buf += </span><span class=cF3>BLK_SIZE</span><span class=cF0>;
<a name="l150"></a> *_blk += </span><span class=cFE>1</span><span class=cF0>;
<a name="l151"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l152"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l153"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l154"></a> }
<a name="l155"></a></span><span class=cF2>//fetch trailing blks from cache</span><span class=cF0>
<a name="l156"></a> </span><span class=cF1>while</span><span class=cF0> (*_count &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l157"></a> {
<a name="l158"></a> </span><span class=cF1>if</span><span class=cF0> (tmpc = </span><span class=cFD>DiskCacheFind</span><span class=cF7>(</span><span class=cF0>drive, *_blk + *_count - </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0>)
<a name="l159"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l160"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(*_buf + </span><span class=cF7>(</span><span class=cF0>*_count - </span><span class=cFE>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="l161"></a> *_count -= </span><span class=cFE>1</span><span class=cF0>;
<a name="l162"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l163"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l164"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l165"></a> }
<a name="l166"></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_DISK_CACHE</span><span class=cF0>], </span><span class=cFE>0</span><span class=cF0>);
<a name="l167"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l168"></a>}
</span></pre></body>
</html>