Skip to content

Commit

Permalink
add back some #[inline]s
Browse files Browse the repository at this point in the history
  • Loading branch information
kadiwa4 committed Feb 13, 2024
1 parent 960f354 commit 5865015
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 29 deletions.
2 changes: 2 additions & 0 deletions deforest_derive/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Derive macro for `deforest`, a `#![no_std]` devicetree parser.
use proc_macro2::{Literal, TokenStream, TokenTree};
use quote::{quote, ToTokens};
use syn::{
Expand Down
7 changes: 5 additions & 2 deletions src/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Features needing memory allocation.
//! Features depending on memory allocation.
use core::{
iter::Map,
Expand Down Expand Up @@ -41,7 +41,10 @@ impl<'a> DevicetreeBuilder<'a> {
}
}

/// Constructs the devicetree. Returns `None` if it is too large.
/// Constructs the devicetree.
///
/// # Errors
/// Throws [`Error::DevicetreeTooLarge`] if it is too large.
pub fn build(&self) -> Result<Box<Devicetree>> {
let struct_offset = blob::Header::SIZE
+ size_of_val(self.mem_reserve_entries)
Expand Down
10 changes: 9 additions & 1 deletion src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
type Result<T, E = BlobError> = core::result::Result<T, E>;

pub(crate) const DTB_OPTIMAL_ALIGN: usize = 8;
/// 4-byte magic signature of Devicetree blobs.
/// 4-byte magic signature of devicetree blobs.
pub const DTB_MAGIC: [u8; 4] = 0xd00d_feed_u32.to_be_bytes();
pub(crate) const LAST_COMPATIBLE_VERSION: u32 = 16;
const MEM_RESERVE_BLOCK_OPTIMAL_ALIGN: usize = 8;
Expand Down Expand Up @@ -119,6 +119,7 @@ impl Devicetree {
}

#[cfg(feature = "alloc")]
#[inline]
pub(crate) unsafe fn from_box_unchecked(blob: Box<[u64]>) -> Box<Self> {
Box::from_raw(Box::into_raw(blob) as *mut Self)
}
Expand Down Expand Up @@ -220,6 +221,7 @@ impl Devicetree {
Ok(())
}

#[inline]
fn header(&self) -> &Header {
// Safety: length check was done in Self::totalsize
unsafe { &*(self as *const _ as *const Header) }
Expand All @@ -243,6 +245,7 @@ impl Devicetree {
/// The last version compatible with this devicetree blob's version.
///
/// Currently required to be 16.
#[inline]
pub fn last_comp_version(&self) -> u32 {
LAST_COMPATIBLE_VERSION
}
Expand All @@ -253,16 +256,19 @@ impl Devicetree {
}

/// The blob data as a `u8` slice.
#[inline]
pub fn blob_u8(&self) -> &[u8] {
self.blob.as_bytes()
}

/// The blob data as a `u32` slice.
#[inline]
pub fn blob_u32(&self) -> &[u32] {
Ref::new_slice(self.blob_u8()).unwrap().into_slice()
}

/// The blob data as a `u64` slice.
#[inline]
pub fn blob(&self) -> &[u64] {
&self.blob
}
Expand Down Expand Up @@ -361,6 +367,7 @@ impl Debug for Devicetree {
}

impl<'a> From<&'a Devicetree> for &'a [u64] {
#[inline]
fn from(dt: &'a Devicetree) -> Self {
&dt.blob
}
Expand Down Expand Up @@ -391,6 +398,7 @@ impl<'dtb> Property<'dtb> {
}

/// The property's value.
#[inline]
pub fn value(self) -> &'dtb [u8] {
debug_assert_eq!(self.value.as_ptr() as usize % 4, 0);
self.value
Expand Down
32 changes: 23 additions & 9 deletions src/blob/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct Node<'dtb> {

impl<'dtb> Node<'dtb> {
/// The devicetree containing this node.
#[inline]
pub fn devicetree(&self) -> &'dtb Devicetree {
self.dt
}
Expand Down Expand Up @@ -64,6 +65,7 @@ impl<'dtb> Node<'dtb> {
}

/// Cursor pointing to the first [`Token`] inside this node.
#[inline]
pub fn content_cursor(&self) -> Cursor {
self.contents
}
Expand Down Expand Up @@ -224,6 +226,7 @@ impl<'dtb> Items<'dtb> {
/// Creates a new iterator over the [`Item`]s contained in a node.
///
/// The cursor has to be inside the node.
#[inline]
pub fn new(node: &Node<'dtb>, cursor: Cursor) -> Self {
debug_assert!(node.contents <= cursor && node.contents.depth <= cursor.depth);
Self {
Expand All @@ -234,6 +237,7 @@ impl<'dtb> Items<'dtb> {
}

/// The cursor has to be inside the node.
#[inline]
pub fn set_cursor(&mut self, cursor: Cursor) {
debug_assert!(self.at_depth <= cursor.depth);
self.cursor = cursor;
Expand Down Expand Up @@ -279,11 +283,13 @@ impl<'dtb> Properties<'dtb> {
/// Creates an iterator over the [`Property`]s contained in a node.
///
/// The cursor has to be inside the node.
#[inline]
pub fn new(dt: &'dtb Devicetree, cursor: Cursor) -> Self {
Self { dt, cursor }
}

/// Cursor pointing to the next [`Token`].
#[inline]
pub fn cursor(&self) -> Cursor {
self.cursor
}
Expand Down Expand Up @@ -324,11 +330,13 @@ impl<'dtb> Children<'dtb> {
/// Creates an iterator over the child [`Node`]s contained in a node.
///
/// The cursor has to be inside the node.
#[inline]
pub fn new(node: &Node<'dtb>, cursor: Cursor) -> Self {
Self(Items::new(node, cursor))
}

/// The cursor has to be inside the node.
#[inline]
pub fn set_cursor(&mut self, cursor: Cursor) {
self.0.set_cursor(cursor);
}
Expand Down Expand Up @@ -449,14 +457,17 @@ impl<'dtb> NamedRange<'dtb> {
}

/// Cursor pointing to the first node's [`Token`].
#[inline]
pub fn first(self) -> Option<Cursor> {
self.0.map(|(_, b)| b.first())
}

#[inline]
pub fn len(self) -> usize {
self.0.map_or(0, |(_, b)| b.len as usize)
}

#[inline]
pub fn is_empty(self) -> bool {
self.0.is_none()
}
Expand Down Expand Up @@ -484,6 +495,7 @@ impl Hash for BaseRange {
}
}
impl BaseRange {
#[inline]
fn first(self) -> Cursor {
Cursor {
depth: self.depth,
Expand All @@ -509,6 +521,16 @@ impl BaseRange {
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct NamedRangeIter<'dtb>(Option<NamedRangeIterInner<'dtb>>);

impl<'dtb> NamedRangeIter<'dtb> {
/// Default empty iterator.
pub const EMPTY: Self = Self(None);

#[inline]
pub fn remaining_len(&self) -> u32 {
self.0.as_ref().map_or(0, |i| i.len)
}
}

impl<'dtb> FallibleIterator for NamedRangeIter<'dtb> {
type Item = Node<'dtb>;
type Error = Error;
Expand All @@ -527,21 +549,13 @@ impl<'dtb> FallibleIterator for NamedRangeIter<'dtb> {
res
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.remaining_len() as usize;
(len, Some(len))
}
}

impl<'dtb> NamedRangeIter<'dtb> {
/// Default empty iterator.
pub const EMPTY: Self = Self(None);

pub fn remaining_len(&self) -> u32 {
self.0.as_ref().map_or(0, |i| i.len)
}
}

#[derive(Clone, Debug)]
struct NamedRangeIterInner<'dtb> {
children: Children<'dtb>,
Expand Down
3 changes: 3 additions & 0 deletions src/blob/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ pub struct Cursor {
}

impl PartialOrd for Cursor {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(Ord::cmp(self, other))
}
}

impl Ord for Cursor {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
let res = Ord::cmp(&self.offset, &other.offset);
if res == Ordering::Equal {
Expand All @@ -62,6 +64,7 @@ impl Ord for Cursor {
}

impl PartialEq for Cursor {
#[inline]
fn eq(&self, other: &Self) -> bool {
Ord::cmp(self, other) == Ordering::Equal
}
Expand Down
32 changes: 18 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,19 @@ pub enum BlobError {

impl Display for BlobError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
use BlobError::*;

let description = match *self {
BlockOutOfBounds => "block out of bounds",
IncompatibleVersion => "incompatible devicetree version",
InvalidPropertyHeader => "invalid property header",
InvalidRootNode => "invalid root node",
InvalidString => "invalid string",
InvalidTotalsize => "invalid totalsize field",
NoMagicSignature => "no magic signature",
UnalignedBlock => "unaligned block",
UnexpectedEnd => "unexpected end",
UnexpectedEndToken => "unexpected END token",
UnexpectedEndNodeToken => "unexpected END_NODE token",
UnknownToken => "unknown token",
Self::BlockOutOfBounds => "block out of bounds",
Self::IncompatibleVersion => "incompatible devicetree version",
Self::InvalidPropertyHeader => "invalid property header",
Self::InvalidRootNode => "invalid root node",
Self::InvalidString => "invalid string",
Self::InvalidTotalsize => "invalid totalsize field",
Self::NoMagicSignature => "no magic signature",
Self::UnalignedBlock => "unaligned block",
Self::UnexpectedEnd => "unexpected end",
Self::UnexpectedEndToken => "unexpected END token",
Self::UnexpectedEndNodeToken => "unexpected END_NODE token",
Self::UnknownToken => "unknown token",
};
write!(f, "devicetree blob error: {description}")
}
Expand All @@ -117,6 +115,7 @@ impl Display for BlobError {
impl std::error::Error for BlobError {}

impl From<BlobError> for Error {
#[inline]
fn from(err: BlobError) -> Self {
Self::Blob(err)
}
Expand Down Expand Up @@ -227,6 +226,7 @@ pub struct NodeContext<'a> {
}

impl Default for NodeContext<'_> {
#[inline]
fn default() -> Self {
Self {
custom: None,
Expand Down Expand Up @@ -290,6 +290,7 @@ pub trait DeserializeProperty<'dtb>: Sized {
}

impl<'dtb> DeserializeProperty<'dtb> for Property<'dtb> {
#[inline]
fn deserialize(blob_prop: Property<'dtb>, _cx: NodeContext<'_>) -> Result<Self> {
Ok(blob_prop)
}
Expand All @@ -314,6 +315,7 @@ impl<'dtb> DeserializeProperty<'dtb> for bool {
}

impl<'dtb> DeserializeProperty<'dtb> for &'dtb [u8] {
#[inline]
fn deserialize(blob_prop: Property<'dtb>, _cx: NodeContext<'_>) -> Result<Self> {
Ok(blob_prop.value())
}
Expand Down Expand Up @@ -474,12 +476,14 @@ pub mod util {

/// Same as `<[_]>::get` with a range except that it takes a length, not an end
/// offset.
#[inline]
pub(crate) fn slice_get_with_len<T>(slice: &[T], offset: usize, len: usize) -> Option<&[T]> {
slice.get(offset..offset + len)
}

/// Same as `<[_]>::get_unchecked` with a range except that it takes a length,
/// not an end offset.
#[inline]
pub(crate) unsafe fn slice_get_with_len_unchecked<T>(
slice: &[T],
offset: usize,
Expand Down
3 changes: 2 additions & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Higher level of abstraction.
//! Higher level of abstraction. This is very incomplete; consider creating your
//! own types instead.
use crate::{
blob::{self, Cursor, Devicetree},
Expand Down
Loading

0 comments on commit 5865015

Please sign in to comment.