@@ -45,34 +45,39 @@ const WASM_PAGE_SIZE: u64 = 65536;
45
45
/// The maximum number of stable memory pages a canister can address.
46
46
pub const MAX_PAGES : u64 = u64:: MAX / WASM_PAGE_SIZE ;
47
47
48
+ /// Abstraction over a WebAssembly-style linear memory (e.g., stable memory).
49
+ ///
50
+ /// Implementations are expected to mirror WebAssembly semantics:
51
+ /// out-of-bounds accesses will cause a panic (in native) or trap (in Wasm).
48
52
pub trait Memory {
49
- /// Returns the current size of the stable memory in WebAssembly
50
- /// pages. (One WebAssembly page is 64Ki bytes.)
53
+ /// Returns the current size of the memory in WebAssembly pages.
54
+ ///
55
+ /// One WebAssembly page is 64 KiB.
51
56
fn size ( & self ) -> u64 ;
52
57
53
- /// Tries to grow the memory by `pages` many pages containing
54
- /// zeroes. If successful, returns the previous size of the
55
- /// memory (in pages). Otherwise, returns -1.
58
+ /// Grows the memory by `pages` pages filled with zeroes.
59
+ ///
60
+ /// Returns the previous size in pages on success, or -1 if the
61
+ /// memory could not be grown.
56
62
fn grow ( & self , pages : u64 ) -> i64 ;
57
63
58
- /// Copies the data referred to by `offset` out of the stable memory
59
- /// and replaces the corresponding bytes in `dst`.
64
+ /// Copies `dst.len()` bytes from memory starting at `offset` into `dst`.
65
+ ///
66
+ /// Panics or traps if the read would go out of bounds.
60
67
fn read ( & self , offset : u64 , dst : & mut [ u8 ] ) ;
61
68
62
- /// Copies `count` number of bytes of the data starting from `offset` out of the stable memory
63
- /// into the buffer starting at `dst`.
69
+ /// Unsafe variant of `read` for advanced use.
64
70
///
65
- /// This method is an alternative to `read` which does not require initializing a buffer and may
66
- /// therefore be faster .
71
+ /// Copies `count` bytes from memory starting at `offset` into the
72
+ /// raw pointer `dst`. Initializes the destination before reading .
67
73
///
68
74
/// # Safety
69
75
///
70
- /// Callers must guarantee that
71
- /// * it is valid to write `count` number of bytes starting from `dst`,
72
- /// * `dst..dst + count` does not overlap with `self`.
76
+ /// Caller must ensure:
77
+ /// - `dst` points to valid writable memory of at least `count` bytes.
78
+ /// - The memory range `dst..dst+ count` does not overlap with `self`.
73
79
///
74
- /// Implementations must guarantee that before the method returns, `count` number of bytes
75
- /// starting from `dst` will be initialized.
80
+ /// Panics or traps if the read would go out of bounds.
76
81
#[ inline]
77
82
unsafe fn read_unsafe ( & self , offset : u64 , dst : * mut u8 , count : usize ) {
78
83
// Initialize the buffer to make the slice valid.
@@ -81,8 +86,9 @@ pub trait Memory {
81
86
self . read ( offset, slice)
82
87
}
83
88
84
- /// Copies the data referred to by `src` and replaces the
85
- /// corresponding segment starting at offset in the stable memory.
89
+ /// Copies `src.len()` bytes from `src` into memory starting at `offset`.
90
+ ///
91
+ /// Panics or traps if the write would go out of bounds.
86
92
fn write ( & self , offset : u64 , src : & [ u8 ] ) ;
87
93
}
88
94
0 commit comments