Skip to content

Commit

Permalink
rust: import alloc
Browse files Browse the repository at this point in the history
This brings the `alloc` crate in-tree.

The code comes from Rust 1.54.0-beta.1, i.e. commit `bf62f4de3`.

Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
ojeda committed Jun 30, 2021
1 parent dfaa42f commit 405c9a1
Show file tree
Hide file tree
Showing 70 changed files with 38,740 additions and 0 deletions.
423 changes: 423 additions & 0 deletions rust/alloc/alloc.rs

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions rust/alloc/alloc/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::*;

extern crate test;
use crate::boxed::Box;
use test::Bencher;

#[test]
fn allocate_zeroed() {
unsafe {
let layout = Layout::from_size_align(1024, 1).unwrap();
let ptr =
Global.allocate_zeroed(layout.clone()).unwrap_or_else(|_| handle_alloc_error(layout));

let mut i = ptr.as_non_null_ptr().as_ptr();
let end = i.add(layout.size());
while i < end {
assert_eq!(*i, 0);
i = i.offset(1);
}
Global.deallocate(ptr.as_non_null_ptr(), layout);
}
}

#[bench]
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn alloc_owned_small(b: &mut Bencher) {
b.iter(|| {
let _: Box<_> = box 10;
})
}
Loading

0 comments on commit 405c9a1

Please sign in to comment.