Browse Source

acpi: add `alloc` feature for easy use of the global allocator

Most people won't care much about the "new" `allocator_api` work, so add
`new` methods that just use the globally-set allocator too.
Isaac Woods 1 year ago
parent
commit
932bae5d07
4 changed files with 33 additions and 1 deletions
  1. 2 1
      acpi/Cargo.toml
  2. 14 0
      acpi/src/lib.rs
  3. 7 0
      acpi/src/managed_slice.rs
  4. 10 0
      acpi/src/mcfg.rs

+ 2 - 1
acpi/Cargo.toml

@@ -15,5 +15,6 @@ log = "0.4"
 rsdp = { version = "2", path = "../rsdp" }
 
 [features]
-default = ["allocator_api"]
+default = ["allocator_api", "alloc"]
 allocator_api = []
+alloc = ["allocator_api"]

+ 14 - 0
acpi/src/lib.rs

@@ -7,6 +7,7 @@
 //! the `aml` crate, which is the (much less complete) AML parser used to parse the DSDT and SSDTs. These crates
 //! are separate because some kernels may want to detect the static tables, but delay AML parsing to a later stage.
 //!
+//! TODO: make this correct re alloc features
 //! This crate requires `alloc` to make heap allocations. If you are trying to find the RSDP in an environment that
 //! does not have a heap (e.g. a bootloader), you can use the `rsdp` crate. The types from that crate are
 //! compatible with `acpi`.
@@ -55,6 +56,9 @@
 #[cfg(test)]
 extern crate std;
 
+#[cfg(feature = "alloc")]
+extern crate alloc;
+
 pub mod address;
 pub mod bgrt;
 pub mod fadt;
@@ -318,6 +322,16 @@ where
         SsdtIterator { tables_phys_ptrs: self.tables_phys_ptrs(), handler: self.handler.clone() }
     }
 
+    /// Convenience method for contructing a [`PlatformInfo`](crate::platform::PlatformInfo). This is one of the
+    /// first things you should usually do with an `AcpiTables`, and allows to collect helpful information about
+    /// the platform from the ACPI tables.
+    ///
+    /// Like `platform_info_in`, but uses the global allocator.
+    #[cfg(feature = "alloc")]
+    pub fn platform_info(&self) -> AcpiResult<PlatformInfo<alloc::alloc::Global>> {
+        PlatformInfo::new_in(self, alloc::alloc::Global)
+    }
+
     /// Convenience method for contructing a [`PlatformInfo`](crate::platform::PlatformInfo). This is one of the
     /// first things you should usually do with an `AcpiTables`, and allows to collect helpful information about
     /// the platform from the ACPI tables.

+ 7 - 0
acpi/src/managed_slice.rs

@@ -33,6 +33,13 @@ where
     }
 }
 
+#[cfg(feature = "alloc")]
+impl<'a, T> ManagedSlice<'a, T, alloc::alloc::Global> {
+    pub fn new(len: usize) -> AcpiResult<Self> {
+        Self::new_in(len, alloc::alloc::Global)
+    }
+}
+
 impl<'a, T, A> Drop for ManagedSlice<'a, T, A>
 where
     A: Allocator,

+ 10 - 0
acpi/src/mcfg.rs

@@ -17,6 +17,16 @@ where
     regions: crate::ManagedSlice<'a, McfgEntry, A>,
 }
 
+#[cfg(feature = "alloc")]
+impl<'a> PciConfigRegions<'a, alloc::alloc::Global> {
+    pub fn new<H>(tables: &crate::AcpiTables<H>) -> crate::AcpiResult<PciConfigRegions<'a, alloc::alloc::Global>>
+    where
+        H: crate::AcpiHandler,
+    {
+        Self::new_in(tables, alloc::alloc::Global)
+    }
+}
+
 #[cfg(feature = "allocator_api")]
 impl<'a, A> PciConfigRegions<'a, A>
 where