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

Eliminate some simple unsafe operations #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions rtt-target/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ pub enum ChannelMode {
BlockIfFull = 2,
}

impl From<usize> for ChannelMode {
fn from(value: usize) -> Self {
match value {
x if x == Self::NoBlockSkip as usize => Self::NoBlockSkip,
x if x == Self::NoBlockTrim as usize => Self::NoBlockTrim,
x if x == Self::BlockIfFull as usize => Self::BlockIfFull,
_ => Self::NoBlockSkip, // default value
}
}
}

/// An up channel that supports writing into multiple virtual terminals within the same buffer.
///
/// An [`UpChannel`] can be turned into a `TerminalChannel` by using the
Expand Down
19 changes: 5 additions & 14 deletions rtt-target/src/rtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ impl RttHeader {

// Copy the ID in two parts to avoid having the ID string in memory in full. The ID is
// copied last to make it less likely an unfinished control block is detected by the host.
const ID_LOWER: &[u8] = b"SEGG_";
const ID_UPPER: &[u8] = b"ER RTT\0\0\0\0\0\0";

ptr::copy_nonoverlapping(b"SEGG_" as *const u8, self.id.as_mut_ptr(), 5);

self.id[..ID_LOWER.len()].copy_from_slice(ID_LOWER);
fence(SeqCst);

ptr::copy_nonoverlapping(
b"ER RTT\0\0\0\0\0\0" as *const u8,
self.id.as_mut_ptr().offset(4),
12,
);
self.id[ID_LOWER.len() - 1..].copy_from_slice(ID_UPPER);
}

pub fn max_up_channels(&self) -> usize {
Expand Down Expand Up @@ -80,12 +76,7 @@ impl RttChannel {

pub(crate) fn mode(&self) -> ChannelMode {
let mode = self.flags.load(SeqCst) & 3;

if mode <= 2 {
unsafe { core::mem::transmute(mode) }
} else {
ChannelMode::NoBlockSkip
}
mode.into()
}

pub(crate) fn set_mode(&self, mode: ChannelMode) {
Expand Down