mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 18:27:56 +00:00
aa5e0658f8
* Decrease bucket size A bucket size of 8192 bytes is quite large and it turned out that this can exhaust the available heap space too too quickly. This is because even for allocating 1 byte a bucket of 8192 bytes is allocated/wasted. * Return 0 if requested size too large * Improve test The test didn't use an offset when setting up the heap. Hence the first successfully allocated pointer was always `0`. This is unfortunate since `0` is also the return value when there is an error. This lead to us not noticing that the test was failing, because it did not distinguish between success and error. * Revert to linear allocator
57 lines
1.6 KiB
Rust
57 lines
1.6 KiB
Rust
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Substrate is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
//! This module implements a linear allocation heap.
|
|
|
|
pub struct Heap {
|
|
end: u32,
|
|
total_size: u32,
|
|
}
|
|
|
|
impl Heap {
|
|
/// Construct new `Heap` struct.
|
|
///
|
|
/// Returns `Err` if the heap couldn't allocate required
|
|
/// number of pages.
|
|
///
|
|
/// This could mean that wasm binary specifies memory
|
|
/// limit and we are trying to allocate beyond that limit.
|
|
pub fn new(reserved: u32) -> Self {
|
|
Heap {
|
|
end: reserved,
|
|
total_size: 0,
|
|
}
|
|
}
|
|
|
|
pub fn allocate(&mut self, size: u32) -> u32 {
|
|
let r = self.end;
|
|
self.end += size;
|
|
let new_total_size = r + size;
|
|
if new_total_size > self.total_size {
|
|
if new_total_size / 1024 > self.total_size / 1024 {
|
|
trace!(target: "wasm-heap", "Allocated over {} MB", new_total_size / 1024 / 1024);
|
|
}
|
|
self.total_size = new_total_size;
|
|
}
|
|
r
|
|
}
|
|
|
|
pub fn deallocate(&mut self, _offset: u32) {
|
|
}
|
|
}
|