Index: soft/giet_vm/giet_common/kernel_malloc.c
===================================================================
--- soft/giet_vm/giet_common/kernel_malloc.c	(revision 466)
+++ soft/giet_vm/giet_common/kernel_malloc.c	(revision 495)
@@ -6,5 +6,5 @@
 ////////////////////////////////////////////////////////////////////////////////
 //   Implementation note:
-// - As this code is used to implement the SBT lock ptotecting TTY0,
+// - As this code is used to implement the SQT lock ptotecting TTY0,
 //   all functions here use the kernel _nolock_printf() function.
 // - It must exist one vseg with the HEAP type in each cluster. The length
@@ -37,5 +37,5 @@
 #include "mapping_info.h"
 #include "kernel_malloc.h"
-#include "locks.h"
+#include "kernel_locks.h"
 #include "tty0.h"
 #include "utils.h"
@@ -45,5 +45,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
-extern kernel_heap_t kernel_heap[X_SIZE][Y_SIZE];
+__attribute__((section(".kdata")))
+kernel_heap_t kernel_heap[X_SIZE][Y_SIZE];
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -83,4 +84,5 @@
                                             (size <= 0x80000000) ? 31 :\
                                                                    32
+
 #if GIET_DEBUG_SYS_MALLOC
 ////////////////////////////////////////////////
@@ -115,5 +117,5 @@
                    " - free[22]    = %x\n"
                    " - free[23]    = %x\n",
-                   kernel_heap[x][y].x, kernel_heap[x][y].y, 
+                   x, y, 
                    kernel_heap[x][y].heap_base, kernel_heap[x][y].heap_size, 
                    kernel_heap[x][y].free[0] , kernel_heap[x][y].free[1], 
@@ -135,9 +137,9 @@
 
 
-/////////////////////////////////////////////
-void _get_heap_info( unsigned int* heap_base,
-                     unsigned int* heap_size,
-                     unsigned int  x,
-                     unsigned int  y )
+/////////////////////////////////////////////////////
+unsigned int _get_heap_info( unsigned int* heap_base,
+                             unsigned int* heap_size,
+                             unsigned int  x,
+                             unsigned int  y )
 {
     mapping_header_t  * header   = (mapping_header_t *)SEG_BOOT_MAPPING_BASE;
@@ -155,6 +157,6 @@
     if ( (x >= X_SIZE) || (y >= Y_SIZE) )
     {
-        _nolock_printf("[GIET ERROR] _get_heap_info() illegal (%d,%d) coordinates\n",
-                       x , y );
+        _nolock_printf("\n[GIET ERROR] _get_heap_info()"
+                       " illegal (%d,%d) coordinates\n", x , y );
         _exit();
     }
@@ -172,13 +174,9 @@
             *heap_base = vsegs[vseg_id].vbase;
             *heap_size = vobjs[vobj_id].length;
-            return;
+            return 0;
         }
     }
 
-    // exit if not found
-    _nolock_printf("[GIET ERROR] _get_heap_info() heap[%d][%d] vseg not found\n",
-                   x , y );
-    _exit();
-
+    return 1;
 } // end _get_heap_info()
 
@@ -191,8 +189,8 @@
     unsigned int heap_size;
     unsigned int heap_index;
-
     unsigned int index;
     unsigned int x;
     unsigned int y;
+    unsigned int ko;
 
     for ( x = 0 ; x < X_SIZE ; x++ )
@@ -201,41 +199,47 @@
         {
             // get heap_base, heap size, and heap index
-            _get_heap_info( &heap_base, &heap_size, x, y );
-            heap_index = GET_SIZE_INDEX( heap_size );
-
-            // checking heap segment constraints
-            if ( heap_size != (1<<heap_index) )
+            ko = _get_heap_info( &heap_base, &heap_size, x, y );
+       
+            if ( ko )  // no kernel heap found in cluster[x][y]
             {
-                _nolock_printf("[GIET ERROR] in _heap_init()"
-                        " kernel_heap[â°d,â°d] not power of 2\n", x , y );
-                _exit();
+                // initialise kernel_heap[x][y] descriptor
+                kernel_heap[x][y].heap_base  = 0;
+                kernel_heap[x][y].heap_size  = 0;
+                _spin_lock_init( &kernel_heap[x][y].lock );
             }
-            if ( heap_base % heap_size ) 
+            else       // kernel heap found in cluster[x][y]
             {
-                _nolock_printf("[GIET ERROR] in _heap_init()"
-                        " kernel_heap[â°d,â°d] not aligned\n", x , y );
-                _exit();
+                heap_index = GET_SIZE_INDEX( heap_size );
+
+                // check heap[x][y] constraints
+                if ( heap_size != (1<<heap_index) )
+                {
+                    _nolock_printf("\n[GIET ERROR] in _heap_init()"
+                                   " kernel_heap[â°d,â°d] not power of 2\n", x , y );
+                    _exit();
+                }
+                if ( heap_base % heap_size ) 
+                {
+                    _nolock_printf("\n[GIET ERROR] in _heap_init()"
+                                   " kernel_heap[â°d,â°d] not aligned\n", x , y );
+                    _exit();
+                }
+
+                // initialise the free[] array 
+                for ( index = 0 ; index < 32 ; index++ ) 
+                {
+                    if (index == heap_index) kernel_heap[x][y].free[index] = heap_base;
+                    else                     kernel_heap[x][y].free[index] = 0;
+                }
+
+                // initialise kernel_heap[x][y] descriptor
+                kernel_heap[x][y].heap_base  = heap_base;
+                kernel_heap[x][y].heap_size  = heap_size;
+                _spin_lock_init( &kernel_heap[x][y].lock );
             }
 
-            // initialise the free[] array 
-            for ( index = 0 ; index < 32 ; index++ ) 
-            {
-                if (index == heap_index) kernel_heap[x][y].free[index] = heap_base;
-                else                     kernel_heap[x][y].free[index] = 0;
-            }
-            unsigned int* ptr = (unsigned int*)heap_base;
-            *ptr = 0;
-
-            // initialise kernel_heap[x][y] descriptor
-            kernel_heap[x][y].x          = x;
-            kernel_heap[x][y].y          = y;
-            kernel_heap[x][y].heap_base  = heap_base;
-            kernel_heap[x][y].heap_size  = heap_size;
-
-            _spin_lock_init( &kernel_heap[x][y].lock );
-
 #if GIET_DEBUG_SYS_MALLOC
-_nolock_printf("\n[DEBUG KERNEL_MALLOC] Completing kernel_heap[%d][%d] initialisation\n",
-               x, y );
+_nolock_printf("\n[DEBUG KERNEL_MALLOC] Completing kernel_heap[%d][%d]"
+               " initialisation\n", x, y );
 _display_free_array(x,y);
 #endif
@@ -248,8 +252,8 @@
 
 //////////////////////////////////////////////
-unsigned int split_block( kernel_heap_t* heap,
-                          unsigned int   vaddr, 
-                          unsigned int   searched_index,
-                          unsigned int   requested_index )
+unsigned int _split_block( kernel_heap_t* heap,
+                           unsigned int   vaddr, 
+                           unsigned int   searched_index,
+                           unsigned int   requested_index )
 {
     // push the upper half block into free[searched_index-1]
@@ -258,5 +262,5 @@
     heap->free[searched_index-1] = (unsigned int)new;
         
-    if ( searched_index == requested_index + 1 )  // terminal case: return lower half block 
+    if ( searched_index == requested_index + 1 )  //  return lower half block 
     {
         return vaddr;
@@ -264,14 +268,14 @@
     else            // non terminal case : lower half block must be split again
     {                               
-        return split_block( heap, vaddr, searched_index-1, requested_index );
-    }
-} // end split_block()
+        return _split_block( heap, vaddr, searched_index-1, requested_index );
+    }
+} // end _split_block()
 
 
 
 /////////////////////////////////////////////
-unsigned int get_block( kernel_heap_t* heap,
-                        unsigned int   searched_index,
-                        unsigned int   requested_index )
+unsigned int _get_block( kernel_heap_t* heap,
+                         unsigned int   searched_index,
+                         unsigned int   requested_index )
 {
     // test terminal case
@@ -285,5 +289,5 @@
         if ( vaddr == 0 )     // block not found : search in free[searched_index+1]
         {
-            return get_block( heap, searched_index+1, requested_index );
+            return _get_block( heap, searched_index+1, requested_index );
         }
         else                // block found : pop it from free[searched_index] 
@@ -300,9 +304,9 @@
             else                                      // split is required
             {
-                return split_block( heap, vaddr, searched_index, requested_index );
+                return _split_block( heap, vaddr, searched_index, requested_index );
             }
         } 
     }
-} // end get_block()
+} // end _get_block()
 
 
@@ -314,18 +318,19 @@
 {
     // checking arguments
-    if (size == 0) 
-    {
-        _nolock_printf("[GIET ERROR] _remote_malloc() : requested size = 0 \n");
+    if ( x >= X_SIZE )
+    {
+        _nolock_printf("\n[GIET ERROR] _remote_malloc() : x coordinate too large\n");
         _exit();
     }
-    if ( x >= X_SIZE )
-    {
-        _nolock_printf("[GIET ERROR] _remote_malloc() : x coordinate too large\n");
+    if ( y >= Y_SIZE )
+    {
+        _nolock_printf("\n[GIET ERROR] _remote_malloc() : y coordinate too large\n");
         _exit();
     }
-    if ( y >= Y_SIZE )
-    {
-        _nolock_printf("[GIET ERROR] _remote_malloc() : y coordinate too large\n");
+    if ( kernel_heap[x][y].heap_size == 0 )
+    {
+        _nolock_printf("\n[GIET ERROR] _remote_malloc() : No heap[%d][%d]\n", x, y );
         _exit();
+     
     }
 
@@ -340,10 +345,16 @@
 
     // call the recursive function get_block
-    unsigned int base = get_block( &kernel_heap[x][y], 
-                                   requested_index, 
-                                   requested_index );
+    unsigned int base = _get_block( &kernel_heap[x][y], 
+                                    requested_index, 
+                                    requested_index );
     // release the lock
     _spin_lock_release( &kernel_heap[x][y].lock );
  
+    if ( base == 0 )
+    {
+        _nolock_printf("\n[GIET ERROR] _remote_malloc() : no more space "
+                       "in heap[%d][%d]", x, y );
+    }
+
 #if GIET_DEBUG_SYS_MALLOC
 _nolock_printf("\n[DEBUG KERNEL_MALLOC] malloc vaddr %x from kernel_heap[%d][%d]\n", 
@@ -354,5 +365,5 @@
     return (void*)base;
 
-} // end remote_malloc()
+} // end _remote_malloc()
 
 
