Skip to content

Commit 9e3b96b

Browse files
authored
Merge pull request #32 from fitzgen/arbitrary-alignment
Arbitrary alignment
2 parents 792f27c + 1046396 commit 9e3b96b

File tree

7 files changed

+208
-125
lines changed

7 files changed

+208
-125
lines changed

.travis.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ cache: cargo
44
rust:
55
- nightly
66

7-
# For `binaryen`.
8-
addons:
9-
apt:
10-
sources:
11-
- 'ubuntu-toolchain-r-test'
12-
packages:
13-
- 'cmake'
14-
- 'g++-5'
15-
167
env:
178
matrix:
189
- SCRIPT="./check.sh"
@@ -23,9 +14,6 @@ before_script:
2314
- rustup target add wasm32-unknown-unknown
2415
- which wasm-gc || cargo install --git https://github.com/alexcrichton/wasm-gc
2516
- which cargo-readme || cargo install cargo-readme
26-
- test -d binaryen || git clone https://github.com/WebAssembly/binaryen.git
27-
- export PATH="$PATH:$(pwd)/binaryen/bin"
28-
- which wasm-opt || (cd binaryen && cmake . && make -j4 && cd -)
2917

3018
script:
3119
- "$SCRIPT"

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
115115
- `wee_alloc` imposes two words of overhead on each allocation for maintaining
116116
its internal free lists.
117117

118-
- The maximum alignment supported is word alignment.
119-
120118
- Deallocation is an *O(1)* operation.
121119

122120
- `wee_alloc` will never return freed pages to the WebAssembly engine /

build.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,23 @@ cargo build --release --target wasm32-unknown-unknown
2222

2323
wasm-gc ../target/wasm32-unknown-unknown/release/wee_alloc_example.wasm \
2424
../target/wasm32-unknown-unknown/release/wee_alloc_example.gc.wasm
25-
wasm-opt -Oz \
26-
../target/wasm32-unknown-unknown/release/wee_alloc_example.gc.wasm \
27-
-o ../target/wasm32-unknown-unknown/release/wee_alloc_example.gc.opt.wasm
25+
26+
if which wasm-opt; then
27+
wasm-opt -Oz \
28+
../target/wasm32-unknown-unknown/release/wee_alloc_example.gc.wasm \
29+
-o ../target/wasm32-unknown-unknown/release/wee_alloc_example.gc.opt.wasm
30+
fi
2831

2932
cargo build --release --features size_classes --target wasm32-unknown-unknown
3033

3134
wasm-gc ../target/wasm32-unknown-unknown/release/wee_alloc_example.wasm \
3235
../target/wasm32-unknown-unknown/release/wee_alloc_example.size_classes.gc.wasm
33-
wasm-opt -Oz \
34-
../target/wasm32-unknown-unknown/release/wee_alloc_example.size_classes.gc.wasm \
35-
-o ../target/wasm32-unknown-unknown/release/wee_alloc_example.size_classes.gc.opt.wasm
36+
37+
if which wasm-opt; then
38+
wasm-opt -Oz \
39+
../target/wasm32-unknown-unknown/release/wee_alloc_example.size_classes.gc.wasm \
40+
-o ../target/wasm32-unknown-unknown/release/wee_alloc_example.size_classes.gc.opt.wasm
41+
fi
3642

3743
wc -c ../target/wasm32-unknown-unknown/release/*.gc.opt.wasm
3844

test/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(alloc, allocator_api)]
22

33
extern crate alloc;
4+
#[macro_use]
45
extern crate quickcheck;
56
extern crate rand;
67
extern crate wee_alloc;
@@ -278,6 +279,20 @@ fn multi_threaded_quickchecks() {
278279
);
279280
}
280281

282+
#[cfg(test)]
283+
static ALIGNS: [usize; 10] = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512];
284+
285+
quickcheck! {
286+
fn single_allocation_with_size_and_align(size: usize, align: usize) -> () {
287+
let size = size % 65536;
288+
let align = ALIGNS[align % ALIGNS.len()];
289+
290+
let mut w = &wee_alloc::WeeAlloc::INIT;
291+
let layout = alloc::heap::Layout::from_size_align(size, align).unwrap();
292+
let _ = unsafe { w.alloc(layout) };
293+
}
294+
}
295+
281296
////////////////////////////////////////////////////////////////////////////////
282297

283298
#[test]

test/tests/global.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,26 @@ fn strings() {
3333
fn threads() {
3434
assert!(thread::spawn(|| panic!()).join().is_err());
3535
}
36+
37+
#[test]
38+
fn test_larger_than_word_alignment() {
39+
use std::mem;
40+
41+
// Align to 32 bytes.
42+
#[repr(align(32))]
43+
struct Align32(u8);
44+
45+
assert_eq!(mem::align_of::<Align32>(), 32);
46+
47+
for _ in 0..100 {
48+
let b = Box::new(Align32(42));
49+
50+
let p = Box::into_raw(b);
51+
assert_eq!(p as usize % 32, 0, "{:p} should be aligned to 32", p);
52+
53+
unsafe {
54+
let b = Box::from_raw(p);
55+
assert_eq!(b.0, 42);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)