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

Commit

Permalink
Merge branch 'main' into drop-nanos-mod
Browse files Browse the repository at this point in the history
  • Loading branch information
tuommaki authored Mar 21, 2024
2 parents 3d741e6 + a5ac802 commit a2b65bc
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions crates/node/src/vmm/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ 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},
sync::Arc,
sync::{
atomic::{AtomicU32, Ordering},
Arc,
},
time::{Duration, Instant},
};
use tokio::{
Expand Down Expand Up @@ -63,41 +66,41 @@ pub struct QEMUVMHandle {

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

impl Qemu {
pub fn new(config: Arc<Config>) -> Self {
Qemu {
config,
cid_allocations: vec![],
next_cid: AtomicU32::new(4),
cid_allocations: Default::default(),
vm_registry: HashMap::new(),
}
}

fn allocate_cid(&mut self) -> u32 {
loop {
let cid = rand::random::<u32>();
let cid = self.next_cid.fetch_add(1, Ordering::Relaxed);

if cid < 3 {
// CIDs 0, 1 and 2 are reserved.
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 a2b65bc

Please sign in to comment.