C Program to Scan the marked blocks for references to other unmarked blocks

How to write a C Program to Scan the marked blocks for references to other unmarked blocks in C Programming Language ?


Solution:
  1. /*
  2.  * Scan the marked blocks for references to other unmarked blocks.
  3.  */
  4. static void
  5. mark_from_heap(void)
  6. {
  7.     unsigned int *vp;
  8.     header_t *bp, *up;
  9.  
  10.     for (bp = UNTAG(usedp->next); bp != usedp; bp = UNTAG(bp->next)) {
  11.         if (!((unsigned int)bp->next & 1))
  12.             continue;
  13.         for (vp = (unsigned int *)(bp + 1);
  14.              vp < (bp + bp->size + 1);
  15.              vp++) {
  16.             unsigned int v = *vp;
  17.             up = UNTAG(bp->next);
  18.             do {
  19.                 if (up != bp &&
  20.                     up + 1 <= v &&
  21.                     up + 1 + up->size > v) {
  22.                     up->next = ((unsigned int) up->next) | 1;
  23.                     break;
  24.                 }
  25.             } while ((up = UNTAG(up->next)) != bp);
  26.         }
  27.     }
  28. }


Learn More :