ZealOS/docs/Kernel/Memory/MemPag.CC.html
2021-08-17 02:47:37 -04:00

232 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.00">
<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=cFD>SysBadFree</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *ptr)
<a name="l2"></a>{
<a name="l3"></a> </span><span class=cF5>Panic</span><span class=cF0>(</span><span class=cF6>&quot;Bad Free:&quot;</span><span class=cF0>, ptr);
<a name="l4"></a>}
<a name="l5"></a>
<a name="l6"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>SysBadMAlloc</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *ptr)
<a name="l7"></a>{
<a name="l8"></a> </span><span class=cF5>Panic</span><span class=cF0>(</span><span class=cF6>&quot;Bad MAlloc:&quot;</span><span class=cF0>, ptr);
<a name="l9"></a>}
<a name="l10"></a>
<a name="l11"></a></span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF5>MemPagAlloc</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> pags, </span><span class=cF9>CBlkPool</span><span class=cF0> *bp=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l12"></a>{</span><span class=cF2>/*Alloc pags from BlkPool. Don't link to task.</span><span class=cF0>
<a name="l13"></a></span><span class=cF2>(Linking to a task means they will be freed when the task dies.)</span><span class=cF0>
<a name="l14"></a></span><span class=cF2>It might give you more than you asked for.</span><span class=cF0>
<a name="l15"></a>
<a name="l16"></a></span><span class=cF2>Return: NULL if out of memory.</span><span class=cF0>
<a name="l17"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l18"></a> </span><span class=cF9>CMemBlk</span><span class=cF0> *res = </span><span class=cF3>NULL</span><span class=cF0>, *m;
<a name="l19"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l20"></a>
<a name="l21"></a> </span><span class=cF1>if</span><span class=cF0> (!bp)
<a name="l22"></a> bp = </span><span class=cFB>sys_code_bp</span><span class=cF0>;
<a name="l23"></a>
<a name="l24"></a> </span><span class=cF1>PUSHFD</span><span class=cF0>
<a name="l25"></a> </span><span class=cF1>CLI</span><span class=cF0>
<a name="l26"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;bp-&gt;locked_flags, </span><span class=cF3>BPlf_LOCKED</span><span class=cF7>)</span><span class=cF0>)
<a name="l27"></a> </span><span class=cF1>PAUSE</span><span class=cF0>
<a name="l28"></a> </span><span class=cF1>if</span><span class=cF0> (pags &lt; </span><span class=cF3>MEM_FREE_PAG_HASH_SIZE</span><span class=cF0>)
<a name="l29"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l30"></a> </span><span class=cF1>if</span><span class=cF0> (res = bp-&gt;free_pag_hash[pags])
<a name="l31"></a> {
<a name="l32"></a> bp-&gt;free_pag_hash[pags] = res-&gt;next;
<a name="l33"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l34"></a> }
<a name="l35"></a> i = </span><span class=cF5>Bsr</span><span class=cF0>(</span><span class=cF3>MEM_FREE_PAG_HASH_SIZE</span><span class=cF0>) + </span><span class=cFE>1</span><span class=cF0>;
<a name="l36"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l37"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l38"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l39"></a> </span><span class=cF2>//We'll now round-up to a power of two.</span><span class=cF0>
<a name="l40"></a> </span><span class=cF2>//There is some overhead on allocations and</span><span class=cF0>
<a name="l41"></a> </span><span class=cF2>//we wouldn't want to round to the next</span><span class=cF0>
<a name="l42"></a> </span><span class=cF2>//power of two if a power of two was requested.</span><span class=cF0>
<a name="l43"></a> </span><span class=cF2>//So we use a little more than a power of two.</span><span class=cF0>
<a name="l44"></a> pags -= </span><span class=cF3>MEM_EXTRA_HASH2_PAGS</span><span class=cF0>;
<a name="l45"></a> i = </span><span class=cF5>Bsr</span><span class=cF0>(pags) + </span><span class=cFE>1</span><span class=cF0>;
<a name="l46"></a> pags = </span><span class=cFE>1</span><span class=cF0> &lt;&lt; i + </span><span class=cF3>MEM_EXTRA_HASH2_PAGS</span><span class=cF0>;
<a name="l47"></a> </span><span class=cF1>if</span><span class=cF0> (res = bp-&gt;free_pag_hash2[i])
<a name="l48"></a> {
<a name="l49"></a> bp-&gt;free_pag_hash2[i] = res-&gt;next;
<a name="l50"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l51"></a> }
<a name="l52"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l53"></a> m = &amp;bp-&gt;mem_free_list;
<a name="l54"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l55"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l56"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>res = m-&gt;next</span><span class=cF7>)</span><span class=cF0>)
<a name="l57"></a> {
<a name="l58"></a> </span><span class=cF2>//We're probably out of luck, but lets search for a</span><span class=cF0>
<a name="l59"></a> </span><span class=cF2>//freed larger size block... and, screw-it, return the whole thing.</span><span class=cF0>
<a name="l60"></a> </span><span class=cF1>do</span><span class=cF0>
<a name="l61"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l62"></a> </span><span class=cF1>if</span><span class=cF0> (res = bp-&gt;free_pag_hash2[++i])
<a name="l63"></a> {
<a name="l64"></a> pags = </span><span class=cFE>1</span><span class=cF0> &lt;&lt; i + </span><span class=cF3>MEM_EXTRA_HASH2_PAGS</span><span class=cF0>;
<a name="l65"></a> bp-&gt;free_pag_hash2[i] = res-&gt;next;
<a name="l66"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l67"></a> }
<a name="l68"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l69"></a> </span><span class=cF1>while</span><span class=cF0> (i &lt; </span><span class=cFE>64</span><span class=cF0> - </span><span class=cF3>MEM_PAG_BITS</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>);
<a name="l70"></a>
<a name="l71"></a> pags = </span><span class=cFE>0</span><span class=cF0>;
<a name="l72"></a> res = </span><span class=cF3>NULL</span><span class=cF0>; </span><span class=cF2>//Out of memory</span><span class=cF0>
<a name="l73"></a> </span><span class=cF1>goto</span><span class=cF0> at_done2;
<a name="l74"></a> }
<a name="l75"></a> </span><span class=cF1>if</span><span class=cF0> (res-&gt;pags &lt; pags)
<a name="l76"></a> m = res;
<a name="l77"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l78"></a> {
<a name="l79"></a> </span><span class=cF1>if</span><span class=cF0> (res-&gt;pags == pags)
<a name="l80"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l81"></a> m-&gt;next = res-&gt;next;
<a name="l82"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l83"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l84"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l85"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l86"></a> res-&gt;pags -= pags;
<a name="l87"></a> res(</span><span class=cF1>U8</span><span class=cF0> *) += res-&gt;pags &lt;&lt; </span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l88"></a> res-&gt;pags = pags;
<a name="l89"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l90"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l91"></a> }
<a name="l92"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l93"></a>at_done:
<a name="l94"></a> bp-&gt;used_u8s += res-&gt;pags &lt;&lt; </span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l95"></a>at_done2:
<a name="l96"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;bp-&gt;locked_flags, </span><span class=cF3>BPlf_LOCKED</span><span class=cF0>);
<a name="l97"></a> </span><span class=cF1>POPFD</span><span class=cF0>
<a name="l98"></a>
<a name="l99"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l100"></a>}
<a name="l101"></a>
<a name="l102"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>MemPagFree</span><span class=cF0>(</span><span class=cF9>CMemBlk</span><span class=cF0> *m, </span><span class=cF9>CBlkPool</span><span class=cF0> *bp=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l103"></a>{</span><span class=cF2>//Return non-task pags to BlkPool.</span><span class=cF0>
<a name="l104"></a> </span><span class=cF9>I64</span><span class=cF0> i, pags;
<a name="l105"></a>
<a name="l106"></a> </span><span class=cF1>if</span><span class=cF0> (m)
<a name="l107"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l108"></a> </span><span class=cF1>if</span><span class=cF0> (!bp)
<a name="l109"></a> bp = </span><span class=cFB>sys_code_bp</span><span class=cF0>;
<a name="l110"></a>
<a name="l111"></a> </span><span class=cF1>PUSHFD</span><span class=cF0>
<a name="l112"></a> </span><span class=cF1>CLI</span><span class=cF0>
<a name="l113"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF5>LBts</span><span class=cF7>(</span><span class=cF0>&amp;bp-&gt;locked_flags, </span><span class=cF3>BPlf_LOCKED</span><span class=cF7>)</span><span class=cF0>)
<a name="l114"></a> </span><span class=cF1>PAUSE</span><span class=cF0>
<a name="l115"></a> pags = m-&gt;pags;
<a name="l116"></a> m-&gt;mb_signature = </span><span class=cF3>MBS_UNUSED_SIGNATURE_VAL</span><span class=cF0>;
<a name="l117"></a> bp-&gt;used_u8s -= pags &lt;&lt; </span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l118"></a> </span><span class=cF1>if</span><span class=cF0> (pags &lt; </span><span class=cF3>MEM_FREE_PAG_HASH_SIZE</span><span class=cF0>)
<a name="l119"></a> {
<a name="l120"></a> m-&gt;next = bp-&gt;free_pag_hash[pags];
<a name="l121"></a> bp-&gt;free_pag_hash[pags] = m;
<a name="l122"></a> }
<a name="l123"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l124"></a> {
<a name="l125"></a></span><span class=cF2>//We'll now round-up to a power of two.</span><span class=cF0>
<a name="l126"></a> </span><span class=cF2>//There is some overhead on allocations and</span><span class=cF0>
<a name="l127"></a> </span><span class=cF2>//we wouldn't want to round to the next</span><span class=cF0>
<a name="l128"></a> </span><span class=cF2>//power of two if a power of two was requested.</span><span class=cF0>
<a name="l129"></a> </span><span class=cF2>//So we use a little more than a power of two.</span><span class=cF0>
<a name="l130"></a> pags -= </span><span class=cF3>MEM_EXTRA_HASH2_PAGS</span><span class=cF0>;
<a name="l131"></a> i = </span><span class=cF5>Bsr</span><span class=cF0>(pags);
<a name="l132"></a> m-&gt;next = bp-&gt;free_pag_hash2[i];
<a name="l133"></a> bp-&gt;free_pag_hash2[i] = m;
<a name="l134"></a> }
<a name="l135"></a> </span><span class=cF5>LBtr</span><span class=cF0>(&amp;bp-&gt;locked_flags, </span><span class=cF3>BPlf_LOCKED</span><span class=cF0>);
<a name="l136"></a> </span><span class=cF1>POPFD</span><span class=cF0>
<a name="l137"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l138"></a>}
<a name="l139"></a>
<a name="l140"></a></span><span class=cF9>CMemBlk</span><span class=cF0> *</span><span class=cFD>MemPagTaskAlloc</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> pags, </span><span class=cF9>CHeapCtrl</span><span class=cF0> *hc)
<a name="l141"></a>{</span><span class=cF2>/*hc must be locked. Don't preempt this routine.</span><span class=cF0>
<a name="l142"></a></span><span class=cF2>Currently, this is only called from </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/Memory/MAllocFree.CC.html#l391"><span class=cF4>MAlloc</span></a><span class=cF2>().</span><span class=cF0>
<a name="l143"></a></span><span class=cF2>Return: NULL if out of memory.</span><span class=cF0>
<a name="l144"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l145"></a> </span><span class=cF9>CMemBlk</span><span class=cF0> *res;
<a name="l146"></a> </span><span class=cF9>I64</span><span class=cF0> threshold, count, size;
<a name="l147"></a> </span><span class=cF9>CMemUnused</span><span class=cF0> *uum, **_uum, **_ptr;
<a name="l148"></a>
<a name="l149"></a> </span><span class=cF1>if</span><span class=cF0> (res = </span><span class=cF5>MemPagAlloc</span><span class=cF7>(</span><span class=cF0>pags, hc-&gt;bp</span><span class=cF7>)</span><span class=cF0>)
<a name="l150"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l151"></a> </span><span class=cF5>QueueInsert</span><span class=cF0>(res, hc-&gt;last_mem_blk);
<a name="l152"></a> res-&gt;mb_signature = </span><span class=cF3>MBS_USED_SIGNATURE_VAL</span><span class=cF0>;
<a name="l153"></a> hc-&gt;alloced_u8s += res-&gt;pags &lt;&lt; </span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l154"></a>
<a name="l155"></a> </span><span class=cF2>//Tidy-up free list (Move into heap hash)</span><span class=cF0>
<a name="l156"></a> </span><span class=cF2>//because if free list gets long, delay causes crash.</span><span class=cF0>
<a name="l157"></a> threshold = </span><span class=cF3>MEM_HEAP_HASH_SIZE</span><span class=cF0> &gt;&gt; </span><span class=cFE>4</span><span class=cF0>;
<a name="l158"></a>#</span><span class=cF1>assert</span><span class=cF0> </span><span class=cF3>MEM_HEAP_HASH_SIZE</span><span class=cF0> &gt;&gt; </span><span class=cFE>4</span><span class=cF0> &gt;= </span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *)
<a name="l159"></a> </span><span class=cF1>do</span><span class=cF0>
<a name="l160"></a> {
<a name="l161"></a> count = </span><span class=cFE>0</span><span class=cF0>;
<a name="l162"></a> _uum = &amp;hc-&gt;malloc_free_list;
<a name="l163"></a> </span><span class=cF1>while</span><span class=cF0> (uum = *_uum)
<a name="l164"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l165"></a>#</span><span class=cF1>assert</span><span class=cF0> !</span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CMemUnused</span><span class=cF0>.next)
<a name="l166"></a> size = uum-&gt;size;
<a name="l167"></a> </span><span class=cF1>if</span><span class=cF0> (size &lt; threshold)
<a name="l168"></a> {
<a name="l169"></a> *_uum = uum-&gt;next;
<a name="l170"></a> _ptr = (&amp;hc-&gt;heap_hash)(</span><span class=cF1>U8</span><span class=cF0> *) + size;
<a name="l171"></a> uum-&gt;next = *_ptr;
<a name="l172"></a> *_ptr = uum;
<a name="l173"></a> }
<a name="l174"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l175"></a> {
<a name="l176"></a> count++;
<a name="l177"></a> _uum = uum;
<a name="l178"></a> }
<a name="l179"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l180"></a> threshold &lt;&lt;= </span><span class=cFE>1</span><span class=cF0>;
<a name="l181"></a> }
<a name="l182"></a> </span><span class=cF1>while</span><span class=cF0> (count &gt; </span><span class=cFE>8</span><span class=cF0> &amp;&amp; threshold &lt;= </span><span class=cF3>MEM_HEAP_HASH_SIZE</span><span class=cF0>);
<a name="l183"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l184"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l185"></a>}
<a name="l186"></a>
<a name="l187"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>MemPagTaskFree</span><span class=cF0>(</span><span class=cF9>CMemBlk</span><span class=cF0> *m, </span><span class=cF9>CHeapCtrl</span><span class=cF0> *hc)
<a name="l188"></a>{</span><span class=cF2>//hc must be locked</span><span class=cF0>
<a name="l189"></a> </span><span class=cF1>if</span><span class=cF0> (m)
<a name="l190"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l191"></a> </span><span class=cF1>PUSHFD</span><span class=cF0>
<a name="l192"></a> </span><span class=cF1>CLI</span><span class=cF0>
<a name="l193"></a> </span><span class=cF1>if</span><span class=cF0> (m-&gt;mb_signature != </span><span class=cF3>MBS_USED_SIGNATURE_VAL</span><span class=cF0>)
<a name="l194"></a> </span><span class=cFD>SysBadFree</span><span class=cF0>(m);
<a name="l195"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l196"></a> {
<a name="l197"></a> </span><span class=cF5>QueueRemove</span><span class=cF0>(m);
<a name="l198"></a> hc-&gt;alloced_u8s -= m-&gt;pags &lt;&lt; </span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l199"></a> </span><span class=cF5>MemPagFree</span><span class=cF0>(m, hc-&gt;bp);
<a name="l200"></a> }
<a name="l201"></a> </span><span class=cF1>POPFD</span><span class=cF0>
<a name="l202"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l203"></a>}
</span></pre></body>
</html>