Skip to content

Commit

Permalink
Support no_std targets
Browse files Browse the repository at this point in the history
This adds `no_std` support by
- adding a default `std` feature for disabling `impl Error` on `no_std`.
- adding the Hashbrown dependency for `no_std` hash sets

Hashbrown also provides the `HashSet` implementation in `std`.
`std`'s `HashSet` is not in liballoc yet, though. By default, Hashbrown
uses AHash as the default hasher. AHash is much faster than `std`'s
SipHash, but is not as HashDoS resistant. That seemed not relevant to me.

Co-authored-by: Andreea Florescu <[email protected]>
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening and andreeaflorescu committed Nov 14, 2023
1 parent d5fe563 commit 71d27b6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
12 changes: 12 additions & 0 deletions .buildkite/custom-tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"tests": [
{
"test_name": "build-alloc",
"command": "rustup target add {target_platform}-unknown-none && env RUSTFLAGS=\"-D warnings\" cargo build --release --target {target_platform}-unknown-none --no-default-features --features alloc",
"platform": [
"x86_64",
"aarch64"
]
}
]
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Upcoming Release

## Added
- [[#68](https://github.com/rust-vmm/vm-fdt/pull/68)] Implemented `no_std`
support by adding a default `std` feature.

## Fixed
- [[#69](https://github.com/rust-vmm/vm-fdt/pull/69)] Fixed
`clippy::incorrect_partial_ord_impl_on_ord_type`.
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ edition = "2018"
repository = "https://github.com/rust-vmm/vm-fdt"
keywords = ["fdt"]

[dependencies]
hashbrown = { version = "0.14", optional = true }

[features]
default = ["std"]
std = []
alloc = ["hashbrown"]
# This feature was added as a way to exclude long running tests in development.
# `cargo test` does not run tests for all features by default. The vm-fdt
# developer can thus choose to "hide" a long running under this feature.
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2021 The Chromium OS Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause

#![cfg_attr(not(feature = "std"), no_std)]
#![deny(missing_docs)]

//! This crate provides the ability to manipulate Flattened Devicetree blobs.
Expand Down Expand Up @@ -81,6 +82,9 @@
//! # let dtb = create_fdt().unwrap();
//! ```
#[macro_use]
extern crate alloc;

mod writer;

pub use writer::Result as FdtWriterResult;
Expand Down
24 changes: 16 additions & 8 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
//! This module writes Flattened Devicetree blobs as defined here:
//! <https://devicetree-specification.readthedocs.io/en/stable/flattened-format.html>
use std::cmp::{Ord, Ordering};
use std::collections::{BTreeMap, HashSet};
use std::convert::TryInto;
use std::ffi::CString;
use std::fmt;
use std::mem::size_of_val;
use alloc::collections::BTreeMap;
use alloc::ffi::CString;
use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::{Ord, Ordering};
use core::convert::TryInto;
use core::fmt;
use core::mem::size_of_val;
#[cfg(feature = "std")]
use std::collections::HashSet;

#[cfg(all(feature = "alloc", not(feature = "std")))]
use hashbrown::HashSet;

use crate::{
FDT_BEGIN_NODE, FDT_END, FDT_END_NODE, FDT_MAGIC, FDT_PROP, NODE_NAME_MAX_LEN,
Expand Down Expand Up @@ -75,10 +82,11 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

/// Result of a FDT writer operation.
pub type Result<T> = std::result::Result<T, Error>;
pub type Result<T> = core::result::Result<T, Error>;

const FDT_HEADER_SIZE: usize = 40;
const FDT_VERSION: u32 = 17;
Expand Down Expand Up @@ -308,7 +316,7 @@ impl FdtWriter {

// Append `num_bytes` padding bytes (0x00).
fn pad(&mut self, num_bytes: usize) {
self.data.extend(std::iter::repeat(0).take(num_bytes));
self.data.extend(core::iter::repeat(0).take(num_bytes));
}

// Append padding bytes (0x00) until the length of data is a multiple of `alignment`.
Expand Down

0 comments on commit 71d27b6

Please sign in to comment.