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
impl Allocator
Sourcepub fn allocate(&mut self, size: NonZeroU64) -> Option<AllocatedRange>
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字节。