module scheme import arena // ArenaUsageLimitRequest reports the usage observed before request/cleanup // allocations. It contains no borrowed VM, arena, or context pointer. pub struct ArenaUsageLimitRequest { Error pub: limit i64 used i64 } pub fn (request &ArenaUsageLimitRequest) msg() string { return 'arena usage limit exceeded: ${request.used} bytes used; limit ${request.limit}' } // with_arena_usage_limit samples the absolute current used() of a host-owned // arena at execution entries, between instructions and after successful work. // The arena must be current on entry and remain alive throughout work. Zero // disables this added limit; active ancestor controls still apply. // // This is a cooperative usage check, not a hard allocator or OOM cap. Parsing, // compilation and native calls can overshoot or shrink again between checks. // Capacity, private scratch arenas, other allocators/threads and resource handles // are outside this count. The controller itself and prior arena allocations count. // The scope does not reset/free/pop the arena or change Value/error ownership. pub fn (mut vm VM) with_arena_usage_limit(region &arena.Arena, limit i64, work EvaluationFn) !Value { vm.ready()! if limit < 0 { return failure('arena usage limit must be nonnegative') } if !region.is_current() { return failure('arena usage limit requires the current arena') } if limit == 0 { vm.check_entry_control()! result := work(mut vm)! vm.check_entry_control()! return result } mut state := vm.system_state() previous := state.evaluation control := &EvaluationControl{ previous: previous kind: .arena_usage region: region limit: limit } state.evaluation = control defer { state.evaluation = previous } control.check_control()! result := work(mut vm)! control.check_control()! return result }