Skip to content

Commit ee75038

Browse files
committed
feat(core): cfg_phaseN! now take &ConstAllocator as a parameter
This is intended to be used to store temporary objects in the CTFE heap, primarily through a retrofitted `ComptimeVec`.
1 parent d64de19 commit ee75038

File tree

2 files changed

+62
-24
lines changed

2 files changed

+62
-24
lines changed

src/r3_core/src/kernel/cfg.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Kernel configuration
22
use crate::{
33
kernel::{hook, interrupt, raw, raw_cfg},
4-
utils::{ComptimeVec, Init, PhantomInvariant},
4+
utils::{ComptimeVec, ConstAllocator, Init, PhantomInvariant},
55
};
66

77
macro overview_ref() {
@@ -29,7 +29,7 @@ enum CfgSt {
2929

3030
impl<'c, C: raw_cfg::CfgBase> Cfg<'c, C> {
3131
/// Construct `Cfg`.
32-
const fn new(raw: &'c mut C, st: CfgSt) -> Self {
32+
const fn new(raw: &'c mut C, _allocator: &'c ConstAllocator, st: CfgSt) -> Self {
3333
Self {
3434
raw,
3535
st,
@@ -42,24 +42,36 @@ impl<'c, C: raw_cfg::CfgBase> Cfg<'c, C> {
4242
}
4343

4444
#[doc(hidden)]
45-
pub const fn __internal_new_phase1(raw: &'c mut C, _dummy: &'c mut ()) -> Self {
46-
Self::new(raw, CfgSt::Phase1)
45+
pub const fn __internal_new_phase1(
46+
raw: &'c mut C,
47+
allocator: &'c ConstAllocator,
48+
_dummy: &'c mut (),
49+
) -> Self {
50+
Self::new(raw, allocator, CfgSt::Phase1)
4751
}
4852

4953
#[doc(hidden)]
50-
pub const fn __internal_new_phase2(raw: &'c mut C, _dummy: &'c mut ()) -> Self
54+
pub const fn __internal_new_phase2(
55+
raw: &'c mut C,
56+
allocator: &'c ConstAllocator,
57+
_dummy: &'c mut (),
58+
) -> Self
5159
where
5260
C::System: CfgPhase1,
5361
{
54-
Self::new(raw, CfgSt::Phase2)
62+
Self::new(raw, allocator, CfgSt::Phase2)
5563
}
5664

5765
#[doc(hidden)]
58-
pub const fn __internal_new_phase3(raw: &'c mut C, _dummy: &'c mut ()) -> Self
66+
pub const fn __internal_new_phase3(
67+
raw: &'c mut C,
68+
allocator: &'c ConstAllocator,
69+
_dummy: &'c mut (),
70+
) -> Self
5971
where
6072
C::System: CfgPhase2,
6173
{
62-
Self::new(raw, CfgSt::Phase3 { interrupts: false })
74+
Self::new(raw, allocator, CfgSt::Phase3 { interrupts: false })
6375
}
6476

6577
/// Mutably borrow the underlying `C`.
@@ -504,28 +516,45 @@ where
504516

505517
/// Construct [`Cfg`]`<$RawCfg>` for the phase 3 configuration.
506518
///
519+
/// - `$raw_cfg: &mut impl `[`CfgBase`][]
520+
/// - `$allocator: &`[`ConstAllocator`][]
521+
///
507522
/// `<$RawCfg as `[`CfgBase`][]`>::System` must implement [`CfgPhase2`][].
508523
///
509524
/// [`CfgBase`]: raw_cfg::CfgBase
510-
pub macro cfg_phase3(let mut $cfg:ident = Cfg::<$RawCfg:ty>::new($raw_cfg:expr)) {
525+
pub macro cfg_phase3(
526+
let mut $cfg:ident = Cfg::<$RawCfg:ty>::new($raw_cfg:expr, $allocator:expr)
527+
) {
511528
let mut dummy = ();
512-
let mut $cfg = Cfg::<$RawCfg>::__internal_new_phase3(&mut *$raw_cfg, &mut dummy);
529+
let mut $cfg = Cfg::<$RawCfg>::__internal_new_phase3(&mut *$raw_cfg, $allocator, &mut dummy);
513530
}
514531

515532
/// Construct [`Cfg`]`<$RawCfg>` for the phase 2 configuration.
516533
///
534+
/// - `$raw_cfg: &mut impl `[`CfgBase`][]
535+
/// - `$allocator: &`[`ConstAllocator`][]
536+
///
517537
/// `<$RawCfg as `[`CfgBase`][]`>::System` must implement [`CfgPhase1`][].
518538
///
519539
/// [`CfgBase`]: raw_cfg::CfgBase
520-
pub macro cfg_phase2(let mut $cfg:ident = Cfg::<$RawCfg:ty>::new($raw_cfg:expr)) {
540+
pub macro cfg_phase2(
541+
let mut $cfg:ident = Cfg::<$RawCfg:ty>::new($raw_cfg:expr, $allocator:expr)
542+
) {
521543
let mut dummy = ();
522-
let mut $cfg = Cfg::<$RawCfg>::__internal_new_phase2(&mut *$raw_cfg, &mut dummy);
544+
let mut $cfg = Cfg::<$RawCfg>::__internal_new_phase2(&mut *$raw_cfg, $allocator, &mut dummy);
523545
}
524546

525547
/// Construct [`Cfg`]`<$RawCfg>` for the phase 1 configuration.
526-
pub macro cfg_phase1(let mut $cfg:ident = Cfg::<$RawCfg:ty>::new($raw_cfg:expr)) {
548+
///
549+
/// - `$raw_cfg: &mut impl `[`CfgBase`][]
550+
/// - `$allocator: &`[`ConstAllocator`][]
551+
///
552+
/// [`CfgBase`]: raw_cfg::CfgBase
553+
pub macro cfg_phase1(
554+
let mut $cfg:ident = Cfg::<$RawCfg:ty>::new($raw_cfg:expr, $allocator:expr)
555+
) {
527556
let mut dummy = ();
528-
let mut $cfg = Cfg::<$RawCfg>::__internal_new_phase1(&mut *$raw_cfg, &mut dummy);
557+
let mut $cfg = Cfg::<$RawCfg>::__internal_new_phase1(&mut *$raw_cfg, $allocator, &mut dummy);
529558
}
530559

531560
/// Implement [`KernelStatic`] on `$Ty` using the given `$params:

src/r3_kernel/src/cfg.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ macro_rules! build {
2828
// r3_kernel::System<$Traits>>) -> $IdMap`
2929
($Traits:ty, $configure:expr => $IdMap:ty) => {{
3030
use $crate::{
31-
r3_core,
31+
r3_core::{
32+
self,
33+
utils::ConstAllocator,
34+
},
3235
cfg::{self, CfgBuilder, CfgBuilderInner},
3336
EventGroupCb, InterruptAttr, InterruptLineInit, KernelCfg1,
3437
KernelCfg2, Port, State, TaskAttr, TaskCb, TimeoutRef, TimerAttr,
@@ -44,11 +47,13 @@ macro_rules! build {
4447
// Kernel-independent configuration process
4548
// ---------------------------------------------------------------------
4649

47-
const fn build_cfg_phase1() -> r3_core::kernel::cfg::CfgPhase1Data<System> {
50+
const fn build_cfg_phase1(
51+
allocator: &ConstAllocator,
52+
) -> r3_core::kernel::cfg::CfgPhase1Data<System> {
4853
// Safety: We are `build!`, so it's okay to use `CfgBuilder::new`
4954
let mut my_cfg = unsafe { CfgBuilder::new() };
5055
r3_core::kernel::cfg::cfg_phase1!(
51-
let mut cfg = Cfg::<CfgBuilder<$Traits>>::new(&mut my_cfg));
56+
let mut cfg = Cfg::<CfgBuilder<$Traits>>::new(&mut my_cfg, allocator));
5257
$configure(&mut cfg);
5358
CfgBuilder::finalize_in_cfg(&mut cfg);
5459

@@ -60,15 +65,17 @@ macro_rules! build {
6065
// Implement `CfgPhase1` on `$Traits` using the information
6166
// collected in phase 1
6267
r3_core::kernel::cfg::attach_phase1!(
63-
build_cfg_phase1(),
68+
ConstAllocator::with(build_cfg_phase1),
6469
impl CfgPhase1<System> for $Traits,
6570
);
6671

67-
const fn build_cfg_phase2() -> r3_core::kernel::cfg::CfgPhase2Data<System> {
72+
const fn build_cfg_phase2(
73+
allocator: &ConstAllocator,
74+
) -> r3_core::kernel::cfg::CfgPhase2Data<System> {
6875
// Safety: We are `build!`, so it's okay to use `CfgBuilder::new`
6976
let mut my_cfg = unsafe { CfgBuilder::new() };
7077
r3_core::kernel::cfg::cfg_phase2!(
71-
let mut cfg = Cfg::<CfgBuilder<$Traits>>::new(&mut my_cfg));
78+
let mut cfg = Cfg::<CfgBuilder<$Traits>>::new(&mut my_cfg, allocator));
7279
$configure(&mut cfg);
7380
CfgBuilder::finalize_in_cfg(&mut cfg);
7481

@@ -80,19 +87,21 @@ macro_rules! build {
8087
// Implement `CfgPhase2` on `$Traits` using the information
8188
// collected in phase 2
8289
r3_core::kernel::cfg::attach_phase2!(
83-
build_cfg_phase2(),
90+
ConstAllocator::with(build_cfg_phase2),
8491
impl CfgPhase2<System> for $Traits,
8592
);
8693

87-
const fn build_cfg_phase3() -> (
94+
const fn build_cfg_phase3(
95+
allocator: &ConstAllocator,
96+
) -> (
8897
CfgBuilderInner<$Traits>,
8998
$IdMap,
9099
r3_core::kernel::cfg::CfgPhase3Data<System>,
91100
) {
92101
// Safety: We are `build!`, so it's okay to use `CfgBuilder::new`
93102
let mut my_cfg = unsafe { CfgBuilder::new() };
94103
r3_core::kernel::cfg::cfg_phase3!(
95-
let mut cfg = Cfg::<CfgBuilder<$Traits>>::new(&mut my_cfg));
104+
let mut cfg = Cfg::<CfgBuilder<$Traits>>::new(&mut my_cfg, allocator));
96105
let id_map = $configure(&mut cfg);
97106
CfgBuilder::finalize_in_cfg(&mut cfg);
98107

@@ -108,7 +117,7 @@ macro_rules! build {
108117
CfgBuilderInner<$Traits>,
109118
$IdMap,
110119
r3_core::kernel::cfg::CfgPhase3Data<System>,
111-
) = build_cfg_phase3();
120+
) = ConstAllocator::with(build_cfg_phase3);
112121
const CFG: CfgBuilderInner<$Traits> = CFG_OUTPUT.0;
113122

114123
// Implement `KernelStatic` on `$Traits` using the information

0 commit comments

Comments
 (0)