Changes in uspace/lib/c/generic/malloc.c [207533f:e6eee2b] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/malloc.c
r207533f re6eee2b 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 85 81 /** Calculate real size of a heap block. 86 82 * … … 187 183 188 184 /** Next heap block to examine (next fit algorithm) */ 189 static heap_block_head_t *next _fit= NULL;185 static heap_block_head_t *next = NULL; 190 186 191 187 /** Futex for thread-safe heap manipulation */ 192 188 static futex_t malloc_futex = FUTEX_INITIALIZER; 193 194 #ifndef NDEBUG195 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 */209 189 210 190 /** Initialize a heap block … … 248 228 heap_block_head_t *head = (heap_block_head_t *) addr; 249 229 250 malloc_assert(head->magic == HEAP_BLOCK_HEAD_MAGIC);230 assert(head->magic == HEAP_BLOCK_HEAD_MAGIC); 251 231 252 232 heap_block_foot_t *foot = BLOCK_FOOT(head); 253 233 254 malloc_assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC);255 malloc_assert(head->size == foot->size);234 assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC); 235 assert(head->size == foot->size); 256 236 } 257 237 … … 267 247 heap_area_t *area = (heap_area_t *) addr; 268 248 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);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); 274 254 } 275 255 … … 382 362 383 363 /* Eventually try to create a new area */ 384 return area_create(AREA_ OVERHEAD(size));364 return area_create(AREA_FIRST_BLOCK_HEAD(size)); 385 365 } 386 366 … … 402 382 403 383 block_check((void *) last_head); 404 malloc_assert(last_head->area == area);384 assert(last_head->area == area); 405 385 406 386 if (last_head->free) { … … 415 395 416 396 block_check((void *) first_head); 417 malloc_assert(first_head->area == area);397 assert(first_head->area == area); 418 398 419 399 size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE); … … 459 439 /* Update heap area parameters */ 460 440 area->end = end; 461 size_t excess = ((size_t) area->end) - ((size_t) last_head); 441 442 /* Update block layout */ 443 void *last = (void *) last_head; 444 size_t excess = (size_t) (area->end - last); 462 445 463 446 if (excess > 0) { … … 468 451 * create a new free block. 469 452 */ 470 block_init( (void *) last_head, excess, true, area);453 block_init(last, excess, true, area); 471 454 } else { 472 455 /* … … 487 470 } 488 471 489 next _fit= NULL;472 next = NULL; 490 473 } 491 474 … … 514 497 static void split_mark(heap_block_head_t *cur, const size_t size) 515 498 { 516 malloc_assert(cur->size >= size);499 assert(cur->size >= size); 517 500 518 501 /* See if we should split the block. */ … … 550 533 { 551 534 area_check((void *) area); 552 malloc_assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));553 malloc_assert((void *) first_block < area->end);535 assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 536 assert((void *) first_block < area->end); 554 537 555 538 for (heap_block_head_t *cur = first_block; (void *) cur < area->end; … … 576 559 split_mark(cur, real_size); 577 560 578 next _fit= cur;561 next = cur; 579 562 return addr; 580 563 } else { … … 628 611 split_mark(next_head, real_size); 629 612 630 next _fit= next_head;613 next = next_head; 631 614 return aligned; 632 615 } else { … … 654 637 split_mark(cur, real_size); 655 638 656 next _fit= cur;639 next = cur; 657 640 return aligned; 658 641 } … … 678 661 static void *malloc_internal(const size_t size, const size_t align) 679 662 { 680 malloc_assert(first_heap_area != NULL);663 assert(first_heap_area != NULL); 681 664 682 665 if (align == 0) … … 692 675 693 676 /* Try the next fit approach */ 694 split = next _fit;677 split = next; 695 678 696 679 if (split != NULL) { … … 803 786 804 787 block_check(head); 805 malloc_assert(!head->free);788 assert(!head->free); 806 789 807 790 heap_area_t *area = head->area; 808 791 809 792 area_check(area); 810 malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));811 malloc_assert((void *) head < area->end);793 assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 794 assert((void *) head < area->end); 812 795 813 796 void *ptr = NULL; … … 848 831 849 832 ptr = ((void *) head) + sizeof(heap_block_head_t); 850 next _fit= NULL;833 next = NULL; 851 834 } else 852 835 reloc = true; … … 880 863 881 864 block_check(head); 882 malloc_assert(!head->free);865 assert(!head->free); 883 866 884 867 heap_area_t *area = head->area; 885 868 886 869 area_check(area); 887 malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));888 malloc_assert((void *) head < area->end);870 assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area)); 871 assert((void *) head < area->end); 889 872 890 873 /* Mark the block itself as free. */ … … 921 904 } 922 905 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 972 906 /** @} 973 907 */
Note:
See TracChangeset
for help on using the changeset viewer.