Skip to content

Commit

Permalink
[feat] adaptive page allocator size
Browse files Browse the repository at this point in the history
  • Loading branch information
hky1999 committed Dec 10, 2024
1 parent a081ea9 commit 2b4a566
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ repository = "https://github.com/arceos-org/allocator"
documentation = "https://arceos-org.github.io/allocator"

[features]
default = []
full = ["bitmap", "tlsf", "slab", "buddy", "allocator_api"]
default = ["page-alloc-4g"]
full = ["bitmap", "tlsf", "slab", "buddy", "allocator_api","page-alloc-4g"]

bitmap = ["dep:bitmap-allocator"]

Expand All @@ -20,9 +20,17 @@ slab = ["dep:slab_allocator"]
buddy = ["dep:buddy_system_allocator"]

allocator_api = []
64G = []

page-alloc-1t = []
page-alloc-64g = []
page-alloc-4g = []
page-alloc-256m = []
page-alloc-16m = []
page-alloc-1m = []
page-alloc-64k = []

[dependencies]
cfg-if = "1.0"
rlsf = { version = "0.2", optional = true }
buddy_system_allocator = { version = "0.10", default-features = false, optional = true }
slab_allocator = { git = "https://github.com/arceos-org/slab_allocator.git", tag = "v0.3.1", optional = true }
Expand Down
44 changes: 28 additions & 16 deletions src/bitmap.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
//! Bitmap allocation in page-granularity.
//!
//! TODO: adaptive size
use bitmap_allocator::BitAlloc;

use crate::{AllocError, AllocResult, BaseAllocator, PageAllocator};

// Support max 1M * 4096 = 4GB memory.
#[cfg(not(feature = "64G"))]
type BitAllocUsed = bitmap_allocator::BitAlloc1M;

// Support max 16M * 4096 = 64GB memory.
#[cfg(feature = "64G")]
type BitAllocUsed = bitmap_allocator::BitAlloc16M;
cfg_if::cfg_if! {
if #[cfg(feature = "page-alloc-1t")] {
/// Support max 256M * PAGE_SIZE = 1TB memory (assume that PAGE_SIZE = 4KB).
type BitAllocUsed = bitmap_allocator::BitAlloc256M;
} else if #[cfg(feature = "page-alloc-64g")] {
/// Support max 16M * PAGE_SIZE = 64GB memory (assume that PAGE_SIZE = 4KB).
type BitAllocUsed = bitmap_allocator::BitAlloc16M;
} else if #[cfg(feature = "page-alloc-4g")] {
/// Support max 1M * PAGE_SIZE = 4GB memory (assume that PAGE_SIZE = 4KB).
type BitAllocUsed = bitmap_allocator::BitAlloc1M;
} else if #[cfg(feature = "page-alloc-256m")] {
/// Support max 64K * PAGE_SIZE = 256MB memory (assume that PAGE_SIZE = 4KB).
type BitAllocUsed = bitmap_allocator::BitAlloc64K;
} else if #[cfg(feature = "page-alloc-16m")] {
/// Support max 4K * PAGE_SIZE = 16MB memory (assume that PAGE_SIZE = 4KB).
type BitAllocUsed = bitmap_allocator::BitAlloc4K;
} else if #[cfg(feature = "page-alloc-1m")] {
/// Support max 256 * PAGE_SIZE = 1MB memory (assume that PAGE_SIZE = 4KB).
type BitAllocUsed = bitmap_allocator::BitAlloc256;
} else {
/// Support max 16 * PAGE_SIZE = 64KB memory (assume that PAGE_SIZE = 4KB).
type BitAllocUsed = bitmap_allocator::BitAlloc16;
}
}

/// A page-granularity memory allocator based on the [bitmap_allocator].
///
Expand Down Expand Up @@ -79,13 +94,10 @@ impl<const PAGE_SIZE: usize> PageAllocator for BitmapPageAllocator<PAGE_SIZE> {
}

fn dealloc_pages(&mut self, pos: usize, num_pages: usize) {
if match num_pages.cmp(&1) {
core::cmp::Ordering::Equal => self.inner.dealloc((pos - self.base) / PAGE_SIZE),
core::cmp::Ordering::Greater => self
.inner
.dealloc_contiguous((pos - self.base) / PAGE_SIZE, num_pages),
_ => false,
} {
if self
.inner
.dealloc_contiguous((pos - self.base) / PAGE_SIZE, num_pages)
{
self.used_pages -= num_pages;
}
}
Expand Down

0 comments on commit 2b4a566

Please sign in to comment.