Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 2349935

Browse files
committed
Refactor chrdev to use a builder pattern
1 parent b82edd9 commit 2349935

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

src/chrdev.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
1+
use core::convert::TryInto;
12
use core::ops::Range;
23

34
use crate::bindings;
45
use crate::c_types;
56
use crate::error;
67

7-
pub struct DeviceNumberRegion {
8-
dev: bindings::dev_t,
9-
count: usize,
8+
pub fn builder(name: &'static str, minors: Range<u16>) -> error::KernelResult<Builder> {
9+
if !name.ends_with('\x00') {
10+
return Err(error::Error::EINVAL);
11+
}
12+
13+
return Ok(Builder { name, minors });
1014
}
1115

12-
impl DeviceNumberRegion {
13-
pub fn allocate(
14-
minors: Range<usize>,
15-
name: &'static str,
16-
) -> error::KernelResult<DeviceNumberRegion> {
17-
if !name.ends_with('\x00') {
18-
return Err(error::Error::EINVAL);
19-
}
16+
pub struct Builder {
17+
name: &'static str,
18+
minors: Range<u16>,
19+
}
2020

21-
let count = minors.end - minors.start;
21+
impl Builder {
22+
pub fn build(self) -> error::KernelResult<Registration> {
2223
let mut dev: bindings::dev_t = 0;
2324
let res = unsafe {
2425
bindings::alloc_chrdev_region(
2526
&mut dev,
26-
minors.start as bindings::dev_t,
27-
count as bindings::dev_t,
28-
name.as_ptr() as *const c_types::c_char,
27+
self.minors.start.into(),
28+
self.minors.len().try_into()?,
29+
self.name.as_ptr() as *const c_types::c_char,
2930
)
3031
};
3132
if res != 0 {
3233
return Err(error::Error::from_kernel_errno(res));
3334
}
34-
return Ok(DeviceNumberRegion { dev, count });
35+
return Ok(Registration {
36+
dev,
37+
count: self.minors.len(),
38+
});
3539
}
3640
}
3741

38-
impl Drop for DeviceNumberRegion {
42+
pub struct Registration {
43+
dev: bindings::dev_t,
44+
count: usize,
45+
}
46+
47+
impl Drop for Registration {
3948
fn drop(&mut self) {
4049
unsafe {
41-
bindings::unregister_chrdev_region(self.dev, self.count as bindings::dev_t);
50+
bindings::unregister_chrdev_region(self.dev, self.count as _);
4251
}
4352
}
4453
}

src/error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::num::TryFromIntError;
2+
13
use crate::bindings;
24
use crate::c_types;
35

@@ -17,4 +19,10 @@ impl Error {
1719
}
1820
}
1921

22+
impl From<TryFromIntError> for Error {
23+
fn from(_: TryFromIntError) -> Error {
24+
return Error::EINVAL;
25+
}
26+
}
27+
2028
pub type KernelResult<T> = Result<T, Error>;

tests/chrdev-region-allocation/src/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
use linux_kernel_module;
55

66
struct ChrdevRegionAllocationTestModule {
7-
_dev: linux_kernel_module::chrdev::DeviceNumberRegion,
7+
_chrdev_reg: linux_kernel_module::chrdev::Registration,
88
}
99

1010
impl linux_kernel_module::KernelModule for ChrdevRegionAllocationTestModule {
1111
fn init() -> linux_kernel_module::KernelResult<Self> {
12+
let chrdev_reg =
13+
linux_kernel_module::chrdev::builder("chrdev-region-allocation-tests\x00", 0..1)?
14+
.build()?;
15+
1216
Ok(ChrdevRegionAllocationTestModule {
13-
_dev: linux_kernel_module::chrdev::DeviceNumberRegion::allocate(
14-
0..1,
15-
"chrdev-region-allocation-tests\x00",
16-
)?,
17+
_chrdev_reg: chrdev_reg,
1718
})
1819
}
1920
}

0 commit comments

Comments
 (0)