pub fn allocate(&mut self, size: u32) -> Result<u32, UserError> {
		if size > MAX_POSSIBLE_ALLOCATION {
			return Err(UserError(REQUESTED_SIZE_TOO_LARGE));
		}

		let size = size.max(8);
		let item_size = size.next_power_of_two();
		if item_size + 8 + self.total_size > self.max_heap_size {
			return Err(UserError(OUT_OF_SPACE));
		}

		let list_index = (item_size.trailing_zeros() - 3) as usize;
		let ptr: u32 = if self.heads[list_index] != 0 {
			// Something from the free list
			let item = self.heads[list_index];
			let four_bytes = self.get_heap_4bytes(item)
				.map_err(|_| UserError("Unable to get bytes at pointer taken from list of free items"))?;
			self.heads[list_index] = FreeingBumpHeapAllocator::le_bytes_to_u32(four_bytes);
			item + 8
		} else {
			// Nothing to be freed. Bump.
			self.bump(item_size + 8) + 8
		};

		for i in 1..8 {
			self.set_heap(ptr - i, 255)
				.map_err(|_| UserError("Unable to successively write bytes into heap at pointer prefix"))?;
		}

		self.set_heap(ptr - 8, list_index as u8)
			.map_err(|_| UserError("Unable to write byte into heap at pointer prefix"))?;

		self.total_size = self.total_size + item_size + 8;
		trace!(target: "wasm-heap", "Heap size is {} bytes after allocation", self.total_size);

		Ok(self.ptr_offset + ptr)
	}
/// Gets requested number of bytes to allocate and returns a pointer.
/// The maximum size which can be allocated at once is 16 MiB.
Gets requested number of bytes to allocate and returns a pointer.
The maximum size which can be allocated at once is 16 MiB.
Gets requested number of bytes to allocate and returns a pointer . The maximum size which can be allocated at once is 16 MiB .
pub fn allocate(&mut self, size: u32) -> Result<u32, UserError> {
		if size > MAX_POSSIBLE_ALLOCATION {
			return Err(UserError(REQUESTED_SIZE_TOO_LARGE));
		}
		let size = size.max(8);
		let item_size = size.next_power_of_two();
		if item_size + 8 + self.total_size > self.max_heap_size {
			return Err(UserError(OUT_OF_SPACE));
		}
		let list_index = (item_size.trailing_zeros() - 3) as usize;
		let ptr: u32 = if self.heads[list_index] != 0 {
			// Something from the free list
			let item = self.heads[list_index];
			let four_bytes = self.get_heap_4bytes(item)
				.map_err(|_| UserError("Unable to get bytes at pointer taken from list of free items"))?;
			self.heads[list_index] = FreeingBumpHeapAllocator::le_bytes_to_u32(four_bytes);
			item + 8
		} else {
			// Nothing to be freed. Bump.
			self.bump(item_size + 8) + 8
		};
		for i in 1..8 {
			self.set_heap(ptr - i, 255)
				.map_err(|_| UserError("Unable to successively write bytes into heap at pointer prefix"))?;
		}
		self.set_heap(ptr - 8, list_index as u8)
			.map_err(|_| UserError("Unable to write byte into heap at pointer prefix"))?;
		self.total_size = self.total_size + item_size + 8;
		trace!(target: "wasm-heap", "Heap size is {} bytes after allocation", self.total_size);
		Ok(self.ptr_offset + ptr)
	}
pub fn allocate ( & mut self , size : u32 ) -> Result < u32 , UserError > { if size > MAX_POSSIBLE_ALLOCATION { return Err ( UserError ( REQUESTED_SIZE_TOO_LARGE ) ) ; } let size = size . max ( 8 ) ; let item_size = size . next_power_of_two ( ) ; if item_size + 8 + self . total_size > self . max_heap_size { return Err ( UserError ( OUT_OF_SPACE ) ) ; } let list_index = ( item_size . trailing_zeros ( ) - 3 ) as usize ; let ptr : u32 = if self . heads [ list_index ] != 0 { // Something from the free list let item = self . heads [ list_index ] ; let four_bytes = self . get_heap_4bytes ( item ) . map_err ( | _ | UserError ( " " ) ) ? ; self . heads [ list_index ] = FreeingBumpHeapAllocator :: le_bytes_to_u32 ( four_bytes ) ; item + 8 } else { // Nothing to be freed. Bump. self . bump ( item_size + 8 ) + 8 } ; for i in 1 .. 8 { self . set_heap ( ptr - i , 255 ) . map_err ( | _ | UserError ( " " ) ) ? ; } self . set_heap ( ptr - 8 , list_index as u8 ) . map_err ( | _ | UserError ( " " ) ) ? ; self . total_size = self . total_size + item_size + 8 ; trace ! ( target " " " " self total_size ) ; Ok ( self . ptr_offset + ptr ) }
Gets requested number of bytes to allocate and returns a pointer.
Gets requested number of bytes to allocate and returns a pointer .
&mut self
size
u32
&mut self
size
u32