Index: soft/giet_vm/giet_libs/malloc.c
===================================================================
--- soft/giet_vm/giet_libs/malloc.c	(revision 541)
+++ soft/giet_vm/giet_libs/malloc.c	(revision 588)
@@ -11,5 +11,5 @@
 
 // Global variables defining the heap descriptors array (one heap per cluster)
-giet_heap_t heap[X_SIZE][Y_SIZE];
+giet_heap_t     heap[X_SIZE][Y_SIZE];
 
 // Macro returning the smallest power of 2 larger or equal to size value
@@ -51,48 +51,26 @@
                          unsigned int y )
 {
-    giet_shr_printf(
-                " - heap_base   = %x\n"
-                " - heap_size   = %x\n"
-                " - alloc_base  = %x\n"
-                " - alloc_size  = %x\n"
-                " - free[0]     = %x\n"
-                " - free[1]     = %x\n"
-                " - free[2]     = %x\n"
-                " - free[3]     = %x\n"
-                " - free[4]     = %x\n"
-                " - free[5]     = %x\n"
-                " - free[6]     = %x\n"
-                " - free[7]     = %x\n"
-                " - free[8]     = %x\n"
-                " - free[9]     = %x\n"
-                " - free[10]    = %x\n"
-                " - free[11]    = %x\n"
-                " - free[12]    = %x\n"
-                " - free[13]    = %x\n"
-                " - free[14]    = %x\n"
-                " - free[15]    = %x\n"
-                " - free[16]    = %x\n"
-                " - free[17]    = %x\n"
-                " - free[18]    = %x\n"
-                " - free[19]    = %x\n"
-                " - free[20]    = %x\n"
-                " - free[21]    = %x\n"
-                " - free[22]    = %x\n"
-                " - free[23]    = %x\n",
-                heap[x][y].heap_base, heap[x][y].heap_size, 
-                heap[x][y].alloc_base, heap[x][y].alloc_size,
-                heap[x][y].free[0], heap[x][y].free[1], 
-                heap[x][y].free[2], heap[x][y].free[3],
-                heap[x][y].free[4], heap[x][y].free[5],
-                heap[x][y].free[6], heap[x][y].free[7],
-                heap[x][y].free[8], heap[x][y].free[9],
-                heap[x][y].free[10], heap[x][y].free[11],
-                heap[x][y].free[12], heap[x][y].free[13],
-                heap[x][y].free[14], heap[x][y].free[15],
-                heap[x][y].free[16], heap[x][y].free[17],
-                heap[x][y].free[18], heap[x][y].free[19],
-                heap[x][y].free[20], heap[x][y].free[21],
-                heap[x][y].free[22], heap[x][y].free[23] );
-}  // end display_free array()
+    unsigned int next;
+    unsigned int id;
+    unsigned int iter;
+
+    giet_shr_printf("\nUser Heap[%d][%d] base = %x / size = %x\n", x , y , 
+                   heap[x][y].heap_base, heap[x][y].heap_size );
+    for ( id = 6 ; id < 28 ; id++ )
+    { 
+        next = heap[x][y].free[id];
+        giet_shr_printf(" - free[%d] = " , id );
+        iter = 0;
+        while ( next != 0 )
+        {
+            giet_shr_printf("%x | ", next );
+            next = (*(unsigned int*)next);
+            iter++;
+        }
+        giet_shr_printf("0\n");
+    }
+}  // end display_free_array()
+
+
 
 ////////////////////////////////
@@ -106,5 +84,5 @@
     unsigned int alloc_base;       // alloc[] array base 
     unsigned int alloc_size;       // alloc[] array size
-    unsigned int alloc_index;      // size_index in free[array]
+    unsigned int alloc_index;      // size_index in alloc[array]
 
     unsigned int index;            // iterator
@@ -149,4 +127,9 @@
     }
 
+    // reset the alloc_size array
+    unsigned int   word;
+    unsigned int*  tab = (unsigned int*)alloc_base;
+    for ( word = 0 ; word < (alloc_size>>2) ; word++ )  tab[word] = 0;
+ 
     // split the heap into various sizes blocks,
     // initializes the free[] array and NEXT pointers
@@ -243,5 +226,5 @@
 
 #if GIET_DEBUG_USER_MALLOC
-giet_shr_printf("\n[DEBUG USER_MALLOC] Malloc request for Heap[%d][%d] / size = %x\n", 
+giet_shr_printf("\n[DEBUG USER_MALLOC] request for Heap[%d][%d] / size = %x\n", 
                  x, y, size );
 #endif
@@ -250,13 +233,13 @@
     if (size == 0) 
     {
-        giet_exit("ERROR in malloc() : requested size = 0 \n");
+        giet_exit("\nERROR in remote_malloc() : requested size = 0 \n");
     }
     if ( x >= X_SIZE )
     {
-        giet_exit("ERROR in malloc() : x coordinate too large\n");
+        giet_exit("\nERROR in remote_malloc() : x coordinate too large\n");
     }
     if ( y >= Y_SIZE )
     {
-        giet_exit("ERROR in malloc() : y coordinate too large\n");
+        giet_exit("\nERROR in remote_malloc() : y coordinate too large\n");
     }
 
@@ -264,5 +247,5 @@
     if ( heap[x][y].init != HEAP_INITIALIZED )
     {
-        giet_exit("ERROR in malloc() : heap not initialized\n");
+        giet_exit("\nERROR in remote_malloc() : heap not initialized\n");
     }
 
@@ -281,11 +264,24 @@
                                    requested_index );
 
-    // update the alloc[] array if block found
-    if ( base != 0 )
-    {
-        unsigned offset = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE;
-        unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset);
-        *ptr = requested_index;
-    }
+    // check block found
+    if ( base == 0 )
+    {
+        lock_release( &heap[x][y].lock );
+        giet_exit("\nERROR in remote_malloc() : no more space\n");
+    }
+
+    // compute pointer in alloc[] array
+    unsigned offset    = (base - heap[x][y].heap_base) / MIN_BLOCK_SIZE;
+    unsigned char* ptr = (unsigned char*)(heap[x][y].alloc_base + offset);
+
+    // check the alloc[] array
+    if ( *ptr != 0 )
+    {
+        lock_release( &heap[x][y].lock );
+        giet_exit("\nERROR in remote_malloc() : block already allocated ???\n");
+    }
+
+    // update alloc_array
+    *ptr = requested_index;
 
     // release the lock
@@ -293,6 +289,6 @@
  
 #if GIET_DEBUG_USER_MALLOC
-giet_shr_printf("\n[DEBUG USER_MALLOC] Malloc vaddr from Heap[%d][%d] = %x\n", 
-                x, y, base );
+giet_shr_printf("\n[DEBUG USER_MALLOC] allocated block from heap[%d][%d] : "
+                "base = %x / size = %x\n", x , y , base , size );
 display_free_array(x,y);
 #endif
@@ -333,8 +329,9 @@
     unsigned int size = 1<<size_index;
 
-    // compute companion_base and merged_base
-    unsigned int companion_base;   // companion block base address
-    unsigned int merged_base;      // merged block base address
-    if ( base % (size<<1) )
+    // compute companion block and merged block base addresses
+    unsigned int companion_base;  
+    unsigned int merged_base;  
+
+    if ( (base & size) == 0 )   // the released block is aligned on (2*size)
     {
         companion_base  = base + size;
@@ -352,5 +349,5 @@
     unsigned int  iter  = heap->free[size_index];
     unsigned int  prev  = (unsigned int)&heap->free[size_index];
-    while ( iter != 0 ) 
+    while ( iter ) 
     {
         if ( iter == companion_base ) 
@@ -359,11 +356,10 @@
             break;
         }
+        prev = iter;
         iter = *(unsigned int*)iter;
-        prev = iter;
-    }
-
-    if ( found == 0 )  // Companion not found 
-    {
-        // push the block in free[size_index]  
+    }
+
+    if ( found == 0 )  // Companion not found => push in free[size_index]  
+    {
         *(unsigned int*)base   = heap->free[size_index];
         heap->free[size_index] = base;
@@ -392,7 +388,4 @@
 #endif
 
-    // get the lock protecting heap[x][y]
-    lock_acquire( &heap[x][y].lock );
-
     // check ptr value
     unsigned int base = (unsigned int)ptr;
@@ -403,4 +396,7 @@
     }
  
+    // get the lock protecting heap[x][y]
+    lock_acquire( &heap[x][y].lock );
+
     // compute released block index in alloc[] array
     unsigned index = (base - heap[x][y].heap_base ) / MIN_BLOCK_SIZE;
@@ -410,8 +406,15 @@
     unsigned int   size_index = (unsigned int)*pchar;
 
+    // check block allocation
+    if ( size_index == 0 )
+    {
+        lock_release( &heap[x][y].lock );
+        giet_exit("\nERROR in free() : released block not allocated ???\n");
+    }
+
     // check released block alignment
     if ( base % (1 << size_index) )
     {
-        giet_exit("ERROR in free() : released block not aligned");
+        giet_exit("\nERROR in free() : released block not aligned\n");
     }
 
