Skip to content

Commit 6076ae7

Browse files
zdivelbissIsaacWoods
authored andcommitted
clean up allocator_api feature
1 parent 24ef7f7 commit 6076ae7

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
lines changed

acpi/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ rsdp = { version = "2", path = "../rsdp" }
1616

1717
[features]
1818
default = ["allocator_api"]
19-
allocator_api = []
19+
allocator_api = []

acpi/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
#![no_std]
5151
#![deny(unsafe_op_in_unsafe_fn)]
52-
#![cfg_attr(feature = "allocator_api", feature(allocator_api, ptr_as_uninit))]
52+
#![cfg_attr(feature = "allocator_api", feature(allocator_api))]
5353

5454
#[cfg_attr(test, macro_use)]
5555
#[cfg(test)]
@@ -68,16 +68,17 @@ mod managed_slice;
6868
#[cfg(feature = "allocator_api")]
6969
pub use managed_slice::*;
7070

71-
#[cfg(feature = "allocator_api")]
72-
pub use crate::platform::{interrupt::InterruptModel, PlatformInfo};
7371
#[cfg(feature = "allocator_api")]
7472
pub mod platform;
73+
#[cfg(feature = "allocator_api")]
74+
pub use crate::platform::{interrupt::InterruptModel, PlatformInfo};
7575

7676
#[cfg(feature = "allocator_api")]
7777
pub use crate::mcfg::PciConfigRegions;
7878

79-
pub use crate::{fadt::PowerProfile, hpet::HpetInfo, madt::MadtError};
80-
79+
pub use fadt::PowerProfile;
80+
pub use hpet::HpetInfo;
81+
pub use madt::MadtError;
8182
pub use rsdp::{
8283
handler::{AcpiHandler, PhysicalMapping},
8384
RsdpError,
@@ -321,9 +322,9 @@ where
321322
/// first things you should usually do with an `AcpiTables`, and allows to collect helpful information about
322323
/// the platform from the ACPI tables.
323324
#[cfg(feature = "allocator_api")]
324-
pub fn platform_info_in<'a, A>(&'a self, allocator: &'a A) -> AcpiResult<PlatformInfo<A>>
325+
pub fn platform_info_in<A>(&self, allocator: A) -> AcpiResult<PlatformInfo<A>>
325326
where
326-
A: core::alloc::Allocator,
327+
A: core::alloc::Allocator + Clone,
327328
{
328329
PlatformInfo::new_in(self, allocator)
329330
}

acpi/src/madt.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ impl Madt {
5252
#[cfg(feature = "allocator_api")]
5353
pub fn parse_interrupt_model_in<'a, A>(
5454
&self,
55-
allocator: &'a A,
55+
allocator: A,
5656
) -> AcpiResult<(InterruptModel<'a, A>, Option<ProcessorInfo<'a, A>>)>
5757
where
58-
A: core::alloc::Allocator,
58+
A: core::alloc::Allocator + Clone,
5959
{
6060
/*
6161
* We first do a pass through the MADT to determine which interrupt model is being used.
@@ -97,10 +97,10 @@ impl Madt {
9797
#[cfg(feature = "allocator_api")]
9898
fn parse_apic_model_in<'a, A>(
9999
&self,
100-
allocator: &'a A,
100+
allocator: A,
101101
) -> AcpiResult<(InterruptModel<'a, A>, Option<ProcessorInfo<'a, A>>)>
102102
where
103-
A: core::alloc::Allocator,
103+
A: core::alloc::Allocator + Clone,
104104
{
105105
use crate::{
106106
platform::{
@@ -138,10 +138,10 @@ impl Madt {
138138
}
139139
}
140140

141-
let mut io_apics = crate::ManagedSlice::new_in(io_apic_count, allocator)?;
142-
let mut interrupt_source_overrides = crate::ManagedSlice::new_in(iso_count, allocator)?;
143-
let mut nmi_sources = crate::ManagedSlice::new_in(nmi_source_count, allocator)?;
144-
let mut local_apic_nmi_lines = crate::ManagedSlice::new_in(local_nmi_line_count, allocator)?;
141+
let mut io_apics = crate::ManagedSlice::new_in(io_apic_count, allocator.clone())?;
142+
let mut interrupt_source_overrides = crate::ManagedSlice::new_in(iso_count, allocator.clone())?;
143+
let mut nmi_sources = crate::ManagedSlice::new_in(nmi_source_count, allocator.clone())?;
144+
let mut local_apic_nmi_lines = crate::ManagedSlice::new_in(local_nmi_line_count, allocator.clone())?;
145145
let mut application_processors =
146146
crate::ManagedSlice::new_in(processor_count.saturating_sub(1), allocator)?; // Subtract one for the BSP
147147
let mut boot_processor = None;

acpi/src/managed_slice.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@ where
88
A: alloc::Allocator,
99
{
1010
slice: &'a mut [T],
11-
allocator: &'a A,
11+
allocator: A,
1212
}
1313

1414
impl<'a, T, A> ManagedSlice<'a, T, A>
1515
where
1616
A: alloc::Allocator,
1717
{
1818
/// Attempts to allocate a new `&mut [T]` in the given allocator.
19-
pub fn new_in(len: usize, allocator: &'a A) -> crate::AcpiResult<Self> {
20-
// Safety: Struct layouts are required to be valid.
21-
let layout =
22-
unsafe { alloc::Layout::from_size_align_unchecked(mem::size_of::<T>() * len, mem::align_of::<T>()) };
23-
24-
unsafe { allocator.allocate(layout).map(|ptr| ptr.as_uninit_slice_mut().align_to_mut::<T>().1) }
25-
.map(|slice| Self { slice, allocator })
26-
.map_err(|_| crate::AcpiError::AllocError)
19+
pub fn new_in(len: usize, allocator: A) -> crate::AcpiResult<Self> {
20+
// Safety: Type automatically deallocated memory on `Drop` and;
21+
// Constructed slice is from valid, aligned, allocated memory.
22+
unsafe {
23+
allocator
24+
.allocate(alloc::Layout::array::<T>(len).map_err(|_| crate::AcpiError::AllocError)?)
25+
.map(|mut ptr| core::slice::from_raw_parts_mut(ptr.as_mut().as_mut_ptr().cast(), len))
26+
.map(|slice| Self { slice, allocator })
27+
.map_err(|_| crate::AcpiError::AllocError)
28+
}
2729
}
2830
}
2931

acpi/src/mcfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'a, A> PciConfigRegions<'a, A>
1919
where
2020
A: core::alloc::Allocator,
2121
{
22-
pub fn new_in<H>(tables: &crate::AcpiTables<H>, allocator: &'a A) -> crate::AcpiResult<PciConfigRegions<'a, A>>
22+
pub fn new_in<H>(tables: &crate::AcpiTables<H>, allocator: A) -> crate::AcpiResult<PciConfigRegions<'a, A>>
2323
where
2424
H: crate::AcpiHandler,
2525
{

acpi/src/platform/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ where
102102

103103
impl<'a, A> PlatformInfo<'a, A>
104104
where
105-
A: Allocator,
105+
A: Allocator + Clone,
106106
{
107-
pub fn new_in<H>(tables: &AcpiTables<H>, allocator: &'a A) -> crate::AcpiResult<Self>
107+
pub fn new_in<H>(tables: &AcpiTables<H>, allocator: A) -> crate::AcpiResult<Self>
108108
where
109109
H: AcpiHandler,
110110
{

0 commit comments

Comments
 (0)