From f70b7a33319253a1eb1c72c3da411497e7455555 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sat, 7 Oct 2023 15:32:58 +0300 Subject: [PATCH] vhost-user-backend: use Cow<'static, str> Change the name's type `String` to `Cow<'static, str>`. This allows the library user to prevent string allocation from static strings, which is how most crates use it. This is an API breaking change. Signed-off-by: Manos Pitsidianakis --- crates/vhost-user-backend/CHANGELOG.md | 3 +++ crates/vhost-user-backend/README.md | 2 +- crates/vhost-user-backend/src/lib.rs | 13 +++++++------ .../vhost-user-backend/tests/vhost-user-server.rs | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/crates/vhost-user-backend/CHANGELOG.md b/crates/vhost-user-backend/CHANGELOG.md index 27c93046..d4080b18 100644 --- a/crates/vhost-user-backend/CHANGELOG.md +++ b/crates/vhost-user-backend/CHANGELOG.md @@ -9,6 +9,9 @@ - [[#192]](https://github.com/rust-vmm/vhost/pull/192) vhost-user-backend: remove return value from handle_event - [[#155]](https://github.com/rust-vmm/vhost/pull/155) Converted generic type parameters of VhostUserBackend into associated types. +- `name` field of VhostUserDaemon is now a `std::borrow::Cow<'static, str>` + instead of `String` to prevent allocating in the heap for static names, which + is the majority of uses of this API. ### Fixed diff --git a/crates/vhost-user-backend/README.md b/crates/vhost-user-backend/README.md index a4e51a83..3d27a9b9 100644 --- a/crates/vhost-user-backend/README.md +++ b/crates/vhost-user-backend/README.md @@ -19,7 +19,7 @@ where V: VringT> + Clone + Send + Sync + 'static, B: Bitmap + 'static, { - pub fn new(name: String, backend: S, atomic_mem: GuestMemoryAtomic>) -> Result; + pub fn new(name: Cow<'static, str>, backend: S, atomic_mem: GuestMemoryAtomic>) -> Result; pub fn start(&mut self, listener: Listener) -> Result<()>; pub fn wait(&mut self) -> Result<()>; pub fn get_epoll_handlers(&self) -> Vec>>; diff --git a/crates/vhost-user-backend/src/lib.rs b/crates/vhost-user-backend/src/lib.rs index 5f4ef2b5..923ffa50 100644 --- a/crates/vhost-user-backend/src/lib.rs +++ b/crates/vhost-user-backend/src/lib.rs @@ -8,6 +8,7 @@ #[macro_use] extern crate log; +use std::borrow::Cow; use std::fmt::{Display, Formatter}; use std::path::Path; use std::sync::{Arc, Mutex}; @@ -81,7 +82,7 @@ pub type Result = std::result::Result; /// This structure is the public API the backend is allowed to interact with in order to run /// a fully functional vhost-user daemon. pub struct VhostUserDaemon { - name: String, + name: Cow<'static, str>, handler: Arc>>, main_thread: Option>>, } @@ -98,7 +99,7 @@ where /// registered event. Those events can be vring events or custom events from the backend, /// but they get to be registered later during the sequence. pub fn new( - name: String, + name: Cow<'static, str>, backend: T, atomic_mem: GuestMemoryAtomic>, ) -> Result { @@ -124,7 +125,7 @@ where mut handler: BackendReqHandler>>, ) -> Result<()> { let handle = thread::Builder::new() - .name(self.name.clone()) + .name(self.name.to_string()) .spawn(move || loop { handler.handle_request().map_err(Error::HandleRequest)?; }) @@ -253,7 +254,7 @@ mod tests { GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(), ); let backend = Arc::new(Mutex::new(MockVhostBackend::new())); - let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap(); + let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap(); let handlers = daemon.get_epoll_handlers(); assert_eq!(handlers.len(), 2); @@ -286,7 +287,7 @@ mod tests { GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(), ); let backend = Arc::new(Mutex::new(MockVhostBackend::new())); - let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap(); + let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap(); let handlers = daemon.get_epoll_handlers(); assert_eq!(handlers.len(), 2); @@ -321,7 +322,7 @@ mod tests { GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(), ); let backend = Arc::new(Mutex::new(MockVhostBackend::new())); - let mut daemon = VhostUserDaemon::new("test".to_owned(), backend.clone(), mem).unwrap(); + let mut daemon = VhostUserDaemon::new("test".into(), backend.clone(), mem).unwrap(); let tmpdir = tempfile::tempdir().unwrap(); let socket_path = tmpdir.path().join("socket"); diff --git a/crates/vhost-user-backend/tests/vhost-user-server.rs b/crates/vhost-user-backend/tests/vhost-user-server.rs index d3e6bb70..3d8c532f 100644 --- a/crates/vhost-user-backend/tests/vhost-user-server.rs +++ b/crates/vhost-user-backend/tests/vhost-user-server.rs @@ -220,7 +220,7 @@ fn vhost_user_client(path: &Path, barrier: Arc) { fn vhost_user_server(cb: fn(&Path, Arc)) { let mem = GuestMemoryAtomic::new(GuestMemoryMmap::<()>::new()); let backend = Arc::new(Mutex::new(MockVhostBackend::new())); - let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap(); + let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap(); let barrier = Arc::new(Barrier::new(2)); let tmpdir = tempfile::tempdir().unwrap();