Tries to create a new sparse memory with `len` addressable bytes and the given `chunk_size`. Prefer this backend when grow behavior matters more than absolute read and write speed.
(len: usize, chunk_size: usize)
| 29 | /// |
| 30 | /// Prefer this backend when grow behavior matters more than absolute read and write speed. |
| 31 | pub fn try_new(len: usize, chunk_size: usize) -> Result<Self, crate::Trap> { |
| 32 | assert!(chunk_size.is_power_of_two(), "chunk_size must be a power of two"); |
| 33 | |
| 34 | let mut memory = Self { |
| 35 | len: 0, |
| 36 | chunk_size, |
| 37 | chunk_shift: chunk_size.trailing_zeros(), |
| 38 | chunk_mask: chunk_size - 1, |
| 39 | chunks: Vec::new(), |
| 40 | }; |
| 41 | memory.grow_to(len)?; |
| 42 | Ok(memory) |
| 43 | } |
| 44 | |
| 45 | #[inline(always)] |
| 46 | fn allocate_chunk(&self) -> Result<Box<[u8]>, crate::Trap> { |