mem_core.c

来自「澳洲人写的Cortex,包括uC_IP协议栈」· C语言 代码 · 共 442 行 · 第 1/2 页

C
442
字号
<HTML><HEAD><TITLE>/home/asysweb/public_html/cortex/kernel/src/mem_core.c</TITLE></HEAD><BODY><pre><font color="#6920ac">/*************************************************************************/</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/*     Copyright (c) 1997-1999 Australian Real Time Embedded Systems     */</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/* PROPRIETARY RIGHTS of Australian Real Time Embedded Systems           */</font><font color="#6920ac">/* are involved in the subject matter of this material. All reproduction,*/</font><font color="#6920ac">/* manufacturing, use, and sales rights pertaining to this subject matter*/</font><font color="#6920ac">/* are governed by the license agreement. The recipient of this software */</font><font color="#6920ac">/* implicitly accepts the terms of the license.                          */</font><font color="#6920ac">/*                                                                       */</font><font color="#6920ac">/*************************************************************************/</font><b><font color='DarkGreen'>#include</font></b> <a href="mem_defs.h.FIND-INC"><font color="blue">"mem_defs.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="sys_defs.h.FIND-INC"><font color="blue">"sys_defs.h"</font></a><b><font color='DarkGreen'>#include</font></b> <a href="thr_priv.h.FIND-INC"><font color="blue">"thr_priv.h"</font></a><font size="+1"><i>static</i> <i>dmem_AllocFunc_t</i> * <b><font color="azure1"><a name="dmem_GetAllocFunc">dmem_GetAllocFunc</a></font></b>(<i>segm_Table_t</i> *pTable_a,                                            <i>crtx_Bool_t</i> Locate_a){</font>        <b>return</b>((((pTable_a-&gt;pAlloc!=<a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>) &amp;&amp; ( !Locate_a )) ||            (pTable_a-&gt;pAllocSt == <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)) ?           pTable_a-&gt;pAlloc : pTable_a-&gt;pAllocSt);}<font color="#6920ac">/* locate TRUE is to allocate memory for all */</font><font color="#6920ac">/* FALSE for stack */</font><font size="+1"><i>static</i> <i>crtx_Void_t</i> * <b><font color="azure1"><a name="dmem_DoAlloc">dmem_DoAlloc</a></font></b>(<i>crtx_SegNum_t</i>   SegNum_a,<font color="#6920ac">/* segment number */</font>                                  <i>dmem_Size_t</i>     Size_a,<font color="#6920ac">/* size of memory */</font>                                  <i>crtx_Uint16_t</i>   Algn_a,  <font color="#6920ac">/* alignment */</font>                                  <i>crtx_Bool_t</i>     Locate_a){</font>    <i>crtx_Void_t</i>     *Result = <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>;    <i>crtx_Int16_t</i>    nlnk;    <i>crtx_Int16_t</i>    nseg;    <i>segm_Table_t</i>    *pTable;    <i>segm_Table_t</i>    *pLinkTable;        <b>if</b> (!(((SegNum_a &lt; <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a> + <a href="SEGM_MAX_DYN_NUMBER.FIND-DEF">SEGM_MAX_DYN_NUMBER</a>) &amp;&amp;           (SegNum_a &gt;=0 )) || (SegNum_a == <a href="DMEM_STACK_SEGMENT.FIND-DEF">DMEM_STACK_SEGMENT</a>)))    {        <a href="CRTX_EXCEPTION.FIND-DEF">CRTX_EXCEPTION</a> ErrWrongSegmNum_Exc;    }        <b>if</b> (SegNum_a == <a href="DMEM_STACK_SEGMENT.FIND-DEF">DMEM_STACK_SEGMENT</a>) {        <font color="#6920ac">/* segment with number DMEM_STACK_SEGMENT belongs task */</font>        pTable = <a href="thrd_Segment.FIND-FUNC">thrd_Segment</a>();        <b>if</b> (pTable == <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)        {            <a href="CRTX_EXCEPTION.FIND-DEF">CRTX_EXCEPTION</a> ErrNoTaskSegm_Exc;        }               Result = (*<a href="#dmem_GetAllocFunc">dmem_GetAllocFunc</a>(pTable,Locate_a))(pTable-&gt;ad,                                                       pTable-&gt;pcontext,                                                       Size_a,Algn_a);    }    <b>else</b>    {        pTable = ( (SegNum_a &lt; <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a>) ?                   &amp;segm_Table_g[SegNum_a] :                   &amp;segm_DynTable_g[SegNum_a - <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a>]);        <b>if</b> (pTable-&gt;ad != <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)        {            Result = (*<a href="#dmem_GetAllocFunc">dmem_GetAllocFunc</a>(pTable, Locate_a))(                pTable-&gt;ad,                pTable-&gt;pcontext, Size_a, Algn_a);        }        <font color="#6920ac">/* if positive result return */</font>                <b>if</b> ( Result == <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a> )        {            <font color="#6920ac">/* searching in linked segments */</font>                        <b>for</b> (nlnk = 0; nlnk &lt; pTable-&gt;numnext; nlnk++)            {                nseg = pTable-&gt;pnexts[nlnk];                pLinkTable = ( (nseg &lt; <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a>) ?                               &amp;segm_Table_g[nseg] :                               &amp;segm_DynTable_g[nseg - <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a>]);                <b>if</b> (pLinkTable-&gt;ad == <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>) <b>continue</b>;                                Result = (*<a href="#dmem_GetAllocFunc">dmem_GetAllocFunc</a>(pLinkTable,Locate_a))(                    pLinkTable-&gt;ad,                    pLinkTable-&gt;pcontext,Size_a,Algn_a);                        <b>if</b> (Result != <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)                {                    <b>break</b>;                }            }        }    }    <b>return</b>(Result);<font color="blue">ErrWrongSegmNum_Exc:</font><font color="blue">ErrNoTaskSegm_Exc:</font>    <b>return</b>(<a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>);}<font size="+1"><i>static</i> <i>crtx_Void_t</i> * <b><font color="azure1"><a name="dmem_ReturnNull">dmem_ReturnNull</a></font></b>(<i>crtx_Void_t</i>){</font>    <font color="#6920ac">/* this is only to can stop in debugger on wrong answer */</font>    <b>return</b>(<a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>);}<font size="+1"><i>crtx_Void_t</i> * <b><font color="azure1"><a name="dmem_Alloc">dmem_Alloc</a></font></b>(<i>crtx_SegNum_t</i>   SegNum_a,<font color="#6920ac">/* segment number */</font>                         <i>dmem_Size_t</i>        Size_a,<font color="#6920ac">/* size of memory */</font>                         <i>crtx_Uint16_t</i>  Algn_a)   <font color="#6920ac">/* alignment */</font>{</font>    <i>crtx_Void_t</i>     *Result;    Result = <a href="#dmem_DoAlloc">dmem_DoAlloc</a>(SegNum_a,Size_a,Algn_a,<a href="CRTX_FALSE.FIND-DEF">CRTX_FALSE</a>);    <b>if</b> (Result == <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)    {        <font color="#6920ac">/* this is only to can stop in debugger on wrong answer */</font>        <a href="CRTX_EXCEPTION.FIND-DEF">CRTX_EXCEPTION</a> ErrCouldnotAlloc_Exc;    }    <b>return</b>(Result);<font color="blue">ErrCouldnotAlloc_Exc:</font>    <b>return</b>(<a href="#dmem_ReturnNull">dmem_ReturnNull</a>());}<font size="+1"><i>crtx_Void_t</i> * <b><font color="azure1"><a name="dmem_AllocStack">dmem_AllocStack</a></font></b>(<i>crtx_SegNum_t</i>   SegNum_a, <font color="#6920ac">/*segment num */</font>                              <i>dmem_Size_t</i>       Size_a,<font color="#6920ac">/*mem size */</font>                              <i>crtx_Uint16_t</i> Algn_a)   <font color="#6920ac">/* alignment */</font>{</font>    <i>crtx_Void_t</i>     *Result;    Result = <a href="#dmem_DoAlloc">dmem_DoAlloc</a>(SegNum_a,Size_a,Algn_a,<a href="CRTX_TRUE.FIND-DEF">CRTX_TRUE</a>);    <b>if</b> (Result == <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)    {        <font color="#6920ac">/* this is only to can stop in debugger on wrong answer */</font>        <a href="CRTX_EXCEPTION.FIND-DEF">CRTX_EXCEPTION</a> ErrCouldnotAlloc_Exc;    }    <b>return</b>(Result);<font color="blue">ErrCouldnotAlloc_Exc:</font>    <b>return</b>(<a href="#dmem_ReturnNull">dmem_ReturnNull</a>());}<font size="+1"><i>static</i> <i>segm_Table_t</i> * <b><font color="azure1"><a name="dmem_deftable">dmem_deftable</a></font></b>(<i>crtx_SegNum_t</i> SegNum_a,                                    <i>crtx_Void_t</i> *Addr_a){</font>    <i>segm_Table_t</i>    *Result = <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>;    <i>segm_Table_t</i>    *pLinkTable;    <i>segm_Table_t</i>    *pTable;    <i>crtx_Int16_t</i>    nlnk;    <i>crtx_Int16_t</i>    nseg;    <b>if</b> (!(((SegNum_a &lt; <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a> + <a href="SEGM_MAX_DYN_NUMBER.FIND-DEF">SEGM_MAX_DYN_NUMBER</a>) &amp;&amp;           (SegNum_a &gt;=0 ))||          (SegNum_a == <a href="DMEM_STACK_SEGMENT.FIND-DEF">DMEM_STACK_SEGMENT</a>)))    {        <a href="CRTX_EXCEPTION.FIND-DEF">CRTX_EXCEPTION</a> ErrWrongSegmNum_Exc;    }    <b>if</b> (SegNum_a == <a href="DMEM_STACK_SEGMENT.FIND-DEF">DMEM_STACK_SEGMENT</a>)    {        Result = <a href="thrd_Segment.FIND-FUNC">thrd_Segment</a>();    }    <b>else</b>    {        <font color="#6920ac">/* if i==0 here free works with task segment */</font>        pTable = ( (SegNum_a &lt; <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a>) ?                   &amp;segm_Table_g[SegNum_a] :                   &amp;segm_DynTable_g[SegNum_a - <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a>]);        <b>if</b> (pTable-&gt;ad != <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)        {            <b>if</b> (((<i>crtx_Char_t</i> *)Addr_a &gt;= (<i>crtx_Char_t</i> *)pTable-&gt;ad) &amp;&amp;                ((<i>crtx_Char_t</i> *)Addr_a &lt;                 (&amp;(((<i>crtx_Char_t</i> *)pTable-&gt;ad)[pTable-&gt;sz]))))            {                Result = pTable;            }        }        <b>if</b> (Result == <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)        {            <font color="#6920ac">/* cycle to find appropiate segment in links */</font>            <b>for</b> (nlnk = 0; nlnk &lt; pTable-&gt;numnext; nlnk++)            {                nseg = pTable-&gt;pnexts[nlnk];                <b>if</b> (nseg &lt; 0 || nseg &gt;= <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a> + <a href="SEGM_MAX_DYN_NUMBER.FIND-DEF">SEGM_MAX_DYN_NUMBER</a>)                {                    <a href="CRTX_EXCEPTION.FIND-DEF">CRTX_EXCEPTION</a> ErrWrongLinkNum_Exc;                }                pLinkTable = ( (nseg &lt; <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a>) ?                               &amp;segm_Table_g[nseg] :                               &amp;segm_DynTable_g[nseg - <a href="SEGM_MAX_NUMBER.FIND-DEF">SEGM_MAX_NUMBER</a>]);                                <b>if</b> (pLinkTable-&gt;ad == <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>) <b>continue</b>;                <b>if</b> (((<i>crtx_Char_t</i> *)Addr_a &gt;= (<i>crtx_Char_t</i> *)pLinkTable-&gt;ad) &amp;&amp;                    ((<i>crtx_Char_t</i> *)Addr_a &lt;                     (&amp;(((<i>crtx_Char_t</i> *)pLinkTable-&gt;ad)[pLinkTable-&gt;sz]))))                {                    Result = pLinkTable;                    <b>break</b>;                }            }        }    }    <b>return</b>(Result);<font color="blue">ErrWrongSegmNum_Exc:</font><font color="blue">ErrWrongLinkNum_Exc:</font>    <b>return</b>(<a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>);}<font size="+1"><i>crtx_Void_t</i> * <b><font color="azure1"><a name="dmem_Calloc">dmem_Calloc</a></font></b>(<i>crtx_SegNum_t</i> SegNum_a, <i>dmem_Size_t</i> Nelem_a,                          <i>dmem_Size_t</i> Size_a,<i>crtx_Uint_t</i> Algn_a){</font>    <i>crtx_Void_t</i> *Result;    Result = <a href="#dmem_Alloc">dmem_Alloc</a>(SegNum_a,(Nelem_a*Size_a),Algn_a);    <b>if</b> (Result != <a href="DMEM_NULL.FIND-DEF">DMEM_NULL</a>)    {        <a href="memset.FIND-FUNC">memset</a>(Result,0,(Nelem_a*Size_a));    }    <b>return</b>(Result);}<font size="+1">

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?