pid-allocator

Crates.iopid-allocator
lib.rspid-allocator
version0.1.5
sourcesrc
created_at2024-04-04 03:49:01.639028
updated_at2024-04-07 14:08:10.294441
descriptionA simple PID allocator for no_std environment
homepagehttps://github.com/KiritanTakechi/pid-allocator
repositoryhttps://github.com/KiritanTakechi/pid-allocator
max_upload_size
id1195818
size16,853
Kiritan (KiritanTakechi)

documentation

https://docs.rs/pid-allocator

README

PidAllocator

Overview

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.

Features

  • Thread-Safe Allocation: Uses Arc and SpinMutex to ensure that PIDs can be safely allocated and recycled across multiple threads.
  • Efficient Recycling: Implements a fast allocation strategy that efficiently recycles PIDs, ensuring minimal wastage of the PID space.
  • no_std Compatibility: Designed to work in no_std environments, making it ideal for low-level system programming.

Usage

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
    }
}

Structures

PidAllocator

The main structure that manages PID allocation and recycling.

Methods

  • 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.

How It Works

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.

Contribution

Contributions are welcome! Please feel free to submit pull requests, report bugs, and suggest features through the issue tracker.

License

This crate is licensed under MIT license.


Commit count: 13

cargo fmt