1
- use alloc:: alloc:: Layout ;
1
+ use alloc:: alloc:: Layout as StdLayout ;
2
2
use core:: cell:: UnsafeCell ;
3
3
use core:: future:: Future ;
4
4
use core:: mem:: { self , ManuallyDrop } ;
@@ -9,7 +9,7 @@ use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
9
9
10
10
use crate :: header:: Header ;
11
11
use crate :: state:: * ;
12
- use crate :: utils:: { abort, abort_on_panic, extend } ;
12
+ use crate :: utils:: { abort, abort_on_panic, max , Layout } ;
13
13
use crate :: Runnable ;
14
14
15
15
/// The vtable for a task.
@@ -45,7 +45,7 @@ pub(crate) struct TaskVTable {
45
45
#[ derive( Clone , Copy ) ]
46
46
pub ( crate ) struct TaskLayout {
47
47
/// Memory layout of the whole task.
48
- pub ( crate ) layout : Layout ,
48
+ pub ( crate ) layout : StdLayout ,
49
49
50
50
/// Offset into the task at which the schedule function is stored.
51
51
pub ( crate ) offset_s : usize ,
@@ -80,6 +80,39 @@ impl<F, T, S> Clone for RawTask<F, T, S> {
80
80
}
81
81
}
82
82
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
+
83
116
impl < F , T , S > RawTask < F , T , S >
84
117
where
85
118
F : Future < Output = T > ,
97
130
/// It is assumed that initially only the `Runnable` and the `Task` exist.
98
131
pub ( crate ) fn allocate ( future : F , schedule : S ) -> NonNull < ( ) > {
99
132
// 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 ( ) ;
101
136
102
137
unsafe {
103
138
// Allocate enough space for the entire task.
@@ -149,32 +184,12 @@ where
149
184
}
150
185
}
151
186
152
- /// Returns the memory layout for a task.
187
+ /// Returns the layout of the task.
153
188
#[ inline]
154
189
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 ( ) ,
178
193
}
179
194
}
180
195
0 commit comments