Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
[![Crates.io](https://img.shields.io/crates/v/bitmap-allocator)](https://crates.io/crates/bitmap-allocator)
[![Docs.rs](https://docs.rs/bitmap-allocator/badge.svg)](https://docs.rs/bitmap-allocator)
[![CI](https://github.com/rcore-os/bitmap-allocator/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/rcore-os/bitmap-allocator/actions/workflows/main.yml)
[![Coverage Status](https://coveralls.io/repos/github/rcore-os/bitmap-allocator/badge.svg?branch=master)](https://coveralls.io/github/rcore-os/bitmap-allocator?branch=master)

Bit allocator based on segment tree algorithm.

## Example

```rust
use bitmap_allocator::{BitAlloc, BitAlloc1M};

let mut ba = BitAlloc1M::default();
ba.insert(0..16);
for i in 0..16 {
assert!(ba.test(i));
}
ba.remove(2..8);
assert_eq!(ba.alloc(), Some(0));
assert_eq!(ba.alloc(), Some(1));
assert_eq!(ba.alloc(), Some(8));
ba.dealloc(0);
ba.dealloc(1);
ba.dealloc(8);
```
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ pub trait BitAlloc: Default {
}

/// A bitmap of 256 bits
///
/// ## Example
///
/// ```rust
/// use bitmap_allocator::{BitAlloc, BitAlloc256};
///
/// let mut ba = BitAlloc256::default();
/// ba.insert(0..16);
/// for i in 0..16 {
/// assert!(ba.test(i));
/// }
/// ba.remove(2..8);
/// assert_eq!(ba.alloc(), Some(0));
/// assert_eq!(ba.alloc(), Some(1));
/// assert_eq!(ba.alloc(), Some(8));
/// ba.dealloc(0);
/// ba.dealloc(1);
/// ba.dealloc(8);
///
/// assert!(!ba.is_empty());
/// ```
pub type BitAlloc256 = BitAllocCascade16<BitAlloc16>;
/// A bitmap of 4K bits
pub type BitAlloc4K = BitAllocCascade16<BitAlloc256>;
Expand Down Expand Up @@ -196,6 +217,28 @@ impl<T: BitAlloc> BitAllocCascade16<T> {

/// A bitmap consisting of only 16 bits.
/// BitAlloc16 acts as the leaf (except the leaf bits of course) nodes in the segment trees.
///
/// ## Example
///
/// ```rust
/// use bitmap_allocator::{BitAlloc, BitAlloc16};
///
/// let mut ba = BitAlloc16::default();
/// assert_eq!(BitAlloc16::CAP, 16);
/// ba.insert(0..16);
/// for i in 0..16 {
/// assert!(ba.test(i));
/// }
/// ba.remove(2..8);
/// assert_eq!(ba.alloc(), Some(0));
/// assert_eq!(ba.alloc(), Some(1));
/// assert_eq!(ba.alloc(), Some(8));
/// ba.dealloc(0);
/// ba.dealloc(1);
/// ba.dealloc(8);
///
/// assert!(!ba.is_empty());
/// ```
#[derive(Default)]
pub struct BitAlloc16(u16);

Expand Down
Loading