Skip to content

Commit f353ff7

Browse files
committed
add basic Box, remove Error::CaughtUnwind and Layout::checked_op, optimize Layout
1 parent 864fba1 commit f353ff7

File tree

18 files changed

+395
-461
lines changed

18 files changed

+395
-461
lines changed

.github/workflows/codeql.yml

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -25,70 +25,50 @@ jobs:
2525
fail-fast: false
2626
matrix:
2727
include:
28-
- language: actions
29-
build-mode: none
30-
- language: c-cpp
31-
build-mode: manual
32-
- language: rust
33-
build-mode: none
34-
# CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift'
35-
# Use `c-cpp` to analyze code written in C, C++ or both
36-
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
37-
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
38-
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
39-
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
40-
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
41-
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
28+
- language: actions
29+
build-mode: none
30+
- language: c-cpp
31+
build-mode: manual
32+
- language: rust
33+
build-mode: none
4234
steps:
43-
- name: Checkout repository
44-
uses: actions/checkout@v4
35+
- name: Checkout repository
36+
uses: actions/checkout@v4
4537

46-
# Add any setup steps before running the `github/codeql-action/init` action.
47-
# This includes steps like installing compilers or runtimes (`actions/setup-node`
48-
# or others). This is typically only required for manual builds.
49-
# - name: Setup runtime (example)
50-
# uses: actions/setup-example@v1
38+
- name: Initialize CodeQL
39+
uses: github/codeql-action/init@v4
40+
with:
41+
languages: ${{ matrix.language }}
42+
build-mode: ${{ matrix.build-mode }}
43+
queries: security-extended,security-and-quality
5144

52-
# Initializes the CodeQL tools for scanning.
53-
- name: Initialize CodeQL
54-
uses: github/codeql-action/init@v4
55-
with:
56-
languages: ${{ matrix.language }}
57-
build-mode: ${{ matrix.build-mode }}
58-
queries: security-extended,security-and-quality
45+
- name: Run manual build steps
46+
if: matrix.build-mode == 'manual'
47+
shell: bash
48+
run: |
49+
set -euxo pipefail
50+
51+
# make sure the system C toolchain is available
52+
sudo apt-get update
53+
sudo apt-get install -y build-essential pkg-config
54+
55+
# show some debug info
56+
which gcc
57+
gcc --version
58+
echo "repo tree (src/ffi):"
59+
ls -la src/ffi || true
60+
ls -la src/ffi/c || true
61+
62+
# compile
63+
mkdir -p build
64+
if [ -f src/ffi/c/calloca.c ]; then
65+
gcc -c -fPIC -I. -o build/calloca.o src/ffi/c/calloca.c
66+
fi
67+
68+
# also build with the build.rs
69+
cargo build --verbose --features stack_alloc
5970
60-
# If the analyze step fails for one of the languages you are analyzing with
61-
# "We were unable to automatically build your code", modify the matrix above
62-
# to set the build mode to "manual" for that language. Then modify this step
63-
# to build your code.
64-
# ℹ️ Command-line programs to run using the OS shell.
65-
- name: Run manual build steps
66-
if: matrix.build-mode == 'manual'
67-
shell: bash
68-
run: |
69-
set -euxo pipefail
70-
71-
# make sure the system C toolchain is available
72-
sudo apt-get update
73-
sudo apt-get install -y build-essential pkg-config
74-
75-
# show some debug info
76-
which gcc
77-
gcc --version
78-
echo "repo tree (src/ffi):"
79-
ls -la src/ffi || true
80-
ls -la src/ffi/c || true
81-
82-
# compile
83-
mkdir -p build
84-
if [ -f src/ffi/c/calloca.c ]; then
85-
gcc -c -fPIC -I. -o build/calloca.o src/ffi/c/calloca.c
86-
fi
87-
88-
# also build with the build.rs
89-
cargo build --verbose --features stack_alloc
90-
91-
- name: Perform CodeQL Analysis
92-
uses: github/codeql-action/analyze@v4
93-
with:
94-
category: "/language:${{matrix.language}}"
71+
- name: Perform CodeQL Analysis
72+
uses: github/codeql-action/analyze@v4
73+
with:
74+
category: "/language:${{matrix.language}}"

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
matrix:
2222
os: [ ubuntu-latest, windows-latest, macos-latest ]
2323
rust: [ stable, nightly, 1.46.0, 1.47.0, 1.49.0, 1.50.0, 1.52.0, 1.56.0, 1.57.0, 1.58.0, 1.61.0, 1.64.0, 1.71.0, 1.73.0, 1.75.0, 1.81.0, 1.83.0, 1.89.0 ]
24-
# TODO: make it work on these versions
2524
exclude:
2625
- os: macos-latest
2726
rust: 1.46.0

Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ checked_ops = []
4848
os_err_reporting = ["std"]
4949
## adds support for catching unwinds to stack_alloc
5050
catch_unwind = ["stack_alloc", "std"]
51+
## stdlib datatype replacements
52+
stdlib_data = []
5153

5254
# feature bundles
53-
full = ["c_alloc", "catch_unwind", "os_err_reporting", "checked_ops"]
55+
full = ["c_alloc", "checked_ops", "stack_alloc", "stdlib_data"]
56+
full_std = ["full", "catch_unwind", "os_err_reporting"]
5457
full_nightly = ["metadata", "full"]
58+
full_nightly_std = ["full_nightly", "full_std"]
5559

5660

5761

@@ -92,6 +96,7 @@ path = "tests/layout.rs"
9296
bitflags = "=1.3.2"
9397
libc = { version = "=0.2.163", default-features = false }
9498
rustversion = "=1.0.22"
99+
ctrlc = { version = "=3.4.0", optional = true }
95100

96101
[build-dependencies]
97102
cc = { version = "=1.0.83", optional = true }
@@ -103,9 +108,8 @@ rustversion = "=1.0.22"
103108
#[[bin]]
104109
#name = "alcm"
105110
#path = "bin/alloc_many.rs"
106-
107-
108-
111+
#required-features = ["ctrlc"]
112+
#
109113
#[dev-dependencies]
110114
#criterion = "0.8.1"
111115
#

benches/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use {
99
memapi2::{
1010
DefaultAlloc,
1111
layout::Layout,
12-
traits::alloc::{Alloc, Dealloc, Grow, Realloc, Shrink}
12+
traits::alloc::{Alloc, Dealloc, Realloc}
1313
},
1414
std::time::Duration
1515
};
1616

1717
fn bench_allocs<A>(c: &mut Criterion, name: &str, alloc: A)
1818
where
19-
A: Alloc + Dealloc + Grow + Shrink + Realloc
19+
A: Alloc + Dealloc + Realloc
2020
{
2121
let mut group = c.benchmark_group(name);
2222
let zero = unsafe { Layout::from_size_align_unchecked(0, 8) };

benches/helpers.rs

Lines changed: 1 addition & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -10,193 +10,13 @@ use {
1010
},
1111
criterion::Criterion,
1212
memapi2::{
13-
error::ArithOp,
14-
helpers::{checked_op, null_q, null_q_dyn},
13+
helpers::{null_q, null_q_dyn},
1514
layout::Layout,
1615
traits::data::type_props::PtrProps
1716
},
1817
std::{rc::Rc, sync::Arc, time::Duration}
1918
};
2019

21-
fn checked_ops(c: &mut Criterion) {
22-
let mut group = c.benchmark_group("checked_ops/add");
23-
24-
group.bench_function("valid", |b| {
25-
b.iter(|| {
26-
let _ = black_box(checked_op(black_box(2), ArithOp::Add, black_box(2)));
27-
});
28-
});
29-
30-
group.bench_function("invalid", |b| {
31-
b.iter(|| {
32-
// overflows
33-
let _ = black_box(checked_op(black_box(usize::MAX), ArithOp::Add, black_box(1)));
34-
});
35-
});
36-
37-
group.finish();
38-
39-
let mut group = c.benchmark_group("checked_ops/sub");
40-
41-
group.bench_function("valid", |b| {
42-
b.iter(|| {
43-
let _ = black_box(checked_op(black_box(2), ArithOp::Sub, black_box(2)));
44-
});
45-
});
46-
47-
group.bench_function("invalid", |b| {
48-
b.iter(|| {
49-
// underflows
50-
let _ = black_box(checked_op(black_box(0), ArithOp::Sub, black_box(1)));
51-
});
52-
});
53-
54-
group.finish();
55-
56-
let mut group = c.benchmark_group("checked_ops/mul");
57-
58-
group.bench_function("valid", |b| {
59-
b.iter(|| {
60-
let _ = black_box(checked_op(black_box(2), ArithOp::Mul, black_box(2)));
61-
});
62-
});
63-
64-
group.bench_function("invalid", |b| {
65-
b.iter(|| {
66-
let _ = black_box(checked_op(black_box(usize::MAX), ArithOp::Mul, black_box(2)));
67-
});
68-
});
69-
70-
group.finish();
71-
72-
let mut group = c.benchmark_group("checked_ops/div");
73-
74-
group.bench_function("valid", |b| {
75-
b.iter(|| {
76-
let _ = black_box(checked_op(black_box(2), ArithOp::Div, black_box(2)));
77-
});
78-
});
79-
80-
group.bench_function("valid_rem", |b| {
81-
b.iter(|| {
82-
// has a remainder of 1
83-
let _ = black_box(checked_op(black_box(3), ArithOp::Div, black_box(2)));
84-
});
85-
});
86-
87-
group.bench_function("valid_toolarge", |b| {
88-
b.iter(|| {
89-
let _ = black_box(checked_op(black_box(2), ArithOp::Div, black_box(3)));
90-
});
91-
});
92-
93-
group.bench_function("invalid", |b| {
94-
b.iter(|| {
95-
let _ = black_box(checked_op(black_box(2), ArithOp::Div, black_box(0)));
96-
});
97-
});
98-
99-
group.finish();
100-
101-
let mut group = c.benchmark_group("checked_ops/div_ceil");
102-
103-
group.bench_function("valid", |b| {
104-
b.iter(|| {
105-
let _ = black_box(checked_op(black_box(2), ArithOp::DivCeil, black_box(2)));
106-
});
107-
});
108-
109-
group.bench_function("valid_rem", |b| {
110-
b.iter(|| {
111-
// has a remainder of 1
112-
let _ = black_box(checked_op(black_box(3), ArithOp::DivCeil, black_box(2)));
113-
});
114-
});
115-
116-
group.bench_function("valid_toolarge", |b| {
117-
b.iter(|| {
118-
let _ = black_box(checked_op(black_box(2), ArithOp::DivCeil, black_box(3)));
119-
});
120-
});
121-
122-
group.bench_function("invalid", |b| {
123-
b.iter(|| {
124-
let _ = black_box(checked_op(black_box(2), ArithOp::DivCeil, black_box(0)));
125-
});
126-
});
127-
128-
group.finish();
129-
130-
let mut group = c.benchmark_group("checked_ops/rem");
131-
132-
group.bench_function("valid", |b| {
133-
b.iter(|| {
134-
let _ = black_box(checked_op(black_box(3), ArithOp::Rem, black_box(2)));
135-
});
136-
});
137-
138-
group.bench_function("valid_exact", |b| {
139-
b.iter(|| {
140-
let _ = black_box(checked_op(black_box(2), ArithOp::Rem, black_box(2)));
141-
});
142-
});
143-
144-
group.bench_function("valid_toolarge", |b| {
145-
b.iter(|| {
146-
let _ = black_box(checked_op(black_box(2), ArithOp::Rem, black_box(3)));
147-
});
148-
});
149-
150-
group.bench_function("invalid", |b| {
151-
b.iter(|| {
152-
let _ = black_box(checked_op(black_box(2), ArithOp::Rem, black_box(0)));
153-
});
154-
});
155-
156-
group.finish();
157-
158-
let mut group = c.benchmark_group("checked_ops/pow");
159-
160-
group.bench_function("valid_0", |b| {
161-
b.iter(|| {
162-
let _ = black_box(checked_op(black_box(2), ArithOp::Pow, black_box(0)));
163-
});
164-
});
165-
166-
group.bench_function("valid", |b| {
167-
b.iter(|| {
168-
let _ = black_box(checked_op(black_box(2), ArithOp::Pow, black_box(1)));
169-
});
170-
});
171-
172-
group.bench_function("valid_long", |b| {
173-
b.iter(|| {
174-
let _ = black_box(checked_op(black_box(1), ArithOp::Pow, black_box(u32::MAX as usize)));
175-
});
176-
});
177-
178-
group.bench_function("invalid_toolarge", |b| {
179-
b.iter(|| {
180-
let _ =
181-
black_box(checked_op(black_box(2), ArithOp::Pow, black_box(u32::MAX as usize + 1)));
182-
});
183-
});
184-
185-
group.bench_function("invalid_overflow_long", |b| {
186-
b.iter(|| {
187-
let _ = black_box(checked_op(black_box(2), ArithOp::Pow, black_box(64)));
188-
});
189-
});
190-
191-
group.bench_function("invalid_overflow_short", |b| {
192-
b.iter(|| {
193-
let _ = black_box(checked_op(black_box(usize::MAX), ArithOp::Pow, black_box(2)));
194-
});
195-
});
196-
197-
group.finish();
198-
}
199-
20020
fn null_q_variants(c: &mut Criterion) {
20121
let mut group = c.benchmark_group("null_q");
20222

@@ -289,7 +109,6 @@ fn main() {
289109
.significance_level(0.1)
290110
.configure_from_args();
291111

292-
checked_ops(&mut c);
293112
null_q_variants(&mut c);
294113
ptr_props(&mut c);
295114

0 commit comments

Comments
 (0)