@@ -6,7 +6,7 @@ use core::mem::{ManuallyDrop, MaybeUninit};
66use core:: num:: NonZeroUsize ;
77use core:: ops:: { Deref , DerefMut } ;
88use core:: ptr:: NonNull ;
9- use log:: trace;
9+ use log:: { error , trace} ;
1010use uefi_raw:: Status ;
1111use uefi_raw:: protocol:: pci:: root_bridge:: PciRootBridgeIoProtocol ;
1212
@@ -15,30 +15,20 @@ use uefi_raw::protocol::pci::root_bridge::PciRootBridgeIoProtocol;
1515/// # Lifetime
1616/// `'p` is the lifetime for Protocol.
1717#[ derive( Debug ) ]
18- pub struct PciPage < ' p , T > {
19- base : NonNull < T > ,
20- pages : NonZeroUsize ,
21- proto : & ' p PciRootBridgeIoProtocol ,
18+ pub struct PciBuffer < ' p , T > {
19+ pub ( super ) base : NonNull < T > ,
20+ pub ( super ) pages : NonZeroUsize ,
21+ pub ( super ) proto : & ' p PciRootBridgeIoProtocol ,
2222}
2323
24- impl < ' p , T > PciPage < ' p , MaybeUninit < T > > {
25- /// Creates wrapper for pages allocated by PCI Root Bridge protocol.
26- #[ must_use]
27- pub const fn new (
28- base : NonNull < MaybeUninit < T > > ,
29- pages : NonZeroUsize ,
30- proto : & ' p PciRootBridgeIoProtocol ,
31- ) -> Self {
32- Self { base, pages, proto }
33- }
34-
24+ impl < ' p , T > PciBuffer < ' p , MaybeUninit < T > > {
3525 /// Assumes the contents of this buffer have been initialized.
3626 ///
3727 /// # Safety
3828 /// Callers of this function must guarantee that the value stored is valid.
3929 #[ must_use]
40- pub const unsafe fn assume_init ( self ) -> PciPage < ' p , T > {
41- let initialized = PciPage {
30+ pub const unsafe fn assume_init ( self ) -> PciBuffer < ' p , T > {
31+ let initialized = PciBuffer {
4232 base : self . base . cast ( ) ,
4333 pages : self . pages ,
4434 proto : self . proto ,
@@ -48,33 +38,47 @@ impl<'p, T> PciPage<'p, MaybeUninit<T>> {
4838 }
4939}
5040
51- impl < T > AsRef < T > for PciPage < ' _ , T > {
41+ impl < ' p , T > PciBuffer < ' p , T > {
42+ /// Returns the base address of this buffer
43+ #[ must_use]
44+ pub fn base ( & self ) -> NonZeroUsize {
45+ self . base . addr ( )
46+ }
47+
48+ /// Returns the number of pages this buffer uses
49+ #[ must_use]
50+ pub const fn pages ( & self ) -> NonZeroUsize {
51+ self . pages
52+ }
53+ }
54+
55+ impl < T > AsRef < T > for PciBuffer < ' _ , T > {
5256 fn as_ref ( & self ) -> & T {
5357 unsafe { self . base . as_ref ( ) }
5458 }
5559}
5660
57- impl < T > AsMut < T > for PciPage < ' _ , T > {
61+ impl < T > AsMut < T > for PciBuffer < ' _ , T > {
5862 fn as_mut ( & mut self ) -> & mut T {
5963 unsafe { self . base . as_mut ( ) }
6064 }
6165}
6266
63- impl < T > Deref for PciPage < ' _ , T > {
67+ impl < T > Deref for PciBuffer < ' _ , T > {
6468 type Target = T ;
6569
6670 fn deref ( & self ) -> & Self :: Target {
6771 self . as_ref ( )
6872 }
6973}
7074
71- impl < T > DerefMut for PciPage < ' _ , T > {
75+ impl < T > DerefMut for PciBuffer < ' _ , T > {
7276 fn deref_mut ( & mut self ) -> & mut Self :: Target {
7377 self . as_mut ( )
7478 }
7579}
7680
77- impl < T > Drop for PciPage < ' _ , T > {
81+ impl < T > Drop for PciBuffer < ' _ , T > {
7882 fn drop ( & mut self ) {
7983 let status = unsafe {
8084 ( self . proto . free_buffer ) ( self . proto , self . pages . get ( ) , self . base . as_ptr ( ) . cast ( ) )
@@ -88,9 +92,11 @@ impl<T> Drop for PciPage<'_, T> {
8892 ) ;
8993 }
9094 Status :: INVALID_PARAMETER => {
91- panic ! ( "PciBuffer was not created through valid protocol usage!" )
95+ error ! ( "PciBuffer was not created through valid protocol usage!" )
96+ }
97+ etc => {
98+ error ! ( "Failed to free PciBuffer: {:?}" , etc) ;
9299 }
93- _ => unreachable ! ( ) ,
94100 }
95101 }
96102}
0 commit comments