- /*===========================================================================*
- * alloc_mem *
- *===========================================================================*/
- PUBLIC phys_clicks alloc_mem(clicks)
- phys_clicks clicks; /* amount of memory requested */
- #define USING_FIRST_FIT
- #ifdef USING_FIRST_FIT
- {
- /* Allocate a block of memory from the free list using first fit. The block
- * consists of a sequence of contiguous bytes, whose length in clicks is
- * given by 'clicks'. A pointer to the block is returned. The block is
- * always on a click boundary. This procedure is called when memory is
- * needed for FORK or EXEC. Swap other processes out if needed.
- */
- register struct hole *hp, *prev_ptr;
- phys_clicks old_base;
- do {
- prev_ptr = NIL_HOLE;
- hp = hole_head;
- while (hp != NIL_HOLE && hp->h_base < swap_base) {
- if (hp->h_len >= clicks) {
- /* We found a hole that is big enough. Use it. */
- old_base = hp->h_base; /* remember where it started */
- hp->h_base += clicks; /* bite a piece off */
- hp->h_len -= clicks; /* ditto */
- /* Remember new high watermark of used memory. */
- if(hp->h_base > high_watermark)
- high_watermark = hp->h_base;
- /* Delete the hole if used up completely. */
- if (hp->h_len == 0) del_slot(prev_ptr, hp);
- /* Return the start address of the acquired block. */
- return(old_base);
- }
- prev_ptr = hp;
- hp = hp->h_next;
- }
- } while (swap_out()); /* try to swap some other process out */
- return(NO_MEM);
- }
- #endif
- #ifdef USING_BEST_FIT
- {
- //先找到第一个满足要求的空洞,
- //再以第一个为标准寻找最适合的空洞。
- //当最适合的空洞完全吻合
- //就直接划给它,当空洞较大时就切割。
- //首先注册目标指针、目标前一个指针、头指针
- //记录目标大小和目前最适合大小
- register struct hole *hp;
- register struct hole *prevAim_ptr;
- register struct hole *Aim_ptr;
- phys_clicks old_base;
- //如果循环一次都没找到
- //就把可以退出内存的进程赶出去
- //再循环
- do{
- hp = hole_head;
- prevAim_ptr = NIL_HOLE;
- Aim_ptr = NIL_HOLE;
- for(;hp != NIL_HOLE && hp->h_base < swap_base;
- prevAim_ptr=hp,hp=hp->h_next)
- {
- //find the best hole
- if(hp->h_len == clicks)
- {
- old_base = hp->h_base; /* remember where it started */
- hp->h_base += clicks; /* bite off */
- hp->h_len -= clicks; /* ditto */
- del_slot(prevAim_ptr, hp);/* Delete the hole which used up completely. */
- /* Remember new high watermark of used memory. */
- if(hp->h_base > high_watermark)
- high_watermark = hp->h_base;
- return(old_base);
- }
- //当从没记录过合适内存时
- //把遇到的第一个合适节点保存在aim中
- if(hp->h_len > clicks && Aim_ptr == NIL_HOLE)
- {
- Aim_ptr=hp;
- }
- //当找到一个比原来aim更合适的空间
- //记录新的空间
- if(hp->h_len > clicks && hp->h_len < Aim_ptr->h_len)
- {
- //mark it down
- Aim_ptr=hp;
- }
- }
- //we found it
- if(Aim_ptr != NIL_HOLE)
- {
- old_base = Aim_ptr->h_base; /* remember where it started */
- Aim_ptr->h_base += clicks; /* bite off */
- Aim_ptr->h_len -= clicks; /* ditto */
- /* Remember new high watermark of used memory. */
- if(Aim_ptr->h_base > high_watermark)
- high_watermark = Aim_ptr->h_base;
- return(old_base);
- }
- }while(swap_out());
- return(NO_MEM);
- }
- #endif
- #ifdef USING_NEXT_FIT
- #endif
- #ifdef USING_WORST_FIT
- #endif
minix内存分配模式默认使用first fit 现改为 best fit