Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Stable Memory always use 64-bit addresses and stable64_* system API. #498

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ic-cdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

### Changed

- BREAKING: Stable Memory always use 64-bit addresses and `stable64_*` system API.

## [0.14.0] - 2024-05-17
## [0.13.3] - 2024-05-10 (yanked)

Expand Down
37 changes: 4 additions & 33 deletions src/ic-cdk/src/api/stable/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,12 @@ use super::*;
pub struct CanisterStableMemory {}

impl StableMemory for CanisterStableMemory {
fn stable_size(&self) -> u32 {
// SAFETY: ic0.stable_size is always safe to call.
unsafe { ic0::stable_size() as u32 }
}

fn stable64_size(&self) -> u64 {
fn stable_size(&self) -> u64 {
// SAFETY: ic0.stable64_size is always safe to call.
unsafe { ic0::stable64_size() as u64 }
}

fn stable_grow(&self, new_pages: u32) -> Result<u32, StableMemoryError> {
// SAFETY: ic0.stable_grow is always safe to call.
unsafe {
match ic0::stable_grow(new_pages as i32) {
-1 => Err(StableMemoryError::OutOfMemory),
x => Ok(x as u32),
}
}
}

fn stable64_grow(&self, new_pages: u64) -> Result<u64, StableMemoryError> {
fn stable_grow(&self, new_pages: u64) -> Result<u64, StableMemoryError> {
// SAFETY: ic0.stable64_grow is always safe to call.
unsafe {
match ic0::stable64_grow(new_pages as i64) {
Expand All @@ -37,28 +22,14 @@ impl StableMemory for CanisterStableMemory {
}
}

fn stable_write(&self, offset: u32, buf: &[u8]) {
// SAFETY: `buf`, being &[u8], is a readable sequence of bytes, and therefore valid to pass to ic0.stable_write.
unsafe {
ic0::stable_write(offset as i32, buf.as_ptr() as i32, buf.len() as i32);
}
}

fn stable64_write(&self, offset: u64, buf: &[u8]) {
fn stable_write(&self, offset: u64, buf: &[u8]) {
// SAFETY: `buf`, being &[u8], is a readable sequence of bytes, and therefore valid to pass to ic0.stable64_write.
unsafe {
ic0::stable64_write(offset as i64, buf.as_ptr() as i64, buf.len() as i64);
}
}

fn stable_read(&self, offset: u32, buf: &mut [u8]) {
// SAFETY: `buf`, being &mut [u8], is a writable sequence of bytes, and therefore valid to pass to ic0.stable_read.
unsafe {
ic0::stable_read(buf.as_ptr() as i32, offset as i32, buf.len() as i32);
}
}

fn stable64_read(&self, offset: u64, buf: &mut [u8]) {
fn stable_read(&self, offset: u64, buf: &mut [u8]) {
// SAFETY: `buf`, being &mut [u8], is a writable sequence of bytes, and therefore valid to pass to ic0.stable64_read.
unsafe {
ic0::stable64_read(buf.as_ptr() as i64, offset as i64, buf.len() as i64);
Expand Down
Loading