Skip to content

Commit

Permalink
cpuid crate refactoring - part 2
Browse files Browse the repository at this point in the history
- move leaf handlers into separate functions
- test leaf handlers individually
- move all the templates into a folder

Signed-off-by: Serban Iorga <seriorga@amazon.com>
  • Loading branch information
Serban Iorga committed Feb 25, 2019
1 parent 3157cc5 commit 20f9be1
Show file tree
Hide file tree
Showing 12 changed files with 573 additions and 1,238 deletions.
26 changes: 26 additions & 0 deletions cpuid/src/bit_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl BitRangeExt<u32> for BitRange {
/// Trait containing helper methods for bit operations.
///
pub trait BitHelper {
/// Reads the value of the bit at position `pos`
///
fn read_bit(&self, pos: u32) -> bool;

/// Changes the value of the bit at position `pos` to `val`
///
fn write_bit(&mut self, pos: u32, val: bool) -> &mut Self;
Expand Down Expand Up @@ -144,6 +148,12 @@ pub trait BitHelper {
}

impl BitHelper for u32 {
fn read_bit(&self, pos: u32) -> bool {
assert!(pos <= MAX_U32_BIT_INDEX, "Invalid pos");

(*self & (1 << pos)) > 0
}

fn write_bit(&mut self, pos: u32, val: bool) -> &mut Self {
assert!(pos <= MAX_U32_BIT_INDEX, "Invalid pos");

Expand Down Expand Up @@ -215,6 +225,22 @@ mod tests {
assert!(val == 0);
}

#[test]
#[should_panic]
fn test_invalid_read_bit() {
// Set bit to 1
let val: u32 = 0;
val.read_bit(32);
}

#[test]
fn test_simple_read_bit() {
// Set bit to 1
let val: u32 = 0b100000;
assert!(val.read_bit(5));
assert!(!val.read_bit(4));
}

#[test]
fn test_chained_write_bit() {
let mut val: u32 = 1 << 12;
Expand Down
3 changes: 2 additions & 1 deletion cpuid/src/brand_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::arch::x86_64::__cpuid as host_cpuid;
use std::slice;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub enum Error {
NotSupported,
Overflow(String),
Expand All @@ -23,6 +23,7 @@ pub enum Reg {
/// This is achieved by bypassing the `O(n)` indexing, heap allocation, and the unicode checks
/// done by `std::string::String`.
///
#[derive(Clone)]
pub struct BrandString {
/// Flattened buffer, holding an array of 32-bit register values.
///
Expand Down
4 changes: 2 additions & 2 deletions cpuid/src/cpu_leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ pub mod leaf_0x4 {
msb_index: 7,
lsb_index: 5,
};
pub const MAX_ADDR_IDS_SHARING_CACHE_BITRANGE: BitRange = BitRange {
pub const MAX_CPUS_PER_CORE: BitRange = BitRange {
msb_index: 25,
lsb_index: 14,
};
pub const MAX_ADDR_IDS_IN_PACKAGE_BITRANGE: BitRange = BitRange {
pub const MAX_CORES_PER_PACKAGE: BitRange = BitRange {
msb_index: 31,
lsb_index: 26,
};
Expand Down
Loading

0 comments on commit 20f9be1

Please sign in to comment.