gp_allocator/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Collection of allocator implementations.
19//!
20//! This crate provides the following allocator implementations:
21//! - A freeing-bump allocator: [`FreeingBumpHeapAllocator`](freeing_bump::FreeingBumpHeapAllocator)
22
23#![cfg_attr(not(feature = "std"), no_std)]
24
25#![warn(missing_docs)]
26
27#[cfg(feature = "std")]
28mod error;
29#[cfg(feature = "std")]
30mod freeing_bump;
31
32#[cfg(feature = "std")]
33pub use error::Error;
34#[cfg(feature = "std")]
35pub use freeing_bump::{AllocationStats, FreeingBumpHeapAllocator};
36
37/// The size of one wasm page in bytes.
38///
39/// The wasm memory is divided into pages, meaning the minimum size of a memory is one page.
40const PAGE_SIZE: u32 = 65536;
41
42/// The maximum number of wasm pages that can be allocated.
43///
44/// 4GiB / [`PAGE_SIZE`].
45const MAX_WASM_PAGES: u32 = (4u64 * 1024 * 1024 * 1024 / PAGE_SIZE as u64) as u32;
46
47/// Grants access to the memory for the allocator.
48///
49/// Memory of wasm is allocated in pages. A page has a constant size of 64KiB. The maximum allowed
50/// memory size as defined in the wasm specification is 4GiB (65536 pages).
51pub trait Memory {
52	/// Run the given closure `run` and grant it write access to the raw memory.
53	fn with_access_mut<R>(&mut self, run: impl FnOnce(&mut [u8]) -> R) -> R;
54	/// Run the given closure `run` and grant it read access to the raw memory.
55	fn with_access<R>(&self, run: impl FnOnce(&[u8]) -> R) -> R;
56	/// Grow the memory by `additional` pages.
57	fn grow(&mut self, additional: u32) -> Result<(), ()>;
58	/// Returns the current number of pages this memory has allocated.
59	fn pages(&self) -> u32;
60	/// Returns the maximum number of pages this memory is allowed to allocate.
61	///
62	/// The returned number needs to be smaller or equal to `65536`. The returned number needs to be
63	/// bigger or equal to [`Self::pages`].
64	///
65	/// If `None` is returned, there is no maximum (besides the maximum defined in the wasm spec).
66	fn max_pages(&self) -> Option<u32>;
67}
68
69/// The maximum number of bytes that can be allocated at one time.
70// The maximum possible allocation size was chosen rather arbitrary, 32 MiB should be enough for
71// everybody.
72// 2^25 bytes, 32 MiB
73pub const MAX_POSSIBLE_ALLOCATION: u32 = 33_554_432;