📄 malloc.htm
字号:
<!doctype html public "-//W3C//DTD HTML 3.2//EN"><html><head><title>Linux/lib/malloc.c</title><meta http-equiv=Content-Type content="text/html; charset=gb2312"><base href="http://oldlinux.org/lxr/http_cn/"></head><body bgcolor=white><div align=center> [<b><i>源代码浏览</i></b>] [<a href="diff/lib/malloc.c">区别标定</a>] [<a href="ident">标识符搜索</a>] [<a href="search">文本搜索</a>] [<a href="find">文件搜索</a>]</div><h1 align=center> <a href="http:/"> OldLinux</a> <a href="http:blurb.html"> 交叉引用</a><br> <a href="source/">Linux</a>/<a href="source/lib/">lib</a>/<a href="source/lib/malloc.c">malloc.c</a></h1><div align=center> <b>版本:</b> [<a href="source/lib/malloc.c?v=1.0">1.0</a>] [<a href="source/lib/malloc.c?v=0.99.11">0.99.11</a>] [<a href="source/lib/malloc.c?v=0.99">0.99</a>] [<a href="source/lib/malloc.c?v=0.98">0.98</a>] [<a href="source/lib/malloc.c?v=0.97">0.97</a>] [<a href="source/lib/malloc.c?v=0.96a">0.96a</a>] [<a href="source/lib/malloc.c?v=0.95">0.95</a>] [<a href="source/lib/malloc.c?v=0.12">0.12</a>] [<b><i>0.11</i></b>] [<a href="source/lib/malloc.c?v=0.01">0.01</a>] <br> <b>体系结构:</b> [<b><i>i386</i></b>] <br></div><hr><pre> <a name=L1 href="source/lib/malloc.c#L1">1</a> <b><i>/*</i></b> <a name=L2 href="source/lib/malloc.c#L2">2</a> <b><i> * malloc.c --- a general purpose kernel memory allocator for Linux.</i></b> <a name=L3 href="source/lib/malloc.c#L3">3</a> <b><i> * </i></b> <a name=L4 href="source/lib/malloc.c#L4">4</a> <b><i> * Written by Theodore Ts'o (tytso@mit.edu), 11/29/91</i></b> <a name=L5 href="source/lib/malloc.c#L5">5</a> <b><i> *</i></b> <a name=L6 href="source/lib/malloc.c#L6">6</a> <b><i> * This routine is written to be as fast as possible, so that it</i></b> <a name=L7 href="source/lib/malloc.c#L7">7</a> <b><i> * can be called from the interrupt level.</i></b> <a name=L8 href="source/lib/malloc.c#L8">8</a> <b><i> *</i></b> <a name=L9 href="source/lib/malloc.c#L9">9</a> <b><i> * Limitations: maximum size of memory we can allocate using this routine</i></b> <a name=L10 href="source/lib/malloc.c#L10">10</a> <b><i> * is 4k, the size of a page in Linux.</i></b> <a name=L11 href="source/lib/malloc.c#L11">11</a> <b><i> *</i></b> <a name=L12 href="source/lib/malloc.c#L12">12</a> <b><i> * The general game plan is that each page (called a bucket) will only hold</i></b> <a name=L13 href="source/lib/malloc.c#L13">13</a> <b><i> * objects of a given size. When all of the object on a page are released,</i></b> <a name=L14 href="source/lib/malloc.c#L14">14</a> <b><i> * the page can be returned to the general free pool. When malloc() is</i></b> <a name=L15 href="source/lib/malloc.c#L15">15</a> <b><i> * called, it looks for the smallest bucket size which will fulfill its</i></b> <a name=L16 href="source/lib/malloc.c#L16">16</a> <b><i> * request, and allocate a piece of memory from that bucket pool.</i></b> <a name=L17 href="source/lib/malloc.c#L17">17</a> <b><i> *</i></b> <a name=L18 href="source/lib/malloc.c#L18">18</a> <b><i> * Each bucket has as its control block a bucket descriptor which keeps </i></b> <a name=L19 href="source/lib/malloc.c#L19">19</a> <b><i> * track of how many objects are in use on that page, and the free list</i></b> <a name=L20 href="source/lib/malloc.c#L20">20</a> <b><i> * for that page. Like the buckets themselves, bucket descriptors are</i></b> <a name=L21 href="source/lib/malloc.c#L21">21</a> <b><i> * stored on pages requested from get_free_page(). However, unlike buckets,</i></b> <a name=L22 href="source/lib/malloc.c#L22">22</a> <b><i> * pages devoted to bucket descriptor pages are never released back to the</i></b> <a name=L23 href="source/lib/malloc.c#L23">23</a> <b><i> * system. Fortunately, a system should probably only need 1 or 2 bucket</i></b> <a name=L24 href="source/lib/malloc.c#L24">24</a> <b><i> * descriptor pages, since a page can hold 256 bucket descriptors (which</i></b> <a name=L25 href="source/lib/malloc.c#L25">25</a> <b><i> * corresponds to 1 megabyte worth of bucket pages.) If the kernel is using </i></b> <a name=L26 href="source/lib/malloc.c#L26">26</a> <b><i> * that much allocated memory, it's probably doing something wrong. :-)</i></b> <a name=L27 href="source/lib/malloc.c#L27">27</a> <b><i> *</i></b> <a name=L28 href="source/lib/malloc.c#L28">28</a> <b><i> * Note: malloc() and free() both call get_free_page() and free_page()</i></b> <a name=L29 href="source/lib/malloc.c#L29">29</a> <b><i> * in sections of code where interrupts are turned off, to allow</i></b> <a name=L30 href="source/lib/malloc.c#L30">30</a> <b><i> * malloc() and free() to be safely called from an interrupt routine.</i></b> <a name=L31 href="source/lib/malloc.c#L31">31</a> <b><i> * (We will probably need this functionality when networking code,</i></b> <a name=L32 href="source/lib/malloc.c#L32">32</a> <b><i> * particularily things like NFS, is added to Linux.) However, this</i></b> <a name=L33 href="source/lib/malloc.c#L33">33</a> <b><i> * presumes that get_free_page() and free_page() are interrupt-level</i></b> <a name=L34 href="source/lib/malloc.c#L34">34</a> <b><i> * safe, which they may not be once paging is added. If this is the</i></b> <a name=L35 href="source/lib/malloc.c#L35">35</a> <b><i> * case, we will need to modify malloc() to keep a few unused pages</i></b> <a name=L36 href="source/lib/malloc.c#L36">36</a> <b><i> * "pre-allocated" so that it can safely draw upon those pages if</i></b> <a name=L37 href="source/lib/malloc.c#L37">37</a> <b><i> * it is called from an interrupt routine.</i></b> <a name=L38 href="source/lib/malloc.c#L38">38</a> <b><i> *</i></b> <a name=L39 href="source/lib/malloc.c#L39">39</a> <b><i> * Another concern is that get_free_page() should not sleep; if it </i></b> <a name=L40 href="source/lib/malloc.c#L40">40</a> <b><i> * does, the code is carefully ordered so as to avoid any race </i></b> <a name=L41 href="source/lib/malloc.c#L41">41</a> <b><i> * conditions. The catch is that if malloc() is called re-entrantly, </i></b> <a name=L42 href="source/lib/malloc.c#L42">42</a> <b><i> * there is a chance that unecessary pages will be grabbed from the </i></b> <a name=L43 href="source/lib/malloc.c#L43">43</a> <b><i> * system. Except for the pages for the bucket descriptor page, the </i></b> <a name=L44 href="source/lib/malloc.c#L44">44</a> <b><i> * extra pages will eventually get released back to the system, though,</i></b> <a name=L45 href="source/lib/malloc.c#L45">45</a> <b><i> * so it isn't all that bad.</i></b> <a name=L46 href="source/lib/malloc.c#L46">46</a> <b><i> */</i></b> <a name=L47 href="source/lib/malloc.c#L47">47</a> <a name=L48 href="source/lib/malloc.c#L48">48</a> #include <linux/kernel.h> <a name=L49 href="source/lib/malloc.c#L49">49</a> #include <linux/mm.h> <a name=L50 href="source/lib/malloc.c#L50">50</a> #include <asm/system.h> <a name=L51 href="source/lib/malloc.c#L51">51</a> <a name=L52 href="source/lib/malloc.c#L52">52</a> struct <a href="ident?i=bucket_desc">bucket_desc</a> { <b><i>/* 16 bytes */</i></b> <a name=L53 href="source/lib/malloc.c#L53">53</a> void *page; <a name=L54 href="source/lib/malloc.c#L54">54</a> struct <a href="ident?i=bucket_desc">bucket_desc</a> *next; <a name=L55 href="source/lib/malloc.c#L55">55</a> void *freeptr; <a name=L56 href="source/lib/malloc.c#L56">56</a> unsigned short refcnt; <a name=L57 href="source/lib/malloc.c#L57">57</a> unsigned short bucket_size; <a name=L58 href="source/lib/malloc.c#L58">58</a> }; <a name=L59 href="source/lib/malloc.c#L59">59</a> <a name=L60 href="source/lib/malloc.c#L60">60</a> struct <a href="ident?i=_bucket_dir">_bucket_dir</a> { <b><i>/* 8 bytes */</i></b> <a name=L61 href="source/lib/malloc.c#L61">61</a> int size; <a name=L62 href="source/lib/malloc.c#L62">62</a> struct <a href="ident?i=bucket_desc">bucket_desc</a> *chain; <a name=L63 href="source/lib/malloc.c#L63">63</a> }; <a name=L64 href="source/lib/malloc.c#L64">64</a> <a name=L65 href="source/lib/malloc.c#L65">65</a> <b><i>/*</i></b> <a name=L66 href="source/lib/malloc.c#L66">66</a> <b><i> * The following is the where we store a pointer to the first bucket</i></b> <a name=L67 href="source/lib/malloc.c#L67">67</a> <b><i> * descriptor for a given size. </i></b> <a name=L68 href="source/lib/malloc.c#L68">68</a> <b><i> *</i></b> <a name=L69 href="source/lib/malloc.c#L69">69</a> <b><i> * If it turns out that the Linux kernel allocates a lot of objects of a</i></b> <a name=L70 href="source/lib/malloc.c#L70">70</a> <b><i> * specific size, then we may want to add that specific size to this list,</i></b> <a name=L71 href="source/lib/malloc.c#L71">71</a> <b><i> * since that will allow the memory to be allocated more efficiently.</i></b> <a name=L72 href="source/lib/malloc.c#L72">72</a> <b><i> * However, since an entire page must be dedicated to each specific size</i></b> <a name=L73 href="source/lib/malloc.c#L73">73</a> <b><i> * on this list, some amount of temperance must be exercised here.</i></b> <a name=L74 href="source/lib/malloc.c#L74">74</a> <b><i> *</i></b> <a name=L75 href="source/lib/malloc.c#L75">75</a> <b><i> * Note that this list *must* be kept in order.</i></b> <a name=L76 href="source/lib/malloc.c#L76">76</a> <b><i> */</i></b> <a name=L77 href="source/lib/malloc.c#L77">77</a> struct <a href="ident?i=_bucket_dir">_bucket_dir</a> <a href="ident?i=bucket_dir">bucket_dir</a>[] = { <a name=L78 href="source/lib/malloc.c#L78">78</a> { 16, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L79 href="source/lib/malloc.c#L79">79</a> { 32, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L80 href="source/lib/malloc.c#L80">80</a> { 64, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L81 href="source/lib/malloc.c#L81">81</a> { 128, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L82 href="source/lib/malloc.c#L82">82</a> { 256, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L83 href="source/lib/malloc.c#L83">83</a> { 512, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L84 href="source/lib/malloc.c#L84">84</a> { 1024, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L85 href="source/lib/malloc.c#L85">85</a> { 2048, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L86 href="source/lib/malloc.c#L86">86</a> { 4096, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}, <a name=L87 href="source/lib/malloc.c#L87">87</a> { 0, (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0}}; <b><i>/* End of list marker */</i></b> <a name=L88 href="source/lib/malloc.c#L88">88</a> <a name=L89 href="source/lib/malloc.c#L89">89</a> <b><i>/*</i></b> <a name=L90 href="source/lib/malloc.c#L90">90</a> <b><i> * This contains a linked list of free bucket descriptor blocks</i></b> <a name=L91 href="source/lib/malloc.c#L91">91</a> <b><i> */</i></b> <a name=L92 href="source/lib/malloc.c#L92">92</a> struct <a href="ident?i=bucket_desc">bucket_desc</a> *<a href="ident?i=free_bucket_desc">free_bucket_desc</a> = (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) 0; <a name=L93 href="source/lib/malloc.c#L93">93</a> <a name=L94 href="source/lib/malloc.c#L94">94</a> <b><i>/*</i></b> <a name=L95 href="source/lib/malloc.c#L95">95</a> <b><i> * This routine initializes a bucket description page.</i></b> <a name=L96 href="source/lib/malloc.c#L96">96</a> <b><i> */</i></b> <a name=L97 href="source/lib/malloc.c#L97">97</a> static inline void <a href="ident?i=init_bucket_desc">init_bucket_desc</a>() <a name=L98 href="source/lib/malloc.c#L98">98</a> { <a name=L99 href="source/lib/malloc.c#L99">99</a> struct <a href="ident?i=bucket_desc">bucket_desc</a> *bdesc, *first;<a name=L100 href="source/lib/malloc.c#L100">100</a> int i;<a name=L101 href="source/lib/malloc.c#L101">101</a> <a name=L102 href="source/lib/malloc.c#L102">102</a> first = bdesc = (struct <a href="ident?i=bucket_desc">bucket_desc</a> *) <a href="ident?i=get_free_page">get_free_page</a>();<a name=L103 href="source/lib/malloc.c#L103">103</a> if (!bdesc)<a name=L104 href="source/lib/malloc.c#L104">104</a> <a href="ident?i=panic">panic</a>(<i>"Out of memory in init_bucket_desc()"</i>);<a name=L105 href="source/lib/malloc.c#L105">105</a> for (i = <a href="ident?i=PAGE_SIZE">PAGE_SIZE</a>/sizeof(struct <a href="ident?i=bucket_desc">bucket_desc</a>); i > 1; i--) {<a name=L106 href="source/lib/malloc.c#L106">106</a> bdesc->next = bdesc+1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -