Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Use BTreeSet for CID allocations (instead of Vec)
Browse files Browse the repository at this point in the history
Using set for CID allocations provides better API.
  • Loading branch information
tuommaki committed Mar 21, 2024
1 parent ed90974 commit 8897da9
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions crates/node/src/vmm/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rand::{distributions::Alphanumeric, Rng};
use serde_json::json;
use std::{
any::Any,
collections::HashMap,
collections::{BTreeSet, HashMap},
fs::File,
path::Path,
process::{Child, Command, Stdio},
Expand Down Expand Up @@ -64,15 +64,15 @@ pub struct QEMUVMHandle {

pub struct Qemu {
config: Arc<Config>,
cid_allocations: Vec<u32>,
cid_allocations: BTreeSet<u32>,
vm_registry: HashMap<u32, QEMUVMHandle>,
}

impl Qemu {
pub fn new(config: Arc<Config>) -> Self {
Qemu {
config,
cid_allocations: vec![],
cid_allocations: Default::default(),
vm_registry: HashMap::new(),
}
}
Expand All @@ -86,19 +86,17 @@ impl Qemu {
continue;
}

if self.cid_allocations.iter().any(|&x| x == cid) {
if !self.cid_allocations.insert(cid) {
// Generated CID found from existing allocations.
continue;
};

self.cid_allocations.push(cid);
return cid;
}
}

fn release_cid(&mut self, cid: u32) {
if let Some(idx) = self.cid_allocations.iter().position(|&x| x == cid) {
self.cid_allocations.remove(idx);
if self.cid_allocations.remove(&cid) {
self.vm_registry.remove(&cid);
}
}
Expand Down

0 comments on commit 8897da9

Please sign in to comment.