Skip to content

Commit fafce4a

Browse files
committed
refactor: 修复clippy警告
但是safety懒得写了
1 parent 7b7f9d7 commit fafce4a

File tree

6 files changed

+86
-62
lines changed

6 files changed

+86
-62
lines changed

kernel/src/libs/bitmap.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ impl<const N: usize> BitMap<N> {
7373
}
7474
}
7575

76+
impl<const N: usize> Default for BitMap<N> {
77+
fn default() -> Self {
78+
Self::new()
79+
}
80+
}
81+
7682
pub struct PreAllocBitMap<const N: usize> {
7783
bits: [u64; N],
7884
}
@@ -132,6 +138,12 @@ impl<const N: usize> PreAllocBitMap<N> {
132138
}
133139
}
134140

141+
impl<const N: usize> Default for PreAllocBitMap<N> {
142+
fn default() -> Self {
143+
Self::new()
144+
}
145+
}
146+
135147
#[cfg(test)]
136148
mod tests {
137149
use super::*;

kernel/src/memory/frame/buddy.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ impl<const MAX_ORDER: usize> BuddyAllocator<MAX_ORDER> {
282282
}
283283

284284
/// Deallocate a frame of the given order.
285+
/// # Safety
286+
/// This function is unsafe because it does not check if the frame is allocated.
285287
pub unsafe fn dealloc(&mut self, frame: PhysFrame<Size4KiB>, order: usize) {
286288
if self.used_frames >= (1 << order) {
287289
self.used_frames -= 1 << order;
@@ -295,6 +297,8 @@ impl<const MAX_ORDER: usize> BuddyAllocator<MAX_ORDER> {
295297

296298
/// Deallocate a range of frames.
297299
/// This splits the range into power-of-two blocks and deallocates them.
300+
/// # Safety
301+
/// This function is unsafe because it does not check if the frames are allocated.
298302
pub unsafe fn dealloc_range(&mut self, start: PhysFrame<Size4KiB>, end: PhysFrame<Size4KiB>) {
299303
let mut current = start;
300304
while current < end {
@@ -306,10 +310,10 @@ impl<const MAX_ORDER: usize> BuddyAllocator<MAX_ORDER> {
306310
while order < MAX_ORDER - 1 {
307311
let next_order = order + 1;
308312
let size_frames = 1 << next_order;
309-
let size_bytes = 4096 * (size_frames as u64);
313+
let size_bytes = 4096 * size_frames;
310314

311315
// Check alignment
312-
if current_addr % size_bytes != 0 {
316+
if !current_addr.is_multiple_of(size_bytes) {
313317
break;
314318
}
315319
// Check size
@@ -321,12 +325,11 @@ impl<const MAX_ORDER: usize> BuddyAllocator<MAX_ORDER> {
321325

322326
self.dealloc(current, order);
323327
// PhysFrame + u64 is supported
324-
current = current + (1 << order);
328+
current += 1 << order;
325329
}
326330
}
327331

328332
/// Get the total number of frames managed.
329-
330333
pub fn total_frames(&self) -> usize {
331334
self.total_frames
332335
}
@@ -341,3 +344,9 @@ impl<const MAX_ORDER: usize> BuddyAllocator<MAX_ORDER> {
341344
self.total_frames - self.used_frames
342345
}
343346
}
347+
348+
impl<const MAX_ORDER: usize> Default for BuddyAllocator<MAX_ORDER> {
349+
fn default() -> Self {
350+
Self::new()
351+
}
352+
}

kernel/src/memory/frame/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ impl LockedFrameAllocator {
6969

7070
// 2. Allocate bitmap
7171
// Bitmap needs 1 bit per page.
72-
let total_pages = (max_phys_addr as usize + PAGE_SIZE - 1) / PAGE_SIZE;
73-
let bitmap_size_u64 = (total_pages + 63) / 64;
72+
let total_pages = (max_phys_addr as usize).div_ceil(PAGE_SIZE);
73+
let bitmap_size_u64 = total_pages.div_ceil(64);
7474
let bitmap_size_bytes = bitmap_size_u64 * 8;
7575

7676
let mut bitmap_slice: Option<&'static mut [u64]> = None;

kernel/src/memory/heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl talc::OomHandler for KernelOomHandler {
3131
.ok_or(())?;
3232
let old_end = heap_area.end;
3333
let expand_size = crate::config::OOM_EXPAND_SIZE.max(4 * 1024 * 1024);
34-
let new_end = old_end + (expand_size as u64);
34+
let new_end = old_end + expand_size;
3535
heap_area.end = new_end;
3636
(old_end, new_end)
3737
};

kernel/src/output/console/console_ttf.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,13 @@ impl<'a> TtfConsole<'a> {
221221
}
222222

223223
pub fn set_font(&mut self, new_font_data: &'static [u8], new_font_size: Option<f32>) {
224-
match FontRef::try_from_slice(new_font_data) {
225-
Ok(new_font) => {
226-
self.font = new_font;
227-
let size_to_use = new_font_size.unwrap_or(self.font_size);
228-
self.init_font_metrics(size_to_use);
229-
self.cursor_needs_redraw = true;
230-
self.glyph_cache.clear();
231-
self.redraw();
232-
}
233-
Err(_) => {
234-
return;
235-
}
224+
if let Ok(new_font) = FontRef::try_from_slice(new_font_data) {
225+
self.font = new_font;
226+
let size_to_use = new_font_size.unwrap_or(self.font_size);
227+
self.init_font_metrics(size_to_use);
228+
self.cursor_needs_redraw = true;
229+
self.glyph_cache.clear();
230+
self.redraw();
236231
}
237232
}
238233

kernel/src/panic.rs

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,13 @@ impl<'a> PanicConsole<'a> {
7171
return;
7272
}
7373
let glyph = FONT8X16[c as usize & 0x7F];
74-
for row in 0..16 {
75-
for col in 0..8 {
76-
let color = if glyph[row] & (0x80 >> col) != 0 {
77-
fg
78-
} else {
79-
bg
80-
};
81-
self.set_pixel(self.x + col as u64, self.y + row as u64, color);
82-
}
74+
for (row, col) in glyph.iter().enumerate() {
75+
let color = if glyph[row] & (0x80 >> col) != 0 {
76+
fg
77+
} else {
78+
bg
79+
};
80+
self.set_pixel(self.x + *col as u64, self.y + row as u64, color);
8381
}
8482
self.x += 8;
8583
if self.x + 8 > self.framebuffer.width() - 20 {
@@ -92,9 +90,9 @@ impl<'a> PanicConsole<'a> {
9290
impl<'a> Write for PanicConsole<'a> {
9391
fn write_str(&mut self, s: &str) -> core::fmt::Result {
9492
let fb = &self.framebuffer;
95-
let fg = ((255 as u32) << fb.red_mask_shift())
96-
| ((255 as u32) << fb.green_mask_shift())
97-
| ((255 as u32) << fb.blue_mask_shift());
93+
let fg = ((255_u32) << fb.red_mask_shift())
94+
| ((255_u32) << fb.green_mask_shift())
95+
| ((255_u32) << fb.blue_mask_shift());
9896
let bg = ((BG_COLOR.r as u32) << fb.red_mask_shift())
9997
| ((BG_COLOR.g as u32) << fb.green_mask_shift())
10098
| ((BG_COLOR.b as u32) << fb.blue_mask_shift());
@@ -108,6 +106,7 @@ impl<'a> Write for PanicConsole<'a> {
108106

109107
// This is the default panic handler
110108
#[cfg(not(test))]
109+
#[warn(unused_must_use)]
111110
#[panic_handler]
112111
pub fn panic(info: &PanicInfo) -> ! {
113112
let boot_time = crate::libs::time::time_since_boot();
@@ -179,48 +178,57 @@ pub fn panic(info: &PanicInfo) -> ! {
179178
let mut console = PanicConsole::new(framebuffer);
180179
console.clear(bg);
181180

182-
let _ = write!(console, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
183-
let _ = write!(console, "!! PROKA KERNEL PANIC !!\n");
184-
let _ = write!(console, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
185-
let _ = write!(console, "\n");
186-
let _ = write!(
181+
let _ = writeln!(
182+
console,
183+
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
184+
);
185+
let _ = writeln!(
186+
console,
187+
"!! PROKA KERNEL PANIC !!"
188+
);
189+
let _ = writeln!(
190+
console,
191+
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
192+
);
193+
let _ = writeln!(console);
194+
let _ = writeln!(
187195
console,
188-
"A problem has been detected and the system has been halted to prevent damage.\n"
196+
"A problem has been detected and the system has been halted to prevent damage."
189197
);
190-
let _ = write!(console, "\n");
191-
let _ = write!(console, "--- ERROR INFO ---\n");
192-
let _ = write!(console, "Reason: {}\n", info.message());
198+
let _ = writeln!(console);
199+
let _ = writeln!(console, "--- ERROR INFO ---");
200+
let _ = writeln!(console, "Reason: {}", info.message());
193201
if is_exception {
194-
let _ = write!(console, "Exception: {}\n", exc_name);
202+
let _ = writeln!(console, "Exception: {}", exc_name);
195203
if let Some(err) = exc_error {
196-
let _ = write!(console, "Error Code: {:#x}\n", err);
204+
let _ = writeln!(console, "Error Code: {:#x}", err);
197205
}
198206
}
199207
if let Some(location) = info.location() {
200-
let _ = write!(
208+
let _ = writeln!(
201209
console,
202-
"Location: {}, line {}\n",
210+
"Location: {}, line {}",
203211
location.file(),
204212
location.line()
205213
);
206214
}
207-
let _ = write!(console, "\n");
208-
let _ = write!(console, "--- SYSTEM STATE ---\n");
209-
let _ = write!(console, "Boot Time: {:.4}s\n", boot_time);
210-
let _ = write!(console, "RIP: {:#018x}\n", rip);
211-
let _ = write!(console, "RFLAGS: {:#018x}\n", rflags);
212-
let _ = write!(console, "\n");
213-
let _ = write!(console, "--- REGISTERS ---\n");
214-
let _ = write!(console, "RAX: {:#018x} RBX: {:#018x}\n", rax, rbx);
215-
let _ = write!(console, "RCX: {:#018x} RDX: {:#018x}\n", rcx, rdx);
216-
let _ = write!(console, "RSI: {:#018x} RDI: {:#018x}\n", rsi, rdi);
217-
let _ = write!(console, "RBP: {:#018x} RSP: {:#018x}\n", rbp, rsp);
218-
let _ = write!(console, "R8: {:#018x} R9: {:#018x}\n", r8, r9);
219-
let _ = write!(console, "R10: {:#018x} R11: {:#018x}\n", r10, r11);
220-
let _ = write!(console, "R12: {:#018x} R13: {:#018x}\n", r12, r13);
221-
let _ = write!(console, "R14: {:#018x} R15: {:#018x}\n", r14, r15);
222-
let _ = write!(console, "\n");
223-
let _ = write!(console, "Please restart your computer.\n");
215+
let _ = writeln!(console);
216+
let _ = writeln!(console, "--- SYSTEM STATE ---");
217+
let _ = writeln!(console, "Boot Time: {:.4}s", boot_time);
218+
let _ = writeln!(console, "RIP: {:#018x}", rip);
219+
let _ = writeln!(console, "RFLAGS: {:#018x}", rflags);
220+
let _ = writeln!(console);
221+
let _ = writeln!(console, "--- REGISTERS ---");
222+
let _ = writeln!(console, "RAX: {:#018x} RBX: {:#018x}", rax, rbx);
223+
let _ = writeln!(console, "RCX: {:#018x} RDX: {:#018x}", rcx, rdx);
224+
let _ = writeln!(console, "RSI: {:#018x} RDI: {:#018x}", rsi, rdi);
225+
let _ = writeln!(console, "RBP: {:#018x} RSP: {:#018x}", rbp, rsp);
226+
let _ = writeln!(console, "R8: {:#018x} R9: {:#018x}", r8, r9);
227+
let _ = writeln!(console, "R10: {:#018x} R11: {:#018x}", r10, r11);
228+
let _ = writeln!(console, "R12: {:#018x} R13: {:#018x}", r12, r13);
229+
let _ = writeln!(console, "R14: {:#018x} R15: {:#018x}", r14, r15);
230+
let _ = writeln!(console);
231+
let _ = writeln!(console, "Please restart your computer.");
224232
}
225233
}
226234

@@ -239,6 +247,6 @@ pub fn panic(info: &PanicInfo) -> ! {
239247
#[cfg(test)]
240248
pub fn panic_for_test(info: &PanicInfo) -> ! {
241249
serial_println!("[FAILED]");
242-
serial_println!("Caused by:\n\t{}", info);
250+
serial_println!("Caused by:\t{}", info);
243251
crate::test::long_jmp();
244252
}

0 commit comments

Comments
 (0)