Chinaunix首页 | 论坛 | 博客
  • 博客访问: 11762
  • 博文数量: 6
  • 博客积分: 150
  • 博客等级: 入伍新兵
  • 技术积分: 55
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-31 00:55
文章分类

全部博文(6)

文章存档

2013年(1)

2012年(5)

我的朋友
最近访客

分类: 系统运维

2012-02-09 01:04:26

两个函数取自UNIX 版本6 malloc.c文件,一个为malloc函数,一个为mfree函数

2515:/*map数组是一个空闲资源列表,其中每个存储区由其长度和相对地址定义*/
      struct map 
2516: {
2517:         char *m_size;
2518:         char *m_addr;
2519: };
2520: /* ---------------------------       */
2521:
2522: /*
2523:  * Allocate size units from the given
2524:  * map. Return the base of the allocated
2525:  * space.
2526:  * Algorithm is first fit.
2527:  */
2528: malloc(mp, size)
2529: struct map *mp;
2530: {
2531:         register int a;
2532:         register struct map *bp;
2533:
2534:         for (bp = mp; bp->m_size; bp++) { /*搜索map数组,直到到达map列表尾部,或者搜索到一个长度满足请求的空闲空间*/
2535:                 if (bp->m_size >= size) { /*该空闲块满足请求*/
2536:                         a = bp->m_addr;   /*记录该区首地址*/
2537:                         bp->m_addr =+ size; /*空闲块地址首端大小为size块被分配,因此首地址前移size*/
2538:                         if ((bp->m_size =- size) == 0) /*分配的空闲块大小刚好等于请求空间的大小*/
2539:                                 do {    /*将该空间块在map中的后继记录前移*/
2540:                                         bp++;
2541:                                         (bp-1)->m_addr = bp->m_addr;
2542:                                 } while ((bp-1)->m_size = bp->m_size);
2543:                         return(a);
2544:                 }
2545:         }
2546:         return(0);
2547: }
2548: /* ---------------------------       */
2549:
2550: /*
2551:  * Free the previously allocated space aa
2552:  * of size units into the specified map.
2553:  * Sort aa into map and combine on
2554:  * one or both ends if possible.
2555:  */
  /*将一个大小为size,起始地址为aa的存储区还给map*/
2556: mfree(mp, size, aa)
2557: struct map *mp;
2558: {
2559:         register struct map *bp;
2560:         register int t;
2561:         register int a;
2562:
2563:         a = aa;
2564:         for (bp = mp; bp->m_addr<=a && bp->m_size!=0; bp++); /*搜索map,直到找到map中起始地址小于释放空间地址的记录*/
2565:         if (bp>mp && (bp-1)->m_addr+(bp-1)->m_size == a) { /*释放空间和前一空间连续*/
2566:                 (bp-1)->m_size =+ size; /*map中前一元素的大小增加被释放空间的大小*/
2567:                 if (a+size == bp->m_addr) { /*释放的存储区是否与map中下一个区块相邻*/
2568:                         (bp-1)->m_size =+ bp->m_size; /*合并被释放块和后一块存储区*/
2569:                         while (bp->m_size) { /*前移余下的存储区*/
2570:                                 bp++;
2571:                                 (bp-1)->m_addr = bp->m_addr;
2572:                                 (bp-1)->m_size = bp->m_size;
2573:                         }
2574:                 }
2575:         } else { /*不与前一空间连续*/
2576:                 if (a+size == bp->m_addr && bp->m_size) { /*是否与后一空间连续*/
2577:                         bp->m_addr =- size;
2578:                         bp->m_size =+ size;
2579:                 } else if (size) do { /*释放空间大小不为0,在map中加入新记录,后移map中余下的空间记录*/
2580:                         t = bp->m_addr;
2581:                         bp->m_addr = a;
2582:                         a = t;
2583:                         t = bp->m_size;
2584:                         bp->m_size = size;
2585:                         bp++;
2586:                 } while (size = t);
2587:         }
2588: }
2589: /* ---------------------------       */

 

阅读(289) | 评论(0) | 转发(0) |
0

上一篇:keep myself current

下一篇:Python 安装和使用

给主人留下些什么吧!~~