📄 user_space_memory_allocation.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head> <title></title> <link rel="stylesheet" media="screen" type="text/css" href="./style.css" /> <link rel="stylesheet" media="screen" type="text/css" href="./design.css" /> <link rel="stylesheet" media="print" type="text/css" href="./print.css" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><a href=start.html>start</a></br><div class="toc"><div class="tocheader toctoggle" id="toc__header">Table of Contents</div><div id="toc__inside"><ul class="toc"><li class="clear"><ul class="toc"><li class="level2"><div class="li"><span class="li"><a href="#user_space_memory_allocation" class="toc">User Space Memory Allocation</a></span></div><ul class="toc"><li class="level3"><div class="li"><span class="li"><a href="#on_mmu_system" class="toc">On MMU System</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#allocate_memory_in_user_space_on_nommu_system" class="toc">Allocate memory in user space on NOMMU system</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#what_the_blackfin_uclinux_kernel_does" class="toc">What the Blackfin uClinux kernel does</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#more_info" class="toc">More info</a></span></div></li></ul></li></ul></li></ul></div></div><h2><a name="user_space_memory_allocation" id="user_space_memory_allocation">User Space Memory Allocation</a></h2><div class="level2"></div><!-- SECTION [1-43] --><h3><a name="on_mmu_system" id="on_mmu_system">On MMU System</a></h3><div class="level3"><p>With an MMU based processor the process of getting memory is as follows </p><ul><li class="level1"><div class="li"> get a block of memory and determine its physical address (pa)</div></li><li class="level1"><div class="li"> decide on a suitable virtual address for the user process (va)</div></li><li class="level1"><div class="li"> create the mapping tables to point va to pa</div></li></ul><p>There is a container for memory structure associated with a user task called a virtual memory area (vma). These are added to the task’s memory map (mm) as required.</p><p>The choice of address is normally decided by simply adding valid virtual addresses to the end of the heap. The heap is the virtual address after the task’s code and data space. </p><p>The user space system can also use <strong>brk</strong> or <strong>sbrk</strong> to adjust the processes data size</p><p>Extract from the <strong>brk</strong> man page</p><pre class="code"> **brk** sets the end of the data segment to the value specified by end_data_segment, when that value is reasonable, the system does have enough memory and the process does not exceed its max data size (see setrlimit(2)). **sbrk** increments the program's data space by increment bytes. sbrk isn't a system call, it is just a C library wrapper. Calling sbrk with an increment of 0 can be used to find the current location of the program break.</pre><p>You can also get and release memory using <strong>malloc</strong> and <strong>free</strong> calls</p><pre class="code c"> <span class="co2">#include <stdio.h></span><span class="co2">#include <unistd.h></span> <span class="co1">// simple brk / sbrk / malloc test</span><span class="kw4">int</span> main <span class="br0">(</span> <span class="kw4">int</span> argc, <span class="kw4">char</span> * argv<span class="br0">[</span><span class="br0">]</span> <span class="br0">)</span><span class="br0">{</span> <span class="kw4">char</span> * edata1; <span class="kw4">char</span> * edata2; <span class="kw4">char</span> * nedata; <span class="kw4">char</span> * mdata; <span class="co1">// find the end of the data segment</span> edata1 = sbrk<span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span>; <span class="co1">// move the end of the data segment</span> nedata = sbrk<span class="br0">(</span><span class="nu0">1024</span><span class="br0">)</span>; <span class="co1">// find the new end of the data segment</span> edata2 = sbrk<span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span>; <span class="co1">// reset the data segment end back to where it was</span> brk<span class="br0">(</span>edata1<span class="br0">)</span>; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">(</span><span class="st0">" brk/sbrk tests <span class="es0">\n</span>"</span> <span class="st0">" edata1 %p old end of data<span class="es0">\n</span>"</span> <span class="st0">" nedata %p new data area <span class="es0">\n</span>"</span> <span class="st0">" edata2 %p reset end of data<span class="es0">\n</span>"</span>, edata1, nedata, edata2 <span class="br0">)</span>; <span class="co1">// malloc free tests</span> mdata = <span class="br0">(</span><span class="kw4">char</span> *<span class="br0">)</span>malloc<span class="br0">(</span>0x1000<span class="br0">)</span>; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">(</span><span class="st0">" malloc tests <span class="es0">\n</span>"</span> <span class="st0">" mdata %p malloc data<span class="es0">\n</span>"</span> mdata<span class="br0">)</span>; <span class="co1">// free the data area</span> free<span class="br0">(</span>mdata<span class="br0">)</span>; <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span></pre></div><!-- SECTION [44-2325] --><h3><a name="allocate_memory_in_user_space_on_nommu_system" id="allocate_memory_in_user_space_on_nommu_system">Allocate memory in user space on NOMMU system</a></h3><div class="level3"></div><h4><a name="brk" id="brk">brk</a></h4><div class="level4"><p><img src="images/smileys/fixme.gif" align="middle" alt="FIXME" />: Still support?</p></div><h4><a name="malloc" id="malloc">malloc</a></h4><div class="level4"><p> <img src="images/smileys/fixme.gif" align="middle" alt="FIXME" />: What is the exact implementation in uclibc? It looks uclibc does not simply do mmap(). Also need to list the difference of malloc on MMU and !MMU system.</p><p>The c library <strong>malloc</strong> function is really a <strong>mmap</strong> call to an invalid file descriptor (-1)</p><p>It reserves a structure to hold the size at the start of the memory area and returns the address of the new data area.</p><p>C librabry support for <strong>mmap</strong> ( from uClibc )</p></div><!-- SECTION [2326-2863] --><h3><a name="what_the_blackfin_uclinux_kernel_does" id="what_the_blackfin_uclinux_kernel_does">What the Blackfin uClinux kernel does</a></h3><div class="level3"><p>The kernel support for these two functions is described as follows: <img src="images/smileys/fixme.gif" align="middle" alt="FIXME" />: The kernel implementation of mmap and brk has change a lot</p><p> The <strong>malloc</strong> and <strong>free</strong> functions will behave just like the version with a full MMU.</p></div><!-- SECTION [2864-3137] --><h3><a name="more_info" id="more_info">More info</a></h3><div class="level3"><p>Why is Malloc different on uClinux: <a href="http://www.linuxdevices.com/articles/AT7777470166.html" class="urlextern" title="http://www.linuxdevices.com/articles/AT7777470166.html" rel="nofollow">http://www.linuxdevices.com/articles/AT7777470166.html</a> </p></div><!-- SECTION [3138-] --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -