Example #1
0
File: mm.c Project: nik-6947/malloc
/* 
 * Requires:
 *   None.
 *
 * Effects:
 *   Perform a minimal check of the heap for consistency. 
 */
void
checkheap(bool verbose) 
{
  void *bp;

  if (verbose)
    printf("Heap (%p):\n", heap_listp);

  if (GET_SIZE(HDRP(heap_listp)) != DSIZE ||
      !GET_ALLOC(HDRP(heap_listp)))
    printf("Bad prologue header\n");
  checkblock(heap_listp);

  for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = (void *)NEXT_BLKP(bp)) {
    if (verbose)
      printblock(bp);
    checkblock(bp);
  }

  if (verbose)
    printblock(bp);
  if (GET_SIZE(HDRP(bp)) != 0 || !GET_ALLOC(HDRP(bp)))
    printf("Bad epilogue header\n");

in_heap(bp);
check_coalescing();
check_free_list();
check_free_blocks();
}
Example #2
0
// Returns 0 if no errors were found, otherwise returns the error
int mm_checkheap(int verbose) {
  // char *bp = heap_listp;

  if (verbose)
    printf("Heap (%p):\n", heap_listp);

  if ((GET_SIZE(HDRP(heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(heap_listp)))
    printf("Bad prologue header\n");
  checkblock(heap_listp);

  void* list;
  int count_free_in_list = 0;
  int count_free_in_heap = 0;
  for (int i =0; i < 9; i++)
    {
      for (list = (void*)(*(long*)GET_BUCKET(root,i)); list != NULL;
           list =  get_succ(list)  ) {
        if (verbose)
          printblock(list);
        checkblock(list);
        if ( get_succ(list) != NULL && get_pred(list) !=NULL)
          check_succ_pred(list);
        check_address(list);
        count_free_in_list++;
        check_in_correct_bucket(list, i);
      }
    }

  char *bp;

  for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
    if (verbose)
      {
        if (GET_ALLOC(HDRP(bp)) == 0)
          count_free_in_heap ++;
        printblock(bp);
        checkblock(bp);
        check_coalescing( (void*)bp);
      }
  }

  if (count_free_in_heap != count_free_in_list)
    printf ("Number of free block not consistent in heap and list list \n");

  if (verbose)
    printblock(bp);
    if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp))))
      {
        printf("Bad epilogue header\n");
        if (GET_SIZE(HDRP(bp)) != 0)
          printf ("size is not 0\n");
        if (!(GET_ALLOC(HDRP(bp))) )
           printf ("not allocated properly\n");
     }
    return 0;
}