11#![ cfg_attr( not( test) , no_std) ]
22#![ doc = include_str ! ( "../README.md" ) ]
33
4- // #[cfg(feature = "alloc")]
4+ #[ cfg( feature = "alloc" ) ]
55extern crate alloc;
66
7- use core:: { alloc :: Layout , ptr:: NonNull } ;
7+ use core:: ptr:: NonNull ;
88
99mod dma;
1010
11+ #[ cfg( feature = "alloc" ) ]
1112pub use dma:: alloc:: { r#box:: DBox , vec:: DVec } ;
1213pub use dma:: slice:: { DSlice , DSliceMut } ;
1314
1415#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
16+ #[ repr( C ) ]
1517pub enum Direction {
1618 ToDevice ,
1719 FromDevice ,
@@ -33,14 +35,10 @@ pub trait Impl {
3335 /// # Safety
3436 ///
3537 /// layout must have non-zero size. Attempting to allocate for a zero-sized layout may result in undefined behavior.
38+ #[ cfg( feature = "alloc" ) ]
3639 #[ allow( unused_variables) ]
37- unsafe fn alloc ( layout : Layout ) -> * mut u8 {
38- #[ cfg( feature = "alloc" ) ]
39- unsafe {
40- alloc:: alloc:: alloc ( layout)
41- }
42- #[ cfg( not( feature = "alloc" ) ) ]
43- core:: ptr:: null_mut ( )
40+ unsafe fn alloc ( layout : core:: alloc:: Layout ) -> * mut u8 {
41+ unsafe { alloc:: alloc:: alloc ( layout) }
4442 }
4543
4644 /// Deallocates the block of memory at the given `ptr` pointer with the given `layout`.
@@ -55,12 +53,9 @@ pub trait Impl {
5553 /// memory.
5654 ///
5755 /// Otherwise undefined behavior can result.
58- #[ allow( unused_variables) ]
59- unsafe fn dealloc ( ptr : * mut u8 , layout : Layout ) {
60- #[ cfg( feature = "alloc" ) ]
61- unsafe {
62- alloc:: alloc:: dealloc ( ptr, layout)
63- }
56+ #[ cfg( feature = "alloc" ) ]
57+ unsafe fn dealloc ( ptr : * mut u8 , layout : core:: alloc:: Layout ) {
58+ unsafe { alloc:: alloc:: dealloc ( ptr, layout) }
6459 }
6560}
6661
@@ -69,8 +64,10 @@ extern "Rust" {
6964 fn __dma_api_unmap ( addr : NonNull < u8 > , size : usize ) ;
7065 fn __dma_api_flush ( addr : NonNull < u8 > , size : usize ) ;
7166 fn __dma_api_invalidate ( addr : NonNull < u8 > , size : usize ) ;
72- fn __dma_api_alloc ( layout : Layout ) -> * mut u8 ;
73- fn __dma_api_dealloc ( ptr : * mut u8 , layout : Layout ) ;
67+ #[ cfg( feature = "alloc" ) ]
68+ fn __dma_api_alloc ( layout : core:: alloc:: Layout ) -> * mut u8 ;
69+ #[ cfg( feature = "alloc" ) ]
70+ fn __dma_api_dealloc ( ptr : * mut u8 , layout : core:: alloc:: Layout ) ;
7471}
7572
7673fn map ( addr : NonNull < u8 > , size : usize , direction : Direction ) -> u64 {
@@ -88,17 +85,17 @@ fn flush(addr: NonNull<u8>, size: usize) {
8885fn invalidate ( addr : NonNull < u8 > , size : usize ) {
8986 unsafe { __dma_api_invalidate ( addr, size) }
9087}
91-
92- unsafe fn alloc ( layout : Layout ) -> * mut u8 {
88+ # [ cfg ( feature = "alloc" ) ]
89+ unsafe fn alloc ( layout : core :: alloc :: Layout ) -> * mut u8 {
9390 unsafe { __dma_api_alloc ( layout) }
9491}
95-
96- unsafe fn dealloc ( ptr : * mut u8 , layout : Layout ) {
92+ # [ cfg ( feature = "alloc" ) ]
93+ unsafe fn dealloc ( ptr : * mut u8 , layout : core :: alloc :: Layout ) {
9794 unsafe { __dma_api_dealloc ( ptr, layout) }
9895}
9996
10097#[ macro_export]
101- macro_rules! set_impl {
98+ macro_rules! __set_impl_base {
10299 ( $t: ty) => {
103100 #[ no_mangle]
104101 fn __dma_api_map(
@@ -120,6 +117,22 @@ macro_rules! set_impl {
120117 fn __dma_api_invalidate( addr: core:: ptr:: NonNull <u8 >, size: usize ) {
121118 <$t as $crate:: Impl >:: invalidate( addr, size)
122119 }
120+ } ;
121+ }
122+
123+ #[ cfg( not( feature = "alloc" ) ) ]
124+ #[ macro_export]
125+ macro_rules! set_impl {
126+ ( $t: ty) => {
127+ $crate:: __set_impl_base!( $t) ;
128+ } ;
129+ }
130+
131+ #[ cfg( feature = "alloc" ) ]
132+ #[ macro_export]
133+ macro_rules! set_impl {
134+ ( $t: ty) => {
135+ $crate:: __set_impl_base!( $t) ;
123136 #[ no_mangle]
124137 fn __dma_api_alloc( layout: core:: alloc:: Layout ) -> * mut u8 {
125138 unsafe { <$t as $crate:: Impl >:: alloc( layout) }
0 commit comments