Changeset 244 for soft/giet_vm/libs
- Timestamp:
- Jun 6, 2013, 5:39:18 PM (12 years ago)
- Location:
- soft/giet_vm/libs
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/libs/malloc.c
r239 r244 6 6 heap_ll * _heap_head[NB_TASKS_MAX]; 7 7 8 unsigned int _heap_base[NB_TASKS_MAX]; 9 unsigned int _heap_length[NB_TASKS_MAX]; 8 heap_ll * _remote_free_list[NB_TASKS_MAX] = { 0 }; 9 giet_lock_t lock_rf_list[NB_TASKS_MAX] = { {"toto",0} }; 10 11 unsigned int _heap_base[NB_TASKS_MAX] = { -1 }; 12 unsigned int _heap_length[NB_TASKS_MAX] = { -1 }; 10 13 11 14 int _first_malloc[NB_TASKS_MAX] = { 0 }; … … 15 18 16 19 #elif MALLOC_SELECTED == 2 17 20 heap_ll * _prev_next_last_allocted[NB_TASKS_MAX] = { 0 }; 18 21 #else 19 20 #define GET_TAB_INDEX(size)(size == 0x00000008) ? 0 :\21 (size <= 0x00000010) ? 1 :\22 (size <= 0x00000020) ? 2 :\23 (size <= 0x00000040) ? 3 :\24 (size <= 0x00000080) ? 4 :\25 (size <= 0x00000100) ? 5 :\26 (size <= 0x00000200) ? 6 :\27 (size <= 0x00000400) ? 7 :\28 (size <= 0x00000800) ? 8 :\29 (size <= 0x00001000) ? 9 :\30 (size <= 0x00002000) ? 10 :\31 (size <= 0x00004000) ? 11 :\32 (size <= 0x00008000) ? 12 :\33 (size <= 0x00010000) ? 13 :\34 (size <= 0x00020000) ? 14 :\35 (size <= 0x00040000) ? 15 :\36 (size <= 0x00080000) ? 16 :\37 (size <= 0x00100000) ? 17 :\38 (size <= 0x00200000) ? 18 :\39 (size <= 0x00400000) ? 19 :\40 (size <= 0x00800000) ? 20 :\41 (size <= 0x01000000) ? 21 :\42 (size <= 0x02000000) ? 22 :\43 (size <= 0x04000000) ? 23 :\44 (size <= 0x08000000) ? 24 :\45 (size <= 0x10000000) ? 25 :\46 (size <= 0x20000000) ? 26 :\47 2748 49 22 #define MAX_SIZE_POW2_TAB 28 23 #define GET_TAB_INDEX(size) (size == 0x00000008) ? 0 :\ 24 (size <= 0x00000010) ? 1 :\ 25 (size <= 0x00000020) ? 2 :\ 26 (size <= 0x00000040) ? 3 :\ 27 (size <= 0x00000080) ? 4 :\ 28 (size <= 0x00000100) ? 5 :\ 29 (size <= 0x00000200) ? 6 :\ 30 (size <= 0x00000400) ? 7 :\ 31 (size <= 0x00000800) ? 8 :\ 32 (size <= 0x00001000) ? 9 :\ 33 (size <= 0x00002000) ? 10 :\ 34 (size <= 0x00004000) ? 11 :\ 35 (size <= 0x00008000) ? 12 :\ 36 (size <= 0x00010000) ? 13 :\ 37 (size <= 0x00020000) ? 14 :\ 38 (size <= 0x00040000) ? 15 :\ 39 (size <= 0x00080000) ? 16 :\ 40 (size <= 0x00100000) ? 17 :\ 41 (size <= 0x00200000) ? 18 :\ 42 (size <= 0x00400000) ? 19 :\ 43 (size <= 0x00800000) ? 20 :\ 44 (size <= 0x01000000) ? 21 :\ 45 (size <= 0x02000000) ? 22 :\ 46 (size <= 0x04000000) ? 23 :\ 47 (size <= 0x08000000) ? 24 :\ 48 (size <= 0x10000000) ? 25 :\ 49 (size <= 0x20000000) ? 26 :\ 50 27 51 52 heap_ll * _pow2tab[NB_TASKS_MAX][MAX_SIZE_POW2_TAB] = {{ 0 }}; 50 53 #endif 51 54 52 /* Todo: meilleure gestion des printf 53 //#if DEBUG_MALLOC == 1 54 unsigned int debug_level = 2; 55 #elif DEBUG_MALLOC == 0 56 57 #define giet_printf(level, ...) \ 58 ({ \ 59 if (debug_level < level) { \ 60 giet_tty_printf(__VA_ARGS__); \ 61 } \ 62 }) 63 */ 55 56 #if DEBUG_MALLOC == 1 57 #define giet_printf(...) \ 58 ({ \ 59 giet_tty_printf(__VA_ARGS__); \ 60 }) 61 #else 62 #define giet_printf(...) ({ }) 63 #endif 64 64 65 65 66 66 67 #if MALLOC_SELECTED == 1 67 68 /*########################################################################*/ 69 /**************************************************************************/ 70 /********** WORST-FIT ***********/ 71 /**************************************************************************/ 72 /*########################################################################*/ 68 73 heap_ll *get_prev_fit_chunk(unsigned int size) { 69 74 int task_id = giet_global_task_id(); 75 int check_remote_free_list = 0; 76 after_remote_list_check : 70 77 if (_heap_head[task_id] == 0x0) { 78 if(check_remote_free_list == 0) 79 { 80 heap_ll * poped_block; 81 heap_ll * head_of_rf; 82 check_remote_free_list = 1; 83 84 head_of_rf = pop_ptr(); 85 86 while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0) 87 { 88 update_chunk_list((unsigned int)poped_block, poped_block->chunk_length); 89 } 90 goto after_remote_list_check; 91 } 71 92 return ((heap_ll *) 0x01); 72 93 } … … 87 108 ptr_to_chunk_list = ptr_to_chunk_list->next; 88 109 } 89 if (found == 1) { 110 if (found == 1) 111 { 90 112 return prev_tmp; // case when prev_tmp == 0x0 is handled in the calling function 91 113 } 92 else { 114 else 115 { 116 if(check_remote_free_list == 0) 117 { 118 heap_ll * poped_block; 119 heap_ll * head_of_rf; 120 check_remote_free_list = 1; 121 122 head_of_rf = pop_ptr(); 123 124 while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0) 125 { 126 update_chunk_list((unsigned int)poped_block, poped_block->chunk_length); 127 } 128 goto after_remote_list_check; 129 } 93 130 return (heap_ll *) 0x1; 94 131 } … … 100 137 int task_id = giet_global_task_id(); 101 138 102 #if DEBUG_MALLOC == 1 103 giet_tty_printf("############ MALLOC ############\n\n"); 104 #endif 139 giet_printf("############ MALLOC ############\n\n"); 105 140 if (size == 0) { 106 #if DEBUG_MALLOC == 1 107 giet_tty_printf(" SIZE = 0 \n"); 108 #endif 141 142 giet_printf(" SIZE = 0 \n"); 109 143 return 0x0; 110 144 } … … 115 149 116 150 if (_heap_length[task_id] == 0) { 117 giet_ tty_printf("*** Error: Malloc called in task %d whileno heap was defined.\n", task_id);151 giet_printf("*** Error: Malloc called in task %d and no heap was defined.\n", task_id); 118 152 return 0x0; 119 153 } … … 133 167 134 168 135 #if DEBUG_MALLOC == 1 136 giet_tty_printf("Looking for size : %d\n", size_align + 4); 137 #endif 169 170 giet_printf("Looking for size : %d\n", size_align + 4); 138 171 /****** Get prev victim from the chunks list ******/ 139 172 heap_ll *victim; … … 171 204 heap_ll * tmp_chunk = victim; 172 205 victim = (heap_ll *) (((unsigned int) victim) + (size_align + 4)); 173 #if DEBUG_MALLOC == 1 174 giet_ tty_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x\n",206 207 giet_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x\n", 175 208 (unsigned int) tmp_chunk, 176 209 (unsigned int) ((size_align + 4) / 4), 177 210 (unsigned int) victim); 178 #endif179 211 180 212 victim->chunk_length = tmp_chunk->chunk_length - (size_align + 4); … … 190 222 /****** Write Overhead ******/ 191 223 *((unsigned int *) victim_vbase) = size_align; // writing overhead on the first word 192 #if DEBUG_MALLOC == 1 193 giet_ tty_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n"\224 225 giet_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n"\ 194 226 ,(unsigned int)(victim_vbase),(unsigned int) (victim_vbase + 4)); 195 #endif196 227 return (unsigned int *) (victim_vbase + 4); 197 228 } … … 210 241 ((heap_ll *) block_base)->next = 0x0; 211 242 _heap_head[task_id] = ((heap_ll *) block_base); 212 #if DEBUG_MALLOC == 1 213 giet_ tty_printf("****** NEW BLOCK ******\n"243 244 giet_printf("****** NEW BLOCK ******\n" 214 245 "@ : %x\n" 215 246 "Length : %d o\n" … … 217 248 block_base, 218 249 (unsigned int) (((heap_ll *) block_base)->chunk_length)); 219 #endif220 250 return; 221 251 } … … 232 262 /* [.....|ptr|block|.....] */ 233 263 if ((unsigned int) (ptr_to_chunk_list) + chunk_length == block_base) { 234 #if DEBUG_MALLOC == 1 235 giet_ tty_printf("****** BLOCK MERGED ******\n"264 265 giet_printf("****** BLOCK MERGED ******\n" 236 266 "CHUNK : %x of size %d o\n" 237 267 "merged with \n" … … 241 271 ptr_to_chunk_list->chunk_length, block_base, 242 272 block_length); 243 #endif244 273 245 274 /* update the size */ … … 264 293 block_base = (unsigned int) ptr_to_chunk_list; 265 294 block_length = ptr_to_chunk_list->chunk_length; 266 #if DEBUG_MALLOC == 1 267 giet_ tty_printf("****** NEW BLOCK ******\n"295 296 giet_printf("****** NEW BLOCK ******\n" 268 297 "@ : %x\n" 269 298 "Length : %d o\n" … … 271 300 (unsigned int) ptr_to_chunk_list, 272 301 (unsigned int) (ptr_to_chunk_list->chunk_length)); 273 #endif274 302 prev_block_base = prev; 275 303 prev = ptr_to_chunk_list; … … 281 309 /* [......|block|ptr|.....] */ 282 310 if (block_base + block_length == (unsigned int) ptr_to_chunk_list) { 283 #if DEBUG_MALLOC == 1 284 giet_ tty_printf("****** BLOCK MERGED ******\n"311 312 giet_printf("****** BLOCK MERGED ******\n" 285 313 "BLOCK : %x of size %d o\n" 286 314 "merged with \n" … … 290 318 (unsigned int) ptr_to_chunk_list, 291 319 ptr_to_chunk_list->chunk_length); 292 #endif293 320 294 321 /* update the size */ … … 316 343 ptr_to_chunk_list = ((heap_ll *) block_base); 317 344 block_length = ptr_to_chunk_list->chunk_length; 318 #if DEBUG_MALLOC == 1 319 giet_ tty_printf("****** NEW BLOCK ******\n"345 346 giet_printf("****** NEW BLOCK ******\n" 320 347 "@ : %x\n" 321 348 "Length : %d o\n" … … 323 350 (unsigned int) ptr_to_chunk_list, 324 351 (unsigned int) (ptr_to_chunk_list->chunk_length)); 325 #endif326 352 prev_block_base = prev; 327 353 block_merged = 1; … … 339 365 ((heap_ll *) block_base)->next = 0x0; 340 366 prev->next = ((heap_ll *) block_base); 341 #if DEBUG_MALLOC == 1 342 giet_ tty_printf("****** NEW BLOCK ******\n"367 368 giet_printf("****** NEW BLOCK ******\n" 343 369 "@ : %x\n" 344 370 "Length : %d o\n" … … 346 372 block_base, 347 373 (unsigned int) (((heap_ll *) block_base)->chunk_length)); 348 #endif349 374 return; 350 375 … … 352 377 353 378 354 static void print_all_heap(void) { 355 int task_id = giet_global_task_id(); 356 heap_ll * ptr_to_chunk_list = _heap_head[task_id]; 357 giet_tty_printf("################### PRINT ALL HEAP ######################\n"); 358 while (ptr_to_chunk_list != 0x0) { 359 giet_tty_printf("CHUNK at @ : %x\n" 360 "OVERHEAD : %d\n" 361 "NEXT : %x\n\n", 362 (unsigned int) ptr_to_chunk_list, 363 ptr_to_chunk_list->chunk_length, 364 (unsigned int) ptr_to_chunk_list->next); 365 ptr_to_chunk_list = ptr_to_chunk_list->next; 366 } 367 giet_tty_printf("#########################################################\n"); 368 } 369 370 371 /* Warning: the division doesn't seem to work well in the giet/soclib ? */ 372 static void frag(int turn) { 373 int task_id = giet_global_task_id(); 374 int nb_blocks = 0; 375 int size = 0; 376 heap_ll * ptr_to_chunk; 377 ptr_to_chunk = _heap_head[task_id]; 378 while(ptr_to_chunk != 0x0) { 379 size += ptr_to_chunk->chunk_length; 380 nb_blocks += 1; 381 ptr_to_chunk = ptr_to_chunk->next; 382 } 383 if (size != 0) { 384 giet_tty_printf("%.3f\t%d\n", size / ((double) nb_blocks), turn); 385 } 386 } 387 388 // End WORST_FIT 379 380 /*########################################################################*/ 381 /**************************************************************************/ 382 /********** END WORST-FIT ***********/ 383 /**************************************************************************/ 384 /*########################################################################*/ 389 385 #elif MALLOC_SELECTED == 2 // Next Fit 386 /*########################################################################*/ 387 /**************************************************************************/ 388 /********** NEXT-FIT ***********/ 389 /**************************************************************************/ 390 /*########################################################################*/ 390 391 391 392 heap_ll * get_prev_fit_chunk(unsigned int size) { 392 393 int task_id = giet_global_task_id(); 394 int check_remote_free_list = 0; 395 after_remote_list_check_n : //n for next_fit 393 396 if (_heap_head[task_id] == 0x0) { 397 if(check_remote_free_list == 0) 398 { 399 heap_ll * poped_block; 400 heap_ll * head_of_rf; 401 check_remote_free_list = 1; 402 403 head_of_rf = pop_ptr(); 404 405 while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0) 406 { 407 update_chunk_list((unsigned int)poped_block, poped_block->chunk_length); 408 } 409 goto after_remote_list_check_n; 410 } 394 411 return ((heap_ll *) 0x1); 395 412 } … … 421 438 ptr_to_chunk_list = ptr_to_chunk_list->next; 422 439 } while (ptr_to_chunk_list != tmp_turn); 440 if(check_remote_free_list == 0) 441 { 442 heap_ll * poped_block; 443 heap_ll * head_of_rf; 444 check_remote_free_list = 1; 445 446 head_of_rf = pop_ptr(); 447 448 while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0) 449 { 450 update_chunk_list((unsigned int)poped_block, poped_block->chunk_length); 451 } 452 goto after_remote_list_check_n; 453 } 423 454 424 455 return (heap_ll *) 0x1; … … 429 460 430 461 int task_id = giet_global_task_id(); 431 #if DEBUG_MALLOC == 1 432 giet_tty_giet_tty_printf("############ MALLOC ############\n\n"); 433 #endif 462 463 giet_printf("############ MALLOC ############\n\n"); 434 464 if (size == 0) { 435 #if DEBUG_MALLOC == 1 436 giet_tty_giet_tty_printf(" SIZE = 0 \n"); 437 #endif 465 466 giet_printf(" SIZE = 0 \n"); 438 467 return 0x0; 439 468 } … … 444 473 445 474 if (_heap_length[task_id] == 0) { 446 giet_ tty_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id);475 giet_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id); 447 476 return 0x0; 448 477 } … … 464 493 465 494 466 #if DEBUG_MALLOC == 1 467 giet_tty_giet_tty_printf("Looking for size : %d\n", size_align + 4); 468 #endif 495 496 giet_printf("Looking for size : %d\n", size_align + 4); 469 497 /****** Get prev victim from the chunks list ******/ 470 498 heap_ll *victim; … … 528 556 heap_ll * tmp_chunk = victim; 529 557 victim = (heap_ll *) (((unsigned int) victim) + (size_align + 4)); 530 #if DEBUG_MALLOC == 1 531 giet_ tty_giet_tty_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x\n",558 559 giet_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x\n", 532 560 (unsigned int) tmp_chunk, 533 561 (unsigned int) ((size_align + 4) / 4), 534 562 (unsigned int) victim); 535 #endif536 563 537 564 victim->chunk_length = tmp_chunk->chunk_length - (size_align + 4); … … 559 586 /****** Write Overhead ******/ 560 587 *((unsigned int *) victim_vbase) = size_align; // writing overhead on the first word 561 #if DEBUG_MALLOC == 1 562 giet_ tty_giet_tty_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n"\588 589 giet_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n"\ 563 590 ,(unsigned int)(victim_vbase),(unsigned int )(victim_vbase + 4)); 564 #endif565 591 return (unsigned int *) (victim_vbase + 4); 566 592 } … … 579 605 ((heap_ll *) block_base)->next = 0x0; 580 606 _heap_head[task_id] = ((heap_ll *) block_base); 581 #if DEBUG_MALLOC == 1 582 giet_ tty_giet_tty_printf("****** NEW BLOCK ******\n"607 608 giet_printf("****** NEW BLOCK ******\n" 583 609 "@ : %x\n" 584 610 "Length : %d o\n" … … 586 612 block_base, 587 613 (unsigned int) (((heap_ll *) block_base)->chunk_length)); 588 #endif589 614 return; 590 615 } … … 600 625 /* [.....|ptr|block|.....] */ 601 626 if ((unsigned int) (ptr_to_chunk_list) + chunk_length == block_base) { 602 #if DEBUG_MALLOC == 1 603 giet_ tty_giet_tty_printf("****** BLOCK MERGED ******\n"627 628 giet_printf("****** BLOCK MERGED ******\n" 604 629 "CHUNK : %x of size %d o\n" 605 630 "merged with \n" … … 609 634 ptr_to_chunk_list->chunk_length, block_base, 610 635 block_length); 611 #endif612 636 if (_prev_next_last_allocted[task_id] == ptr_to_chunk_list) { 613 637 _prev_next_last_allocted[task_id] = ptr_to_chunk_list->next; … … 636 660 block_base = (unsigned int) ptr_to_chunk_list; 637 661 block_length = ptr_to_chunk_list->chunk_length; 638 #if DEBUG_MALLOC == 1 639 giet_ tty_giet_tty_printf("****** NEW BLOCK ******\n"662 663 giet_printf("****** NEW BLOCK ******\n" 640 664 "@ : %x\n" 641 665 "Length : %d o\n" … … 643 667 (unsigned int) ptr_to_chunk_list, 644 668 (unsigned int) (ptr_to_chunk_list->chunk_length)); 645 #endif646 669 prev_block_base = prev; 647 670 prev = ptr_to_chunk_list; … … 653 676 /* [......|block|ptr|.....] */ 654 677 if (block_base + block_length == (unsigned int) ptr_to_chunk_list) { 655 #if DEBUG_MALLOC == 1 656 giet_ tty_giet_tty_printf("****** BLOCK MERGED ******\n"678 679 giet_printf("****** BLOCK MERGED ******\n" 657 680 "BLOCK : %x of size %d o\n" 658 681 "merged with \n" … … 662 685 (unsigned int) ptr_to_chunk_list, 663 686 ptr_to_chunk_list->chunk_length); 664 #endif665 687 666 688 if (_prev_next_last_allocted[task_id] == ptr_to_chunk_list) { … … 698 720 ptr_to_chunk_list = ((heap_ll *) block_base); 699 721 block_length = ptr_to_chunk_list->chunk_length; 700 #if DEBUG_MALLOC == 1 701 giet_ tty_giet_tty_printf("****** NEW BLOCK ******\n"722 723 giet_printf("****** NEW BLOCK ******\n" 702 724 "@ : %x\n" 703 725 "Length : %d o\n" … … 705 727 (unsigned int) ptr_to_chunk_list, 706 728 (unsigned int) (ptr_to_chunk_list->chunk_length)); 707 #endif708 729 prev_block_base = prev; 709 730 block_merged = 1; … … 725 746 prev->next = ((heap_ll *) block_base); 726 747 727 #if DEBUG_MALLOC == 1 728 giet_ tty_giet_tty_printf("****** NEW BLOCK ******\n"748 749 giet_printf("****** NEW BLOCK ******\n" 729 750 "@ : %x\n" 730 751 "Length : %d o\n" … … 732 753 block_base, 733 754 (unsigned int) (((heap_ll *) block_base)->chunk_length)); 734 #endif735 755 return; 736 756 } 737 757 738 758 739 void print_all_heap(void) { 740 int task_id = giet_global_task_id(); 741 heap_ll * ptr_to_chunk_list = _heap_head[task_id]; 742 giet_tty_printf("################### PRINT ALL HEAP ######################\n"); 743 while (ptr_to_chunk_list != 0x0) { 744 giet_tty_printf("CHUNK at @ : %x\n" 745 "OVERHEAD : %d\n" 746 "NEXT : %x\n\n", 747 (unsigned int) ptr_to_chunk_list, 748 ptr_to_chunk_list->chunk_length, 749 (unsigned int) ptr_to_chunk_list->next); 750 ptr_to_chunk_list = ptr_to_chunk_list->next; 751 } 752 giet_tty_printf("#########################################################\n"); 753 } 754 755 756 void frag(int turn) { 757 int task_id = giet_global_task_id(); 758 int nb_blocks = 0; 759 int size = 0; 760 heap_ll * ptr_to_chunk; 761 ptr_to_chunk = _heap_head[task_id]; 762 while (ptr_to_chunk != 0x0) { 763 size += ptr_to_chunk->chunk_length; 764 nb_blocks += 1; 765 ptr_to_chunk = ptr_to_chunk->next; 766 } 767 // assert(size != 0 || nb_blocks == 0); 768 if (size != 0) { 769 giet_tty_printf("%.3f\t%d\n", size / ((double) nb_blocks), turn); 770 } 771 } 772 773 774 759 760 /*########################################################################*/ 761 /**************************************************************************/ 762 /********** END NEXT-FIT ***********/ 763 /**************************************************************************/ 764 /*########################################################################*/ 775 765 776 766 #else // Default case : New Fit 767 768 /*########################################################################*/ 769 /**************************************************************************/ 770 /********** CLOSE-FIT ***********/ 771 /**************************************************************************/ 772 /*########################################################################*/ 777 773 778 774 779 775 int get_prev_fit_chunk(unsigned int size) { 780 776 int task_id = giet_global_task_id(); 781 int index = GET_TAB_INDEX(size); 777 int check_remote_free_list = 0; 778 int index; 779 after_remote_list_check_c : //c for close fit 780 index = GET_TAB_INDEX(size); 782 781 while (index != MAX_SIZE_POW2_TAB) { 783 782 if (_pow2tab[task_id][index] != 0x0) { … … 786 785 index++; 787 786 } 787 788 if(check_remote_free_list == 0) 789 { 790 heap_ll * poped_block; 791 heap_ll * head_of_rf; 792 check_remote_free_list = 1; 793 794 head_of_rf = pop_ptr(); 795 796 while((poped_block = pop_remote_free_list(&head_of_rf)) != 0x0) 797 { 798 update_chunk_list((unsigned int)poped_block, poped_block->chunk_length); 799 } 800 goto after_remote_list_check_c; 801 } 802 788 803 return -1; 789 804 } … … 793 808 void * malloc(unsigned int size) { 794 809 int task_id = giet_global_task_id(); 795 #if DEBUG_MALLOC == 1 796 giet_tty_printf("############ MALLOC ############\n\n"); 797 #endif 810 811 giet_printf("############ MALLOC ############\n\n"); 798 812 if(size == 0) { 799 #if DEBUG_MALLOC == 1 800 giet_tty_printf(" SIZE = 0 \n"); 801 #endif 813 814 giet_printf(" SIZE = 0 \n"); 802 815 return 0x0; 803 816 } 804 817 if (size > 0x20000000) { 805 #if DEBUG_MALLOC == 1 806 giet_tty_printf("ERROR max size = %d\n",0x20000000); 807 #endif 818 819 giet_printf("ERROR max size = %d\n",0x20000000); 808 820 return 0x0; 809 821 } … … 817 829 818 830 if (_heap_length[task_id] == 0) { 819 giet_ tty_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id);831 giet_printf("*** Error: Malloc called in task %d while no heap was defined.\n", task_id); 820 832 return 0x0; 821 833 } … … 838 850 } 839 851 840 #if DEBUG_MALLOC == 1 841 giet_tty_printf("Looking for size : %d %d = %d\n", size_align, 4,size_align+4); 842 #endif 852 853 giet_printf("Looking for size : %d %d = %d\n", size_align, 4,size_align+4); 843 854 844 855 … … 871 882 _pow2tab[task_id][new_index] = new_chunk; 872 883 873 #if DEBUG_MALLOC == 1 874 giet_ tty_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x : @ of size over the block : %x\n",884 885 giet_printf("NEXT CHUNK is at @ : 0x%x + 0x%x = 0x%x : @ of size over the block : %x\n", 875 886 (unsigned int) victim, 876 887 (unsigned int) ((size_align + 4) / 4), 877 888 (unsigned int) new_chunk, 878 889 ((unsigned int) new_chunk + new_chunk->chunk_length) - 4); 879 #endif880 890 } 881 891 882 892 /****** Write Overhead ******/ 883 893 *((unsigned int *) victim_vbase) = size_align; // writing overhead on the first word 884 #if DEBUG_MALLOC == 1 885 giet_ tty_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n",\894 895 giet_printf("BLOCK SUCCESSFULLY ALLOCATED at @ 0x%x user will write at @ 0x%x\n\n",\ 886 896 (unsigned int)(victim_vbase), (unsigned int ) (victim_vbase + 4)); 887 #endif888 897 889 898 return (unsigned int *) (victim_vbase + 4); … … 910 919 if (block_base - 4 >= _heap_base[task_id]) { 911 920 912 #if DEBUG_MALLOC == 1 913 giet_tty_printf("############# LEFT MERGE #############\n\n"); 914 #endif 921 922 giet_printf("############# LEFT MERGE #############\n\n"); 915 923 left_block_size = *((unsigned int *) (block_base - 4)); 916 924 left_block_index = GET_TAB_INDEX(left_block_size); … … 920 928 921 929 if ((block_base + block_length) <= (_heap_base[task_id] + _heap_length[task_id]) - 8) { 922 #if DEBUG_MALLOC == 1 923 giet_tty_printf("############# RIGHT MERGE #############\n\n"); 924 #endif 930 931 giet_printf("############# RIGHT MERGE #############\n\n"); 925 932 right_block_size = ((heap_ll *)(block_base + block_length))->chunk_length; 926 933 right_block_index = GET_TAB_INDEX(right_block_size); … … 929 936 } 930 937 931 #if DEBUG_MALLOC == 1 932 giet_tty_printf("############# END PREP MERGE #############\n\n"); 933 #endif 938 939 giet_printf("############# END PREP MERGE #############\n\n"); 934 940 935 941 heap_ll * ptr_to_chunk = 0x0; … … 1012 1018 _pow2tab[task_id][new_index] = new_addr; 1013 1019 1020 giet_printf("######################## UPDT SUCCESS ######################\n"); 1021 1014 1022 return; 1015 1023 } 1016 1024 1017 1025 1018 static void print_all_heap(void) { 1026 1027 1028 /*########################################################################*/ 1029 /**************************************************************************/ 1030 /********** END CLOSE-FIT ***********/ 1031 /**************************************************************************/ 1032 /*########################################################################*/ 1033 1034 #endif // New Fit 1035 1036 heap_ll * pop_remote_free_list(heap_ll ** head) 1037 { 1038 1039 heap_ll * poped_block; 1040 1041 if (*head == 0x0) 1042 { 1043 giet_printf(" no block to pop\n"); 1044 return 0x0; 1045 } 1046 else 1047 { 1048 poped_block = *head; 1049 *head = (*head)->next; 1050 1051 giet_printf(" : @%x, size : %x\n", poped_block, poped_block->chunk_length); 1052 } 1053 1054 return poped_block; 1055 } 1056 1057 heap_ll * pop_ptr(void) 1058 { 1019 1059 int task_id = giet_global_task_id(); 1060 heap_ll * head_of_rf_list; 1061 giet_printf(" -- POP lock acquire -- for %d : @ : %x\n", task_id, &lock_rf_list[task_id]); 1062 lock_acquire(&lock_rf_list[task_id]); 1063 giet_printf(" -- POP lock acquired --\n"); 1064 1065 head_of_rf_list = _remote_free_list[task_id]; 1066 _remote_free_list[task_id] = 0x0; 1067 1068 lock_release(&lock_rf_list[task_id]); 1069 return head_of_rf_list; 1070 1071 } 1072 1073 void insert_in_remote_free_list(unsigned int remote_owner_id, unsigned int block_base, unsigned int block_length) 1074 { 1075 1076 giet_printf("############### INSERT \n"); 1077 ((heap_ll *)block_base)->chunk_length = block_length; 1078 1079 giet_printf(" -- INSERT lock acquire -- for %d : @ : %x\n", remote_owner_id, &lock_rf_list[remote_owner_id]); 1080 lock_acquire(&lock_rf_list[remote_owner_id]); 1081 1082 giet_printf(" -- INSERT lock acquired -- \n"); 1083 1084 // insertion en début de liste 1085 giet_printf(" in remote_owner %d ...\n", remote_owner_id); 1086 if (_remote_free_list[remote_owner_id] == 0x0) 1087 { 1088 ((heap_ll *)block_base)->next = 0x0; 1089 } 1090 else 1091 { 1092 ((heap_ll *)block_base)->next = _remote_free_list[remote_owner_id]; 1093 } 1094 _remote_free_list[remote_owner_id] = ((heap_ll *)block_base); 1095 1096 giet_printf(" : OK\n\n"); 1097 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); 1098 1099 lock_release(&lock_rf_list[remote_owner_id]); 1100 1101 return; 1102 } 1103 1104 void free(void * ptr) { 1020 1105 int i = 0; 1021 int nb_blocks = 0; 1022 heap_ll * ptr_to_chunk_list; 1023 giet_tty_printf("################### PRINT ALL HEAP ######################\n"); 1024 while (i != MAX_SIZE_POW2_TAB) { 1025 ptr_to_chunk_list = _pow2tab[task_id][i]; 1026 1027 if (ptr_to_chunk_list != 0x0) { 1028 giet_tty_printf("###################### %d ####################\n", i); 1029 } 1030 while (ptr_to_chunk_list != 0x0) { 1031 giet_tty_printf("CHUNK at @ : %x\n" 1032 "OVERHEAD : %d\n" 1033 "NEXT : %x\n\n", 1034 (unsigned int) ptr_to_chunk_list, 1035 ptr_to_chunk_list->chunk_length, 1036 (unsigned int) ptr_to_chunk_list->next); 1037 ptr_to_chunk_list = ptr_to_chunk_list->next; 1038 nb_blocks++; 1106 while (i != NB_TASKS_MAX) 1107 { 1108 if( _heap_base[i] == -1) 1109 { 1110 continue; 1111 } 1112 if ((unsigned int)ptr > _heap_base[i] && (unsigned int)ptr < _heap_base[i] + _heap_length[i]) 1113 { 1114 break; 1039 1115 } 1040 1116 i++; 1041 1117 } 1042 giet_tty_printf("####################### ##%d## ##########################\n", nb_blocks); 1043 } 1044 1045 1046 static void frag(int turn) { 1047 int task_id = giet_global_task_id(); 1048 int i = 0; 1049 int nb_blocks = 0; 1050 int size = 0; 1051 heap_ll * ptr_to_chunk; 1052 while (i != MAX_SIZE_POW2_TAB) { 1053 ptr_to_chunk = _pow2tab[task_id][i]; 1054 while (ptr_to_chunk != 0x0) { 1055 size += ptr_to_chunk->chunk_length; 1056 nb_blocks += 1; 1057 ptr_to_chunk = ptr_to_chunk->next; 1058 } 1059 i++; 1060 } 1061 if (size != 0) { 1062 giet_tty_printf("%.3f\t%d\n",size / ((double) nb_blocks), turn); 1063 } 1064 } 1065 1066 1067 1068 #endif // New Fit 1069 1070 1071 void free(void * ptr) { 1118 1072 1119 int task_id = giet_global_task_id(); 1073 1120 unsigned int * to_free = ptr; 1074 unsigned int heapB = _heap_base[task_id];1075 unsigned int heapL = _heap_length[task_id];1121 unsigned int heapB; 1122 unsigned int heapL; 1076 1123 unsigned int overhead; 1077 1124 unsigned int block_base; 1078 1125 unsigned int block_length; 1079 1080 #if DEBUG_MALLOC == 1 1081 giet_tty_printf("############# FREE #############\n\n"); 1082 #endif 1126 unsigned int remote_free_needed; 1127 unsigned int remote_owner_id; 1128 if (i == task_id) 1129 { 1130 1131 giet_printf("############# FREE #############\n\n"); 1132 remote_free_needed = 0; 1133 remote_owner_id = -1; 1134 heapB = _heap_base[task_id]; 1135 heapL = _heap_length[task_id]; 1136 } 1137 else 1138 { 1139 1140 giet_printf("############# REMOTE FREE #############\n\n"); 1141 remote_free_needed = 1; 1142 remote_owner_id = i; 1143 heapB = _heap_base[remote_owner_id]; 1144 heapL = _heap_length[remote_owner_id]; 1145 } 1146 1147 1083 1148 /****** CHECK PTR ******/ 1084 1149 if (to_free == 0x0) { 1085 #if DEBUG_MALLOC == 1 1086 giet_tty_printf("free == 0x0\n"); 1087 #endif 1150 1151 giet_printf("free == 0x0\n"); 1088 1152 return; 1089 1153 } 1090 1154 // check alignement of ptr 1091 1155 if (((unsigned int) to_free) % 4 != 0) { 1092 #if DEBUG_MALLOC == 1 1093 giet_tty_printf("allignement of ptr %x = %d\n", (unsigned int) to_free, (unsigned int) to_free % 4); 1094 #endif 1156 1157 giet_printf("allignement of ptr %x = %d\n", (unsigned int) to_free, (unsigned int) to_free % 4); 1095 1158 return; 1096 1159 } 1097 1160 // check if the @ of ptr matches the range of the heap 1098 1161 if (!((unsigned int) to_free >= heapB && (unsigned int) to_free < heapB + heapL)) { 1099 #if DEBUG_MALLOC == 1 1100 giet_tty_printf("check if it matches the range of the heap\n"); 1101 #endif 1162 1163 giet_printf("check if it matches the range of the heap\n"); 1102 1164 return; 1103 1165 } … … 1107 1169 // check alignement of overhead 1108 1170 if (overhead % 4 != 0) { 1109 #if DEBUG_MALLOC == 1 1110 giet_tty_printf("check alignement of overhead\n"); 1111 #endif 1171 1172 giet_printf("check alignement of overhead\n"); 1112 1173 return; 1113 1174 } 1114 1175 // check if (overhead + to_free) matches the range of the heap 1115 1176 if ((((unsigned int) (to_free) + overhead) - 4) > heapB + heapL) { 1116 #if DEBUG_MALLOC == 1 1117 giet_tty_printf ("check if (overhead + to_free) matches the range of the heap\n"); 1118 #endif 1177 1178 giet_printf ("check if (overhead + to_free) matches the range of the heap\n"); 1119 1179 return; 1120 1180 } … … 1123 1183 block_length = overhead + 4; 1124 1184 1125 #if DEBUG_MALLOC == 1 1126 giet_tty_printf("############# UPDATE CL : FREE %x of size %d #############\n\n",block_base, block_length); 1127 #endif 1128 update_chunk_list(block_base, block_length); 1129 1130 #if DEBUG_MALLOC == 1 1131 giet_tty_printf("BLOCK SUCCESSFULLY FREED\n\n"); 1132 #endif 1185 if (remote_free_needed == 0) 1186 { 1187 1188 giet_printf("############# UPDATE CL : FREE %x of size %d #############\n\n",block_base, block_length); 1189 update_chunk_list(block_base, block_length); 1190 } 1191 else 1192 { 1193 1194 giet_printf("############# INSERT IN RF %d : FREE %x of size %d #############\n\n", remote_owner_id, block_base, block_length); 1195 insert_in_remote_free_list(remote_owner_id, block_base, block_length); 1196 } 1197 1198 1199 giet_printf("BLOCK SUCCESSFULLY FREED\n\n"); 1133 1200 1134 1201 return; -
soft/giet_vm/libs/malloc.h
r233 r244 8 8 #ifndef _MALLOC_H_ 9 9 #define _MALLOC_H_ 10 //#define DEBUG_MALLOC 1 10 11 11 12 #include "giet_config.h" 13 #include "spin_lock.h" 12 14 13 15 //#define MALLOC_SELECTED 2 -
soft/giet_vm/libs/malloc_private.h
r233 r244 15 15 } heap_ll; 16 16 17 void insert_in_remote_free_list(unsigned int remote_owner_id, unsigned int block_base, unsigned int block_length); 18 heap_ll * pop_remote_free_list(heap_ll ** head); 19 heap_ll * pop_ptr(void); 20 void update_chunk_list(unsigned int block_base, unsigned int block_length); 21 17 22 #if MALLOC_SELECTED == 1 || MALLOC_SELECTED == 2 18 static heap_ll * get_prev_fit_chunk(unsigned int size);23 static heap_ll * get_prev_fit_chunk(unsigned int size); 19 24 #else 20 int get_prev_fit_chunk(unsigned int size);25 int get_prev_fit_chunk(unsigned int size); 21 26 #endif 22 27 23 static void print_all_heap(void);24 static void frag(int turn);25 28 26 29 #endif -
soft/giet_vm/libs/spin_lock.c
r228 r244 32 32 33 33 asm volatile ( 34 "giet_lock_try: \n" 35 "ll $2, 0(%0) \n" /* $2 <= lock current value */ 34 "move $16, %0 \n" 35 "giet_lock_try : \n" 36 "ll $2, 0($16) \n" /* $2 <= lock current value */ 36 37 "bnez $2, giet_lock_delay \n" /* retry if lock already taken */ 37 38 "li $3, 1 \n" /* $3 <= argument for sc */ 38 "sc $3, 0( %0)\n" /* try to get lock */39 "sc $3, 0($16) \n" /* try to get lock */ 39 40 "bnez $3, giet_lock_ok \n" /* exit if atomic */ 40 41 … … 53 54 : 54 55 :"r"(plock) 55 :"$2", "$3", "$4" );56 :"$2", "$3", "$4", "$16"); 56 57 } 57 58
Note: See TracChangeset
for help on using the changeset viewer.