Index: trunk/kernel/mm/ppm.c
===================================================================
--- trunk/kernel/mm/ppm.c	(revision 634)
+++ trunk/kernel/mm/ppm.c	(revision 635)
@@ -212,6 +212,7 @@
 	page_t   * found_block;  
 
+    thread_t * this = CURRENT_THREAD;
+
 #if DEBUG_PPM_ALLOC_PAGES
-thread_t * this = CURRENT_THREAD;
 uint32_t cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PPM_ALLOC_PAGES < cycle )
@@ -237,22 +238,24 @@
 
 	current_block = NULL;
-
-	// find a free block equal or larger to requested size
-	for( current_order = order ; current_order < CONFIG_PPM_MAX_ORDER ; current_order ++ )
-	{
-		if( !list_is_empty( &ppm->free_pages_root[current_order] ) )
+    current_order = order;
+
+	// search a free block equal or larger than requested size
+	while( current_order < CONFIG_PPM_MAX_ORDER )
+	{
+        // get local pointer on the root of relevant free_list (same in all clusters)
+        list_entry_t * root = &ppm->free_pages_root[current_order];
+
+		if( !list_is_empty( root ) )
 		{
             // get first free block in this free_list
-			current_block = LIST_FIRST( &ppm->free_pages_root[current_order] , page_t , list );
+			current_block = LIST_FIRST( root , page_t , list );
 
             // remove this block from this free_list
 			list_unlink( &current_block->list );
+	        ppm->free_pages_nr[current_order] --;
 
             // register pointer on found block
             found_block = current_block;
 
-            // update this free-list number of blocks
-	        ppm->free_pages_nr[current_order] --;
-
             // compute found block size
 	        current_size = (1 << current_order);
@@ -260,4 +263,7 @@
 			break;	
 		}
+
+        // increment loop index
+        current_order++;
 	}
 
@@ -267,10 +273,6 @@
 		remote_busylock_release( lock_xp );
 
-#if DEBUG_PPM_ALLOC_PAGES
-cycle = (uint32_t)hal_get_cycles();
-if( DEBUG_PPM_ALLOC_PAGES < cycle )
-printk("\n[%s] thread[%x,%x] cannot allocate %d page(s) in cluster %x / cycle %d\n",
-__FUNCTION__, this->process->pid, this->trdid, 1<<order, local_cxy, cycle );
-#endif
+        printk("\n[%s] thread[%x,%x] cannot allocate %d page(s) in cluster %x\n",
+        __FUNCTION__, this->process->pid, this->trdid, 1<<order, local_cxy );
 
 		return NULL;
@@ -282,8 +284,9 @@
 	while( current_order > order )
 	{
+        // update size and order
 		current_order --;
-
-        // update pointer, size, and order fiels for new free block
 		current_size >>= 1;
+
+        // update order fiels in new free block
 		current_block = found_block + current_size;
 		current_block->order = current_order;
@@ -291,6 +294,4 @@
         // insert new free block in relevant free_list
 		list_add_first( &ppm->free_pages_root[current_order] , &current_block->list );
-
-        // update number of blocks in free list
 		ppm->free_pages_nr[current_order] ++;
 	}
@@ -312,5 +313,5 @@
 printk("\n[%s] thread[%x,%x] exit for %d page(s) in cluster %x / ppn = %x / cycle %d\n",
 __FUNCTION__, this->process->pid, this->trdid, 
-1<<order, ppm_page2ppn(XPTR( local_cxy , found_block )), local_cxy, cycle );
+1<<order, local_cxy, ppm_page2ppn(XPTR( local_cxy , found_block )), cycle );
 #endif
 
@@ -374,5 +375,5 @@
 
 /////////////////////////////////////////////
-xptr_t ppm_remote_alloc_pages( cxy_t     cxy,
+void * ppm_remote_alloc_pages( cxy_t     cxy,
                                uint32_t  order )
 {
@@ -382,6 +383,7 @@
     page_t   * found_block;
 
+    thread_t * this  = CURRENT_THREAD;
+
 #if DEBUG_PPM_REMOTE_ALLOC_PAGES
-thread_t * this  = CURRENT_THREAD;
 uint32_t   cycle = (uint32_t)hal_get_cycles();
 if( DEBUG_PPM_REMOTE_ALLOC_PAGES < cycle )
@@ -408,12 +410,13 @@
 
     current_block = NULL;    
-
-	// find in remote cluster a free block equal or larger to requested size
-	for( current_order = order ; current_order < CONFIG_PPM_MAX_ORDER ; current_order ++ )
-	{
-        // get local pointer on the root of relevant free_list in remote cluster
+    current_order = order;
+
+    // search a free block equal or larger than requested size
+    while( current_order < CONFIG_PPM_MAX_ORDER )
+    {
+        // get local pointer on the root of relevant free_list (same in all clusters)
         list_entry_t * root = &ppm->free_pages_root[current_order];
 
-		if( !list_remote_is_empty( cxy , root ) )
+		if( !list_remote_is_empty( cxy , root ) )  // list non empty => success
 		{
             // get local pointer on first free page descriptor in remote cluster
@@ -422,11 +425,9 @@
             // remove first free page from the free-list in remote cluster
 			list_remote_unlink( cxy , &current_block->list );
+	        hal_remote_atomic_add( XPTR( cxy , &ppm->free_pages_nr[current_order] ), -1 );
 
             // register found block
             found_block = current_block;
 
-	        // decrement relevant free-list number of items in remote cluster
-	        hal_remote_atomic_add( XPTR( cxy , &ppm->free_pages_nr[current_order] ), -1 );
-
             // compute found block size
 	        current_size = (1 << current_order);
@@ -434,4 +435,7 @@
 			break;
 		}
+
+        // increment loop index
+        current_order++;
 	}
 
@@ -441,10 +445,6 @@
 		remote_busylock_release( lock_xp );
 
-#if DEBUG_REMOTE_PPM_ALLOC_PAGES
-cycle = (uint32_t)hal_get_cycles();
-if( DEBUG_PPM_REMOTE_ALLOC_PAGES < cycle )
-printk("\n[%s] thread[%x,%x] cannot allocate %d page(s) in cluster %x / cycle %d\n",
-__FUNCTION__, this->process->pid, this->trdid, 1<<order, cxy, cycle );
-#endif
+        printk("\n[ERROR] in %s : thread[%x,%x] cannot allocate %d page(s) in cluster %x\n",
+        __FUNCTION__, this->process->pid, this->trdid, 1<<order, cxy );
 
 		return XPTR_NULL;
@@ -455,10 +455,10 @@
 	while( current_order > order )
 	{
-        // update order, size, and local pointer for new free block
+        // update order and size
 		current_order --;
 		current_size >>= 1;
+
+        // update new free block order field in remote cluster 
 		current_block = found_block + current_size;
-
-        // update new free block order field in remote cluster 
 		hal_remote_s32( XPTR( cxy , &current_block->order ) , current_order );
 
@@ -497,5 +497,5 @@
 #endif
 
-	return XPTR( cxy , found_block );
+	return found_block;
 
 }  // end ppm_remote_alloc_pages()
