Skip to content

Commit 6f7eada

Browse files
aero_proc: add downcastable proc macro :)
Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent 078384e commit 6f7eada

File tree

15 files changed

+127
-52
lines changed

15 files changed

+127
-52
lines changed

src/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/aero_kernel/src/drivers/drm/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,15 @@ use crate::fs::devfs;
3232
use crate::fs::inode::INodeInterface;
3333
use crate::fs::FileSystemError;
3434

35-
use crate::utils;
36-
use crate::utils::Downcastable;
37-
3835
use crate::mem::paging::*;
3936
use crate::utils::sync::Mutex;
4037

4138
use uapi::drm::*;
4239

4340
/// Represents modset objects visible to userspace; this includes connectors,
4441
/// CRTCs, encoders, frambuffers and planes.
45-
trait ModeObject: Send + Sync + Downcastable {
42+
#[downcastable]
43+
trait ModeObject: Send + Sync {
4644
/// Returns the mode object's ID.
4745
fn id(&self) -> u32;
4846
fn object(&self) -> Arc<dyn ModeObject>;
@@ -51,22 +49,22 @@ trait ModeObject: Send + Sync + Downcastable {
5149

5250
/// Converts this mode object into a connector.
5351
fn as_connector(&self) -> Option<Arc<Connector>> {
54-
utils::downcast::<dyn ModeObject, Connector>(&self.object())
52+
self.object().downcast_arc::<Connector>()
5553
}
5654

5755
/// Converts this mode object into an encoder.
5856
fn as_encoder(&self) -> Option<Arc<Encoder>> {
59-
utils::downcast::<dyn ModeObject, Encoder>(&self.object())
57+
self.object().downcast_arc::<Encoder>()
6058
}
6159

6260
/// Converts this mode object into a CRTC.
6361
fn as_crtc(&self) -> Option<Arc<Crtc>> {
64-
utils::downcast::<dyn ModeObject, Crtc>(&self.object())
62+
self.object().downcast_arc::<Crtc>()
6563
}
6664

6765
/// Converts this mode object into a framebuffer.
6866
fn as_framebuffer(&self) -> Option<Arc<Framebuffer>> {
69-
utils::downcast::<dyn ModeObject, Framebuffer>(&self.object())
67+
self.object().downcast_arc::<Framebuffer>()
7068
}
7169
}
7270

src/aero_kernel/src/fs/inode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use crate::socket::SocketAddr;
3535
use crate::userland::scheduler;
3636
use crate::utils::sync::BlockQueue;
3737
use crate::utils::sync::Mutex;
38-
use crate::utils::Downcastable;
3938

4039
use super::cache;
4140
use super::cache::Cacheable;
@@ -71,7 +70,8 @@ impl Drop for PollTable {
7170
/// inode which includes its type, size, the number of links referring to it,
7271
/// and the list of blocks holding the file's content. For example device files,
7372
/// files on the disk, etc...
74-
pub trait INodeInterface: Send + Sync + Downcastable {
73+
#[downcastable]
74+
pub trait INodeInterface: Send + Sync {
7575
/// Returns the inode metadata of `this` inode.
7676
fn metadata(&self) -> Result<Metadata> {
7777
Err(FileSystemError::NotSupported)

src/aero_kernel/src/fs/procfs.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use alloc::collections::BTreeMap;
44
use alloc::string::{String, ToString};
55
use alloc::sync::{Arc, Weak};
66

7-
use crate::utils::downcast;
87
use spin::{Once, RwLock};
98

109
use crate::fs::inode::FileType;
@@ -142,7 +141,9 @@ impl LockedProcINode {
142141
let inode = filesystem.allocate_inode(file_type, contents);
143142
let inode_cached = icache.make_item_no_cache(CachedINode::new(inode));
144143

145-
downcast::<dyn INodeInterface, LockedProcINode>(&inode_cached.inner())
144+
inode_cached
145+
.inner()
146+
.downcast_arc::<LockedProcINode>()
146147
.unwrap()
147148
.init(
148149
&this.node,
@@ -263,17 +264,20 @@ impl ProcFs {
263264

264265
root_dir.filesystem.call_once(|| Arc::downgrade(&copy));
265266

266-
let down = downcast::<dyn INodeInterface, LockedProcINode>(root_cached.inner()).unwrap();
267+
let inode = root_cached
268+
.inner()
269+
.downcast_arc::<LockedProcINode>()
270+
.unwrap();
267271

268-
down.init(
272+
inode.init(
269273
&ramfs.root_inode.downgrade(),
270274
&&root_cached.downgrade(),
271275
&Arc::downgrade(&ramfs),
272276
FileType::Directory,
273277
);
274278

275-
down.make_inode("cpuinfo", FileType::File, FileContents::CpuInfo)?;
276-
down.make_inode("cmdline", FileType::File, FileContents::CmdLine)?;
279+
inode.make_inode("cpuinfo", FileType::File, FileContents::CpuInfo)?;
280+
inode.make_inode("cmdline", FileType::File, FileContents::CmdLine)?;
277281

278282
Ok(ramfs)
279283
}

src/aero_kernel/src/fs/ramfs.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use alloc::vec::Vec;
3333
use spin::RwLock;
3434

3535
use crate::mem::paging::*;
36-
use crate::utils::downcast;
3736
use crate::utils::sync::Mutex;
3837

3938
use super::cache::{self, CacheWeak};
@@ -98,8 +97,10 @@ impl LockedRamINode {
9897
let inode = filesystem.allocate_inode(file_type, contents);
9998
let inode_cached = icache.make_item_no_cache(CachedINode::new(inode));
10099

101-
downcast::<dyn INodeInterface, LockedRamINode>(&inode_cached.inner())
102-
.expect("Failed to downcast cached inode on creation")
100+
inode_cached
101+
.inner()
102+
.downcast_arc::<LockedRamINode>()
103+
.unwrap()
103104
.init(
104105
&this.node,
105106
&inode_cached.downgrade(),
@@ -440,8 +441,10 @@ impl RamFs {
440441

441442
root_dir.filesystem.call_once(|| Arc::downgrade(&copy));
442443

443-
downcast::<dyn INodeInterface, LockedRamINode>(root_cached.inner())
444-
.expect("cannot downcast inode to ram inode")
444+
root_cached
445+
.inner()
446+
.downcast_arc::<LockedRamINode>()
447+
.unwrap()
445448
.init(
446449
&ramfs.root_inode.downgrade(),
447450
&&root_cached.downgrade(),

src/aero_kernel/src/socket/unix.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use spin::RwLock;
2828
use crate::fs;
2929
use crate::fs::inode::{DirEntry, FileType, INodeInterface, Metadata, PollTable};
3030
use crate::fs::{FileSystemError, Path, Result};
31-
use crate::utils::downcast;
3231
use crate::utils::sync::BlockQueue;
3332

3433
use super::SocketAddr;
@@ -137,7 +136,10 @@ impl INodeInterface for UnixSocket {
137136
let path = path_from_unix_sock(address)?;
138137
let socket = fs::lookup_path(path)?;
139138

140-
let target = downcast::<dyn INodeInterface, UnixSocket>(&socket.inode().as_unix_socket()?)
139+
let target = socket
140+
.inode()
141+
.as_unix_socket()?
142+
.downcast_arc::<UnixSocket>()
141143
.ok_or(FileSystemError::NotSocket)?; // NOTE: the provided socket was not a unix socket.
142144

143145
let mut target = target.inner.write();

src/aero_kernel/src/syscall/fs.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ use aero_syscall::{AeroSyscallError, OpenFlags, Stat};
2424
use crate::fs::epoll::EPoll;
2525
use crate::fs::eventfd::EventFd;
2626
use crate::fs::file_table::DuplicateHint;
27-
use crate::fs::inode::{DirEntry, INodeInterface};
27+
use crate::fs::inode::DirEntry;
2828
use crate::fs::pipe::Pipe;
2929
use crate::fs::{self, lookup_path, LookupMode};
3030
use crate::userland::scheduler;
3131

3232
use crate::fs::Path;
33-
use crate::utils::downcast;
3433

3534
#[syscall]
3635
pub fn write(fd: usize, buffer: &[u8]) -> Result<usize, AeroSyscallError> {
@@ -433,7 +432,9 @@ pub fn epoll_ctl(
433432

434433
match mode {
435434
EPOLL_CTL_ADD => {
436-
let epoll = downcast::<dyn INodeInterface, EPoll>(&epfd.inode())
435+
let epoll = epfd
436+
.inode()
437+
.downcast_arc::<EPoll>()
437438
.ok_or(AeroSyscallError::EINVAL)?;
438439

439440
epoll.add_event(fd, event.clone())?;
@@ -461,8 +462,10 @@ pub fn epoll_pwait(
461462
.get_handle(epfd)
462463
.ok_or(AeroSyscallError::EBADFD)?;
463464

464-
let epfd =
465-
downcast::<dyn INodeInterface, EPoll>(&epfd.inode()).ok_or(AeroSyscallError::EINVAL)?;
465+
let epfd = epfd
466+
.inode()
467+
.downcast_arc::<EPoll>()
468+
.ok_or(AeroSyscallError::EINVAL)?;
466469

467470
let mut old_mask = 0;
468471

src/aero_kernel/src/userland/scheduler/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@ use crate::{fs::cache::DirCacheItem, syscall::ExecArgs};
3030

3131
use spin::Once;
3232

33-
use crate::utils::Downcastable;
34-
3533
use self::round_robin::RoundRobin;
3634
use super::signals::SignalResult;
3735
use super::task::{Task, TaskId};
3836

3937
static SCHEDULER: Once<Scheduler> = Once::new();
4038

41-
/// Scheduler interface for each scheduling algorithm. The struct implementing
42-
/// this trait has to implement [Send], [Sync] and [Downcastable].
43-
pub trait SchedulerInterface: Send + Sync + Downcastable {
39+
#[downcastable]
40+
pub trait SchedulerInterface: Send + Sync {
4441
/// Register the provided task into the task scheduler queue.
4542
fn register_task(&self, task: Arc<Task>);
4643

src/aero_kernel/src/userland/scheduler/round_robin.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::userland::signals::{SignalError, SignalResult};
2626
use crate::userland::task::{SchedTaskAdapter, Task, TaskState};
2727

2828
use crate::utils::sync::IrqGuard;
29-
use crate::utils::{downcast, PerCpu};
29+
use crate::utils::PerCpu;
3030

3131
use super::SchedulerInterface;
3232

@@ -314,19 +314,21 @@ unsafe impl Sync for RoundRobin {}
314314
/// the child process is sweeped, it will be listed in the process table as a zombie
315315
/// or defunct process.
316316
fn sweeper() {
317-
let scheduler = super::get_scheduler();
318-
let round_robin: Option<Arc<RoundRobin>> = downcast(&scheduler.inner);
319-
let scheduler_ref = round_robin.expect("Failed to downcast the scheduler");
317+
let scheduler_ref = super::get_scheduler()
318+
.inner
319+
.downcast_arc::<RoundRobin>()
320+
.unwrap();
320321

321322
loop {
322323
scheduler_ref.sweep_dead();
323324
}
324325
}
325326

326327
fn preempter() {
327-
let scheduler = super::get_scheduler();
328-
let round_robin: Option<Arc<RoundRobin>> = downcast(&scheduler.inner);
329-
let scheduler_ref = round_robin.expect("Failed to downcast the scheduler");
328+
let scheduler_ref = super::get_scheduler()
329+
.inner
330+
.downcast_arc::<RoundRobin>()
331+
.unwrap();
330332

331333
loop {
332334
scheduler_ref.schedule_next_task();

src/aero_kernel/src/utils/mod.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,6 @@ impl<T: Copy> VolatileCell<T> {
105105
}
106106
}
107107

108-
pub fn downcast<S, T>(arc: &Arc<S>) -> Option<Arc<T>>
109-
where
110-
S: Downcastable + ?Sized,
111-
T: Send + Sync + 'static,
112-
{
113-
arc.clone().as_any().downcast::<T>().ok()
114-
}
115-
116108
pub struct PerCpu<T> {
117109
data: UnsafeCell<Unique<T>>,
118110
}

0 commit comments

Comments
 (0)