Changeset 1b3e854 in mainline for uspace/lib/c/generic/malloc.c


Ignore:
Timestamp:
2011-05-21T14:29:50Z (13 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
faeb7cc
Parents:
955f2a5
Message:

add library function for explicit heap structure consistency check
use extensive heap consistency checks in the heap allocator tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/malloc.c

    r955f2a5 r1b3e854  
    904904}
    905905
     906void *heap_check(void)
     907{
     908        futex_down(&malloc_futex);
     909       
     910        if (first_heap_area == NULL) {
     911                futex_up(&malloc_futex);
     912                return (void *) -1;
     913        }
     914       
     915        /* Walk all heap areas */
     916        for (heap_area_t *area = first_heap_area; area != NULL;
     917            area = area->next) {
     918               
     919                /* Check heap area consistency */
     920                if ((area->magic != HEAP_AREA_MAGIC) ||
     921                    ((void *) area != area->start) ||
     922                    (area->start >= area->end) ||
     923                    (((uintptr_t) area->start % PAGE_SIZE) != 0) ||
     924                    (((uintptr_t) area->end % PAGE_SIZE) != 0)) {
     925                        futex_up(&malloc_futex);
     926                        return (void *) area;
     927                }
     928               
     929                /* Walk all heap blocks */
     930                for (heap_block_head_t *head = (heap_block_head_t *)
     931                    AREA_FIRST_BLOCK_HEAD(area); (void *) head < area->end;
     932                    head = (heap_block_head_t *) (((void *) head) + head->size)) {
     933                       
     934                        /* Check heap block consistency */
     935                        if (head->magic != HEAP_BLOCK_HEAD_MAGIC) {
     936                                futex_up(&malloc_futex);
     937                                return (void *) head;
     938                        }
     939                       
     940                        heap_block_foot_t *foot = BLOCK_FOOT(head);
     941                       
     942                        if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) ||
     943                            (head->size != foot->size)) {
     944                                futex_up(&malloc_futex);
     945                                return (void *) foot;
     946                        }
     947                }
     948        }
     949       
     950        futex_up(&malloc_futex);
     951       
     952        return NULL;
     953}
     954
    906955/** @}
    907956 */
Note: See TracChangeset for help on using the changeset viewer.