Allocator

Struct Allocator 

Source
pub struct Allocator { /* private fields */ }
Expand description

Sequential range allocator for file regions

文件区域的顺序范围分配器

This allocator sequentially allocates non-overlapping ranges from the beginning to the end of a file. It returns AllocatedRange types, guaranteeing that all allocated ranges are valid and non-overlapping.

此分配器从文件开头向结尾顺序分配不重叠的范围。 返回 AllocatedRange 类型,保证所有分配的范围都是有效且不重叠的。

§Example

let mut allocator = Allocator::new(NonZeroU64::new(ALIGNMENT * 3).unwrap());

// Allocate 4K bytes (allocations are 4K aligned)
// 分配 4K 字节(分配是4K对齐的)
let range1 = allocator.allocate(NonZeroU64::new(ALIGNMENT).unwrap()).unwrap();
assert_eq!(range1.start(), 0);
assert_eq!(range1.end(), ALIGNMENT);

let range2 = allocator.allocate(NonZeroU64::new(ALIGNMENT).unwrap()).unwrap();
assert_eq!(range2.start(), ALIGNMENT);
assert_eq!(range2.end(), ALIGNMENT * 2);

// When remaining space is less than requested, allocate remaining space
// 当剩余空间小于请求大小时,分配剩余空间
let range3 = allocator.allocate(NonZeroU64::new(ALIGNMENT * 2).unwrap()).unwrap();
assert_eq!(range3.start(), ALIGNMENT * 2);
assert_eq!(range3.end(), ALIGNMENT * 3); // Only 4K bytes allocated

// Returns None when no space left
// 当没有剩余空间时返回 None
assert!(allocator.allocate(NonZeroU64::new(1).unwrap()).is_none());

Implementations§

Source§

impl Allocator

Source

pub fn allocate(&mut self, size: NonZeroU64) -> Option<AllocatedRange>

Allocate a range of the specified size (4K aligned)

分配指定大小的范围(4K对齐)

Allocates from the current unallocated position. The allocation size is rounded up to 4K boundary to ensure alignment. When remaining space is less than the aligned requested size, allocates all remaining space instead. Returns None only when no space is left.

从当前未分配位置开始分配。分配大小会向上对齐到4K边界以确保对齐。 当剩余空间小于对齐后的请求大小时,分配所有剩余空间。 仅当没有剩余空间时返回 None

§Note

The actual allocated size may be larger than requested due to 4K alignment. For example, requesting 100 bytes will allocate 4096 bytes.

§注意

由于4K对齐,实际分配的大小可能大于请求的大小。 例如,请求100字节将分配4096字节。

Source

pub fn remaining(&self) -> u64

Get the number of remaining allocatable bytes

获取剩余可分配字节数

Source

pub fn next_pos(&self) -> u64

Get the next allocation position

获取下一个分配位置

Trait Implementations§

Source§

impl RangeAllocator for Allocator

Source§

fn new(total_size: NonZeroU64) -> Self

Create a new range allocator Read more
Source§

fn total_size(&self) -> NonZeroU64

Get the total size Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.