Browse Source

clean up allocator_api feature

Zavier Divelbiss 2 years ago
parent
commit
6076ae7511
6 changed files with 31 additions and 28 deletions
  1. 1 1
      acpi/Cargo.toml
  2. 8 7
      acpi/src/lib.rs
  3. 8 8
      acpi/src/madt.rs
  4. 11 9
      acpi/src/managed_slice.rs
  5. 1 1
      acpi/src/mcfg.rs
  6. 2 2
      acpi/src/platform/mod.rs

+ 1 - 1
acpi/Cargo.toml

@@ -16,4 +16,4 @@ rsdp = { version = "2", path = "../rsdp" }
 
 [features]
 default = ["allocator_api"]
-allocator_api = []
+allocator_api = []

+ 8 - 7
acpi/src/lib.rs

@@ -49,7 +49,7 @@
 
 #![no_std]
 #![deny(unsafe_op_in_unsafe_fn)]
-#![cfg_attr(feature = "allocator_api", feature(allocator_api, ptr_as_uninit))]
+#![cfg_attr(feature = "allocator_api", feature(allocator_api))]
 
 #[cfg_attr(test, macro_use)]
 #[cfg(test)]
@@ -68,16 +68,17 @@ mod managed_slice;
 #[cfg(feature = "allocator_api")]
 pub use managed_slice::*;
 
-#[cfg(feature = "allocator_api")]
-pub use crate::platform::{interrupt::InterruptModel, PlatformInfo};
 #[cfg(feature = "allocator_api")]
 pub mod platform;
+#[cfg(feature = "allocator_api")]
+pub use crate::platform::{interrupt::InterruptModel, PlatformInfo};
 
 #[cfg(feature = "allocator_api")]
 pub use crate::mcfg::PciConfigRegions;
 
-pub use crate::{fadt::PowerProfile, hpet::HpetInfo, madt::MadtError};
-
+pub use fadt::PowerProfile;
+pub use hpet::HpetInfo;
+pub use madt::MadtError;
 pub use rsdp::{
     handler::{AcpiHandler, PhysicalMapping},
     RsdpError,
@@ -321,9 +322,9 @@ where
     /// first things you should usually do with an `AcpiTables`, and allows to collect helpful information about
     /// the platform from the ACPI tables.
     #[cfg(feature = "allocator_api")]
-    pub fn platform_info_in<'a, A>(&'a self, allocator: &'a A) -> AcpiResult<PlatformInfo<A>>
+    pub fn platform_info_in<A>(&self, allocator: A) -> AcpiResult<PlatformInfo<A>>
     where
-        A: core::alloc::Allocator,
+        A: core::alloc::Allocator + Clone,
     {
         PlatformInfo::new_in(self, allocator)
     }

+ 8 - 8
acpi/src/madt.rs

@@ -52,10 +52,10 @@ impl Madt {
     #[cfg(feature = "allocator_api")]
     pub fn parse_interrupt_model_in<'a, A>(
         &self,
-        allocator: &'a A,
+        allocator: A,
     ) -> AcpiResult<(InterruptModel<'a, A>, Option<ProcessorInfo<'a, A>>)>
     where
-        A: core::alloc::Allocator,
+        A: core::alloc::Allocator + Clone,
     {
         /*
          * We first do a pass through the MADT to determine which interrupt model is being used.
@@ -97,10 +97,10 @@ impl Madt {
     #[cfg(feature = "allocator_api")]
     fn parse_apic_model_in<'a, A>(
         &self,
-        allocator: &'a A,
+        allocator: A,
     ) -> AcpiResult<(InterruptModel<'a, A>, Option<ProcessorInfo<'a, A>>)>
     where
-        A: core::alloc::Allocator,
+        A: core::alloc::Allocator + Clone,
     {
         use crate::{
             platform::{
@@ -138,10 +138,10 @@ impl Madt {
             }
         }
 
-        let mut io_apics = crate::ManagedSlice::new_in(io_apic_count, allocator)?;
-        let mut interrupt_source_overrides = crate::ManagedSlice::new_in(iso_count, allocator)?;
-        let mut nmi_sources = crate::ManagedSlice::new_in(nmi_source_count, allocator)?;
-        let mut local_apic_nmi_lines = crate::ManagedSlice::new_in(local_nmi_line_count, allocator)?;
+        let mut io_apics = crate::ManagedSlice::new_in(io_apic_count, allocator.clone())?;
+        let mut interrupt_source_overrides = crate::ManagedSlice::new_in(iso_count, allocator.clone())?;
+        let mut nmi_sources = crate::ManagedSlice::new_in(nmi_source_count, allocator.clone())?;
+        let mut local_apic_nmi_lines = crate::ManagedSlice::new_in(local_nmi_line_count, allocator.clone())?;
         let mut application_processors =
             crate::ManagedSlice::new_in(processor_count.saturating_sub(1), allocator)?; // Subtract one for the BSP
         let mut boot_processor = None;

+ 11 - 9
acpi/src/managed_slice.rs

@@ -8,7 +8,7 @@ where
     A: alloc::Allocator,
 {
     slice: &'a mut [T],
-    allocator: &'a A,
+    allocator: A,
 }
 
 impl<'a, T, A> ManagedSlice<'a, T, A>
@@ -16,14 +16,16 @@ where
     A: alloc::Allocator,
 {
     /// Attempts to allocate a new `&mut [T]` in the given allocator.
-    pub fn new_in(len: usize, allocator: &'a A) -> crate::AcpiResult<Self> {
-        // Safety: Struct layouts are required to be valid.
-        let layout =
-            unsafe { alloc::Layout::from_size_align_unchecked(mem::size_of::<T>() * len, mem::align_of::<T>()) };
-
-        unsafe { allocator.allocate(layout).map(|ptr| ptr.as_uninit_slice_mut().align_to_mut::<T>().1) }
-            .map(|slice| Self { slice, allocator })
-            .map_err(|_| crate::AcpiError::AllocError)
+    pub fn new_in(len: usize, allocator: A) -> crate::AcpiResult<Self> {
+        // Safety: Type automatically deallocated memory on `Drop` and;
+        //         Constructed slice is from valid, aligned, allocated memory.
+        unsafe {
+            allocator
+                .allocate(alloc::Layout::array::<T>(len).map_err(|_| crate::AcpiError::AllocError)?)
+                .map(|mut ptr| core::slice::from_raw_parts_mut(ptr.as_mut().as_mut_ptr().cast(), len))
+                .map(|slice| Self { slice, allocator })
+                .map_err(|_| crate::AcpiError::AllocError)
+        }
     }
 }
 

+ 1 - 1
acpi/src/mcfg.rs

@@ -19,7 +19,7 @@ impl<'a, A> PciConfigRegions<'a, A>
 where
     A: core::alloc::Allocator,
 {
-    pub fn new_in<H>(tables: &crate::AcpiTables<H>, allocator: &'a A) -> crate::AcpiResult<PciConfigRegions<'a, A>>
+    pub fn new_in<H>(tables: &crate::AcpiTables<H>, allocator: A) -> crate::AcpiResult<PciConfigRegions<'a, A>>
     where
         H: crate::AcpiHandler,
     {

+ 2 - 2
acpi/src/platform/mod.rs

@@ -102,9 +102,9 @@ where
 
 impl<'a, A> PlatformInfo<'a, A>
 where
-    A: Allocator,
+    A: Allocator + Clone,
 {
-    pub fn new_in<H>(tables: &AcpiTables<H>, allocator: &'a A) -> crate::AcpiResult<Self>
+    pub fn new_in<H>(tables: &AcpiTables<H>, allocator: A) -> crate::AcpiResult<Self>
     where
         H: AcpiHandler,
     {