std/mem — Memory blocks¶
import std/mem
Routines that operate on raw byte blocks in SRAM, plus a helper to copy from program memory. Lengths are byte counts; pointers are caller-owned.
- @memcpy($dest: ptr ram u8, $src: ptr ram u8, $len: u16)¶
Copy
$lenbytes from$srcto$destin SRAM. The regions must not overlap; use@memmoveif they might.
- @memmove($dest: ptr ram u8, $src: ptr ram u8, $len: u16)¶
Copy
$lenbytes from$srcto$dest, correctly handling the case where the two regions overlap.
- @memcpy_P($dest: ptr ram u8, $src: ptr flash u8, $len: u16)¶
Copy
$lenbytes from program (Flash) memory$srcinto SRAM$dest. This is how you bringflash-resident data (such as aflash str) into SRAM where the rest of the library can use it.
- @memset($dest: ptr ram u8, $val: u8, $len: u16)¶
Fill the first
$lenbytes at$destwith the constant byte$val.
- @memcmp($s1: ptr ram u8, $s2: ptr ram u8, $len: u16) -> i16¶
Compare the first
$lenbytes of$s1and$s2. Return0if they are identical, otherwise the signed difference of the first differing pair of bytes.
- @memchr($s: ptr ram u8, $c: u8, $n: u16) -> ptr ram u8¶
Return a pointer to the first occurrence of byte
$cwithin the first$nbytes of$s, or0if not found.
- @memrchr($s: ptr ram u8, $c: u8, $n: u16) -> ptr ram u8¶
Return a pointer to the last occurrence of byte
$cwithin the first$nbytes of$s, or0.
- @memccpy($dest: ptr ram u8, $src: ptr ram u8, $c: u8, $len: u16) -> ptr ram u8¶
Copy bytes from
$srcto$dest, stopping after the first byte equal to$cis copied. Return a pointer to the byte in$destjust past the copied$c, or0if$cdid not appear within$lenbytes.
Example¶
import std/mem
@main {
ram mut $a: u8[8] = 0 # two 8-byte blocks
ram mut $b: u8[8] = 0
@memset(&$a[0], 0xFF, 8) # fill block a with 0xFF
@memcpy(&$b[0], &$a[0], 8) # copy a -> b (regions must not overlap)
ram imut $same: i16 = @memcmp(&$a[0], &$b[0], 8) # 0 -> the blocks are identical
loop * { }
}