Skip to content

Commit 1609a38

Browse files
committed
multiboot2-common: move more functionality to common
1 parent 0ba4ee6 commit 1609a38

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

multiboot2-common/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ impl From<DynSizedStructureError> for MemoryError {
204204

205205
// todo impl core::error::Error
206206

207+
/// Increases the given size to the next alignment boundary, if it is not a
208+
/// multiple of the alignment yet. This is relevant as in Rust's [type layout],
209+
/// the allocated size of a type is always a multiple of the alignment, even
210+
/// if the type is smaller.
211+
///
212+
/// [type layout]: https://doc.rust-lang.org/reference/type-layout.html
213+
#[must_use]
214+
pub const fn increase_to_alignment(size: usize) -> usize {
215+
let mask = ALIGNMENT - 1;
216+
(size + mask) & !mask
217+
}
218+
207219
#[cfg(test)]
208220
mod tests {
209221
use super::*;
@@ -241,4 +253,13 @@ mod tests {
241253
})
242254
);
243255
}
256+
257+
#[test]
258+
fn test_increase_to_alignment() {
259+
assert_eq!(increase_to_alignment(0), 0);
260+
assert_eq!(increase_to_alignment(1), 8);
261+
assert_eq!(increase_to_alignment(7), 8);
262+
assert_eq!(increase_to_alignment(8), 8);
263+
assert_eq!(increase_to_alignment(9), 16);
264+
}
244265
}

multiboot2/src/builder/information.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Exports item [`InformationBuilder`].
22
use crate::builder::AsBytes;
3-
use crate::util::increase_to_alignment;
43
use crate::{
54
BasicMemoryInfoTag, BootInformationHeader, BootLoaderNameTag, CommandLineTag,
65
EFIBootServicesNotExitedTag, EFIImageHandle32Tag, EFIImageHandle64Tag, EFIMemoryMapTag,
@@ -11,6 +10,7 @@ use alloc::vec::Vec;
1110
use core::fmt::{Display, Formatter};
1211
use core::mem::size_of;
1312
use core::ops::Deref;
13+
use multiboot2_common::increase_to_alignment;
1414
use multiboot2_common::ALIGNMENT;
1515

1616
/// Holds the raw bytes of a boot information built with [`InformationBuilder`]

multiboot2/src/tag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
//! - [`TagBytesRef`] + [`TagHeader`] --> [`GenericTag`]
1010
//! - [`GenericTag`] --> cast to desired tag
1111
12-
use crate::util::increase_to_alignment;
1312
use crate::{TagTrait, TagType, TagTypeId};
1413
use core::fmt::{Debug, Formatter};
1514
use core::mem;
1615
use core::ptr;
16+
use multiboot2_common::increase_to_alignment;
1717
use multiboot2_common::{BytesRef, DynSizedStructure, Header};
1818

1919
/// The common header that all tags have in common. This type is ABI compatible.

multiboot2/src/util.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use core::fmt;
44
use core::fmt::{Display, Formatter};
55
use core::str::Utf8Error;
6-
use multiboot2_common::ALIGNMENT;
6+
use multiboot2_common::{increase_to_alignment, ALIGNMENT};
77
#[cfg(feature = "alloc")]
88
use {
99
crate::{TagHeader, TagTrait},
@@ -91,17 +91,6 @@ pub fn parse_slice_as_string(bytes: &[u8]) -> Result<&str, StringError> {
9191
cstr.to_str().map_err(StringError::Utf8)
9292
}
9393

94-
/// Increases the given size to the next alignment boundary, if it is not a
95-
/// multiple of the alignment yet. This is relevant as in Rust's [type layout],
96-
/// the allocated size of a type is always a multiple of the alignment, even
97-
/// if the type is smaller.
98-
///
99-
/// [type layout]: https://doc.rust-lang.org/reference/type-layout.html
100-
pub const fn increase_to_alignment(size: usize) -> usize {
101-
let mask = ALIGNMENT - 1;
102-
(size + mask) & !mask
103-
}
104-
10594
#[cfg(test)]
10695
mod tests {
10796
use super::*;
@@ -134,15 +123,6 @@ mod tests {
134123
assert_eq!(parse_slice_as_string(b"hello\0foo"), Ok("hello"));
135124
}
136125

137-
#[test]
138-
fn test_increase_to_alignment() {
139-
assert_eq!(increase_to_alignment(0), 0);
140-
assert_eq!(increase_to_alignment(1), 8);
141-
assert_eq!(increase_to_alignment(7), 8);
142-
assert_eq!(increase_to_alignment(8), 8);
143-
assert_eq!(increase_to_alignment(9), 16);
144-
}
145-
146126
#[test]
147127
#[cfg(feature = "alloc")]
148128
fn test_new_boxed() {

0 commit comments

Comments
 (0)