Crates.io | pid-allocator |
lib.rs | pid-allocator |
version | 0.1.5 |
source | src |
created_at | 2024-04-04 03:49:01.639028 |
updated_at | 2024-04-07 14:08:10.294441 |
description | A simple PID allocator for no_std environment |
homepage | https://github.com/KiritanTakechi/pid-allocator |
repository | https://github.com/KiritanTakechi/pid-allocator |
max_upload_size | |
id | 1195818 |
size | 16,853 |
The PidAllocator
crate provides a thread-safe and efficient PID (Process Identifier) allocation system, suitable for systems where PIDs need to be dynamically allocated and recycled. This crate is designed to work in no_std
environments, making it suitable for use in embedded systems, operating systems, and other contexts where standard library facilities are not available.
Arc
and SpinMutex
to ensure that PIDs can be safely allocated and recycled across multiple threads.no_std
Compatibility: Designed to work in no_std
environments, making it ideal for low-level system programming.Add the following to your Cargo.toml
file:
[dependencies]
pid_allocator = "0.1.5"
And then in your Rust code:
#![no_std]
extern crate alloc;
use pid_allocator::{PidAllocator, Pid};
const ORDER: usize = 32; // Customize based on your requirements
fn main() {
let allocator = PidAllocator::<ORDER>::new();
// Attempt to allocate a PID
if let Some(pid) = allocator.allocate() {
// Use the PID for your purposes
println!("Allocated PID: {}", *pid);
// PID will be automatically recycled when `pid` goes out of scope
}
}
PidAllocator
The main structure that manages PID allocation and recycling.
new() -> Self
: Creates a new instance of the PID allocator.allocate() -> Option<Pid>
: Allocates a new PID, if available, and wraps it in a Pid
structure.contains(usize) -> bool
: Checks whether a given PID is currently allocated.Pid
A handle to an allocated PID. Automatically recycles the PID when dropped.
The PidAllocator
crate utilizes a layered approach to manage the allocation and recycling of PIDs. Each layer represents a group of PIDs, with the state of each PID (allocated or free) tracked using a bit in a usize
value. The allocator scans these layers to quickly find free PIDs and to recycle them when no longer in use.
Contributions are welcome! Please feel free to submit pull requests, report bugs, and suggest features through the issue tracker.
This crate is licensed under MIT license.