Skip to content

Commit 9a3923e

Browse files
committed
allow optional stdlib request to get access to the builtin malloc functions within this allocator interface
1 parent 9c6fdab commit 9a3923e

File tree

5 files changed

+142
-6
lines changed

5 files changed

+142
-6
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ license = "BSD-3-Clause"
1111
repository = "https://github.com/dropbox/rust-alloc-no-stdlib"
1212

1313
[[bin]]
14-
name = "example"
14+
name = "example"
15+
16+
[features]
17+
unsafe = []
18+
stdlib = []

src/bin/tests.rs

100644100755
Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
extern crate core;
44
use alloc_no_stdlib::Allocator;
55
use super::HeapAllocator;
6-
7-
6+
#[cfg(feature="stdlib")]
7+
use alloc_no_stdlib::StdAlloc;
8+
#[cfg(all(feature="unsafe", feature="stdlib"))]
9+
use alloc_no_stdlib::StdAllocUninitialized;
810
#[test]
911
fn heap_test() {
1012
let mut halloc : HeapAllocator<u8> = HeapAllocator::<u8>{default_value: 0};
@@ -23,3 +25,44 @@ fn heap_test() {
2325
}
2426

2527
}
28+
29+
30+
#[cfg(all(feature="unsafe", feature="stdlib"))]
31+
#[test]
32+
fn std_unsafe_heap_test() {
33+
let mut halloc = unsafe{StdAllocUninitialized::<u8>::new()};
34+
for _i in 1..10 { // heap test
35+
let mut x = halloc.alloc_cell(100000);
36+
x[0] = 4;
37+
let mut y = halloc.alloc_cell(110000);
38+
y[0] = 5;
39+
let mut z = halloc.alloc_cell(120000);
40+
z[0] = 6;
41+
assert_eq!(y[0], 5);
42+
halloc.free_cell(y);
43+
assert_eq!(x[0], 4);
44+
assert_eq!(x[9], 0);
45+
assert_eq!(z[0], 6);
46+
}
47+
48+
}
49+
50+
#[cfg(feature="stdlib")]
51+
#[test]
52+
fn std_heap_test() {
53+
let mut halloc = unsafe{StdAllocUninitialized::<u8>::new()};
54+
for _i in 1..10 { // heap test
55+
let mut x = halloc.alloc_cell(100000);
56+
x[0] = 4;
57+
let mut y = halloc.alloc_cell(110000);
58+
y[0] = 5;
59+
let mut z = halloc.alloc_cell(120000);
60+
z[0] = 6;
61+
assert_eq!(y[0], 5);
62+
halloc.free_cell(y);
63+
assert_eq!(x[0], 4);
64+
assert_eq!(x[9], 0);
65+
assert_eq!(z[0], 6);
66+
}
67+
68+
}

src/heap_alloc.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#![cfg(feature="stdlib")]
2+
use std;
3+
use core;
4+
use core::ops;
5+
pub struct WrapBox<T> {
6+
b : std::boxed::Box<[T]>,
7+
}
8+
9+
impl<T> core::default::Default for WrapBox<T> {
10+
fn default() -> Self {
11+
let v : std::vec::Vec<T> = std::vec::Vec::new();
12+
let b = v.into_boxed_slice();
13+
return WrapBox::<T>{b : b};
14+
}
15+
}
16+
17+
impl<T> ops::Index<usize> for WrapBox<T>{
18+
type Output = T;
19+
fn index(&self, index : usize) -> &T {
20+
return &(*self.b)[index]
21+
}
22+
}
23+
24+
impl<T> ops::IndexMut<usize> for WrapBox<T>{
25+
fn index_mut(&mut self, index : usize) -> &mut T {
26+
return &mut (*self.b)[index]
27+
}
28+
}
29+
30+
impl<T> super::SliceWrapper<T> for WrapBox<T> {
31+
fn slice(&self) -> & [T] {
32+
return &*self.b
33+
}
34+
}
35+
36+
impl<T> super::SliceWrapperMut<T> for WrapBox<T> {
37+
fn slice_mut(&mut self) -> &mut [T] {
38+
return &mut*self.b
39+
}
40+
}
41+
42+
pub struct StdAlloc<T : core::clone::Clone>{
43+
pub default_value : T,
44+
}
45+
46+
impl<T : core::clone::Clone> super::Allocator<T> for StdAlloc<T> {
47+
type AllocatedMemory = WrapBox<T>;
48+
fn alloc_cell(self : &mut StdAlloc<T>, len : usize) -> WrapBox<T> {
49+
50+
let v : std::vec::Vec<T> = vec![self.default_value.clone();len];
51+
let b = v.into_boxed_slice();
52+
return WrapBox::<T>{b : b};
53+
}
54+
fn free_cell(self : &mut StdAlloc<T>, _data : WrapBox<T>) {
55+
56+
}
57+
}
58+
59+
#[cfg(feature="unsafe")]
60+
pub struct StdAllocUninitialized<T : core::clone::Clone>{
61+
#[allow(dead_code)]
62+
default_value : Option<T>,
63+
}
64+
65+
#[cfg(feature="unsafe")]
66+
impl<T : core::clone::Clone> StdAllocUninitialized<T>{
67+
pub unsafe fn new() -> StdAllocUninitialized<T> {
68+
return StdAllocUninitialized::<T>{default_value:None};
69+
}
70+
}
71+
#[cfg(feature="unsafe")]
72+
impl<T : core::clone::Clone> super::Allocator<T> for StdAllocUninitialized<T> {
73+
type AllocatedMemory = WrapBox<T>;
74+
fn alloc_cell(self : &mut Self, len : usize) -> WrapBox<T> {
75+
76+
let mut v : std::vec::Vec<T> = std::vec::Vec::with_capacity(len);
77+
unsafe {v.set_len(len)};
78+
let b = v.into_boxed_slice();
79+
return WrapBox::<T>{b : b};
80+
}
81+
fn free_cell(self : &mut Self, _data : WrapBox<T>) {
82+
83+
}
84+
}

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
#![no_std]
22

3+
#[cfg(feature="stdlib")]
34
#[macro_use]
45
extern crate std;
6+
mod heap_alloc;
57
mod allocated_memory;
68
mod stack_allocator;
79
mod allocated_stack_memory;
810
pub mod init;
9-
1011
pub use allocated_memory::SliceWrapper;
1112
pub use allocated_memory::SliceWrapperMut;
1213
pub use allocated_memory::AllocatedSlice;
1314

1415
pub use allocated_stack_memory::AllocatedStackMemory;
1516
pub use stack_allocator::Allocator;
1617
pub use stack_allocator::StackAllocator;
18+
#[cfg(feature="stdlib")]
19+
pub use heap_alloc::StdAlloc;
20+
#[cfg(all(feature="stdlib", feature="unsafe"))]
21+
pub use heap_alloc::StdAllocUninitialized;
1722
use core::default::Default;
1823
pub fn bzero<T : Default> (data : &mut [T]) {
1924
for iter in data.iter_mut() {
@@ -23,6 +28,8 @@ pub fn bzero<T : Default> (data : &mut [T]) {
2328

2429
pub fn uninitialized<T> (_data : &mut[T]) {}
2530

31+
32+
2633
#[derive(Debug)]
2734
pub struct CallocBackingStore<'a, T : 'a> {
2835
pub raw_data : *mut u8,

src/stack_allocator.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ impl<'a, T : 'a, U : allocated_memory::AllocatedSlice<&'a mut[T]> >
3636
type AllocatedMemory = AllocatedStackMemory<'a, T>;
3737
fn alloc_cell(self : &mut StackAllocator<'a, T, U>,
3838
len : usize) -> AllocatedStackMemory<'a, T> {
39-
println!("Allocated {:}", len);
4039
if len == 0 {
4140
return AllocatedStackMemory::<'a, T>::default();
4241
}
@@ -81,7 +80,6 @@ impl<'a, T : 'a, U : allocated_memory::AllocatedSlice<&'a mut[T]> >
8180
if val.slice().len() == 0 {
8281
return;
8382
}
84-
println!("Freed {:}", val.slice().len());
8583
if self.free_list_start > 0 {
8684
self.free_list_start -=1;
8785
core::mem::replace(&mut self.system_resources.slice_mut()[self.free_list_start],

0 commit comments

Comments
 (0)