minix best fit改first fit

来源:互联网 发布:centos6 yum php7 编辑:程序博客网 时间:2024/04/30 19:56
Code:
  1. /*===========================================================================* 
  2.  *              alloc_mem                    * 
  3.  *===========================================================================*/  
  4. PUBLIC phys_clicks alloc_mem(clicks)  
  5. phys_clicks clicks;     /* amount of memory requested */  
  6. #define USING_FIRST_FIT  
  7. #ifdef USING_FIRST_FIT  
  8. {  
  9. /* Allocate a block of memory from the free list using first fit. The block 
  10.  * consists of a sequence of contiguous bytes, whose length in clicks is 
  11.  * given by 'clicks'.  A pointer to the block is returned.  The block is 
  12.  * always on a click boundary.  This procedure is called when memory is 
  13.  * needed for FORK or EXEC.  Swap other processes out if needed. 
  14.  */  
  15.   register struct hole *hp, *prev_ptr;  
  16.   phys_clicks old_base;  
  17.   
  18.   do {  
  19.         prev_ptr = NIL_HOLE;  
  20.     hp = hole_head;  
  21.     while (hp != NIL_HOLE && hp->h_base < swap_base) {  
  22.         if (hp->h_len >= clicks) {  
  23.             /* We found a hole that is big enough.  Use it. */  
  24.             old_base = hp->h_base;  /* remember where it started */  
  25.             hp->h_base += clicks;   /* bite a piece off */  
  26.             hp->h_len -= clicks;    /* ditto */   
  27.   
  28.             /* Remember new high watermark of used memory. */  
  29.             if(hp->h_base > high_watermark)  
  30.                 high_watermark = hp->h_base;  
  31.   
  32.             /* Delete the hole if used up completely. */  
  33.             if (hp->h_len == 0) del_slot(prev_ptr, hp);  
  34.   
  35.             /* Return the start address of the acquired block. */  
  36.             return(old_base);  
  37.         }  
  38.   
  39.         prev_ptr = hp;  
  40.         hp = hp->h_next;  
  41.     }  
  42.   } while (swap_out());     /* try to swap some other process out */  
  43.   return(NO_MEM);  
  44. }  
  45. #endif  
  46. #ifdef USING_BEST_FIT  
  47. {  
  48.     //先找到第一个满足要求的空洞,  
  49.     //再以第一个为标准寻找最适合的空洞。  
  50.     //当最适合的空洞完全吻合  
  51.     //就直接划给它,当空洞较大时就切割。  
  52.   
  53.     //首先注册目标指针、目标前一个指针、头指针  
  54.     //记录目标大小和目前最适合大小  
  55.     register struct hole *hp;  
  56.     register struct hole *prevAim_ptr;  
  57.     register struct hole *Aim_ptr;  
  58.     phys_clicks old_base;  
  59.   
  60.     //如果循环一次都没找到  
  61.     //就把可以退出内存的进程赶出去  
  62.     //再循环  
  63.     do{  
  64.         hp = hole_head;  
  65.         prevAim_ptr = NIL_HOLE;  
  66.         Aim_ptr = NIL_HOLE;  
  67.           
  68.         for(;hp != NIL_HOLE && hp->h_base < swap_base;  
  69.             prevAim_ptr=hp,hp=hp->h_next)  
  70.         {  
  71.             //find the best hole  
  72.             if(hp->h_len == clicks)  
  73.             {  
  74.                 old_base = hp->h_base;  /* remember where it started */  
  75.                 hp->h_base += clicks;   /* bite off */  
  76.                 hp->h_len -= clicks;    /* ditto */  
  77.                   
  78.                 del_slot(prevAim_ptr, hp);/* Delete the hole which used up completely. */  
  79.   
  80.                 /* Remember new high watermark of used memory. */  
  81.                 if(hp->h_base > high_watermark)  
  82.                     high_watermark = hp->h_base;  
  83.                 return(old_base);  
  84.             }  
  85.   
  86.             //当从没记录过合适内存时  
  87.             //把遇到的第一个合适节点保存在aim中  
  88.             if(hp->h_len > clicks && Aim_ptr == NIL_HOLE)  
  89.             {  
  90.                 Aim_ptr=hp;  
  91.             }  
  92.               
  93.   
  94.             //当找到一个比原来aim更合适的空间  
  95.             //记录新的空间  
  96.             if(hp->h_len > clicks && hp->h_len < Aim_ptr->h_len)  
  97.             {  
  98.                 //mark it down  
  99.                 Aim_ptr=hp;  
  100.             }  
  101.   
  102.         }  
  103.   
  104.         //we found it  
  105.         if(Aim_ptr != NIL_HOLE)  
  106.         {  
  107.             old_base = Aim_ptr->h_base;  /* remember where it started */  
  108.             Aim_ptr->h_base += clicks;   /* bite off */  
  109.             Aim_ptr->h_len -= clicks;    /* ditto */  
  110.               
  111.             /* Remember new high watermark of used memory. */  
  112.             if(Aim_ptr->h_base > high_watermark)  
  113.                 high_watermark = Aim_ptr->h_base;  
  114.               
  115.             return(old_base);  
  116.         }  
  117.           
  118.     }while(swap_out());  
  119.     return(NO_MEM);  
  120. }  
  121. #endif  
  122. #ifdef USING_NEXT_FIT  
  123. #endif  
  124. #ifdef USING_WORST_FIT  
  125. #endif  

 

minix内存分配模式默认使用first fit 现改为 best fit