Git Product home page Git Product logo

pid-allocator's Introduction

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.


pid-allocator's People

Contributors

kiritantakechi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.