soroban-sdk crate provides a lightweight bump-pointer allocator which can be used to emulate heap memory allocation in a Wasm smart contract.
Run the Example
First go through the Setup process to get your development environment configured, then clone thev23.0.0 tag of soroban-examples repository:
alloc directory, and use cargo test.
Dependencies
This example depends on thealloc feature in soroban-sdk. To include it, add “alloc” to the “features” list of soroban-sdk in the Cargo.toml file:
alloc/Cargo.toml
Code
alloc/src/lib.rs
How it Works
alloc crate, which is required in order to support allocation under no_std. See Contract Rust dialect for more info about no_std.
v1 with contents allocated on the heap memory.
The heap memory in the context of a smart contract actually refers to the Wasm linear memory. The
alloc will use the global allocator provided by the soroban sdk to interact with the linear memory.(0..count) to v1, then computes and returns their sum. This is the simplest example to illustrate how to use the allocator.