-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example that does allocation using the SDK's alloc function.
- Loading branch information
Showing
7 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "example_alloc" | ||
version = "0.0.0" | ||
authors = ["Stellar Development Foundation <[email protected]>"] | ||
license = "Apache-2.0" | ||
edition = "2021" | ||
publish = false | ||
rust-version = "1.65" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
doctest = false | ||
|
||
[dependencies] | ||
soroban-sdk = { workspace = true, features = ["alloc"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#![no_std] | ||
use soroban_sdk::{contract, contractimpl, Env}; | ||
|
||
extern crate alloc; | ||
|
||
#[contract] | ||
pub struct AllocContract; | ||
|
||
struct Large { | ||
x: u32, | ||
space: [u8;4096] | ||
} | ||
|
||
#[contractimpl] | ||
impl AllocContract { | ||
/// Allocates a temporary vector holding values (0..count), then computes | ||
/// and returns their sum. Also allocates these values in a "large" | ||
/// structure (with a bunch of pointless padding) to ensure the contract | ||
/// allocates lots of memory. | ||
pub fn sum(_env: Env, count: u32) -> u32 { | ||
let mut v1 = alloc::vec![]; | ||
for i in 0..count { | ||
v1.push(Large{x: i, space: [0u8; 4096]}) | ||
} | ||
v1.iter().map(|l| l.x + l.space[0] as u32).sum() | ||
} | ||
} |
Binary file not shown.