1- use alloc:: alloc:: Layout ;
1+ use alloc:: alloc:: Layout as StdLayout ;
22use core:: cell:: UnsafeCell ;
33use core:: future:: Future ;
44use core:: mem:: { self , ManuallyDrop } ;
@@ -9,7 +9,7 @@ use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
99
1010use crate :: header:: Header ;
1111use crate :: state:: * ;
12- use crate :: utils:: { abort, abort_on_panic, extend } ;
12+ use crate :: utils:: { abort, abort_on_panic, max , Layout } ;
1313use crate :: Runnable ;
1414
1515/// The vtable for a task.
@@ -45,7 +45,7 @@ pub(crate) struct TaskVTable {
4545#[ derive( Clone , Copy ) ]
4646pub ( crate ) struct TaskLayout {
4747 /// Memory layout of the whole task.
48- pub ( crate ) layout : Layout ,
48+ pub ( crate ) layout : StdLayout ,
4949
5050 /// Offset into the task at which the schedule function is stored.
5151 pub ( crate ) offset_s : usize ,
@@ -80,6 +80,39 @@ impl<F, T, S> Clone for RawTask<F, T, S> {
8080 }
8181}
8282
83+ impl < F , T , S > RawTask < F , T , S > {
84+ const TASK_LAYOUT : Option < TaskLayout > = Self :: eval_task_layout ( ) ;
85+
86+ /// Computes the memory layout for a task.
87+ #[ inline]
88+ const fn eval_task_layout ( ) -> Option < TaskLayout > {
89+ // Compute the layouts for `Header`, `S`, `F`, and `T`.
90+ let layout_header = Layout :: new :: < Header > ( ) ;
91+ let layout_s = Layout :: new :: < S > ( ) ;
92+ let layout_f = Layout :: new :: < F > ( ) ;
93+ let layout_r = Layout :: new :: < T > ( ) ;
94+
95+ // Compute the layout for `union { F, T }`.
96+ let size_union = max ( layout_f. size ( ) , layout_r. size ( ) ) ;
97+ let align_union = max ( layout_f. align ( ) , layout_r. align ( ) ) ;
98+ let layout_union = Layout :: from_size_align ( size_union, align_union) ;
99+
100+ // Compute the layout for `Header` followed `S` and `union { F, T }`.
101+ let layout = layout_header;
102+ let ( layout, offset_s) = leap ! ( layout. extend( layout_s) ) ;
103+ let ( layout, offset_union) = leap ! ( layout. extend( layout_union) ) ;
104+ let offset_f = offset_union;
105+ let offset_r = offset_union;
106+
107+ Some ( TaskLayout {
108+ layout : unsafe { layout. into_std ( ) } ,
109+ offset_s,
110+ offset_f,
111+ offset_r,
112+ } )
113+ }
114+ }
115+
83116impl < F , T , S > RawTask < F , T , S >
84117where
85118 F : Future < Output = T > ,
97130 /// It is assumed that initially only the `Runnable` and the `Task` exist.
98131 pub ( crate ) fn allocate ( future : F , schedule : S ) -> NonNull < ( ) > {
99132 // Compute the layout of the task for allocation. Abort if the computation fails.
100- let task_layout = abort_on_panic ( || Self :: task_layout ( ) ) ;
133+ //
134+ // n.b. notgull: task_layout now automatically aborts instead of panicking
135+ let task_layout = Self :: task_layout ( ) ;
101136
102137 unsafe {
103138 // Allocate enough space for the entire task.
@@ -149,32 +184,12 @@ where
149184 }
150185 }
151186
152- /// Returns the memory layout for a task.
187+ /// Returns the layout of the task.
153188 #[ inline]
154189 fn task_layout ( ) -> TaskLayout {
155- // Compute the layouts for `Header`, `S`, `F`, and `T`.
156- let layout_header = Layout :: new :: < Header > ( ) ;
157- let layout_s = Layout :: new :: < S > ( ) ;
158- let layout_f = Layout :: new :: < F > ( ) ;
159- let layout_r = Layout :: new :: < T > ( ) ;
160-
161- // Compute the layout for `union { F, T }`.
162- let size_union = layout_f. size ( ) . max ( layout_r. size ( ) ) ;
163- let align_union = layout_f. align ( ) . max ( layout_r. align ( ) ) ;
164- let layout_union = unsafe { Layout :: from_size_align_unchecked ( size_union, align_union) } ;
165-
166- // Compute the layout for `Header` followed `S` and `union { F, T }`.
167- let layout = layout_header;
168- let ( layout, offset_s) = extend ( layout, layout_s) ;
169- let ( layout, offset_union) = extend ( layout, layout_union) ;
170- let offset_f = offset_union;
171- let offset_r = offset_union;
172-
173- TaskLayout {
174- layout,
175- offset_s,
176- offset_f,
177- offset_r,
190+ match Self :: TASK_LAYOUT {
191+ Some ( tl) => tl,
192+ None => abort ( ) ,
178193 }
179194 }
180195
0 commit comments