This preview shows page 1. Sign up to view the full content.
Unformatted text preview: unction shown in Figure 10.53(a) once for each root node. The mark function returns immediately if p does not point to an allocated and unmarked heap block. Otherwise, it marks the block and calls itself recursively on each word in block. Each call to the mark function marks any unmarked and reachable descendents of some root node. At the end of the mark phase, any allocated block that is not marked is guaranteed to be unreachable, and hence garbage that can be reclaimed in the sweep phase. void mark(ptr p) if ((b = isPtr(p)) == NULL) return; if (blockMarked(b)) return; markBlock(b); len = length(b); for (i=0; i < len; i++) mark(b[i]); return; void sweep(ptr b, ptr end) while (b < end) if (blockMarked(b)) unmarkBlock(b); else if (blockAllocated(b)) free(b); b = nextBlock(b); return; Figure 10.53: Pseudo-code for the mark and sweep functions. The sweep phase is a single call to the sweep function shown in Figure 10.53(b). The sweep function iterates over each block in the heap, freeing...
View Full Document
- Spring '10
- The American