Index: /tags/minix3.1.2a-orig/minix/servers/pm/alloc.c
===================================================================
--- /tags/minix3.1.2a-orig/minix/servers/pm/alloc.c	(revision 13)
+++ /tags/minix3.1.2a-orig/minix/servers/pm/alloc.c	(revision 14)
@@ -68,32 +68,46 @@
  * needed for FORK or EXEC.  Swap other processes out if needed.
  */
-  register struct hole *hp, *prev_ptr;
+  register struct hole *hp, *prev_ptr, *best, *prev_best;
+  int delta=-1;
   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 */
+        hp = hole_head;
+        while (hp != NIL_HOLE && hp->h_base < swap_base) {
+                printf("base:%d len:%d (req:%d)\n", hp->h_base, hp->h_len, clicks);
+                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. */
+                        printf("Allocazione 'perfetta'\n");
+                        return(old_base);
+                if (hp->h_len > clicks) {
+                        if (hp->h_len - clicks < delta || delta == -1){
+                                delta = hp->h_len - clicks;
+                                best = hp;
+                                prev_best = prev_ptr;
+                        }
+                prev_ptr = hp;
+                hp = hp->h_next;
+        if (delta > -1){
+           printf("Allocazione con delta=%d\n",delta);
+           old_base = best->h_base;     /* remember where it started */
+           best->h_base += clicks;      /* bite a piece off */
+           best->h_len -= clicks;       /* cioe` delta */
+           /* Remember new high watermark of used memory. */
+           if(best->h_base > high_watermark)
+                high_watermark = best->h_base;
+           /* Delete the hole if used up completely. */
+           if (best->h_len == 0) printf("Non dovrebbe mai succedere\n");
+           /* Return the start address of the acquired block. */
+           return(old_base);
+  } while (swap_out());         /* try to swap some other process out */
   return(NO_MEM);
 }
