pub unsafe fn allocate(size: u64) -> Result<SSlice, OutOfMemory>Expand description
Attempts to allocate a new SSlice of at least the required size or returns an OutOfMemory error if there is no continuous stable memory memory block of that size can be allocated.
Memory block that is returned can be bigger than requested. This happens because:
- Sizes for allocation are always getting rounded up to the next multiple of 8 bytes. For example, if requested 100 bytes to allocate requested, the resulting memory block can’t be smaller than 104 bytes.
- Minimum memory block size is 16 bytes. To find out more see documentation for the allocator.
- If the allocator has a free block of size less than
requested size + minimum block size, this block won’t be split and will be returned as is.
If the allocator only has a memory block which is bigger than requested size + minimum block size,
that block gets split it two. The first half is returned as the result of this function, and the other
half goes back to the free list.
If the allocator has no apropriate free memory block to allocate, it will try to grow stable memory
by the number of pages enough to allocate a block of that size. If it can’t grow due to lack of
stable memory in a subnet or due to reaching max_pages limit set earlier - it will return an
OutOfMemory error.
Internally calls StableMemoryAllocator::allocate.
§Example
// slice size is in [104..136) bytes range, despite requesting for only 100 bytes
let slice = allocate(100).expect("Not enough stable memory");§Panics
Panics if there is no initialized stable memory allocator.
§Safety
Don’t forget to deallocate the memory block, when you’re done!