Skip to content

Commit f570e56

Browse files
committed
Make IdGenerator non-static
1 parent 6426e70 commit f570e56

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

src/filesystem/search_id.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ pub struct SearchId(pub(crate) u32);
77
///
88
/// This object will always return a different ID.
99
pub struct IdGenerator {
10-
next_id: core::sync::atomic::AtomicU32,
10+
next_id: u32,
1111
}
1212

1313
impl IdGenerator {
1414
/// Create a new [`IdGenerator`].
1515
pub const fn new() -> Self {
16-
Self {
17-
next_id: core::sync::atomic::AtomicU32::new(0),
18-
}
16+
Self { next_id: 0 }
1917
}
2018

2119
/// Generate a new, unique [`SearchId`].
22-
pub fn next(&self) -> SearchId {
23-
use core::sync::atomic::Ordering;
24-
let id = self.next_id.load(Ordering::Acquire);
25-
self.next_id.store(id + 1, Ordering::Release);
20+
pub fn get(&mut self) -> SearchId {
21+
let id = self.next_id;
22+
self.next_id += 1;
2623
SearchId(id)
2724
}
2825
}

src/volume_mgr.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use crate::{
1414
};
1515
use heapless::Vec;
1616

17-
static ID_GENERATOR: IdGenerator = IdGenerator::new();
18-
1917
#[derive(PartialEq, Eq)]
2018
struct ClusterDescriptor {
2119
volume_idx: VolumeIdx,
@@ -24,6 +22,14 @@ struct ClusterDescriptor {
2422
}
2523

2624
impl ClusterDescriptor {
25+
fn new(volume_idx: VolumeIdx, cluster: Cluster, search_id: SearchId) -> Self {
26+
Self {
27+
volume_idx,
28+
cluster,
29+
search_id,
30+
}
31+
}
32+
2733
fn compare_volume_and_cluster(&self, volume_idx: VolumeIdx, cluster: Cluster) -> bool {
2834
self.volume_idx == volume_idx && self.cluster == cluster
2935
}
@@ -42,6 +48,7 @@ where
4248
{
4349
pub(crate) block_device: D,
4450
pub(crate) timesource: T,
51+
id_generator: IdGenerator,
4552
open_dirs: Vec<ClusterDescriptor, MAX_DIRS>,
4653
open_files: Vec<ClusterDescriptor, MAX_FILES>,
4754
}
@@ -81,6 +88,7 @@ where
8188
VolumeManager {
8289
block_device,
8390
timesource,
91+
id_generator: IdGenerator::new(),
8492
open_dirs: Vec::new(),
8593
open_files: Vec::new(),
8694
}
@@ -186,14 +194,14 @@ where
186194
return Err(Error::DirAlreadyOpen);
187195
}
188196

189-
let search_id = ID_GENERATOR.next();
197+
let search_id = self.id_generator.get();
190198
// Remember this open directory
191199
self.open_dirs
192-
.push(ClusterDescriptor {
193-
volume_idx: volume.idx,
194-
cluster: Cluster::ROOT_DIR,
200+
.push(ClusterDescriptor::new(
201+
volume.idx,
202+
Cluster::ROOT_DIR,
195203
search_id,
196-
})
204+
))
197205
.map_err(|_| Error::TooManyOpenDirs)?;
198206

199207
Ok(Directory {
@@ -234,13 +242,13 @@ where
234242
}
235243

236244
// Remember this open directory.
237-
let search_id = ID_GENERATOR.next();
245+
let search_id = self.id_generator.get();
238246
self.open_dirs
239-
.push(ClusterDescriptor {
240-
volume_idx: volume.idx,
241-
cluster: dir_entry.cluster,
247+
.push(ClusterDescriptor::new(
248+
volume.idx,
249+
dir_entry.cluster,
242250
search_id,
243-
})
251+
))
244252
.map_err(|_| Error::TooManyOpenDirs)?;
245253

246254
Ok(Directory {
@@ -255,7 +263,7 @@ where
255263
// Unwrap, because we should never be in a situation where we're attempting to close a dir
256264
// with an ID which doesn't exist in our open dirs list.
257265
let idx_to_close = cluster_position_by_id(&self.open_dirs, dir.search_id).unwrap();
258-
self.open_dirs.swap_remove(idx_to_close);
266+
self.open_dirs.remove(idx_to_close);
259267
drop(dir);
260268
}
261269

@@ -310,7 +318,7 @@ where
310318
}
311319

312320
let mode = solve_mode_variant(mode, true);
313-
let search_id = ID_GENERATOR.next();
321+
let search_id = self.id_generator.get();
314322

315323
let file = match mode {
316324
Mode::ReadOnly => File {
@@ -367,11 +375,11 @@ where
367375

368376
// Remember this open file
369377
self.open_files
370-
.push(ClusterDescriptor {
371-
volume_idx: volume.idx,
372-
cluster: file.starting_cluster,
378+
.push(ClusterDescriptor::new(
379+
volume.idx,
380+
file.starting_cluster,
373381
search_id,
374-
})
382+
))
375383
.map_err(|_| Error::TooManyOpenDirs)?;
376384

377385
Ok(file)
@@ -421,7 +429,7 @@ where
421429
}
422430
};
423431

424-
let search_id = ID_GENERATOR.next();
432+
let search_id = self.id_generator.get();
425433

426434
let file = File {
427435
starting_cluster: entry.cluster,
@@ -435,11 +443,11 @@ where
435443

436444
// Remember this open file
437445
self.open_files
438-
.push(ClusterDescriptor {
439-
volume_idx: volume.idx,
440-
cluster: file.starting_cluster,
446+
.push(ClusterDescriptor::new(
447+
volume.idx,
448+
file.starting_cluster,
441449
search_id,
442-
})
450+
))
443451
.map_err(|_| Error::TooManyOpenFiles)?;
444452

445453
Ok(file)
@@ -478,8 +486,19 @@ where
478486
}
479487

480488
match &volume.volume_type {
481-
VolumeType::Fat(fat) => fat.delete_directory_entry(self, dir, name),
489+
VolumeType::Fat(fat) => fat.delete_directory_entry(self, dir, name)?,
482490
}
491+
492+
// Unwrap, because we should never be in a situation where we're attempting to close a file
493+
// which doesn't exist in our open files list.
494+
let idx_to_remove = self
495+
.open_files
496+
.iter()
497+
.position(|d| d.compare_volume_and_cluster(volume.idx, dir_entry.cluster))
498+
.unwrap();
499+
self.open_files.remove(idx_to_remove);
500+
501+
Ok(())
483502
}
484503

485504
/// Read from an open file.
@@ -643,7 +662,7 @@ where
643662
// Unwrap, because we should never be in a situation where we're attempting to close a file
644663
// with an ID which doesn't exist in our open files list.
645664
let idx_to_close = cluster_position_by_id(&self.open_files, file.search_id).unwrap();
646-
self.open_files.swap_remove(idx_to_close);
665+
self.open_files.remove(idx_to_close);
647666

648667
drop(file);
649668
Ok(())

0 commit comments

Comments
 (0)