module scheme import arena fn attempt_frozen_scratch(tree &SyntaxTree, mut result BoundedResult) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } tree.require_finite_transformer() or { panic(err) } } fn attempt_frozen_job(storage arena.Arena, mut result BoundedResult) { defer { if message := recover() { result.exhausted = arena.allocation_failure() or { panic(message) } } } mut jobs := []i32{} frozen_syntax_job(mut jobs, storage, 0) } fn test_denied_frozen_transformer_scratch_restores_the_callers_allocator() { tree := &SyntaxTree{ nodes: [SyntaxTreeNode{ kind: .empty_list }] } mut owner := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 }) defer { owner.free() } mut result := BoundedResult{} owner.push() attempt_frozen_scratch(tree, mut result) restored := owner.is_current() // Release the leaked child when exercising the old implementation so the // failed assertion can report outside the exhausted allocator. if !restored { child := v_arena_current() unsafe { v_arena_pop(child) v_arena_free(child) } } owner.pop() assert restored reason := result.exhausted or { panic('expected scratch allocation denial') } assert reason == .chunk_limit assert owner.used() == 0 } fn test_denied_frozen_job_growth_restores_the_callers_allocator() { mut owner := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 }) defer { owner.free() } owner.push() mut storage := arena.new(chunk_size: 1024) defer { storage.free() } mut result := BoundedResult{} attempt_frozen_job(storage, mut result) restored := owner.is_current() if !restored { unsafe { v_arena_pop(v_arena_current()) } } owner.pop() assert restored reason := result.exhausted or { panic('expected scratch allocation denial') } assert reason == .chunk_limit assert owner.used() == 0 }