/* Stop-the-world mark/sweep with an allocation-failable gray stack. */
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct Object Object;
struct Object { int id; bool marked; Object *edges[3]; Object *next; };
typedef struct Frame Frame;
struct Frame { Object **roots; size_t count; Frame *previous; };
typedef struct { Object *objects; Frame *frames; } VM;
typedef struct { Object **items; size_t len, cap; } Gray;
static size_t growth_budget = SIZE_MAX; /* successful growths before test failure */

static Object *new_object(VM *vm, int id) {
    Object *o = malloc(sizeof *o);
    if (!o) return NULL;
    *o = (Object){.id = id, .next = vm->objects};
    vm->objects = o; return o;
}
static bool push(Gray *g, Object *o) {
    if (g->len == g->cap) {
        if (growth_budget == 0 || g->cap > SIZE_MAX / 2) return false;
        if (growth_budget != SIZE_MAX) --growth_budget;
        size_t cap = g->cap ? g->cap * 2 : 2;
        if (cap > SIZE_MAX / sizeof *g->items) return false;
        Object **p = realloc(g->items, cap * sizeof *p);
        if (!p) return false;
        g->items = p; g->cap = cap;
    }
    g->items[g->len++] = o; return true;
}
static bool shade(Gray *g, Object *o) {
    if (!o || o->marked) return true;
    /* Mark only after the push succeeds: no marked object is lost. */
    if (!push(g, o)) return false;
    o->marked = true; return true;
}
static bool collect(VM *vm) {
    Gray g = {0};
    for (Frame *f = vm->frames; f; f = f->previous)
        for (size_t i = 0; i < f->count; ++i)
            if (!shade(&g, f->roots[i])) goto abort_trace;
    while (g.len) {
        Object *o = g.items[--g.len];
        for (size_t i = 0; i < 3; ++i)
            if (!shade(&g, o->edges[i])) goto abort_trace;
    }
    free(g.items);
    Object **link = &vm->objects;
    while (*link) {
        Object *o = *link;
        if (o->marked) { o->marked = false; link = &o->next; }
        else { *link = o->next; printf("collect %d\n", o->id); free(o); }
    }
    return true;
abort_trace:
    free(g.items);
    for (Object *o = vm->objects; o; o = o->next) o->marked = false;
    return false; /* Never sweep with an incomplete trace. */
}
int main(void) {
    VM vm = {0};
    Object *a = new_object(&vm, 1), *b = new_object(&vm, 2);
    Object *c = new_object(&vm, 3), *d = new_object(&vm, 4);
    if (!a || !b || !c || !d) { collect(&vm); return 1; }
    a->edges[0] = b; b->edges[0] = a;
    c->edges[0] = d; d->edges[0] = c;
    Object *roots[] = {a, b, c};
    Frame frame = {.roots = roots, .count = 3, .previous = NULL};
    vm.frames = &frame;
    growth_budget = 1; /* Two roots marked; growth for the third fails. */
    assert(!collect(&vm));
    assert(vm.objects == d && !a->marked && !b->marked && !c->marked);
    puts("failed trace: no sweep");
    frame.count = 2; /* c is no longer a root */
    growth_budget = SIZE_MAX;
    puts("first collection:"); assert(collect(&vm));
    vm.frames = frame.previous;
    puts("second collection:"); assert(collect(&vm));
    assert(vm.objects == NULL);
    return 0;
}
