sandbox/lib.rs
1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::sync::atomic::{AtomicU64, Ordering};
6use std::sync::Arc;
7
8/// A helper struct to generate unused capability ids.
9/// This is clonable and thread safe. There should generally be one of these
10/// for each `fuchsia.component.sandbox.CapabilityStore` connection.
11#[derive(Clone)]
12pub struct CapabilityIdGenerator {
13 next_id: Arc<AtomicU64>,
14}
15
16impl CapabilityIdGenerator {
17 pub fn new() -> Self {
18 Self { next_id: Arc::new(AtomicU64::new(0)) }
19 }
20
21 /// Get the next free id.
22 pub fn next(&self) -> u64 {
23 self.range(1)
24 }
25
26 /// Get a range of free ids of size `size`.
27 /// This returns the first id in the range.
28 pub fn range(&self, size: u64) -> u64 {
29 self.next_id.fetch_add(size, Ordering::Relaxed)
30 }
31}