Skip to content

Commit f70b7a3

Browse files
committed
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 <[email protected]>
1 parent 562974f commit f70b7a3

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

crates/vhost-user-backend/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
- [[#192]](https://github.com/rust-vmm/vhost/pull/192) vhost-user-backend: remove return value from handle_event
1010
- [[#155]](https://github.com/rust-vmm/vhost/pull/155) Converted generic type
1111
parameters of VhostUserBackend into associated types.
12+
- `name` field of VhostUserDaemon is now a `std::borrow::Cow<'static, str>`
13+
instead of `String` to prevent allocating in the heap for static names, which
14+
is the majority of uses of this API.
1215

1316
### Fixed
1417

crates/vhost-user-backend/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ where
1919
V: VringT<GM<B>> + Clone + Send + Sync + 'static,
2020
B: Bitmap + 'static,
2121
{
22-
pub fn new(name: String, backend: S, atomic_mem: GuestMemoryAtomic<GuestMemoryMmap<B>>) -> Result<Self>;
22+
pub fn new(name: Cow<'static, str>, backend: S, atomic_mem: GuestMemoryAtomic<GuestMemoryMmap<B>>) -> Result<Self>;
2323
pub fn start(&mut self, listener: Listener) -> Result<()>;
2424
pub fn wait(&mut self) -> Result<()>;
2525
pub fn get_epoll_handlers(&self) -> Vec<Arc<VringEpollHandler<S, V, B>>>;

crates/vhost-user-backend/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#[macro_use]
99
extern crate log;
1010

11+
use std::borrow::Cow;
1112
use std::fmt::{Display, Formatter};
1213
use std::path::Path;
1314
use std::sync::{Arc, Mutex};
@@ -81,7 +82,7 @@ pub type Result<T> = std::result::Result<T, Error>;
8182
/// This structure is the public API the backend is allowed to interact with in order to run
8283
/// a fully functional vhost-user daemon.
8384
pub struct VhostUserDaemon<T: VhostUserBackend> {
84-
name: String,
85+
name: Cow<'static, str>,
8586
handler: Arc<Mutex<VhostUserHandler<T>>>,
8687
main_thread: Option<thread::JoinHandle<Result<()>>>,
8788
}
@@ -98,7 +99,7 @@ where
9899
/// registered event. Those events can be vring events or custom events from the backend,
99100
/// but they get to be registered later during the sequence.
100101
pub fn new(
101-
name: String,
102+
name: Cow<'static, str>,
102103
backend: T,
103104
atomic_mem: GuestMemoryAtomic<GuestMemoryMmap<T::Bitmap>>,
104105
) -> Result<Self> {
@@ -124,7 +125,7 @@ where
124125
mut handler: BackendReqHandler<Mutex<VhostUserHandler<T>>>,
125126
) -> Result<()> {
126127
let handle = thread::Builder::new()
127-
.name(self.name.clone())
128+
.name(self.name.to_string())
128129
.spawn(move || loop {
129130
handler.handle_request().map_err(Error::HandleRequest)?;
130131
})
@@ -253,7 +254,7 @@ mod tests {
253254
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
254255
);
255256
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
256-
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
257+
let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap();
257258

258259
let handlers = daemon.get_epoll_handlers();
259260
assert_eq!(handlers.len(), 2);
@@ -286,7 +287,7 @@ mod tests {
286287
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
287288
);
288289
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
289-
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
290+
let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap();
290291

291292
let handlers = daemon.get_epoll_handlers();
292293
assert_eq!(handlers.len(), 2);
@@ -321,7 +322,7 @@ mod tests {
321322
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
322323
);
323324
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
324-
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend.clone(), mem).unwrap();
325+
let mut daemon = VhostUserDaemon::new("test".into(), backend.clone(), mem).unwrap();
325326
let tmpdir = tempfile::tempdir().unwrap();
326327
let socket_path = tmpdir.path().join("socket");
327328

crates/vhost-user-backend/tests/vhost-user-server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn vhost_user_client(path: &Path, barrier: Arc<Barrier>) {
220220
fn vhost_user_server(cb: fn(&Path, Arc<Barrier>)) {
221221
let mem = GuestMemoryAtomic::new(GuestMemoryMmap::<()>::new());
222222
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
223-
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();
223+
let mut daemon = VhostUserDaemon::new("test".into(), backend, mem).unwrap();
224224

225225
let barrier = Arc::new(Barrier::new(2));
226226
let tmpdir = tempfile::tempdir().unwrap();

0 commit comments

Comments
 (0)