pub trait GlobalFrameAllocator: Sync {
// Required methods
fn alloc(&self, layout: Layout) -> Option<Paddr>;
fn dealloc(&self, addr: Paddr, size: usize);
fn add_free_memory(&self, addr: Paddr, size: usize);
}
Expand description
The trait for the global frame allocator.
OSTD allows a customized frame allocator by the global_frame_allocator
attribute, which marks a static variable of this type.
The API mimics the standard Rust allocator API (GlobalAlloc
and
[global_allocator
]). However, this trait is much safer. Double free
or freeing in-use memory through this trait only messes up the allocator’s
state rather than causing undefined behavior.
Whenever OSTD or other modules need to allocate or deallocate frames via
FrameAllocOptions
, they are forwarded to the global frame allocator.
It is not encouraged to call the global allocator directly.
Required Methods§
Sourcefn alloc(&self, layout: Layout) -> Option<Paddr>
fn alloc(&self, layout: Layout) -> Option<Paddr>
Allocates a contiguous range of frames.
The caller guarantees that layout.size()
is aligned to PAGE_SIZE
.
When any of the allocated memory is not in use, OSTD returns them by
calling GlobalFrameAllocator::dealloc
. If multiple frames are
allocated, they may be returned in any order with any number of calls.
Sourcefn dealloc(&self, addr: Paddr, size: usize)
fn dealloc(&self, addr: Paddr, size: usize)
Deallocates a contiguous range of frames.
The caller guarantees that addr
and size
are both aligned to
PAGE_SIZE
. The deallocated memory should always be allocated by
GlobalFrameAllocator::alloc
. However, if
GlobalFrameAllocator::alloc
returns multiple frames, it is possible
that some of them are deallocated before others. The deallocated memory
must never overlap with any memory that is already deallocated or
added, without being allocated in between.
The deallocated memory can be uninitialized.
Sourcefn add_free_memory(&self, addr: Paddr, size: usize)
fn add_free_memory(&self, addr: Paddr, size: usize)
Adds a contiguous range of frames to the allocator.
The memory being added must never overlap with any memory that was added before.
The added memory can be uninitialized.