Skip to content

Commit 3818ec7

Browse files
authored
Merge pull request #1 from numpy1314/no_std
bump to v0.1.4-pre.1
2 parents fc826e5 + 2631ca1 commit 3818ec7

File tree

7 files changed

+292
-5
lines changed

7 files changed

+292
-5
lines changed

.github/workflows/check.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Check
2+
3+
on:
4+
push:
5+
branches: [ no_std ]
6+
pull_request:
7+
branches: [ no_std ]
8+
9+
jobs:
10+
check:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Install Rust
16+
uses: dtolnay/rust-toolchain@stable
17+
with:
18+
components: rustfmt, clippy
19+
20+
- name: Check Formatting
21+
run: cargo fmt --all -- --check
22+
23+
- name: Clippy
24+
run: cargo clippy --all-features -- -D warnings
25+
26+
- name: Build
27+
run: cargo build --all-features

.github/workflows/deploy.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Deploy Docs
2+
3+
on:
4+
push:
5+
branches: [ no_std ]
6+
7+
permissions:
8+
contents: write
9+
10+
jobs:
11+
deploy:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Install Rust
17+
uses: dtolnay/rust-toolchain@stable
18+
19+
- name: Build Documentation
20+
run: cargo doc --no-deps --all-features
21+
22+
- name: Create index.html
23+
run: echo '<meta http-equiv="refresh" content="0;url=range_alloc_arceos/index.html">' > target/doc/index.html
24+
25+
- name: Deploy to GitHub Pages
26+
uses: peaceiris/actions-gh-pages@v3
27+
with:
28+
github_token: ${{ secrets.GITHUB_TOKEN }}
29+
publish_dir: ./target/doc

.github/workflows/release.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
- 'v*.*.*-pre.*'
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: write
12+
13+
jobs:
14+
check:
15+
if: github.repository_owner == 'arceos-hypervisor'
16+
name: Check
17+
uses: ./.github/workflows/check.yml
18+
19+
test:
20+
if: github.repository_owner == 'arceos-hypervisor'
21+
name: Test
22+
uses: ./.github/workflows/test.yml
23+
needs: check
24+
25+
publish-crates:
26+
if: github.repository_owner == 'arceos-hypervisor'
27+
name: Publish to crates.io
28+
runs-on: ubuntu-latest
29+
needs: test
30+
steps:
31+
- name: Checkout code
32+
uses: actions/checkout@v4
33+
with:
34+
fetch-depth: 0
35+
36+
- name: Validate tag and branch (HEAD-based)
37+
shell: bash
38+
run: |
39+
set -e
40+
41+
TAG="${{ github.ref_name }}"
42+
TAG_COMMIT=$(git rev-list -n 1 "$TAG")
43+
44+
git fetch origin no_std
45+
46+
NO_STD_HEAD=$(git rev-parse origin/no_std)
47+
48+
echo "Tag: $TAG"
49+
echo "Tag commit: $TAG_COMMIT"
50+
echo "no_std HEAD: $NO_STD_HEAD"
51+
52+
if [ "$TAG_COMMIT" != "$NO_STD_HEAD" ]; then
53+
echo "❌ release tag must be created from no_std HEAD"
54+
exit 1
55+
fi
56+
echo "✅ tag validated on no_std"
57+
58+
- name: Install Rust
59+
uses: dtolnay/rust-toolchain@stable
60+
61+
- name: Publish to crates.io
62+
run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
63+
64+
create-release:
65+
if: github.repository_owner == 'arceos-hypervisor'
66+
name: Create GitHub Release
67+
runs-on: ubuntu-latest
68+
needs: publish-crates
69+
steps:
70+
- name: Checkout code
71+
uses: actions/checkout@v4
72+
with:
73+
fetch-depth: 0
74+
75+
- name: Create GitHub Release
76+
uses: softprops/action-gh-release@v2
77+
with:
78+
draft: false
79+
prerelease: ${{ contains(github.ref_name, '-pre.') }}
80+
generate_release_notes: true
81+
body: |
82+
## ${{ github.ref_name }}
83+
84+
- Documentation: https://docs.rs/range-alloc-arceos
85+
- crates.io: https://crates.io/crates/range-alloc-arceos

.github/workflows/test.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [ no_std ]
6+
pull_request:
7+
branches: [ no_std ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Install Rust
16+
uses: dtolnay/rust-toolchain@stable
17+
18+
- name: Run Tests
19+
run: cargo test --all-features

Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
[package]
2-
name = "range-alloc"
3-
version = "0.1.4"
2+
name = "range-alloc-arceos"
3+
version = "0.1.4-pre.1"
44
description = "Generic range allocator"
5-
homepage = "https://github.com/gfx-rs/range-alloc"
6-
repository = "https://github.com/gfx-rs/range-alloc"
5+
homepage = "https://github.com/arceos-hypervisor/range-alloc"
6+
repository = "https://github.com/arceos-hypervisor/range-alloc"
77
keywords = ["allocator"]
88
license = "MIT OR Apache-2.0"
99
authors = ["the gfx-rs Developers"]
10-
documentation = "https://docs.rs/range-alloc"
10+
documentation = "https://docs.rs/range-alloc-arceos"
1111
categories = ["memory-management"]
1212
edition = "2018"
13+
readme = "README.md"

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# range-alloc-arceos
2+
3+
[![Crates.io](https://img.shields.io/crates/v/range-alloc-arceos.svg)](https://crates.io/crates/range-alloc-arceos)
4+
[![Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://numpy1314.github.io/range-alloc)
5+
[![License](https://img.shields.io/crates/l/range-alloc-arceos.svg)](https://github.com/numpy1314/range-alloc/blob/main/LICENSE)
6+
[![CI](https://github.com/numpy1314/range-alloc/actions/workflows/check.yml/badge.svg)](https://github.com/numpy1314/range-alloc/actions/workflows/check.yml)
7+
8+
**range-alloc-arceos** is a generic range allocator tailored for the ArceOS ecosystem.
9+
10+
It is a fork of the excellent [gfx-rs/range-alloc](https://github.com/gfx-rs/range-alloc), adapted for use in kernel development and embedded scenarios (`no_std`). It allows you to dynamically allocate and free ranges from a predefined memory block or address space.
11+
12+
## Features
13+
14+
- **`no_std` Support**: Designed for bare-metal and kernel environments.
15+
- **Generic**: Works with any type that satisfies the `Range` requirements (e.g., memory addresses, port numbers).
16+
- **`markdown
17+
# range-alloc-arceos
18+
19+
[![Crates.io](https://img.shields.io/crates/v/range-alloc-arceos.svg)](https://crates.io/crates/range-alloc-arceos)
20+
[![Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://numpy1314.github.io/range-alloc)
21+
[![License](https://img.shields.io/crates/l/range-alloc-arceos.svg)](https://github.com/numpy1314/range-alloc/blob/main/LICENSE)
22+
[![CI](https://github.com/numpy1314/range-alloc/actions/workflows/check.yml/badge.svg)](https://github.com/numpy1314/range-alloc/actions/workflows/check.yml)
23+
24+
**range-alloc-arceos** is a generic range allocator tailored for the ArceOS ecosystem.
25+
26+
It is a fork of the excellent [gfx-rs/range-alloc](https://github.com/gfx-rs/range-alloc), adapted for use in kernel development and embedded scenarios (`no_std`). It allows you to dynamically allocate and free ranges from a predefined memory block or address space.
27+
28+
## Features
29+
30+
- **`no_std` Support**: Designed for bare-metal and kernel environments.
31+
- **Generic**: Works with any type that satisfies the `Range` requirements (e.g., memory addresses, port numbers).
32+
- **Efficient**: Merges adjacent free ranges to minimize fragmentation.
33+
34+
## Usage
35+
36+
Add this to your `Cargo.toml`:
37+
38+
```toml
39+
[dependencies]
40+
range-alloc-arceos = "0.1.0-alpha.1"
41+
```
42+
43+
## Example
44+
```rust
45+
use range_alloc_arceos::RangeAllocator;
46+
47+
fn main() {
48+
// Initialize the allocator with a range (e.g., 0..100)
49+
let mut allocator = RangeAllocator::new(0..100);
50+
51+
}
52+
53+
```
54+
55+
## License
56+
This project is licensed under either of
57+
58+
- Apache License, Version 2.0, (LICENSE-APACHE
59+
or http://www.apache.org/licenses/LICENSE-2.0
60+
)
61+
62+
- MIT license (LICENSE-MIT
63+
or http://opensource.org/licenses/MIT
64+
)
65+
66+
at your option.

tests/test.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use range_alloc_arceos::RangeAllocator;
2+
3+
#[test]
4+
fn test_simple_allocation() {
5+
let mut allocator = RangeAllocator::new(0..100);
6+
7+
let r1 = allocator.allocate_range(10).expect("Alloc 10 failed");
8+
assert_eq!(r1, 0..10);
9+
10+
let r2 = allocator.allocate_range(20).expect("Alloc 20 failed");
11+
assert_eq!(r2, 10..30);
12+
13+
allocator.free_range(r1);
14+
15+
let r3 = allocator.allocate_range(5).expect("Alloc 5 failed");
16+
assert_eq!(r3, 0..5);
17+
}
18+
19+
#[test]
20+
fn test_out_of_memory() {
21+
let mut allocator = RangeAllocator::new(0..10);
22+
23+
let _r1 = allocator.allocate_range(10).unwrap();
24+
25+
let r2 = allocator.allocate_range(1);
26+
assert!(r2.is_err(), "Should return error when OOM");
27+
}
28+
29+
#[test]
30+
fn test_fragmentation_and_merge() {
31+
let mut allocator = RangeAllocator::new(0..100);
32+
33+
let a = allocator.allocate_range(20).unwrap();
34+
let b = allocator.allocate_range(20).unwrap();
35+
let c = allocator.allocate_range(20).unwrap();
36+
let _d = allocator.allocate_range(40).unwrap();
37+
38+
allocator.free_range(a);
39+
allocator.free_range(c);
40+
41+
assert!(allocator.allocate_range(30).is_err());
42+
43+
allocator.free_range(b);
44+
45+
let big = allocator
46+
.allocate_range(60)
47+
.expect("Should merge ranges A, B, C");
48+
assert_eq!(big, 0..60);
49+
}
50+
51+
#[test]
52+
fn test_alignment_gaps() {
53+
let mut allocator = RangeAllocator::new(1000..2000);
54+
55+
let r1 = allocator.allocate_range(100).unwrap();
56+
assert_eq!(r1, 1000..1100);
57+
58+
let r2 = allocator.allocate_range(100).unwrap();
59+
assert_eq!(r2, 1100..1200);
60+
}

0 commit comments

Comments
 (0)