Changes in uspace/lib/c/generic/malloc.c [e6eee2b:207533f] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/malloc.c
re6eee2b r207533f 79 79 (sizeof(heap_block_head_t) + sizeof(heap_block_foot_t)) 80 80 81 /** Overhead of each area. */ 82 #define AREA_OVERHEAD(size) \ 83 (ALIGN_UP(size + sizeof(heap_area_t), BASE_ALIGN)) 84 81 85 /** Calculate real size of a heap block. 82 86 * … … 183 187 184 188 /** Next heap block to examine (next fit algorithm) */ 185 static heap_block_head_t *next = NULL;189 static heap_block_head_t *next_fit = NULL; 186 190 187 191 /** Futex for thread-safe heap manipulation */ 188 192 static futex_t malloc_futex = FUTEX_INITIALIZER; 193 194 #ifndef NDEBUG 195 196 #define malloc_assert(expr) \ 197 do { \ 198 if (!(expr)) {\ 199 futex_up(&malloc_futex); \ 200 assert_abort(#expr, __FILE__, __LINE__); \ 201 } \ 202 } while (0) 203 204 #else /* NDEBUG */ 205 206 #define malloc_assert(expr) 207 208 #endif /* NDEBUG */ 189 209 190 210 /** Initialize a heap block … … 228 248 heap_block_head_t *head = (heap_block_head_t *) addr; 229 249 230 assert(head->magic == HEAP_BLOCK_HEAD_MAGIC);250 malloc_assert(head->magic == HEAP_BLOCK_HEAD_MAGIC); 231 251 232 252 heap_block_foot_t *foot = BLOCK_FOOT(head); 233 253 234 assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC);235 assert(head->size == foot->size);254 malloc_assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC); 255 malloc_assert(head->size == foot->size); 236 256 } 237 257 … … 247 267 heap_area_t *area = (heap_area_t *) addr; 248 268 249 assert(area->magic == HEAP_AREA_MAGIC);250 assert(addr == area->start);251 assert(area->start < area->end);252 assert(((uintptr_t) area->start % PAGE_SIZE) == 0);253 assert(((uintptr_t) area->end % PAGE_SIZE) == 0);269 malloc_assert(area->magic == HEAP_AREA_MAGIC); 270 malloc_assert(addr == area->start); 271 malloc_assert(area->start < area->end); 272 malloc_assert(((uintptr_t) area->start % PAGE_SIZE) == 0); 273 malloc_assert(((uintptr_t) area->end % PAGE_SIZE) == 0); 254 274 } 255 275 … … 362 382 363 383 /* Eventually try to create a new area */ 364 return area_create(AREA_ FIRST_BLOCK_HEAD(size));384 return area_create(AREA_OVERHEAD(size)); 365 385 } 366 386 … … 382 402 383 403 block_check((void *) last_head); 384 assert(last_head->area == area);404 malloc_assert(last_head->area == area); 385 405 386 406 if (last_head->free) { … … 395 415 396 416 block_check((void *) first_head); 397 assert(first_head->area == area);417 malloc_assert(first_head->area == area); 398 418 399 419 size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE); … … 439 459 /* Update heap area parameters */ 440 460 area->end = end; 441 442 /* Update block layout */ 443 void *last = (void *) last_head; 444 size_t excess = (size_t) (area->end - last); 461 size_t excess = ((size_t) area->end) - ((size_t) last_head); 445 462 446 463 if (excess > 0) { … … 451 468 * create a new free block. 452 469 */ 453 block_init( last, excess, true, area);470 block_init((void *) last_head, excess, true, area); 454 471 } else { 455 472 /* … … 470 487 } 471 488 472 next = NULL;489 next_fit = NULL; 473 490 } 474 491 … … 497 514 static void split_mark(heap_block_head_t *cur, const size_t size) 498 515 { 499 assert(cur->size >= size);516 malloc_assert(cur->size >= size); 500 517 501 518 /* See if we should split the block. */ … … 533 550 { 534 551 area_check((void *) area); 535 assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));536 assert((void *) first_block < area->end);552 malloc_assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 553 malloc_assert((void *) first_block < area->end); 537 554 538 555 for (heap_block_head_t *cur = first_block; (void *) cur < area->end; … … 559 576 split_mark(cur, real_size); 560 577 561 next = cur;578 next_fit = cur; 562 579 return addr; 563 580 } else { … … 611 628 split_mark(next_head, real_size); 612 629 613 next = next_head;630 next_fit = next_head; 614 631 return aligned; 615 632 } else { … … 637 654 split_mark(cur, real_size); 638 655 639 next = cur;656 next_fit = cur; 640 657 return aligned; 641 658 } … … 661 678 static void *malloc_internal(const size_t size, const size_t align) 662 679 { 663 assert(first_heap_area != NULL);680 malloc_assert(first_heap_area != NULL); 664 681 665 682 if (align == 0) … … 675 692 676 693 /* Try the next fit approach */ 677 split = next ;694 split = next_fit; 678 695 679 696 if (split != NULL) { … … 786 803 787 804 block_check(head); 788 assert(!head->free);805 malloc_assert(!head->free); 789 806 790 807 heap_area_t *area = head->area; 791 808 792 809 area_check(area); 793 assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));794 assert((void *) head < area->end);810 malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 811 malloc_assert((void *) head < area->end); 795 812 796 813 void *ptr = NULL; … … 831 848 832 849 ptr = ((void *) head) + sizeof(heap_block_head_t); 833 next = NULL;850 next_fit = NULL; 834 851 } else 835 852 reloc = true; … … 863 880 864 881 block_check(head); 865 assert(!head->free);882 malloc_assert(!head->free); 866 883 867 884 heap_area_t *area = head->area; 868 885 869 886 area_check(area); 870 assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));871 assert((void *) head < area->end);887 malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 888 malloc_assert((void *) head < area->end); 872 889 873 890 /* Mark the block itself as free. */ … … 904 921 } 905 922 923 void *heap_check(void) 924 { 925 futex_down(&malloc_futex); 926 927 if (first_heap_area == NULL) { 928 futex_up(&malloc_futex); 929 return (void *) -1; 930 } 931 932 /* Walk all heap areas */ 933 for (heap_area_t *area = first_heap_area; area != NULL; 934 area = area->next) { 935 936 /* Check heap area consistency */ 937 if ((area->magic != HEAP_AREA_MAGIC) || 938 ((void *) area != area->start) || 939 (area->start >= area->end) || 940 (((uintptr_t) area->start % PAGE_SIZE) != 0) || 941 (((uintptr_t) area->end % PAGE_SIZE) != 0)) { 942 futex_up(&malloc_futex); 943 return (void *) area; 944 } 945 946 /* Walk all heap blocks */ 947 for (heap_block_head_t *head = (heap_block_head_t *) 948 AREA_FIRST_BLOCK_HEAD(area); (void *) head < area->end; 949 head = (heap_block_head_t *) (((void *) head) + head->size)) { 950 951 /* Check heap block consistency */ 952 if (head->magic != HEAP_BLOCK_HEAD_MAGIC) { 953 futex_up(&malloc_futex); 954 return (void *) head; 955 } 956 957 heap_block_foot_t *foot = BLOCK_FOOT(head); 958 959 if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) || 960 (head->size != foot->size)) { 961 futex_up(&malloc_futex); 962 return (void *) foot; 963 } 964 } 965 } 966 967 futex_up(&malloc_futex); 968 969 return NULL; 970 } 971 906 972 /** @} 907 973 */
Note:
See TracChangeset
for help on using the changeset viewer.