// A version of function heapStatistics that performs some // checks of the heap's structure; helps debugging a garbage // collector. sestoft@itu.dk 2009-11-11 // Delete the old heapStatistics from listmachine.c and insert // this one instead. void heapStatistics() { int blocks = 0, free = 0, orphans = 0, blocksSize = 0, freeSize = 0, largestFree = 0; word* heapPtr = heap; while (heapPtr < afterHeap) { if (Length(heapPtr[0]) > 0) { blocks++; blocksSize += Length(heapPtr[0]); } else orphans++; word* nextBlock = heapPtr + Length(heapPtr[0]) + 1; if (nextBlock > afterHeap) { printf("HEAP ERROR: block at heap[%d] extends beyond heap\n", heapPtr-heap); exit(-1); } heapPtr = nextBlock; } word* freePtr = freelist; while (freePtr != 0) { free++; int length = Length(freePtr[0]); if (freePtr < heap || afterHeap < freePtr+length+1) { printf("HEAP ERROR: freelist item %d (at heap[%d], length %d) is outside heap\n", free, freePtr-heap, length); exit(-1); } freeSize += length; largestFree = length > largestFree ? length : largestFree; if (Color(freePtr[0])!=Blue) printf("Non-blue block at heap[%d] on freelist\n", (int)freePtr); freePtr = (word*)freePtr[1]; } printf("Heap: %d blocks (%d words); of which %d free (%d words, largest %d words); %d orphans\n", blocks, blocksSize, free, freeSize, largestFree, orphans); }