免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3622 | 回复: 2
打印 上一主题 下一主题

在哪里可以得到malloc的源码呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-01 04:53 |只看该作者 |倒序浏览
谢谢了!
偶想看看这个神秘的东西了

论坛徽章:
0
2 [报告]
发表于 2006-01-01 10:39 |只看该作者

  1. #include "../h/param.h"
  2. #include "../h/systm.h"
  3. #include "../h/uba.h"
  4. #include "../h/map.h"
  5. #include "../h/page.h"
  6. #include "../h/mem.h"

  7. /*
  8. * Allocate 'size' units from the given
  9. * map. Return the base of the allocated
  10. * space.
  11. * In a map, the addresses are increasing and the
  12. * list is terminated by a 0 size.
  13. * The swap map unit is 512 bytes.
  14. * Algorithm is first-fit.
  15. */
  16. malloc(mp, size)
  17. struct map *mp;
  18. {
  19.         register unsigned int a;
  20.         register struct map *bp;

  21.         for (bp=mp; bp->m_size; bp++) {
  22.                 if (bp->m_size >= size) {
  23.                         a = bp->m_addr;
  24.                         bp->m_addr += size;
  25.                         if ((bp->m_size -= size) == 0) {
  26.                                 do {
  27.                                         bp++;
  28.                                         (bp-1)->m_addr = bp->m_addr;
  29.                                 } while ((bp-1)->m_size = bp->m_size);
  30.                         }
  31.                         return(a);
  32.                 }
  33.         }
  34.         return(0);
  35. }

  36. /*
  37. * Free the previously allocated space aa
  38. * of size units into the specified map.
  39. * Sort aa into map and combine on
  40. * one or both ends if possible.
  41. */
  42. mfree(mp, size, a)
  43. struct map *mp;
  44. register unsigned int a;
  45. {
  46.         register struct map *bp;
  47.         register unsigned int t;

  48.         bp = mp;
  49.         for (; bp->m_addr<=a && bp->m_size!=0; bp++);
  50.         if (bp>mp && (bp-1)->m_addr+(bp-1)->m_size == a) {
  51.                 (bp-1)->m_size += size;
  52.                 if (a+size == bp->m_addr) {
  53.                         (bp-1)->m_size += bp->m_size;
  54.                         while (bp->m_size) {
  55.                                 bp++;
  56.                                 (bp-1)->m_addr = bp->m_addr;
  57.                                 (bp-1)->m_size = bp->m_size;
  58.                         }
  59.                 }
  60.         } else {
  61.                 if (a+size == bp->m_addr && bp->m_size) {
  62.                         bp->m_addr -= size;
  63.                         bp->m_size += size;
  64.                 } else if (size) {
  65.                         do {
  66.                                 t = bp->m_addr;
  67.                                 bp->m_addr = a;
  68.                                 a = t;
  69.                                 t = bp->m_size;
  70.                                 bp->m_size = size;
  71.                                 bp++;
  72.                         } while (size = t);
  73.                 }
  74.         }
  75. }




  76. /*
  77. * allocate memory
  78. * Note: the memory map is stored in the array
  79. * `memmap', one bit per page frame.  If the
  80. * i'th bit of memmap is 0, then page frame i
  81. * is free (available), if 1, then it is allocated.
  82. * Up to NICMEM free page frame numbers are
  83. * stored in the array mem.m_pnum.
  84. */
  85. memall(base, size)
  86. register int *base;
  87. {
  88.         register int i, pos, ndx, mask;

  89.         if (size <= 0 || size > freemem)
  90.                 return(0);
  91.         if (mem.m_free < 0)
  92.                 panic("bad mem free-list");
  93.         for(i=size; --i>=0; ) {
  94.                 if (mem.m_free == 0) {
  95.                         pos = lastpos + 1;
  96.                         if (pos >= maxfree)
  97.                                 pos = firstfree;
  98.                         while(mem.m_free<NICMEM) {
  99.                                 if ((memmap[pos>>5]&masktab[pos&0x1f])==0)
  100.                                         mem.m_pnum[mem.m_free++] = pos;
  101.                                 if (pos == lastpos)
  102.                                         break;
  103.                                 if (++pos >= maxfree)
  104.                                         pos = firstfree;
  105.                         }
  106.                         if (mem.m_free <= 0)
  107.                                 panic("lost mem");
  108.                         lastpos = pos;
  109.                 }
  110.                 pos = mem.m_pnum[--mem.m_free];
  111.                 mask = masktab[pos&0x1f];
  112.                 ndx = pos >> 5;
  113.                 if (memmap[ndx]&mask)
  114.                         panic("dup alloc");
  115.                 memmap[ndx] |= mask;
  116.                 *base++ = pos;
  117.                 freemem--;
  118.         }
  119.         return(size);
  120. }


  121. /*
  122. * Free memory
  123. */
  124. memfree(base, size)
  125. register int *base, size;
  126. {
  127.         register int ndx, mask;

  128.         while(--size>=0) {
  129.                 *base &= PG_PFNUM;
  130.                 if (*base<firstfree || *base>=maxfree)
  131.                         panic("bad mem free");
  132.                 if (mem.m_free < NICMEM && mem.m_free >= 0)
  133.                         mem.m_pnum[mem.m_free++] = *base;
  134.                 ndx = *base >> 5;
  135.                 mask = masktab[*base++ & 0x1f];
  136.                 if ((memmap[ndx]&mask)==0)
  137.                         panic("dup free");
  138.                 memmap[ndx] &= ~mask;
  139.                 freemem++;
  140.         }
  141. }

  142. /*
  143. * Initialize memory map
  144. */
  145. meminit(first, last)
  146. {
  147.         firstfree = first;
  148.         maxfree = lastpos = last;
  149.         freemem = last - first;
  150.         mem.m_free = 0;
  151. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-01-01 16:16 |只看该作者
man malloc应该可以看到头文件
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP