Skip to content

Commit 5c2d952

Browse files
committed
Initial commit
0 parents  commit 5c2d952

File tree

8 files changed

+587
-0
lines changed

8 files changed

+587
-0
lines changed

.github/workflows/cd.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: CD
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
deploy:
8+
name: Publish to crates.io
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: actions-rust-lang/setup-rust-toolchain@v1
13+
- uses: chetan/git-restore-mtime-action@v2
14+
- run: cargo publish --token ${CRATES_IO_API_TOKEN}
15+
env:
16+
CRATES_IO_API_TOKEN: ${{ secrets.CRATES_IO_API_TOKEN }}

.github/workflows/ci.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
jobs:
10+
ci:
11+
name: Run checks and tests
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: actions-rust-lang/setup-rust-toolchain@v1
16+
with:
17+
components: rustfmt, clippy
18+
- uses: chetan/git-restore-mtime-action@v2
19+
- uses: actions-rust-lang/rustfmt@v1
20+
- uses: giraffate/clippy-action@v1
21+
with:
22+
reporter: github-check
23+
- run: cargo test

.gitignore

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

Cargo.lock

Lines changed: 75 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "ordered-pool-allocator"
3+
version = "0.1.0"
4+
authors = ["George Lim <lim.george@me.com>"]
5+
edition = "2021"
6+
description = "A fast and compact pool allocator with block sorting support."
7+
repository = "https://github.com/george-lim/ordered-pool-allocator"
8+
license = "MIT OR Apache-2.0"
9+
keywords = ["allocator", "memory-pool"]
10+
categories = ["data-structures", "memory-management"]
11+
12+
[dev-dependencies]
13+
rand = "0.8"
14+
15+
[profile.test]
16+
opt-level = 3

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 George Lim
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Ordered Pool Allocator
2+
3+
[![crates.io](https://img.shields.io/crates/v/ordered-pool-allocator)](https://crates.io/crates/ordered-pool-allocator)
4+
[![ci](https://github.com/george-lim/ordered-pool-allocator/workflows/CI/badge.svg)](https://github.com/george-lim/ordered-pool-allocator/actions)
5+
[![license](https://img.shields.io/github/license/george-lim/ordered-pool-allocator)](https://github.com/george-lim/ordered-pool-allocator/blob/main/LICENSE)
6+
7+
A fast and compact pool allocator with O(1) block access, allocation, and deallocation; as well as O(nlog(n)) block sorting.
8+
9+
This project is a learning exercise to explore how memory pools work and how to optimize for small header size and for specific operations, like sorting memory blocks.
10+
11+
## Design
12+
13+
### Physical vs virtual block indices
14+
15+
To ensure that existing allocated block pointers still point to their respective blocks after sorting, we need to sort block indices instead of the blocks themselves. This means we need to define two types of indices: physical and virtual.
16+
17+
Physical block indices are used to locate the physical block in memory and are fixed. Virtual block indices represent the current virtual order of the blocks in the allocator. These indices should not be stored, as they can point to different physical blocks after sorting or block deallocation. Assuming blocks are never deallocated or sorted, the virtual and physical block indices are guaranteed to be in sync.
18+
19+
### Header
20+
21+
In order to achieve the fastest time complexity for block allocation and deallocation, we need to store three things in addition to the physical blocks:
22+
23+
1. A map of currently allocated virtual block indices to physical block indices (`physical_block_indices`). This allows the allocator to find the correct physical block while performing block indexing.
24+
2. A map of currently allocated physical block indices to virtual block indices (`virtual_block_indices`). This allows the allocator to find the virtual block index to update in O(1) time for the given block pointer during block deallocation.
25+
3. A stack of physical block indices that were previously deallocated. This allows the allocator to find existing deallocated blocks in O(1) time during block allocation.
26+
27+
### Memory Layout
28+
29+
![memory-layout](https://github.com/george-lim/ordered-pool-allocator/assets/21700768/24c78c1d-e0d4-48ce-9d0d-963fb45dde9b)

0 commit comments

Comments
 (0)