Skip to content

Commit c2d5b4e

Browse files
committed
initial commit
0 parents  commit c2d5b4e

10 files changed

Lines changed: 602 additions & 0 deletions

File tree

.github/workflows/pipeline.yaml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: pipeline
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- feature/*
8+
pull_request:
9+
branches:
10+
- main
11+
12+
env:
13+
CARGO_TERM_COLOR: always
14+
15+
jobs:
16+
cargo_fmt:
17+
name: "Cargo Format"
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v3
21+
- name: Install nightly toolchain
22+
run: rustup toolchain install nightly --component rustfmt
23+
- name: Check formatting
24+
run: cargo +nightly fmt --all -- --check
25+
cargo_clippy:
26+
name: "Cargo Clippy"
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@v3
30+
- name: Install Clippy
31+
run: rustup component add clippy
32+
- name: Run Clippy
33+
run: cargo clippy --all-targets --all-features -- -D warnings
34+
cargo_test:
35+
name: "Cargo Test"
36+
runs-on: ubuntu-latest
37+
steps:
38+
- uses: actions/checkout@v3
39+
- name: Build
40+
run: cargo build --verbose
41+
- name: Test with Cargo
42+
run: cargo test --verbose
43+
cargo_test_release:
44+
name: "Cargo Test (Release)"
45+
runs-on: ubuntu-latest
46+
needs: [cargo_test]
47+
steps:
48+
- uses: actions/checkout@v3
49+
- name: Build
50+
run: cargo build --release --verbose
51+
- name: Test with Cargo (Release)
52+
run: cargo test --release --verbose
53+
cargo_test_no_default_features:
54+
name: "Cargo Test (No Default Features)"
55+
runs-on: ubuntu-latest
56+
needs: [cargo_test_release]
57+
steps:
58+
- uses: actions/checkout@v3
59+
- name: Build
60+
run: cargo build --no-default-features --verbose
61+
- name: Test with Cargo (No Default Features)
62+
run: cargo test --no-default-features --verbose

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "stack-allocator"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Khashayar Fereidani"]
6+
description = "A stack-based memory allocator with optional fallback to a global/secondary allocator."
7+
license = "MIT"
8+
repository = "https://github.com/fereidani/stack-allocator"
9+
readme = "README.md"
10+
keywords = ["allocator", "stack", "alloc", "memory-management", "no-std"]
11+
categories = ["data-structures", "no-std"]
12+
13+
[dependencies]
14+
allocator-api2 = "0"
15+
16+
[build-dependencies]
17+
rustc_version = "0.4"
18+
19+
[features]
20+
std = []
21+
alloc = []

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2025 Khashayar Fereidani
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Stack Allocator
2+
3+
[![Crates.io](https://img.shields.io/crates/v/stack-allocator.svg)](https://crates.io/crates/stack-allocator)
4+
[![Documentation](https://docs.rs/stack-allocator/badge.svg)](https://docs.rs/stack-allocator)
5+
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
6+
7+
This crate provides two allocator types:
8+
9+
- **`StackAllocator<N>`** - a bump‑allocator that uses a fixed‑size buffer stored on the stack (or in static memory). Allocations are fast and require no system calls. Individual blocks can be freed/shrinked/growed but only if they are the latest allocation.
10+
11+
- **`HybridAllocator<N, F>`** - a hybrid allocator that first tries to allocate from a `StackAllocator<N>` and, if the stack buffer is exhausted, falls back to a user‑provided allocator `F` (e.g. `std::alloc::Global`). This gives the performance benefits of stack allocation while still supporting unbounded allocations via the fallback.
12+
13+
## Features
14+
15+
- **`allocator_api`** - uses the nightly `allocator_api` feature to implement `std::alloc::Allocator`.
16+
- **`allocator-api2`** – A stable fallback that mirrors the core allocation API, allowing this crate to be used on stable Rust with libraries like `hashbrown` that depend on `allocator-api2`.
17+
**Note:** This crate is `#![no_std]` compatible.
18+
19+
## Usage
20+
21+
```rust:ignore
22+
#![feature(allocator_api)]
23+
use stack_allocator::{StackAllocator, HybridAllocator};
24+
use std::alloc::Global;
25+
26+
// A pure stack allocator with a 1 KiB buffer.
27+
let mut stack = StackAllocator::<1024>::new();
28+
let mut v = Vec::new_in(stack);
29+
for i in 0..10 {
30+
v.push(i);
31+
}
32+
assert_eq!(v.len(), 10);
33+
for (i, &val) in v.iter().enumerate() {
34+
assert_eq!(i, val);
35+
}
36+
v.clear();
37+
assert_eq!(v.len(), 0);
38+
v.shrink_to_fit();
39+
assert_eq!(v.capacity(), 0);
40+
41+
// A hybrid allocator that falls back to the global allocator(heap).
42+
let hybrid = HybridAllocator::<1024, Global>::new(Global);
43+
let mut v = Vec::new_in(hybrid);
44+
for i in 0..2048 {
45+
v.push(i);
46+
}
47+
assert_eq!(v.len(), 2048);
48+
for (i, &val) in v.iter().enumerate() {
49+
assert_eq!(i, val);
50+
}
51+
v.clear();
52+
assert_eq!(v.len(), 0);
53+
v.shrink_to_fit();
54+
assert_eq!(v.capacity(), 0);
55+
```
56+
57+
The crate is `#![no_std]` compatible.
58+
It is usable in stable rust using `allocator-api2`.

build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use rustc_version::{version_meta, Channel};
2+
3+
fn main() {
4+
if let Ok(meta) = version_meta() {
5+
if meta.channel == Channel::Nightly {
6+
println!("cargo:rustc-cfg=nightly");
7+
}
8+
}
9+
println!("cargo::rustc-check-cfg=cfg(nightly)");
10+
}

0 commit comments

Comments
 (0)