From 1e8ad16b354bc3f8ee944803561d290a602db048 Mon Sep 17 00:00:00 2001 From: longjin Date: Thu, 5 Jun 2025 14:14:36 +0800 Subject: [PATCH 1/2] refactor(console): optimize buffer allocation with explicit type Signed-off-by: longjin --- src/device/console.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/device/console.rs b/src/device/console.rs index fe1d996..a6e673f 100644 --- a/src/device/console.rs +++ b/src/device/console.rs @@ -7,6 +7,8 @@ use crate::volatile::{volread, ReadOnly, WriteOnly}; use crate::{Result, PAGE_SIZE}; use alloc::boxed::Box; use bitflags::bitflags; +use alloc::vec; +use core::convert::TryInto; use core::ptr::NonNull; const QUEUE_RECEIVEQ_PORT_0: u16 = 0; @@ -83,7 +85,7 @@ impl VirtIOConsole { // Safe because no alignment or initialisation is required for [u8], the DMA buffer is // dereferenceable, and the lifetime of the reference matches the lifetime of the DMA buffer // (which we don't otherwise access). - let queue_buf_rx = Box::new([0; PAGE_SIZE]); + let queue_buf_rx: Box<[u8; 4096]> = vec![0u8; PAGE_SIZE].into_boxed_slice().try_into().unwrap(); transport.finish_init(); let mut console = VirtIOConsole { From bd37bc20bbcd1ccf9b6d3fdbba9abef1fc14916a Mon Sep 17 00:00:00 2001 From: longjin Date: Thu, 5 Jun 2025 14:16:36 +0800 Subject: [PATCH 2/2] feat: add optional logging support with 'log' and 'fake-log' features Add optional logging dependencies and features to enable runtime logging. Make 'fake-log' feature part of default build. Signed-off-by: longjin --- Cargo.toml | 7 +++++-- src/lib.rs | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b12ddf8..8309246 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,15 @@ keywords = ["virtio"] categories = ["hardware-support", "no-std"] [dependencies] -log = "0.4" +fake-log = { version = "0.1", optional = true } +log = { version = "0.4", optional = true } bitflags = "2.3.0" zerocopy = { version = "0.7.5", features = ["derive"] } [features] -default = ["alloc"] +default = ["alloc", "fake-log"] +log = ["dep:log"] +fake-log = ["dep:fake-log"] alloc = ["zerocopy/alloc"] [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index f2f2f12..42f1241 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,6 +49,12 @@ #[cfg(any(feature = "alloc", test))] extern crate alloc; +#[cfg(feature = "fake-log")] +extern crate fake_log as log; + +#[cfg(feature = "log")] +extern crate log; + pub mod device; mod hal; mod queue;