-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4fa2122
Showing
10 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[unstable] | ||
build-std-features = ["compiler-builtins-mem"] | ||
build-std = ["core", "compiler_builtins"] | ||
|
||
[build] | ||
target = ["i686-unknown-none.json", "x86_64-unknown-none"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
name: Build and Release | ||
|
||
permissions: | ||
contents: write | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
paths-ignore: | ||
- 'README.md' | ||
- '.gitignore' | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Rust | ||
uses: actions-rust-lang/setup-rust-toolchain@v1 | ||
with: | ||
toolchain: nightly | ||
override: true | ||
|
||
- name: Add x86_64-unknown-none target | ||
run: rustup target add x86_64-unknown-none | ||
|
||
- name: Install cbindgen | ||
run: cargo install cbindgen | ||
|
||
- name: Add rust-src component | ||
run: rustup component add rust-src | ||
|
||
- name: Build general release | ||
run: | | ||
cargo build --release | ||
mv target/x86_64-unknown-none/release/liballoc.a liballoc-x86_64.a | ||
mv target/i686-unknown-none/release/liballoc.a liballoc-i686.a | ||
- name: Generate header | ||
run: cbindgen --output alloc.h | ||
|
||
- name: Release artifacts | ||
uses: softprops/action-gh-release@v2 | ||
with: | ||
name: Nightly build | ||
tag_name: release | ||
files: | | ||
liballoc-i686.a | ||
liballoc-x86_64.a | ||
alloc.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Cbindgen files | ||
*.h | ||
|
||
# Build cache | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "liballoc" | ||
edition = "2021" | ||
|
||
[lib] | ||
name = "alloc" | ||
crate-type = ["staticlib"] | ||
|
||
[profile.dev] | ||
panic = "abort" | ||
|
||
[profile.release] | ||
panic = "abort" | ||
lto = true | ||
opt-level = 3 | ||
strip = true | ||
codegen-units = 1 | ||
|
||
[dependencies] | ||
spin = "0.9.8" | ||
talc = "4.4.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023-2024 wenxuanjun | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# liballoc | ||
|
||
C binding of `talc` crate for x86 or x86_64 OS to create a scalable, high performance heap. | ||
|
||
## Usage | ||
|
||
Download the header file and lib from [releases](https://github.com/plos-clan/liballoc/releases/tag/release). | ||
|
||
Link the library to your project. | ||
|
||
## Build | ||
|
||
Build directly to get the two target files: | ||
|
||
```bash | ||
cargo build --release | ||
``` | ||
|
||
The production build will be in `target/release/<target>/` directory. | ||
|
||
And use `cbindgen` to generate the header file: | ||
|
||
```bash | ||
cargo install cbindgen | ||
cbindgen --output alloc.h | ||
``` | ||
|
||
## Example | ||
|
||
```c | ||
#include <stdint.h> | ||
#include "alloc.h" | ||
|
||
void test_alloc() { | ||
uint8_t alloc_test[500]; | ||
bool result = heap_init((uint8_t *)alloc_test, 500); | ||
if (result) { | ||
void *ptr1 = malloc(10); | ||
free(ptr1); | ||
} | ||
} | ||
``` | ||
|
||
For general use, you can map the address space of the memory you want to use as a heap in the page table, and then pass the start address and size to the `heap_init`. | ||
|
||
```c | ||
#include <stdint.h> | ||
#include "alloc.h" | ||
|
||
const uint8_t *heap_start = (uint8_t *)0x100000; | ||
const size_t heap_size = 8 * 1024 * 1024; | ||
|
||
void test_alloc() { | ||
map_page(heap_start, heap_size); | ||
if (heap_init(heap_start, heap_size)) { | ||
void *ptr1 = malloc(10); | ||
free(ptr1); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language = "C" | ||
pragma_once = true | ||
no_includes = true | ||
cpp_compat = true | ||
usize_is_size_t = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"llvm-target": "i686-unknown-none", | ||
"data-layout": "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128", | ||
"arch": "x86", | ||
"target-endian": "little", | ||
"target-pointer-width": "32", | ||
"target-c-int-width": "32", | ||
"os": "none", | ||
"executables": true, | ||
"linker-flavor": "ld.lld", | ||
"linker": "rust-lld", | ||
"panic-strategy": "abort", | ||
"disable-redzone": true, | ||
"features": "-mmx,-sse,+soft-float" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use core::alloc::Layout; | ||
use core::ffi::c_void; | ||
use core::{panic::PanicInfo, ptr::NonNull}; | ||
use talc::{ClaimOnOom, Span, Talc, Talck}; | ||
|
||
#[panic_handler] | ||
unsafe fn panic(_info: &PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
static ALLOCATOR: Talck<spin::Mutex<()>, ClaimOnOom> = | ||
Talc::new(unsafe { ClaimOnOom::new(Span::empty()) }).lock(); | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn heap_init(address: *mut u8, size: usize) -> bool { | ||
let arena = Span::from_base_size(address as *mut u8, size); | ||
ALLOCATOR.lock().claim(arena).is_ok() | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn malloc(size: usize) -> *mut c_void { | ||
let size_with_meta = size + size_of::<usize>(); | ||
let layout = Layout::from_size_align_unchecked(size_with_meta, align_of::<usize>()); | ||
|
||
match ALLOCATOR.lock().malloc(layout) { | ||
Ok(ptr) => { | ||
let size_ptr = ptr.as_ptr() as *mut usize; | ||
size_ptr.write(size); | ||
(size_ptr.offset(1) as *mut u8) as *mut c_void | ||
} | ||
Err(_) => return core::ptr::null_mut(), | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub unsafe extern "C" fn free(ptr: *mut c_void) { | ||
if ptr.is_null() { | ||
return; | ||
} | ||
|
||
let size_ptr = ptr as *mut usize; | ||
let origin_ptr = size_ptr.offset(-1); | ||
|
||
let size_with_meta = origin_ptr.read() + size_of::<usize>(); | ||
let layout = Layout::from_size_align_unchecked(size_with_meta, align_of::<usize>()); | ||
|
||
let origin_ptr = NonNull::new_unchecked(origin_ptr as *mut u8); | ||
ALLOCATOR.lock().free(origin_ptr, layout); | ||
} |