forked from DragonOS-Community/DragonOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mm): 添加slab内存分配器 (DragonOS-Community#683)
feat(mm): 添加slab内存分配器 --------- Co-authored-by: longjin <[email protected]>
- Loading branch information
1 parent
c345041
commit 6f8d288
Showing
9 changed files
with
1,246 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "slabmalloc" | ||
version = "0.11.0" | ||
edition = "2018" | ||
|
||
[features] | ||
unstable = [] | ||
default = [ "unstable" ] | ||
|
||
[dependencies] | ||
log = "0.4" | ||
|
||
[target.'cfg(unix)'.dev-dependencies] | ||
rand = "0.8" | ||
env_logger = "0.9" | ||
spin = "0.9.8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! A slab allocator implementation for objects less than a page-size (4 KiB or 2MiB). | ||
//! | ||
//! # Overview | ||
//! | ||
//! The organization is as follows: | ||
//! | ||
//! * A `ZoneAllocator` manages many `SCAllocator` and can | ||
//! satisfy requests for different allocation sizes. | ||
//! * A `SCAllocator` allocates objects of exactly one size. | ||
//! It stores the objects and meta-data in one or multiple `AllocablePage` objects. | ||
//! * A trait `AllocablePage` that defines the page-type from which we allocate objects. | ||
//! | ||
//! Lastly, it provides two default `AllocablePage` implementations `ObjectPage` and `LargeObjectPage`: | ||
//! * A `ObjectPage` that is 4 KiB in size and contains allocated objects and associated meta-data. | ||
//! * A `LargeObjectPage` that is 2 MiB in size and contains allocated objects and associated meta-data. | ||
//! | ||
//! | ||
//! # Implementing GlobalAlloc | ||
//! See the [global alloc](https://github.com/gz/rust-slabmalloc/tree/master/examples/global_alloc.rs) example. | ||
#![allow(unused_features)] | ||
#![cfg_attr(feature = "unstable", feature(const_mut_refs))] | ||
#![no_std] | ||
#![crate_name = "slabmalloc"] | ||
#![crate_type = "lib"] | ||
#![feature(new_uninit)] | ||
#![feature(maybe_uninit_as_bytes)] | ||
|
||
extern crate alloc; | ||
|
||
mod pages; | ||
mod sc; | ||
mod zone; | ||
|
||
pub use pages::*; | ||
pub use sc::*; | ||
pub use zone::*; | ||
|
||
use core::alloc::Layout; | ||
use core::fmt; | ||
use core::ptr::{self, NonNull}; | ||
|
||
use log::trace; | ||
|
||
/// How many bytes in the page are used by allocator meta-data. | ||
const OBJECT_PAGE_METADATA_OVERHEAD: usize = 80; | ||
|
||
/// How many bytes a [`ObjectPage`] is. | ||
const OBJECT_PAGE_SIZE: usize = 4096; | ||
|
||
type VAddr = usize; | ||
|
||
/// Error that can be returned for `allocation` and `deallocation` requests. | ||
#[derive(Debug)] | ||
pub enum AllocationError { | ||
/// Can't satisfy the allocation request for Layout because the allocator | ||
/// does not have enough memory (you may be able to `refill` it). | ||
OutOfMemory, | ||
/// Allocator can't deal with the provided size of the Layout. | ||
InvalidLayout, | ||
} | ||
|
||
/// Allocator trait to be implemented by users of slabmalloc to provide memory to slabmalloc. | ||
/// | ||
/// # Safety | ||
/// Needs to adhere to safety requirements of a rust allocator (see GlobalAlloc et. al.). | ||
pub unsafe trait Allocator<'a> { | ||
fn allocate(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocationError>; | ||
fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout) -> Result<(), AllocationError>; | ||
|
||
/// Refill the allocator with a [`ObjectPage`]. | ||
/// | ||
/// # Safety | ||
/// TBD (this API needs to change anyways, likely new page should be a raw pointer) | ||
unsafe fn refill( | ||
&mut self, | ||
layout: Layout, | ||
new_page: &'a mut ObjectPage<'a>, | ||
) -> Result<(), AllocationError>; | ||
} |
Oops, something went wrong.