Skip to content

Commit

Permalink
feat!: Stable Memory always use 64-bit addresses and stable64_* sys…
Browse files Browse the repository at this point in the history
…tem API. (#498)

* feat!: only use stable64 API

* changelog

* clippy
  • Loading branch information
lwshang authored Jul 1, 2024
1 parent 8857ee4 commit 1a1cb07
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 298 deletions.
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

0 comments on commit 1a1cb07

Please sign in to comment.