//! returns: 0 //! rw-data-size: 0 //! uses-std //! The default function takes the environment and two storage parameters. The //! compiler supplies one zero-filled region for each, sized from its type. use std::mem::heap; use std::sys; /// Fill every byte with `v`. fn fillBytes(bytes: &mut [u8; 64], v: u8) { for i in 0 .. bytes.len { set bytes[i] = v; } } /// Fill every word with `v`. fn fillWords(words: &mut [u32; 16], v: u32) { for i in 0 .. words.len { set words[i] = v; } } /// Check that all bytes equal `v`. fn bytesAre(bytes: &[u8; 64], v: u8) -> bool { for i in 0 .. bytes.len { if bytes[i] <> v { return false; } } return true; } /// Check that all words equal `v`. fn wordsAre(words: &[u32; 16], v: u32) -> bool { for i in 0 .. words.len { if words[i] <> v { return false; } } return true; } /// Allocate from a heap over the byte region, until it is used up. fn allocFrom(storage: *mut [u8; 64]) -> bool { let arena = heap::over(&mut storage[..]); let mut first: *mut [u8] = &mut []; try first.resize(8, 0, arena) catch { return false; }; // The region cannot hold 64 bytes besides the header of the heap. let mut second: *mut [u8] = &mut []; try second.resize(64, 0, arena) catch { return true; }; return false; } @default fn main(env: *sys::Env, bytes: *mut [u8; 64], words: *mut [u32; 16]) -> i32 { // Both regions start zeroed. assert bytesAre(bytes, 0); assert wordsAre(words, 0); // Filling one region leaves the other untouched, and the reverse. fillBytes(bytes, 0xAA); assert bytesAre(bytes, 0xAA); assert wordsAre(words, 0); fillWords(words, 0xFFFFFFFF); assert wordsAre(words, 0xFFFFFFFF); assert bytesAre(bytes, 0xAA); // The last element is writable and its neighbors are unaffected. fillBytes(bytes, 0); fillWords(words, 0); set bytes[63] = 7; set words[15] = 9; assert bytes[62] == 0; assert words[14] == 0; assert words[0] == 0; assert bytes[0] == 0; assert bytes[63] == 7; assert words[15] == 9; // The environment reaches the function. assert env.args.len >= 0; // A region backs an arena. fillBytes(bytes, 0); assert allocFrom(bytes); return 0; }