From 906019f7c6efa5984dba8ff34a62578be50c9dac Mon Sep 17 00:00:00 2001 From: dzy Date: Fri, 7 Sep 2018 20:53:37 +0800 Subject: [PATCH] Add little notes for BitAllocator --- crate/bit-allocator/src/lib.rs | 15 +++++++++++++++ crate/process/src/scheduler.rs | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crate/bit-allocator/src/lib.rs b/crate/bit-allocator/src/lib.rs index bc49dcb3..02c38c2c 100644 --- a/crate/bit-allocator/src/lib.rs +++ b/crate/bit-allocator/src/lib.rs @@ -6,6 +6,18 @@ extern crate bit_field; use bit_field::BitField; use core::ops::Range; +/// Allocator of a bitmap, able to allocate / free bits. +/// +/// CAP: the bitmap has a total of CAP bits, numbered from 0 to CAP-1 inclusively. +/// +/// alloc: allocate a free bit. +/// dealloc: free an allocated bit. +/// +/// insert: mark bits in the range as allocated +/// remove: reverse of insert +/// +/// any: whether there are free bits remaining +/// test: whether a specific bit is free pub trait BitAlloc: Default { const CAP: usize; fn alloc(&mut self) -> Option; @@ -23,6 +35,7 @@ pub type BitAlloc1M = BitAllocCascade16; pub type BitAlloc16M = BitAllocCascade16; pub type BitAlloc256M = BitAllocCascade16; +/// Implement the bit allocator by segment tree algorithm. #[derive(Default)] pub struct BitAllocCascade16 { bitset: u16, @@ -77,6 +90,8 @@ impl BitAllocCascade16 { #[derive(Default)] pub struct BitAlloc16(u16); +/// BitAlloc16 acts as the leaf (except the leaf bits of course) nodes +/// in the segment trees. impl BitAlloc for BitAlloc16 { const CAP: usize = 16; diff --git a/crate/process/src/scheduler.rs b/crate/process/src/scheduler.rs index f29df1f6..4d8e6964 100644 --- a/crate/process/src/scheduler.rs +++ b/crate/process/src/scheduler.rs @@ -211,4 +211,5 @@ mod stride { fn expand(vec: &mut Vec, id: usize) { let len = vec.len(); vec.resize(len.max(id + 1), T::default()); -} \ No newline at end of file +} +