Index: soft/giet_vm/libs/malloc.c
===================================================================
--- soft/giet_vm/libs/malloc.c	(revision 239)
+++ soft/giet_vm/libs/malloc.c	(revision 244)
@@ -6,6 +6,9 @@
 heap_ll * _heap_head[NB_TASKS_MAX];
 
-unsigned int _heap_base[NB_TASKS_MAX];
-unsigned int _heap_length[NB_TASKS_MAX];
+heap_ll * _remote_free_list[NB_TASKS_MAX] = { 0 };
+giet_lock_t lock_rf_list[NB_TASKS_MAX] = { {"toto",0} };
+
+unsigned int _heap_base[NB_TASKS_MAX] = { -1 };
+unsigned int _heap_length[NB_TASKS_MAX] = { -1 };
 
 int _first_malloc[NB_TASKS_MAX] = { 0 };
@@ -15,58 +18,76 @@
 
 #elif MALLOC_SELECTED == 2
-    heap_ll * _prev_next_last_allocted[NB_TASKS_MAX] = { 0 };
+heap_ll * _prev_next_last_allocted[NB_TASKS_MAX] = { 0 };
 #else
-    #define MAX_SIZE_POW2_TAB 28 
-    #define GET_TAB_INDEX(size) (size == 0x00000008) ? 0 :\
-                            (size <= 0x00000010) ? 1 :\
-                            (size <= 0x00000020) ? 2 :\
-                            (size <= 0x00000040) ? 3 :\
-                            (size <= 0x00000080) ? 4 :\
-                            (size <= 0x00000100) ? 5 :\
-                            (size <= 0x00000200) ? 6 :\
-                            (size <= 0x00000400) ? 7 :\
-                            (size <= 0x00000800) ? 8 :\
-                            (size <= 0x00001000) ? 9 :\
-                            (size <= 0x00002000) ? 10 :\
-                            (size <= 0x00004000) ? 11 :\
-                            (size <= 0x00008000) ? 12 :\
-                            (size <= 0x00010000) ? 13 :\
-                            (size <= 0x00020000) ? 14 :\
-                            (size <= 0x00040000) ? 15 :\
-                            (size <= 0x00080000) ? 16 :\
-                            (size <= 0x00100000) ? 17 :\
-                            (size <= 0x00200000) ? 18 :\
-                            (size <= 0x00400000) ? 19 :\
-                            (size <= 0x00800000) ? 20 :\
-                            (size <= 0x01000000) ? 21 :\
-                            (size <= 0x02000000) ? 22 :\
-                            (size <= 0x04000000) ? 23 :\
-                            (size <= 0x08000000) ? 24 :\
-                            (size <= 0x10000000) ? 25 :\
-                            (size <= 0x20000000) ? 26 :\
-                                                   27
-
-    heap_ll * _pow2tab[NB_TASKS_MAX][MAX_SIZE_POW2_TAB] = {{ 0 }};
+#define MAX_SIZE_POW2_TAB 28 
+#define GET_TAB_INDEX(size)                 (size == 0x00000008) ? 0 :\
+                                            (size <= 0x00000010) ? 1 :\
+                                            (size <= 0x00000020) ? 2 :\
+                                            (size <= 0x00000040) ? 3 :\
+                                            (size <= 0x00000080) ? 4 :\
+                                            (size <= 0x00000100) ? 5 :\
+                                            (size <= 0x00000200) ? 6 :\
+                                            (size <= 0x00000400) ? 7 :\
+                                            (size <= 0x00000800) ? 8 :\
+                                            (size <= 0x00001000) ? 9 :\
+                                            (size <= 0x00002000) ? 10 :\
+                                            (size <= 0x00004000) ? 11 :\
+                                            (size <= 0x00008000) ? 12 :\
+                                            (size <= 0x00010000) ? 13 :\
+                                            (size <= 0x00020000) ? 14 :\
+                                            (size <= 0x00040000) ? 15 :\
+                                            (size <= 0x00080000) ? 16 :\
+                                            (size <= 0x00100000) ? 17 :\
+                                            (size <= 0x00200000) ? 18 :\
+                                            (size <= 0x00400000) ? 19 :\
+                                            (size <= 0x00800000) ? 20 :\
+                                            (size <= 0x01000000) ? 21 :\
+                                            (size <= 0x02000000) ? 22 :\
+                                            (size <= 0x04000000) ? 23 :\
+                                            (size <= 0x08000000) ? 24 :\
+                                            (size <= 0x10000000) ? 25 :\
+                                            (size <= 0x20000000) ? 26 :\
+                                                                   27
+
+heap_ll * _pow2tab[NB_TASKS_MAX][MAX_SIZE_POW2_TAB] = {{ 0 }};
 #endif
 
-/* Todo: meilleure gestion des printf
-//#if DEBUG_MALLOC == 1
-unsigned int debug_level = 2;
-#elif DEBUG_MALLOC == 0
-
-#define giet_printf(level, ...)                 \
-({                                           \
-if (debug_level < level) {              \
-giet_tty_printf(__VA_ARGS__);       \
-}                                       \
-})
-*/
+
+#if DEBUG_MALLOC == 1              
+#define giet_printf(...)                \
+    ({                                      \
+     giet_tty_printf(__VA_ARGS__);       \
+     })                                  
+#else
+#define giet_printf(...) ({   })
+#endif
+
 
 
 #if MALLOC_SELECTED == 1
-
+/*########################################################################*/
+/**************************************************************************/
+/**********                     WORST-FIT                       ***********/
+/**************************************************************************/
+/*########################################################################*/
 heap_ll *get_prev_fit_chunk(unsigned int size) {
     int task_id = giet_global_task_id();
+    int check_remote_free_list = 0;
+after_remote_list_check :
     if (_heap_head[task_id] == 0x0) {
+        if(check_remote_free_list == 0)
+        {
+            heap_ll * poped_block;
+            heap_ll * head_of_rf;
+            check_remote_free_list = 1;
+
+            head_of_rf = pop_ptr();
+
+            while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0)
+            {
+                update_chunk_list((unsigned int)poped_block, poped_block->chunk_length);
+            }
+            goto after_remote_list_check; 
+        }
         return ((heap_ll *) 0x01);
     }
@@ -87,8 +108,24 @@
         ptr_to_chunk_list = ptr_to_chunk_list->next;
     }
-    if (found == 1) {
+    if (found == 1)
+    {
         return prev_tmp; // case when prev_tmp == 0x0 is handled in the calling function
     }
-    else {
+    else
+    {
+        if(check_remote_free_list == 0)
+        {
+            heap_ll * poped_block;
+            heap_ll * head_of_rf;
+            check_remote_free_list = 1;
+
+            head_of_rf = pop_ptr();
+
+            while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0)
+            {
+                update_chunk_list((unsigned int)poped_block, poped_block->chunk_length);
+            }
+            goto after_remote_list_check; 
+        }
         return (heap_ll *) 0x1;
     }
@@ -100,11 +137,8 @@
     int task_id = giet_global_task_id();
 
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("############ MALLOC ############\n\n");
-#endif
+    giet_printf("############ MALLOC ############\n\n");
     if (size == 0) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf(" SIZE = 0 \n");
-#endif
+
+        giet_printf(" SIZE = 0 \n");
         return 0x0;
     }
@@ -115,5 +149,5 @@
 
         if (_heap_length[task_id] == 0) {
-            giet_tty_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id);
+            giet_printf("*** Error: Malloc called in task %d and no heap was defined.\n", task_id);
             return 0x0;
         }
@@ -133,7 +167,6 @@
 
 
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("Looking for size : %d\n", size_align + 4);
-#endif
+
+    giet_printf("Looking for size : %d\n", size_align + 4);
     /****** Get prev victim from the chunks list ******/
     heap_ll *victim;
@@ -171,10 +204,9 @@
         heap_ll * tmp_chunk = victim;
         victim = (heap_ll *) (((unsigned int) victim) + (size_align + 4));
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x\n",
+
+        giet_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x\n",
                 (unsigned int) tmp_chunk,
                 (unsigned int) ((size_align + 4) / 4),
                 (unsigned int) victim);
-#endif
 
         victim->chunk_length = tmp_chunk->chunk_length - (size_align + 4);
@@ -190,8 +222,7 @@
     /****** Write Overhead ******/
     *((unsigned int *) victim_vbase) = size_align;	// writing overhead on the first word
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n"\
+
+    giet_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n"\
             ,(unsigned int)(victim_vbase),(unsigned int) (victim_vbase + 4));
-#endif
     return (unsigned int *) (victim_vbase + 4);
 }
@@ -210,6 +241,6 @@
         ((heap_ll *) block_base)->next = 0x0;
         _heap_head[task_id] = ((heap_ll *) block_base);
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("****** NEW BLOCK ******\n"
+
+        giet_printf("****** NEW BLOCK ******\n"
                 "@ : %x\n"
                 "Length : %d o\n"
@@ -217,5 +248,4 @@
                 block_base,
                 (unsigned int) (((heap_ll *) block_base)->chunk_length));
-#endif
         return;
     }
@@ -232,6 +262,6 @@
         /*  [.....|ptr|block|.....] */
         if ((unsigned int) (ptr_to_chunk_list) + chunk_length == block_base) {
-#if DEBUG_MALLOC == 1
-            giet_tty_printf("****** BLOCK MERGED ******\n"
+
+            giet_printf("****** BLOCK MERGED ******\n"
                     "CHUNK : %x of size %d o\n"
                     "merged with \n"
@@ -241,5 +271,4 @@
                     ptr_to_chunk_list->chunk_length, block_base,
                     block_length);
-#endif
 
             /* update the size */
@@ -264,6 +293,6 @@
             block_base = (unsigned int) ptr_to_chunk_list;
             block_length = ptr_to_chunk_list->chunk_length;
-#if DEBUG_MALLOC == 1
-            giet_tty_printf("****** NEW BLOCK ******\n"
+
+            giet_printf("****** NEW BLOCK ******\n"
                     "@ : %x\n"
                     "Length : %d o\n"
@@ -271,5 +300,4 @@
                     (unsigned int) ptr_to_chunk_list,
                     (unsigned int) (ptr_to_chunk_list->chunk_length));
-#endif
             prev_block_base = prev;
             prev = ptr_to_chunk_list;
@@ -281,6 +309,6 @@
         /*  [......|block|ptr|.....] */
         if (block_base + block_length == (unsigned int) ptr_to_chunk_list) {
-#if DEBUG_MALLOC == 1
-            giet_tty_printf("****** BLOCK MERGED ******\n"
+
+            giet_printf("****** BLOCK MERGED ******\n"
                     "BLOCK : %x of size %d o\n"
                     "merged with \n"
@@ -290,5 +318,4 @@
                     (unsigned int) ptr_to_chunk_list,
                     ptr_to_chunk_list->chunk_length);
-#endif
 
             /* update the size */
@@ -316,6 +343,6 @@
             ptr_to_chunk_list = ((heap_ll *) block_base);
             block_length = ptr_to_chunk_list->chunk_length;
-#if DEBUG_MALLOC == 1
-            giet_tty_printf("****** NEW BLOCK ******\n"
+
+            giet_printf("****** NEW BLOCK ******\n"
                     "@ : %x\n"
                     "Length : %d o\n"
@@ -323,5 +350,4 @@
                     (unsigned int) ptr_to_chunk_list,
                     (unsigned int) (ptr_to_chunk_list->chunk_length));
-#endif
             prev_block_base = prev;
             block_merged = 1;
@@ -339,6 +365,6 @@
     ((heap_ll *) block_base)->next = 0x0;
     prev->next = ((heap_ll *) block_base);
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("****** NEW BLOCK ******\n"
+
+    giet_printf("****** NEW BLOCK ******\n"
             "@ : %x\n"
             "Length : %d o\n"
@@ -346,5 +372,4 @@
             block_base,
             (unsigned int) (((heap_ll *) block_base)->chunk_length));
-#endif
     return;
 
@@ -352,44 +377,36 @@
 
 
-static void print_all_heap(void) {
-    int task_id = giet_global_task_id();
-    heap_ll * ptr_to_chunk_list = _heap_head[task_id];
-    giet_tty_printf("################### PRINT ALL HEAP ######################\n");
-    while (ptr_to_chunk_list != 0x0) {
-        giet_tty_printf("CHUNK at @ : %x\n"
-                "OVERHEAD : %d\n"
-                "NEXT : %x\n\n",
-                (unsigned int) ptr_to_chunk_list,
-                ptr_to_chunk_list->chunk_length,
-                (unsigned int) ptr_to_chunk_list->next);
-        ptr_to_chunk_list = ptr_to_chunk_list->next;
-    }
-    giet_tty_printf("#########################################################\n");
-}
-
-
-/* Warning: the division doesn't seem to work well in the giet/soclib ? */
-static void frag(int turn) {
-    int task_id = giet_global_task_id();
-    int nb_blocks = 0;
-    int size = 0;
-    heap_ll * ptr_to_chunk;
-    ptr_to_chunk = _heap_head[task_id];
-    while(ptr_to_chunk != 0x0) {
-        size += ptr_to_chunk->chunk_length;
-        nb_blocks += 1;
-        ptr_to_chunk = ptr_to_chunk->next;
-    }
-    if (size != 0) {
-        giet_tty_printf("%.3f\t%d\n", size / ((double) nb_blocks), turn);
-    }
-}
-
-// End WORST_FIT
+
+/*########################################################################*/
+/**************************************************************************/
+/**********                 END WORST-FIT                       ***********/
+/**************************************************************************/
+/*########################################################################*/
 #elif MALLOC_SELECTED == 2 // Next Fit 
+/*########################################################################*/
+/**************************************************************************/
+/**********                   NEXT-FIT                          ***********/
+/**************************************************************************/
+/*########################################################################*/
 
 heap_ll * get_prev_fit_chunk(unsigned int size) {
     int task_id = giet_global_task_id();
+    int check_remote_free_list = 0;
+after_remote_list_check_n : //n for next_fit
     if (_heap_head[task_id] == 0x0) {
+        if(check_remote_free_list == 0)
+        {
+            heap_ll * poped_block;
+            heap_ll * head_of_rf;
+            check_remote_free_list = 1;
+
+            head_of_rf = pop_ptr();
+
+            while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0)
+            {
+                update_chunk_list((unsigned int)poped_block, poped_block->chunk_length);
+            }
+            goto after_remote_list_check_n; 
+        }
         return ((heap_ll *) 0x1);
     }
@@ -421,4 +438,18 @@
         ptr_to_chunk_list = ptr_to_chunk_list->next;
     } while (ptr_to_chunk_list != tmp_turn);
+    if(check_remote_free_list == 0)
+    {
+        heap_ll * poped_block;
+        heap_ll * head_of_rf;
+        check_remote_free_list = 1;
+
+        head_of_rf = pop_ptr();
+
+        while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0)
+        {
+            update_chunk_list((unsigned int)poped_block, poped_block->chunk_length);
+        }
+        goto after_remote_list_check_n; 
+    }
 
     return (heap_ll *) 0x1;
@@ -429,11 +460,9 @@
 
     int task_id = giet_global_task_id();
-#if DEBUG_MALLOC == 1
-    giet_tty_giet_tty_printf("############ MALLOC ############\n\n");
-#endif
+
+    giet_printf("############ MALLOC ############\n\n");
     if (size == 0) {
-#if DEBUG_MALLOC == 1
-        giet_tty_giet_tty_printf(" SIZE = 0 \n");
-#endif
+
+        giet_printf(" SIZE = 0 \n");
         return 0x0;
     }
@@ -444,5 +473,5 @@
 
         if (_heap_length[task_id] == 0) {
-            giet_tty_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id);
+            giet_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id);
             return 0x0;
         }
@@ -464,7 +493,6 @@
 
 
-#if DEBUG_MALLOC == 1
-    giet_tty_giet_tty_printf("Looking for size : %d\n", size_align + 4);
-#endif
+
+    giet_printf("Looking for size : %d\n", size_align + 4);
     /****** Get prev victim from the chunks list ******/
     heap_ll *victim;
@@ -528,10 +556,9 @@
         heap_ll * tmp_chunk = victim;
         victim = (heap_ll *) (((unsigned int) victim) + (size_align + 4));
-#if DEBUG_MALLOC == 1
-        giet_tty_giet_tty_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x\n",
+
+        giet_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x\n",
                 (unsigned int) tmp_chunk,
                 (unsigned int) ((size_align + 4) / 4),
                 (unsigned int) victim);
-#endif
 
         victim->chunk_length = tmp_chunk->chunk_length - (size_align + 4);
@@ -559,8 +586,7 @@
     /****** Write Overhead ******/
     *((unsigned int *) victim_vbase) = size_align;	// writing overhead on the first word
-#if DEBUG_MALLOC == 1
-    giet_tty_giet_tty_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n"\
+
+    giet_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n"\
             ,(unsigned int)(victim_vbase),(unsigned int )(victim_vbase + 4));
-#endif
     return (unsigned int *) (victim_vbase + 4);
 }
@@ -579,6 +605,6 @@
         ((heap_ll *) block_base)->next = 0x0;
         _heap_head[task_id] = ((heap_ll *) block_base);
-#if DEBUG_MALLOC == 1
-        giet_tty_giet_tty_printf("****** NEW BLOCK ******\n"
+
+        giet_printf("****** NEW BLOCK ******\n"
                 "@ : %x\n"
                 "Length : %d o\n"
@@ -586,5 +612,4 @@
                 block_base,
                 (unsigned int) (((heap_ll *) block_base)->chunk_length));
-#endif
         return;
     }
@@ -600,6 +625,6 @@
         /*  [.....|ptr|block|.....] */
         if ((unsigned int) (ptr_to_chunk_list) + chunk_length == block_base) {
-#if DEBUG_MALLOC == 1
-            giet_tty_giet_tty_printf("****** BLOCK MERGED ******\n"
+
+            giet_printf("****** BLOCK MERGED ******\n"
                     "CHUNK : %x of size %d o\n"
                     "merged with \n"
@@ -609,5 +634,4 @@
                     ptr_to_chunk_list->chunk_length, block_base,
                     block_length);
-#endif
             if (_prev_next_last_allocted[task_id] == ptr_to_chunk_list) {
                 _prev_next_last_allocted[task_id] = ptr_to_chunk_list->next;
@@ -636,6 +660,6 @@
             block_base = (unsigned int) ptr_to_chunk_list;
             block_length = ptr_to_chunk_list->chunk_length;
-#if DEBUG_MALLOC == 1
-            giet_tty_giet_tty_printf("****** NEW BLOCK ******\n"
+
+            giet_printf("****** NEW BLOCK ******\n"
                     "@ : %x\n"
                     "Length : %d o\n"
@@ -643,5 +667,4 @@
                     (unsigned int) ptr_to_chunk_list,
                     (unsigned int) (ptr_to_chunk_list->chunk_length));
-#endif
             prev_block_base = prev;
             prev = ptr_to_chunk_list;
@@ -653,6 +676,6 @@
         /*  [......|block|ptr|.....] */
         if (block_base + block_length == (unsigned int) ptr_to_chunk_list) {
-#if DEBUG_MALLOC == 1
-            giet_tty_giet_tty_printf("****** BLOCK MERGED ******\n"
+
+            giet_printf("****** BLOCK MERGED ******\n"
                     "BLOCK : %x of size %d o\n"
                     "merged with \n"
@@ -662,5 +685,4 @@
                     (unsigned int) ptr_to_chunk_list,
                     ptr_to_chunk_list->chunk_length);
-#endif
 
             if (_prev_next_last_allocted[task_id] == ptr_to_chunk_list) {
@@ -698,6 +720,6 @@
             ptr_to_chunk_list = ((heap_ll *) block_base);
             block_length = ptr_to_chunk_list->chunk_length;
-#if DEBUG_MALLOC == 1
-            giet_tty_giet_tty_printf("****** NEW BLOCK ******\n"
+
+            giet_printf("****** NEW BLOCK ******\n"
                     "@ : %x\n"
                     "Length : %d o\n"
@@ -705,5 +727,4 @@
                     (unsigned int) ptr_to_chunk_list,
                     (unsigned int) (ptr_to_chunk_list->chunk_length));
-#endif
             prev_block_base = prev;
             block_merged = 1;
@@ -725,6 +746,6 @@
     prev->next = ((heap_ll *) block_base);
 
-#if DEBUG_MALLOC == 1
-    giet_tty_giet_tty_printf("****** NEW BLOCK ******\n"
+
+    giet_printf("****** NEW BLOCK ******\n"
             "@ : %x\n"
             "Length : %d o\n"
@@ -732,52 +753,30 @@
             block_base,
             (unsigned int) (((heap_ll *) block_base)->chunk_length));
-#endif
     return;
 }
 
 
-void print_all_heap(void) {
-    int task_id = giet_global_task_id();
-    heap_ll * ptr_to_chunk_list = _heap_head[task_id];
-    giet_tty_printf("################### PRINT ALL HEAP ######################\n");
-    while (ptr_to_chunk_list != 0x0) {
-        giet_tty_printf("CHUNK at @ : %x\n"
-                "OVERHEAD : %d\n"
-                "NEXT : %x\n\n",
-                (unsigned int) ptr_to_chunk_list,
-                ptr_to_chunk_list->chunk_length,
-                (unsigned int) ptr_to_chunk_list->next);
-        ptr_to_chunk_list = ptr_to_chunk_list->next;
-    }
-    giet_tty_printf("#########################################################\n");
-}
-
-
-void frag(int turn) {
-    int task_id = giet_global_task_id();
-    int nb_blocks = 0;
-    int size = 0;
-    heap_ll * ptr_to_chunk;
-    ptr_to_chunk = _heap_head[task_id];
-    while (ptr_to_chunk != 0x0) {
-        size += ptr_to_chunk->chunk_length;
-        nb_blocks += 1;
-        ptr_to_chunk = ptr_to_chunk->next;
-    }
-    //   assert(size != 0 || nb_blocks == 0);
-    if (size != 0) {
-        giet_tty_printf("%.3f\t%d\n", size / ((double) nb_blocks), turn);
-    }
-}
-
-
-
+
+/*########################################################################*/
+/**************************************************************************/
+/**********                 END NEXT-FIT                        ***********/
+/**************************************************************************/
+/*########################################################################*/
 
 #else // Default case : New Fit
+
+/*########################################################################*/
+/**************************************************************************/
+/**********                    CLOSE-FIT                        ***********/
+/**************************************************************************/
+/*########################################################################*/
 
 
 int get_prev_fit_chunk(unsigned int size) {
     int task_id = giet_global_task_id();
-    int index = GET_TAB_INDEX(size);
+    int check_remote_free_list = 0;
+    int index;
+after_remote_list_check_c : //c for close fit
+    index = GET_TAB_INDEX(size);
     while (index != MAX_SIZE_POW2_TAB) {
         if (_pow2tab[task_id][index] != 0x0) {
@@ -786,4 +785,20 @@
         index++;
     }
+
+    if(check_remote_free_list == 0)
+    {
+        heap_ll * poped_block;
+        heap_ll * head_of_rf;
+        check_remote_free_list = 1;
+
+        head_of_rf = pop_ptr();
+
+        while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0)
+        {
+            update_chunk_list((unsigned int)poped_block, poped_block->chunk_length);
+        }
+        goto after_remote_list_check_c; 
+    }
+
     return -1;
 }
@@ -793,17 +808,14 @@
 void * malloc(unsigned int size) {
     int task_id = giet_global_task_id();
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("############ MALLOC ############\n\n");
-#endif
+
+    giet_printf("############ MALLOC ############\n\n");
     if(size == 0) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf(" SIZE = 0 \n");
-#endif
+
+        giet_printf(" SIZE = 0 \n");
         return 0x0;
     }
     if (size > 0x20000000) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("ERROR max size = %d\n",0x20000000); 
-#endif
+
+        giet_printf("ERROR max size = %d\n",0x20000000); 
         return 0x0;
     }
@@ -817,5 +829,5 @@
 
         if (_heap_length[task_id] == 0) {
-            giet_tty_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id);
+            giet_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id);
             return 0x0;
         }
@@ -838,7 +850,6 @@
     }
 
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("Looking for size : %d %d = %d\n", size_align, 4,size_align+4);
-#endif
+
+    giet_printf("Looking for size : %d %d = %d\n", size_align, 4,size_align+4);
 
 
@@ -871,19 +882,17 @@
         _pow2tab[task_id][new_index] = new_chunk;
 
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x : @ of size over the block : %x\n",
+
+        giet_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x : @ of size over the block : %x\n",
                 (unsigned int) victim,
                 (unsigned int) ((size_align + 4) / 4),
                 (unsigned int) new_chunk,
                 ((unsigned int) new_chunk + new_chunk->chunk_length) - 4);
-#endif
     }
 
     /****** Write Overhead ******/
     *((unsigned int *) victim_vbase) = size_align;	// writing overhead on the first word
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n",\
+
+    giet_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n",\
             (unsigned int)(victim_vbase), (unsigned int ) (victim_vbase + 4));
-#endif
 
     return (unsigned int *) (victim_vbase + 4);
@@ -910,7 +919,6 @@
     if (block_base - 4 >= _heap_base[task_id]) {
 
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("############# LEFT MERGE #############\n\n");
-#endif
+
+        giet_printf("############# LEFT MERGE #############\n\n");
         left_block_size = *((unsigned int *) (block_base - 4));
         left_block_index = GET_TAB_INDEX(left_block_size);
@@ -920,7 +928,6 @@
 
     if ((block_base + block_length) <= (_heap_base[task_id] + _heap_length[task_id]) - 8) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("############# RIGHT MERGE #############\n\n");
-#endif
+
+        giet_printf("############# RIGHT MERGE #############\n\n");
         right_block_size = ((heap_ll *)(block_base + block_length))->chunk_length;
         right_block_index = GET_TAB_INDEX(right_block_size);
@@ -929,7 +936,6 @@
     }
 
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("############# END PREP MERGE #############\n\n");
-#endif
+
+    giet_printf("############# END PREP MERGE #############\n\n");
 
     heap_ll * ptr_to_chunk = 0x0;
@@ -1012,92 +1018,148 @@
     _pow2tab[task_id][new_index] = new_addr;
 
+    giet_printf("########################  UPDT SUCCESS ######################\n");
+
     return;
 }
 
 
-static void print_all_heap(void) {
+
+
+/*########################################################################*/
+/**************************************************************************/
+/**********                 END CLOSE-FIT                       ***********/
+/**************************************************************************/
+/*########################################################################*/
+
+#endif // New Fit
+
+heap_ll * pop_remote_free_list(heap_ll ** head)
+{
+
+    heap_ll * poped_block;
+
+    if (*head == 0x0)
+    {
+        giet_printf(" no block to pop\n");
+        return 0x0;
+    }
+    else
+    {
+        poped_block = *head;
+        *head = (*head)->next;
+
+        giet_printf(" : @%x, size : %x\n", poped_block, poped_block->chunk_length);
+    }
+
+    return poped_block;
+}
+
+heap_ll * pop_ptr(void)
+{
     int task_id = giet_global_task_id();
+    heap_ll * head_of_rf_list;
+    giet_printf(" -- POP lock acquire -- for %d : @ : %x\n", task_id, &lock_rf_list[task_id]);
+    lock_acquire(&lock_rf_list[task_id]);
+    giet_printf(" -- POP lock acquired --\n");
+
+    head_of_rf_list = _remote_free_list[task_id];
+    _remote_free_list[task_id] = 0x0;
+
+    lock_release(&lock_rf_list[task_id]);
+    return head_of_rf_list;
+
+}
+
+void insert_in_remote_free_list(unsigned int remote_owner_id, unsigned int block_base, unsigned int block_length)
+{
+
+    giet_printf("############### INSERT \n");
+    ((heap_ll *)block_base)->chunk_length = block_length;
+
+    giet_printf(" -- INSERT lock acquire -- for %d : @ : %x\n", remote_owner_id, &lock_rf_list[remote_owner_id]);
+    lock_acquire(&lock_rf_list[remote_owner_id]);
+
+    giet_printf(" -- INSERT lock acquired -- \n");
+
+    // insertion en dÃ©but de liste
+    giet_printf("  in remote_owner %d ...\n", remote_owner_id);
+    if (_remote_free_list[remote_owner_id] == 0x0)
+    {
+        ((heap_ll *)block_base)->next = 0x0;
+    } 
+    else
+    {
+        ((heap_ll *)block_base)->next = _remote_free_list[remote_owner_id];
+    }
+    _remote_free_list[remote_owner_id] = ((heap_ll *)block_base);
+
+    giet_printf("  : OK\n\n");
+    giet_printf(" new head : %x : of size : %x, with next : %x\n", _remote_free_list[remote_owner_id], _remote_free_list[remote_owner_id]->chunk_length, _remote_free_list[remote_owner_id]->next);
+
+    lock_release(&lock_rf_list[remote_owner_id]);
+
+    return;
+}
+
+void free(void * ptr) {
     int i = 0;
-    int nb_blocks = 0;
-    heap_ll * ptr_to_chunk_list;
-    giet_tty_printf("################### PRINT ALL HEAP ######################\n");
-    while (i != MAX_SIZE_POW2_TAB) {
-        ptr_to_chunk_list = _pow2tab[task_id][i];
-
-        if (ptr_to_chunk_list != 0x0) {
-            giet_tty_printf("###################### %d ####################\n", i);
-        }
-        while (ptr_to_chunk_list != 0x0) {
-            giet_tty_printf("CHUNK at @ : %x\n"
-                    "OVERHEAD : %d\n"
-                    "NEXT : %x\n\n",
-                    (unsigned int) ptr_to_chunk_list,
-                    ptr_to_chunk_list->chunk_length,
-                    (unsigned int) ptr_to_chunk_list->next);
-            ptr_to_chunk_list = ptr_to_chunk_list->next;
-            nb_blocks++;
+    while (i != NB_TASKS_MAX)
+    {
+        if( _heap_base[i] == -1)
+        {
+            continue;
+        }
+        if ((unsigned int)ptr > _heap_base[i] && (unsigned int)ptr < _heap_base[i] + _heap_length[i])
+        {
+            break;
         }
         i++;
     }
-    giet_tty_printf("####################### ##%d## ##########################\n", nb_blocks);
-}
-
-
-static void frag(int turn) {
-    int task_id = giet_global_task_id();
-    int i = 0;
-    int nb_blocks = 0;
-    int size = 0;
-    heap_ll * ptr_to_chunk;
-    while (i != MAX_SIZE_POW2_TAB) {
-        ptr_to_chunk = _pow2tab[task_id][i];
-        while (ptr_to_chunk != 0x0) {
-            size += ptr_to_chunk->chunk_length;
-            nb_blocks += 1;
-            ptr_to_chunk = ptr_to_chunk->next;
-        }
-        i++;
-    }
-    if (size != 0) {
-        giet_tty_printf("%.3f\t%d\n",size / ((double) nb_blocks), turn);
-    }
-}
-
-
-
-#endif // New Fit
-
-
-void free(void * ptr) {
+
     int task_id = giet_global_task_id();
     unsigned int * to_free = ptr;
-    unsigned int heapB = _heap_base[task_id];
-    unsigned int heapL = _heap_length[task_id];
+    unsigned int heapB;
+    unsigned int heapL;
     unsigned int overhead;
     unsigned int block_base;
     unsigned int block_length;
-
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("############# FREE  #############\n\n");
-#endif
+    unsigned int remote_free_needed;
+    unsigned int remote_owner_id;
+    if (i == task_id)
+    {
+
+        giet_printf("############# FREE  #############\n\n");
+        remote_free_needed = 0;
+        remote_owner_id = -1;
+        heapB = _heap_base[task_id];
+        heapL = _heap_length[task_id];
+    }
+    else
+    {
+
+        giet_printf("############# REMOTE FREE  #############\n\n");
+        remote_free_needed = 1;
+        remote_owner_id = i;
+        heapB = _heap_base[remote_owner_id];
+        heapL = _heap_length[remote_owner_id];
+    }
+
+
     /****** CHECK PTR ******/
     if (to_free == 0x0) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("free == 0x0\n");
-#endif
+
+        giet_printf("free == 0x0\n");
         return;
     }
     // check alignement of ptr
     if (((unsigned int) to_free) % 4 != 0) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("allignement of ptr  %x = %d\n", (unsigned int) to_free, (unsigned int) to_free % 4);
-#endif
+
+        giet_printf("allignement of ptr  %x = %d\n", (unsigned int) to_free, (unsigned int) to_free % 4);
         return;
     }
     // check if the @ of ptr matches the range of the heap
     if (!((unsigned int) to_free >= heapB && (unsigned int) to_free < heapB + heapL)) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("check if it matches the range of the heap\n");
-#endif
+
+        giet_printf("check if it matches the range of the heap\n");
         return;
     }
@@ -1107,14 +1169,12 @@
     // check alignement of overhead
     if (overhead % 4 != 0) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf("check alignement of overhead\n");
-#endif
+
+        giet_printf("check alignement of overhead\n");
         return;
     }
     // check if (overhead + to_free) matches the range of the heap
     if ((((unsigned int) (to_free) + overhead) - 4) > heapB + heapL) {
-#if DEBUG_MALLOC == 1
-        giet_tty_printf ("check if (overhead + to_free) matches the range of the heap\n");
-#endif
+
+        giet_printf ("check if (overhead + to_free) matches the range of the heap\n");
         return;
     }
@@ -1123,12 +1183,19 @@
     block_length = overhead + 4;
 
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("############# UPDATE CL :  FREE %x of size %d #############\n\n",block_base, block_length);
-#endif
-    update_chunk_list(block_base, block_length);
-
-#if DEBUG_MALLOC == 1
-    giet_tty_printf("BLOCK SUCCESSFULLY FREED\n\n");
-#endif
+    if (remote_free_needed == 0)
+    {
+
+        giet_printf("############# UPDATE CL :  FREE %x of size %d #############\n\n",block_base, block_length);
+        update_chunk_list(block_base, block_length);
+    }
+    else
+    {
+
+        giet_printf("############# INSERT IN RF %d :  FREE %x of size %d #############\n\n", remote_owner_id, block_base, block_length);
+        insert_in_remote_free_list(remote_owner_id, block_base, block_length);
+    }
+
+
+    giet_printf("BLOCK SUCCESSFULLY FREED\n\n");
 
     return;
