Index: trunk/kernel/mm/kcm.c
===================================================================
--- trunk/kernel/mm/kcm.c	(revision 1)
+++ trunk/kernel/mm/kcm.c	(revision 7)
@@ -33,63 +33,32 @@
 #include <page.h>
 #include <cluster.h>
+#include <kmem.h>
 #include <kcm.h>
 
-/////////////////////////////////////////////////////////////////////////////////////
-// Extern global variable (defined in kmem.c file)
-/////////////////////////////////////////////////////////////////////////////////////
-
-extern uint32_t kmem_size_tbl[KMEM_TYPES_NR];
-
-
-/////////////////////////////////////////////////////////////////////////////////////
-// This function allocates one page from local PPM.
-/////////////////////////////////////////////////////////////////////////////////////
-static page_t * kcm_page_alloc( kcm_t * kcm )
-{
-	page_t    * page;
-    kmem_req_t  req;
-
-    req.type  = KMEM_PAGE;
-    req.size  = 0;
-    req.flags = AF_KERNEL;
-    page = kmem_alloc( &req );
-  
-	if( page == NULL )
-	{
-		printk("\n[ERROR] in %s failed to allocate page in cluster %d\n", 
-               __FUNCTION__ , local_cxy );
-	}
-
-	return page;
-}
-
 //////////////////////////////////////////////////////////////////////////////////////
-// This static function returns pointer on allocated block from an active page.
+// This static function returns pointer on an allocated block from an active page.
 // It returns NULL if no block available in selected page.
 // It changes the page status if required.
 //////////////////////////////////////////////////////////////////////////////////////
-static void * get_block( kcm_t      * kcm,
-                         kcm_page_t * page )
-{
-	int32_t index = bitmap_ffs( page->bitmap , sizeof(page->bitmap) ); 
-
-    // the page should be active 
-    if( page->active == 0 )
-    {
-        printk("\n[PANIC] in %s : kcm page should be active\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
-
-    // the page should not be full
-    if( index == -1 )
-    {
-        printk("\n[PANIC] in %s : kcm page should be active\n", __FUNCTION__ );
-        hal_core_sleep();
-    }
+// @ kcm   : pointer on kcm allocator.
+// @ ptr   : pointer on active kcm page to use.
+/////////////////////////////////////////////////////////////////////////////////////
+static void * kcm_get_block( kcm_t      * kcm,
+                             kcm_page_t * page )
+{
+    assert( page->active , __FUNCTION__ , "kcm page should be active" );
+
+    // get first block available
+	int32_t index = bitmap_ffs( page->bitmap , kcm->blocks_nr ); 
+
+    assert( (index != -1) , __FUNCTION__ , "kcm page should not be full" );
  
+    // allocate block
 	bitmap_clear( page->bitmap , index );
+
+    // increase page refcount
 	page->refcount ++;
 
-    // change the page to busy if last block in page
+    // change the page to busy no more free block in page
     if( page->refcount >= kcm->blocks_nr )
     {
@@ -103,6 +72,7 @@
     }
 
-	return (page->blk_tbl + index * kcm->block_size );
-}
+	return (page->base + index * kcm->block_size );
+
+}  // kcm_get_block()
 
 /////////////////////////////////////////////////////////////////////////////////////
@@ -110,6 +80,9 @@
 // It changes the page status if required.
 /////////////////////////////////////////////////////////////////////////////////////
-static void put_block ( kcm_t * kcm,
-                        void  * ptr )
+// @ kcm   : pointer on kcm allocator.
+// @ ptr   : pointer on block to be released.
+/////////////////////////////////////////////////////////////////////////////////////
+static void kcm_put_block ( kcm_t * kcm,
+                            void  * ptr )
 {
 	kcm_page_t * page;
@@ -117,5 +90,5 @@
   
 	page = (kcm_page_t*)((intptr_t)ptr & CONFIG_PPM_PAGE_MASK);
-	index = ((uint8_t*)ptr - page->blk_tbl) / kcm->block_size;
+	index = ((uint8_t*)ptr - page->base) / kcm->block_size;
   
 	bitmap_set( page->bitmap , index );
@@ -144,8 +117,8 @@
 		kcm->free_pages_nr ++;
 	}
-}
-
-/////////////////////////////////////////////////////////////////////////////////////
-// This static function tries to allocate one page from PPM. It initializes
+}  // kcm_put_block()
+
+/////////////////////////////////////////////////////////////////////////////////////
+// This static function allocates one page from PPM. It initializes
 // the KCM-page descriptor, and introduces the new page into freelist. 
 /////////////////////////////////////////////////////////////////////////////////////
@@ -154,10 +127,20 @@
 	page_t     * page;
 	kcm_page_t * ptr;
+    kmem_req_t   req;
 
     // get one page from local PPM
-	page = kcm_page_alloc( kcm );
-	if( page == NULL ) return ENOMEM;
- 
-    // get physical page base address
+    req.type  = KMEM_PAGE;
+    req.size  = 0;
+    req.flags = AF_KERNEL;
+    page = kmem_alloc( &req );
+  
+	if( page == NULL )
+	{
+		printk("\n[ERROR] in %s : failed to allocate page in cluster %d\n", 
+               __FUNCTION__ , local_cxy );
+        return ENOMEM;
+	}
+
+    // get page base address
 	ptr = ppm_page2base( page );
 
@@ -168,5 +151,5 @@
 	ptr->active        = 0;
 	ptr->refcount      = 0;
-	ptr->blk_tbl       = (uint8_t*)ptr + kcm->block_size;
+	ptr->base          = (uint8_t*)ptr + kcm->block_size;
 	ptr->kcm           = kcm;
 	ptr->page          = page;
@@ -177,5 +160,6 @@
  
 	return 0;
-}
+
+}  // freelist_populate()
 
 /////////////////////////////////////////////////////////////////////////////////////
@@ -185,5 +169,5 @@
 static kcm_page_t * freelist_get( kcm_t * kcm )
 {
-	error_t      err;
+	error_t      error;
 	kcm_page_t * page;
 
@@ -191,6 +175,6 @@
 	if( kcm->free_pages_nr == 0 )
 	{
-        err = freelist_populate( kcm );
-        if( err	) return NULL;
+        error = freelist_populate( kcm );
+        if( error	) return NULL;
 	}
 
@@ -199,23 +183,19 @@
 	list_unlink( &page->list );
 	kcm->free_pages_nr --;
-    page->active = 1;
-
-    list_add_first( &kcm->active_root , &page->list);
-	kcm->active_pages_nr ++;
-    page->active = 1;
 
 	return page;
-}
-
-
-////////////////////////////////////
-error_t kcm_init( kcm_t       * kcm,
-		          uint32_t      type )
+
+} // freelist_get()
+
+
+//////////////////////////////
+void kcm_init( kcm_t    * kcm,
+	           uint32_t   type )
 {
 	uint32_t     blocks_nr;
 	uint32_t     block_size;
 	uint32_t     remaining;
-	kcm_page_t * page;
-
+
+    // initialize lock
 	spinlock_init( &kcm->lock );
 
@@ -231,7 +211,6 @@
 	list_root_init( &kcm->active_root );
 
-    // initialize block size and number of blocks per page,
-    // using the global kmem_size_tbl[] array 
-	block_size      = ARROUND_UP( kmem_size_tbl[type] , 64 );
+    // initialize block size and number of blocks per page
+	block_size      = ARROUND_UP( kmem_type_size( type ) , 64 );
 	blocks_nr       = CONFIG_PPM_PAGE_SIZE / block_size;
 	remaining       = CONFIG_PPM_PAGE_SIZE % block_size;
@@ -240,16 +219,9 @@
 	kcm->blocks_nr  = blocks_nr;
 	kcm->block_size = block_size;
-
-    // get one page from free_list
-	page = freelist_get( kcm );
-	if( page == NULL ) return ENOMEM;
      
-    // register page in active list
-	list_add_first( &kcm->active_root , &page->list );
-	page->active = 1;
-	kcm->active_pages_nr ++;
-
-	return 0;
-}
+    kcm_dmsg("\n[INFO] %s : KCM %s initialised / block_size = %d / blocks_nr = %d\n",
+             __FUNCTION__ , kmem_type_str( type ) , block_size , blocks_nr );
+
+}  // kcm_init()
 
 ///////////////////////////////
@@ -292,6 +264,5 @@
     spinlock_unlock( &kcm->lock );
 
-    
-}
+}  // kcm_destroy()
 
 ///////////////////////////////
@@ -304,21 +275,41 @@
 	spinlock_lock( &kcm->lock );
     
-    // get block
-    if( list_is_empty( &kcm->active_root ) )  // no active page
+    // get an active page
+    if( list_is_empty( &kcm->active_root ) )  // no active page => get one
     {
+        kcm_dmsg("\n[INFO] %s : enters for type %s but no active page => get one\n",
+                 __FUNCTION__ , kmem_type_str( kcm->type ) );
+
+        // get a page from free list
+		page = freelist_get( kcm );
+	    if( page == NULL ) return NULL;
+
+        // insert page in active list
+        list_add_first( &kcm->active_root , &page->list );
+	    kcm->active_pages_nr ++;
+        page->active = 1;
+    }
+    else                     // get first page from active list
+    {
+        kcm_dmsg("\n[INFO] %s : enters for type %s with an active page\n",
+                 __FUNCTION__ , kmem_type_str( kcm->type ) );
+
+        // get page pointer from active list
 		page = (kcm_page_t *)LIST_FIRST( &kcm->active_root , kcm_page_t , list );
-        ptr  = get_block( kcm , page );
     }
-    else                                      // active page cannot be full
-    {
-        page = LIST_FIRST( &kcm->active_root , kcm_page_t , list );
-        ptr  = get_block( kcm , page );
-    }
+
+    // get a block from selected active page
+    // cannot fail, as an active page cannot be full...
+    ptr  = kcm_get_block( kcm , page );
 
     // release lock  
 	spinlock_unlock(&kcm->lock);
 
+    kcm_dmsg("\n[INFO] %s : allocated one block of type %s / ptr = %x\n",
+             __FUNCTION__ , kmem_type_str( kcm->type ) , (uint32_t)ptr );
+
 	return ptr;
-}
+
+}  // kcm_alloc()
 
 ///////////////////////////
@@ -337,5 +328,5 @@
 
     // release block
-	put_block( kcm , ptr );
+	kcm_put_block( kcm , ptr );
 
     // release lock  
@@ -346,6 +337,6 @@
 void kcm_print (kcm_t * kcm)
 {
-	printk("*** KCM type = %d / free_pages = %d / busy_pages = %d / active_pages = %d\n",
-           kcm->type , 
+	printk("*** KCM type = %s / free_pages = %d / busy_pages = %d / active_pages = %d\n",
+           kmem_type_str( kcm->type ) , 
            kcm->free_pages_nr , 
            kcm->busy_pages_nr ,
