Move ranged integer to aster-util crate

This commit is contained in:
Qingsong Chen 2025-11-13 09:06:07 +00:00 committed by Tate, Hongliang Tian
parent 623fbb5e5b
commit d954e3d006
4 changed files with 61 additions and 49 deletions

View File

@ -13,6 +13,7 @@ pub mod fixed_point;
pub mod mem_obj_slice;
pub mod per_cpu_counter;
pub mod printer;
pub mod ranged_integer;
pub mod safe_ptr;
pub mod slot_vec;
pub mod union_read_ptr;

View File

@ -0,0 +1,57 @@
// SPDX-License-Identifier: MPL-2.0
//! Ranged integer types.
//!
//! This module provides generic ranged integer types that enforce value always stay
//! within the specified range.
macro_rules! define_ranged_integer {
($visibility: vis, $name: ident, $type: ty) => {
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
$visibility struct $name<const MIN: $type, const MAX: $type>($type);
impl<const MIN: $type, const MAX: $type> $name<MIN, MAX> {
$visibility const MIN: Self = Self::new(MIN);
$visibility const MAX: Self = Self::new(MAX);
$visibility const fn new(val: $type) -> Self {
assert!(val >= MIN && val <= MAX);
Self(val)
}
$visibility fn set(&mut self, val: $type) {
assert!(val >= MIN && val <= MAX);
self.0 = val;
}
$visibility const fn get(self) -> $type {
self.0
}
}
impl<const MIN: $type, const MAX: $type> From<$name<MIN, MAX>> for $type {
fn from(value: $name<MIN, MAX>) -> Self {
value.0
}
}
impl<const MIN: $type, const MAX: $type> TryFrom<$type> for $name<MIN, MAX> {
type Error = &'static str;
fn try_from(value: $type) -> Result<Self, Self::Error> {
if value < MIN || value > MAX {
Err("Initialized with out-of-range value.")
} else {
Ok(Self(value))
}
}
}
};
}
define_ranged_integer!(pub, RangedI8, i8);
define_ranged_integer!(pub, RangedU8, u8);
define_ranged_integer!(pub, RangedI16, i16);
define_ranged_integer!(pub, RangedU16, u16);
define_ranged_integer!(pub, RangedI32, i32);
define_ranged_integer!(pub, RangedU32, u32);

View File

@ -2,6 +2,7 @@
use core::sync::atomic::AtomicI8;
use aster_util::ranged_integer::RangedI8;
use atomic_integer_wrapper::define_atomic_version_of_integer_like_type;
/// The process scheduling nice value.
@ -55,51 +56,3 @@ impl TryFrom<i8> for Nice {
Ok(Self::new(range))
}
}
macro_rules! define_ranged_integer {
($visibility: vis, $name: ident, $type: ty) => {
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
$visibility struct $name<const MIN: $type, const MAX: $type>($type);
impl<const MIN: $type, const MAX: $type> $name<MIN, MAX> {
$visibility const MIN: Self = Self::new(MIN);
$visibility const MAX: Self = Self::new(MAX);
$visibility const fn new(val: $type) -> Self {
assert!(val >= MIN && val <= MAX);
Self(val)
}
$visibility fn set(&mut self, val: $type) {
assert!(val >= MIN && val <= MAX);
self.0 = val;
}
$visibility const fn get(self) -> $type {
self.0
}
}
impl<const MIN: $type, const MAX: $type> From<$name<MIN, MAX>> for $type {
fn from(value: $name<MIN, MAX>) -> Self {
value.0
}
}
impl<const MIN: $type, const MAX: $type> TryFrom<$type> for $name<MIN, MAX> {
type Error = &'static str;
fn try_from(value: $type) -> Result<Self, Self::Error> {
if value < MIN || value > MAX {
Err("Initialized with out-of-range value.")
} else {
Ok(Self(value))
}
}
}
};
}
define_ranged_integer!(pub, RangedI8, i8);
define_ranged_integer!(pub, RangedU8, u8);

View File

@ -7,6 +7,7 @@ use core::{
sync::atomic::{AtomicU64, AtomicU8, Ordering::Relaxed},
};
use aster_util::ranged_integer::RangedU8;
use bitvec::{bitarr, BitArr};
use ostd::{
cpu::CpuId,
@ -17,7 +18,7 @@ use ostd::{
};
use super::{time::base_slice_clocks, CurrentRuntime, SchedAttr, SchedClassRq};
use crate::{sched::nice::RangedU8, thread::AsThread};
use crate::thread::AsThread;
pub type RealTimePriority = RangedU8<1, 99>;