Skip to content

Commit

Permalink
rust: compile readq/writeq only on 64-bit arch
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Guo <gary@garyguo.net>
  • Loading branch information
nbdd0121 committed Aug 24, 2021
1 parent 19fa6be commit 3676553
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions rust/kernel/io_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ pub struct IoMem<const SIZE: usize> {
}

macro_rules! define_read {
($name:ident, $try_name:ident, $type_name:ty) => {
($(#[$attr:meta])* $name:ident, $try_name:ident, $type_name:ty) => {
/// Reads IO data from the given offset known, at compile time.
///
/// If the offset is not known at compile time, the build will fail.
$(#[$attr])*
pub fn $name(&self, offset: usize) -> $type_name {
Self::check_offset::<$type_name>(offset);
let ptr = self.ptr.wrapping_add(offset);
Expand All @@ -77,6 +78,7 @@ macro_rules! define_read {
/// Reads IO data from the given offset.
///
/// It fails if/when the offset (plus the type size) is out of bounds.
$(#[$attr])*
pub fn $try_name(&self, offset: usize) -> Result<$type_name> {
if !Self::offset_ok::<$type_name>(offset) {
return Err(Error::EINVAL);
Expand All @@ -91,10 +93,11 @@ macro_rules! define_read {
}

macro_rules! define_write {
($name:ident, $try_name:ident, $type_name:ty) => {
($(#[$attr:meta])* $name:ident, $try_name:ident, $type_name:ty) => {
/// Writes IO data to the given offset, known at compile time.
///
/// If the offset is not known at compile time, the build will fail.
$(#[$attr])*
pub fn $name(&self, value: $type_name, offset: usize) {
Self::check_offset::<$type_name>(offset);
let ptr = self.ptr.wrapping_add(offset);
Expand All @@ -107,6 +110,7 @@ macro_rules! define_write {
/// Writes IO data to the given offset.
///
/// It fails if/when the offset (plus the type size) is out of bounds.
$(#[$attr])*
pub fn $try_name(&self, value: $type_name, offset: usize) -> Result {
if !Self::offset_ok::<$type_name>(offset) {
return Err(Error::EINVAL);
Expand Down Expand Up @@ -176,12 +180,22 @@ impl<const SIZE: usize> IoMem<SIZE> {
define_read!(readb, try_readb, u8);
define_read!(readw, try_readw, u16);
define_read!(readl, try_readl, u32);
define_read!(readq, try_readq, u64);
define_read!(
#[cfg(target_pointer_width = "64")]
readq,
try_readq,
u64
);

define_write!(writeb, try_writeb, u8);
define_write!(writew, try_writew, u16);
define_write!(writel, try_writel, u32);
define_write!(writeq, try_writeq, u64);
define_write!(
#[cfg(target_pointer_width = "64")]
writeq,
try_writeq,
u64
);
}

impl<const SIZE: usize> Drop for IoMem<SIZE> {
Expand Down

0 comments on commit 3676553

Please sign in to comment.