Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

Cheney's Copying Collector

Prerequisites: malloc Internals and the Hot Path, Container Invalidation and Stable Handles

C++ has no garbage collector, but managed runtimes such as the JVM and .NET relocate live objects when they collect garbage, and C++ arenas and pools sometimes move objects to close the gaps left by freed ones. Build Cheney's copying collector.

  • Semispace copying collector: the heap is split into two equal halves. The program bump-allocates in from-space until it fills. The collector then copies every object the program can still reach into to-space, packed from address 0, and all of from-space becomes free at once.
  • Root: a pointer the program holds directly, such as a local variable.
  • Reachable (live): a chain of pointers leads to the object from a root. Unreachable objects, the garbage, are never touched.
  • Forwarding address: moving an object makes every pointer to it stale, the problem from Container Invalidation and Stable Handles applied to a whole heap. The collector overwrites the old header with the copy's address, so a stale pointer can look there and be rewritten.

Cheney's algorithm runs the whole collection with two indices into to-space:

  • Free pointer: the next unused to-space cell. Each copy is placed there and free advances past it, exactly like an arena's bump pointer.
  • Scan pointer: the first copy whose fields still hold from-space addresses. The copies between scan and free are a queue waiting to have their pointers rewritten. The collection ends when scan catches free.

What to implement

  • cheney_gc(heap_size, root_count, roots, objects) returns the output lines.
  • heap_size is the semispace size H, in cells.
  • root_count is R, the number of entries in roots, which lists the root addresses from left to right. A root of -1 is null.
  • Each row of objects is one from-space object, addr k T1 v1 ... Tk vk.
  • On stdin: the line H, the line R, one line of R root addresses, then a count line followed by one object per line.
  • There are two parts. Part 2 removes a restriction from Part 1, and one solution must pass all eight tests.

Heap format

  • The heap is measured in cells. An object starting at address addr with k fields occupies 1 + k cells: a header cell, then its fields in order.
  • A field is an immediate integer, written I value, or a pointer, written P address.
  • The null pointer is P -1. Every other pointer names the start of an input object.
  • Immediates are plain data: I 4 stays I 4 even if an object starts at address 4.

Part 1: Copy a tree of objects

  1. Forward each root from left to right. A null root stays -1.
  2. To forward a non-null root, copy the object it names to the free pointer, advance free by 1 + k, and replace the root with the copy's address.
  3. Then scan. Starting with scan at 0, take the copy at the scan pointer and visit its fields from left to right.
  4. A non-null pointer field names a from-space object: copy that object to the free pointer and rewrite the field to the copy's address.
  5. Immediates and null pointers are left unchanged.
  6. Advance scan by 1 + k, and continue until scan equals free.
  7. Objects that are never reached are not copied.

Output, in this order:

used
root root ...
copied
new_addr k T1 v1 ... Tk vk
  • used is the number of to-space cells used, the final free pointer.
  • The second line holds the rewritten roots, separated by spaces.
  • copied is the number of copied objects.
  • Then one line per copied object, in copy order, with every pointer field rewritten to a to-space address.

Limits and guarantees

  • Every input object lies inside the semispace (addr + 1 + k <= H), and objects do not overlap.
  • The live objects fit in a to-space of the same size.
  • In this part the reachable objects form a tree. Every live object has exactly one incoming pointer, counting the roots and all object fields, so no object is reached twice.

Tests 1–4 cover this part.

Part 2: Handle sharing and cycles

Real object graphs share children and point back to objects already copied. Part 2 removes the tree restriction: an object may be reached from several roots and fields, and pointers may form cycles, including an object whose field points to itself.

  1. Copy each reachable object exactly once.
  2. When an object is first reached, copy it and record its forwarding address in its old header before any of its fields is scanned.
  3. Every later root or field that names the same object takes the forwarding address instead of copying again. This also makes the collection terminate on cycles.
  4. Garbage may point at live objects or form cycles of its own. Those pointers are never followed, because the collector scans only copies.

Tests 5–8 cover this part.

Example
Input
20
2
0 9
4
0 2 P 6 I 7
6 1 P -1
9 1 I 42
12 1 I 99
Output
7
0 3
3
0 2 P 5 I 7
3 1 I 42
5 1 P -1
Explanation

The roots are forwarded first, in order. Object 0 has 2 fields, so its 3 cells are copied to address 0 and free moves to 3. Object 9 is copied to address 3 and free moves to 5, so the roots become 0 3.

Scanning the copy at 0 finds P 6. Object 6 has not been copied yet, so it goes to address 5, free moves to 7, and the field becomes P 5, while I 7 stays as it is.

The copies at 3 and 5 hold only an immediate and a null pointer, so scan reaches free at 7. Object 12 is never reached; it is garbage and is not copied.