templeos-info/public/Wb/Kernel/Mem/MemPag.HC.HTML

199 lines
17 KiB
HTML
Executable File

<!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=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> </span><span class=cF1>if</span><span class=cF0> (!bp) bp=</span><span class=cFB>sys_code_bp</span><span class=cF0>;
<a name="l21"></a> </span><span class=cF1>PUSHFD</span><span class=cF0>
<a name="l22"></a> </span><span class=cF1>CLI</span><span class=cF0>
<a name="l23"></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="l24"></a> </span><span class=cF1>PAUSE</span><span class=cF0>
<a name="l25"></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>) </span><span class=cF7>{</span><span class=cF0>
<a name="l26"></a> </span><span class=cF1>if</span><span class=cF0> (res=bp-&gt;free_pag_hash[pags]) {
<a name="l27"></a> bp-&gt;free_pag_hash[pags]=res-&gt;next;
<a name="l28"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l29"></a> }
<a name="l30"></a> i=</span><span class=cF5>Bsr</span><span class=cF0>(</span><span class=cF3>MEM_FREE_PAG_HASH_SIZE</span><span class=cF0>)+1;
<a name="l31"></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="l32"></a></span><span class=cF2>//We'll now round-up to a power of two.</span><span class=cF0>
<a name="l33"></a> </span><span class=cF2>//There is some overhead on allocations and</span><span class=cF0>
<a name="l34"></a> </span><span class=cF2>//we wouldn't want to round to the next</span><span class=cF0>
<a name="l35"></a> </span><span class=cF2>//power of two if a power of two was requested.</span><span class=cF0>
<a name="l36"></a> </span><span class=cF2>//So we use a little more than a power of two.</span><span class=cF0>
<a name="l37"></a> pags-=</span><span class=cF3>MEM_EXTRA_HASH2_PAGS</span><span class=cF0>;
<a name="l38"></a> i=</span><span class=cF5>Bsr</span><span class=cF0>(pags)+1;
<a name="l39"></a> pags=1&lt;&lt;i+</span><span class=cF3>MEM_EXTRA_HASH2_PAGS</span><span class=cF0>;
<a name="l40"></a> </span><span class=cF1>if</span><span class=cF0> (res=bp-&gt;free_pag_hash2[i]) {
<a name="l41"></a> bp-&gt;free_pag_hash2[i]=res-&gt;next;
<a name="l42"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l43"></a> }
<a name="l44"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l45"></a> m=&amp;bp-&gt;mem_free_lst;
<a name="l46"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l47"></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="l48"></a></span><span class=cF2>//We're probably out of luck, but lets search for a</span><span class=cF0>
<a name="l49"></a> </span><span class=cF2>//freed larger size block... and, screw-it, return the whole thing.</span><span class=cF0>
<a name="l50"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l51"></a> </span><span class=cF1>if</span><span class=cF0> (res=bp-&gt;free_pag_hash2[++i]) {
<a name="l52"></a> pags=1&lt;&lt;i+</span><span class=cF3>MEM_EXTRA_HASH2_PAGS</span><span class=cF0>;
<a name="l53"></a> bp-&gt;free_pag_hash2[i]=res-&gt;next;
<a name="l54"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l55"></a> }
<a name="l56"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>while</span><span class=cF0> (i&lt;64-</span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>-1);
<a name="l57"></a> pags=0;
<a name="l58"></a> res=</span><span class=cF3>NULL</span><span class=cF0>; </span><span class=cF2>//Out of memory</span><span class=cF0>
<a name="l59"></a> </span><span class=cF1>goto</span><span class=cF0> at_done2;
<a name="l60"></a> }
<a name="l61"></a> </span><span class=cF1>if</span><span class=cF0> (res-&gt;pags&lt;pags)
<a name="l62"></a> m=res;
<a name="l63"></a> </span><span class=cF1>else</span><span class=cF0> {
<a name="l64"></a> </span><span class=cF1>if</span><span class=cF0> (res-&gt;pags==pags) </span><span class=cF7>{</span><span class=cF0>
<a name="l65"></a> m-&gt;next=res-&gt;next;
<a name="l66"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l67"></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="l68"></a> res-&gt;pags-=pags;
<a name="l69"></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="l70"></a> res-&gt;pags=pags;
<a name="l71"></a> </span><span class=cF1>goto</span><span class=cF0> at_done;
<a name="l72"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l73"></a> }
<a name="l74"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l75"></a>at_done:
<a name="l76"></a> bp-&gt;used_u8s+=res-&gt;pags&lt;&lt;</span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l77"></a>at_done2:
<a name="l78"></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="l79"></a> </span><span class=cF1>POPFD</span><span class=cF0>
<a name="l80"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l81"></a>}
<a name="l82"></a>
<a name="l83"></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="l84"></a>{</span><span class=cF2>//Return non-task pags to BlkPool.</span><span class=cF0>
<a name="l85"></a> </span><span class=cF9>I64</span><span class=cF0> i,pags;
<a name="l86"></a> </span><span class=cF1>if</span><span class=cF0> (m) </span><span class=cF7>{</span><span class=cF0>
<a name="l87"></a> </span><span class=cF1>if</span><span class=cF0> (!bp) bp=</span><span class=cFB>sys_code_bp</span><span class=cF0>;
<a name="l88"></a> </span><span class=cF1>PUSHFD</span><span class=cF0>
<a name="l89"></a> </span><span class=cF1>CLI</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;bp-&gt;locked_flags,</span><span class=cF3>BPlf_LOCKED</span><span class=cF7>)</span><span class=cF0>)
<a name="l91"></a> </span><span class=cF1>PAUSE</span><span class=cF0>
<a name="l92"></a> pags=m-&gt;pags;
<a name="l93"></a> m-&gt;mb_signature=</span><span class=cF3>MBS_UNUSED_SIGNATURE_VAL</span><span class=cF0>;
<a name="l94"></a> bp-&gt;used_u8s-=pags&lt;&lt;</span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l95"></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="l96"></a> m-&gt;next=bp-&gt;free_pag_hash[pags];
<a name="l97"></a> bp-&gt;free_pag_hash[pags]=m;
<a name="l98"></a> } </span><span class=cF1>else</span><span class=cF0> {
<a name="l99"></a></span><span class=cF2>//We'll now round-up to a power of two.</span><span class=cF0>
<a name="l100"></a> </span><span class=cF2>//There is some overhead on allocations and</span><span class=cF0>
<a name="l101"></a> </span><span class=cF2>//we wouldn't want to round to the next</span><span class=cF0>
<a name="l102"></a> </span><span class=cF2>//power of two if a power of two was requested.</span><span class=cF0>
<a name="l103"></a> </span><span class=cF2>//So we use a little more than a power of two.</span><span class=cF0>
<a name="l104"></a> pags-=</span><span class=cF3>MEM_EXTRA_HASH2_PAGS</span><span class=cF0>;
<a name="l105"></a> i=</span><span class=cF5>Bsr</span><span class=cF0>(pags);
<a name="l106"></a> m-&gt;next=bp-&gt;free_pag_hash2[i];
<a name="l107"></a> bp-&gt;free_pag_hash2[i]=m;
<a name="l108"></a> }
<a name="l109"></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="l110"></a> </span><span class=cF1>POPFD</span><span class=cF0>
<a name="l111"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l112"></a>}
<a name="l113"></a>
<a name="l114"></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="l115"></a>{</span><span class=cF2>/*hc must be locked. Don't preempt this routine.</span><span class=cF0>
<a name="l116"></a></span><span class=cF2>Currently, this is only called from </span><a href="/Wb/Kernel/Mem/MAllocFree.HC.HTML#l387"><span class=cF4>MAlloc</span></a><span class=cF2>().</span><span class=cF0>
<a name="l117"></a></span><span class=cF2>Return: NULL if out of memory.</span><span class=cF0>
<a name="l118"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l119"></a> </span><span class=cF9>CMemBlk</span><span class=cF0> *res;
<a name="l120"></a> </span><span class=cF9>I64</span><span class=cF0> threshold,cnt,size;
<a name="l121"></a> </span><span class=cF9>CMemUnused</span><span class=cF0> *uum,**_uum,**_ptr;
<a name="l122"></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>) </span><span class=cF7>{</span><span class=cF0>
<a name="l123"></a> </span><span class=cF5>QueIns</span><span class=cF0>(res,hc-&gt;last_mem_blk);
<a name="l124"></a> res-&gt;mb_signature=</span><span class=cF3>MBS_USED_SIGNATURE_VAL</span><span class=cF0>;
<a name="l125"></a> hc-&gt;alloced_u8s+=res-&gt;pags&lt;&lt;</span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l126"></a>
<a name="l127"></a> </span><span class=cF2>//Tidy-up free lst (Move into heap hash)</span><span class=cF0>
<a name="l128"></a> </span><span class=cF2>//because if free lst gets long, delay causes crash.</span><span class=cF0>
<a name="l129"></a> threshold=</span><span class=cF3>MEM_HEAP_HASH_SIZE</span><span class=cF0>&gt;&gt;4;
<a name="l130"></a>#</span><span class=cF1>assert</span><span class=cF0> </span><span class=cF3>MEM_HEAP_HASH_SIZE</span><span class=cF0>&gt;&gt;4&gt;=</span><span class=cF1>sizeof</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *)
<a name="l131"></a> </span><span class=cF1>do</span><span class=cF0> {
<a name="l132"></a> cnt=0;
<a name="l133"></a> _uum=&amp;hc-&gt;malloc_free_lst;
<a name="l134"></a> </span><span class=cF1>while</span><span class=cF0> (uum=*_uum) </span><span class=cF7>{</span><span class=cF0>
<a name="l135"></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="l136"></a> size=uum-&gt;size;
<a name="l137"></a> </span><span class=cF1>if</span><span class=cF0> (size&lt;threshold) {
<a name="l138"></a> *_uum=uum-&gt;next;
<a name="l139"></a> _ptr=(&amp;hc-&gt;heap_hash)(</span><span class=cF1>U8</span><span class=cF0> *)+size;
<a name="l140"></a> uum-&gt;next=*_ptr;
<a name="l141"></a> *_ptr=uum;
<a name="l142"></a> } </span><span class=cF1>else</span><span class=cF0> {
<a name="l143"></a> cnt++;
<a name="l144"></a> _uum=uum;
<a name="l145"></a> }
<a name="l146"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l147"></a> threshold&lt;&lt;=1;
<a name="l148"></a> } </span><span class=cF1>while</span><span class=cF0> (cnt&gt;8 &amp;&amp; threshold&lt;=</span><span class=cF3>MEM_HEAP_HASH_SIZE</span><span class=cF0>);
<a name="l149"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l150"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l151"></a>}
<a name="l152"></a>
<a name="l153"></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="l154"></a>{</span><span class=cF2>//hc must be locked</span><span class=cF0>
<a name="l155"></a> </span><span class=cF1>if</span><span class=cF0> (m) </span><span class=cF7>{</span><span class=cF0>
<a name="l156"></a> </span><span class=cF1>PUSHFD</span><span class=cF0>
<a name="l157"></a> </span><span class=cF1>CLI</span><span class=cF0>
<a name="l158"></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="l159"></a> </span><span class=cFD>SysBadFree</span><span class=cF0>(m);
<a name="l160"></a> </span><span class=cF1>else</span><span class=cF0> {
<a name="l161"></a> </span><span class=cF5>QueRem</span><span class=cF0>(m);
<a name="l162"></a> hc-&gt;alloced_u8s-=m-&gt;pags&lt;&lt;</span><span class=cF3>MEM_PAG_BITS</span><span class=cF0>;
<a name="l163"></a> </span><span class=cF5>MemPagFree</span><span class=cF0>(m,hc-&gt;bp);
<a name="l164"></a> }
<a name="l165"></a> </span><span class=cF1>POPFD</span><span class=cF0>
<a name="l166"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l167"></a>}
</span></pre></body>
</html>