// This file is part of Substrate. // Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! This module defines `HostState` struct which provide logic and state //! required for execution of host. use sp_allocator::{AllocationStats, FreeingBumpHeapAllocator}; /// The state required to construct a HostContext context. The context only lasts for one host /// call, whereas the state is maintained for the duration of a Wasm runtime call, which may make /// many different host calls that must share state. pub struct HostState { /// The allocator instance to keep track of allocated memory. /// /// This is stored as an `Option` as we need to temporarly set this to `None` when we are /// allocating/deallocating memory. The problem being that we can only mutable access `caller` /// once. pub allocator: Option, pub panic_message: Option, } impl HostState { /// Constructs a new `HostState`. pub fn new(allocator: FreeingBumpHeapAllocator) -> Self { HostState { allocator: Some(allocator), panic_message: None } } /// Takes the error message out of the host state, leaving a `None` in its place. pub fn take_panic_message(&mut self) -> Option { self.panic_message.take() } pub fn allocation_stats(&self) -> AllocationStats { self.allocator.as_ref() .expect("Allocator is always set and only unavailable when doing an allocation/deallocation; qed") .stats() } }