Skip to content

feat: add fixed-address allocation for FrameAllocator#48

Merged
jiegec merged 1 commit intorcore-os:masterfrom
Acture:master
Mar 30, 2026
Merged

feat: add fixed-address allocation for FrameAllocator#48
jiegec merged 1 commit intorcore-os:masterfrom
Acture:master

Conversation

@Acture
Copy link
Copy Markdown
Contributor

@Acture Acture commented Mar 30, 2026

Summary

Add FrameAllocator::alloc_at(start, count) for fixed-address frame allocation, as requested in #30.

There are common scenarios (MMIO regions, DMA buffers, identity mapping) where frames must be allocated at a specific physical address rather than an arbitrary one chosen by the allocator. The new method finds the free block containing the requested range, splits it down to the exact target size, and returns the remaining fragments to the free lists.

API

/// Try to allocate a specific range of frames [start, start + count).
/// count is rounded up to the next power of two (same as alloc).
/// start must be aligned to the rounded-up size.
/// Returns Some(start) on success, None if unavailable.
pub fn alloc_at(&mut self, start: usize, count: usize) -> Option<usize>

The method follows the existing alloc/alloc_power_of_two pattern with a public wrapper and a private alloc_at_power_of_two helper. Existing dealloc works unchanged for blocks allocated via alloc_at.

Algorithm

  1. Round count up to next power of two, validate alignment
  2. Search upward through free list orders to find a block containing the target range
  3. Split downward from the found order to the target order, returning non-target halves to the free lists
  4. Complexity: O(ORDER × log n), same as existing alloc

Test plan

  • Basic alloc_at where block exists at exact order
  • alloc_at requiring block splitting, verify remaining fragments are usable
  • alloc_at on already-allocated memory returns None
  • alloc_at with misaligned start returns None
  • alloc_at → dealloc roundtrip, verify buddy merge restores full block
  • alloc_at on address outside managed range returns None
  • Multiple non-overlapping alloc_at calls exhaust the range

All 25 tests pass (cargo test), no new clippy warnings.

Closes #30

Add `alloc_at(start, count)` to allocate a specific range of frames,
addressing the need for fixed-address allocation in OS scenarios like
MMIO regions, DMA buffers, and identity mapping.

Closes rcore-os#30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds fixed-address frame allocation support to the buddy-system FrameAllocator, enabling callers to request allocation of a specific physical frame range (useful for MMIO/DMA/identity-mapped regions), and validates behavior via new unit tests.

Changes:

  • Add FrameAllocator::alloc_at(start, count) public API plus internal alloc_at_power_of_two helper to allocate an aligned, power-of-two-sized range at a specific start frame.
  • Implement split-down logic that returns non-target fragments back into the appropriate free lists.
  • Add a focused suite of unit tests covering success, splitting behavior, misalignment, unavailable ranges, out-of-range requests, and dealloc/merge roundtrips.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/frame.rs Introduces fixed-address allocation API and implements the buddy split logic needed to allocate at an exact start frame.
src/test.rs Adds unit tests validating alloc_at correctness across common and edge scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jiegec jiegec merged commit 49e161a into rcore-os:master Mar 30, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is it possible to add fixed address allocation for FrameAllocator

3 participants