Skip to content

Commit 8056ed2

Browse files
committed
Refactor HeapAlloc to be called HeapPrealloc and be defined in code if stdlib feature is active rather than as a macro. Bumped verison to 1.1
1 parent 9a3923e commit 8056ed2

File tree

7 files changed

+111
-93
lines changed

7 files changed

+111
-93
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "alloc-no-stdlib"
33
description = "A dynamic allocator that may be used with or without the stdlib. This allows a package with nostd to allocate memory dynamically and be used either with a custom allocator, items on the stack, or by a package that wishes to simply use Box<>. It also provides options to use calloc or a mutable global variable for pre-zeroed memory"
4-
version = "1.0.0"
4+
version = "1.1.0"
55
authors = ["Daniel Reiter Horn <[email protected]>"]
66
documentation = "https://raw.githubusercontent.com/dropbox/rust-alloc-no-stdlib/master/tests/lib.rs"
77
homepage = "https://github.com/dropbox/rust-alloc-no-stdlib"

src/bin/example.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use core::ops;
66
mod heap_alloc;
77

88
pub use heap_alloc::HeapAllocator;
9-
9+
#[cfg(feature="stdlib")]
10+
use alloc_no_stdlib::HeapPrealloc;
1011
mod tests;
1112
extern {
1213
fn calloc(n_elem : usize, el_size : usize) -> *mut u8;
@@ -26,7 +27,36 @@ use alloc_no_stdlib::StackAllocator;
2627
use alloc_no_stdlib::bzero;
2728
declare_stack_allocator_struct!(CallocAllocatedFreelist4, 4, calloc);
2829
declare_stack_allocator_struct!(StackAllocatedFreelist16, 16, stack);
29-
declare_stack_allocator_struct!(BoxAllocatedFreelist, heap);
30+
#[cfg(not(feature="stdlib"))]
31+
fn show_heap_prealloc() {
32+
33+
}
34+
#[cfg(feature="stdlib")]
35+
fn show_heap_prealloc() {
36+
let mut zero_global_buffer = define_allocator_memory_pool!(4, u8, [0; 1024 * 1024 * 20], heap);
37+
38+
let mut boxallocator = HeapPrealloc::<u8>::new_allocator(1024 * 1024, &mut zero_global_buffer, bzero);
39+
40+
{
41+
let mut x = boxallocator.alloc_cell(9999);
42+
x.slice_mut()[0] = 3;
43+
let mut y = boxallocator.alloc_cell(4);
44+
y[0] = 5;
45+
boxallocator.free_cell(y);
46+
47+
let mut three = boxallocator.alloc_cell(3);
48+
three[0] = 6;
49+
boxallocator.free_cell(three);
50+
51+
let mut z = boxallocator.alloc_cell(4);
52+
z.slice_mut()[1] = 8;
53+
let mut reget_three = boxallocator.alloc_cell(4);
54+
reget_three.slice_mut()[1] = 9;
55+
//y.mem[0] = 6; // <-- this is an error (use after free)
56+
println!("x[0] = {:?} z[0] = {:?} z[1] = {:?} r3[0] = {:?} r3[1] = {:?}", x.mem[0], z.mem[0], z.mem[1], reget_three[0], reget_three.slice()[1]);
57+
let mut _z = boxallocator.alloc_cell(1);
58+
}
59+
}
3060

3161
fn main() {
3262
let mut global_buffer = unsafe {define_allocator_memory_pool!(4, u8, [0; 1024 * 1024 * 200], calloc)};
@@ -56,29 +86,7 @@ fn main() {
5686
}
5787
}
5888
}
59-
let mut zero_global_buffer = define_allocator_memory_pool!(4, u8, [0; 1024 * 1024 * 20], heap);
60-
61-
let mut boxallocator = BoxAllocatedFreelist::<u8>::new_allocator(1024 * 1024, &mut zero_global_buffer, bzero);
62-
63-
{
64-
let mut x = boxallocator.alloc_cell(9999);
65-
x.slice_mut()[0] = 3;
66-
let mut y = boxallocator.alloc_cell(4);
67-
y[0] = 5;
68-
boxallocator.free_cell(y);
69-
70-
let mut three = boxallocator.alloc_cell(3);
71-
three[0] = 6;
72-
boxallocator.free_cell(three);
7389

74-
let mut z = boxallocator.alloc_cell(4);
75-
z.slice_mut()[1] = 8;
76-
let mut reget_three = boxallocator.alloc_cell(4);
77-
reget_three.slice_mut()[1] = 9;
78-
//y.mem[0] = 6; // <-- this is an error (use after free)
79-
println!("x[0] = {:?} z[0] = {:?} z[1] = {:?} r3[0] = {:?} r3[1] = {:?}", x.mem[0], z.mem[0], z.mem[1], reget_three[0], reget_three.slice()[1]);
80-
let mut _z = boxallocator.alloc_cell(1);
81-
}
8290

8391
let mut stack_global_buffer = define_allocator_memory_pool!(16, u8, [0; 1024 * 1024], stack);
8492
let mut stackallocator = StackAllocatedFreelist16::<u8>::new_allocator(&mut stack_global_buffer, bzero);
@@ -102,8 +110,6 @@ fn main() {
102110
let mut _z = stackallocator.alloc_cell(1);
103111
}
104112

105-
106-
107113
let mut halloc : HeapAllocator<u8> = HeapAllocator::<u8>{default_value: 0};
108114
for _i in 1..10 { // heap test
109115
let mut x = halloc.alloc_cell(100000);
@@ -116,4 +122,5 @@ fn main() {
116122
println!("x[0] {:?} x[9] {:?} y[0] {:?} z[0] {:?}",
117123
x[0], x[9], -999, z[0]);
118124
}
125+
show_heap_prealloc();
119126
}

src/bin/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ extern crate core;
44
use alloc_no_stdlib::Allocator;
55
use super::HeapAllocator;
66
#[cfg(feature="stdlib")]
7-
use alloc_no_stdlib::StdAlloc;
7+
use alloc_no_stdlib::HeapAlloc;
88
#[cfg(all(feature="unsafe", feature="stdlib"))]
9-
use alloc_no_stdlib::StdAllocUninitialized;
9+
use alloc_no_stdlib::HeapAllocUninitialized;
1010
#[test]
1111
fn heap_test() {
1212
let mut halloc : HeapAllocator<u8> = HeapAllocator::<u8>{default_value: 0};
@@ -30,7 +30,7 @@ fn heap_test() {
3030
#[cfg(all(feature="unsafe", feature="stdlib"))]
3131
#[test]
3232
fn std_unsafe_heap_test() {
33-
let mut halloc = unsafe{StdAllocUninitialized::<u8>::new()};
33+
let mut halloc = unsafe{HeapAllocUninitialized::<u8>::new()};
3434
for _i in 1..10 { // heap test
3535
let mut x = halloc.alloc_cell(100000);
3636
x[0] = 4;
@@ -50,7 +50,7 @@ fn std_unsafe_heap_test() {
5050
#[cfg(feature="stdlib")]
5151
#[test]
5252
fn std_heap_test() {
53-
let mut halloc = unsafe{StdAllocUninitialized::<u8>::new()};
53+
let mut halloc = unsafe{HeapAllocUninitialized::<u8>::new()};
5454
for _i in 1..10 { // heap test
5555
let mut x = halloc.alloc_cell(100000);
5656
x[0] = 4;

src/heap_alloc.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#![cfg(feature="stdlib")]
22
use std;
3+
4+
5+
use super::{SliceWrapper, SliceWrapperMut, Allocator};
6+
37
use core;
48
use core::ops;
9+
use std::boxed::Box;
10+
use std::vec::Vec;
511
pub struct WrapBox<T> {
612
b : std::boxed::Box<[T]>,
713
}
@@ -39,37 +45,38 @@ impl<T> super::SliceWrapperMut<T> for WrapBox<T> {
3945
}
4046
}
4147

42-
pub struct StdAlloc<T : core::clone::Clone>{
48+
pub struct HeapAlloc<T : core::clone::Clone>{
4349
pub default_value : T,
4450
}
4551

46-
impl<T : core::clone::Clone> super::Allocator<T> for StdAlloc<T> {
52+
impl<T : core::clone::Clone> super::Allocator<T> for HeapAlloc<T> {
4753
type AllocatedMemory = WrapBox<T>;
48-
fn alloc_cell(self : &mut StdAlloc<T>, len : usize) -> WrapBox<T> {
54+
fn alloc_cell(self : &mut HeapAlloc<T>, len : usize) -> WrapBox<T> {
4955

5056
let v : std::vec::Vec<T> = vec![self.default_value.clone();len];
5157
let b = v.into_boxed_slice();
5258
return WrapBox::<T>{b : b};
5359
}
54-
fn free_cell(self : &mut StdAlloc<T>, _data : WrapBox<T>) {
60+
fn free_cell(self : &mut HeapAlloc<T>, _data : WrapBox<T>) {
5561

5662
}
5763
}
5864

5965
#[cfg(feature="unsafe")]
60-
pub struct StdAllocUninitialized<T : core::clone::Clone>{
66+
pub struct HeapAllocUninitialized<T>{
6167
#[allow(dead_code)]
6268
default_value : Option<T>,
6369
}
6470

6571
#[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};
72+
impl<T> HeapAllocUninitialized<T>{
73+
pub unsafe fn new() -> HeapAllocUninitialized<T> {
74+
return HeapAllocUninitialized::<T>{default_value:None};
6975
}
7076
}
77+
7178
#[cfg(feature="unsafe")]
72-
impl<T : core::clone::Clone> super::Allocator<T> for StdAllocUninitialized<T> {
79+
impl<T> super::Allocator<T> for HeapAllocUninitialized<T> {
7380
type AllocatedMemory = WrapBox<T>;
7481
fn alloc_cell(self : &mut Self, len : usize) -> WrapBox<T> {
7582

@@ -82,3 +89,41 @@ impl<T : core::clone::Clone> super::Allocator<T> for StdAllocUninitialized<T> {
8289

8390
}
8491
}
92+
93+
94+
pub struct HeapPrealloc<'a, T : 'a> {
95+
freelist : std::boxed::Box<[&'a mut [T]]>,
96+
}
97+
define_stack_allocator_traits!(HeapPrealloc, heap);
98+
99+
impl<'a, T : core::clone::Clone+'a> HeapPrealloc<'a, T> {
100+
fn make_freelist(freelist_size : usize) -> std::boxed::Box<[&'a mut[T]]> {
101+
let mut retval = Vec::<&'a mut[T]>::with_capacity(freelist_size);
102+
for _i in 0..freelist_size {
103+
retval.push(&mut[]);
104+
}
105+
return retval.into_boxed_slice();
106+
}
107+
pub fn new_allocator(freelist_size : usize,
108+
memory_pool : &'a mut Box<[T]>,
109+
initializer : fn(&mut[T])) -> super::StackAllocator<'a, T, HeapPrealloc<'a, T> > {
110+
let mut retval = super::StackAllocator::<T, HeapPrealloc<T> > {
111+
nop : &mut [],
112+
system_resources : HeapPrealloc::<T> {
113+
freelist : Self::make_freelist(freelist_size),
114+
},
115+
free_list_start : freelist_size,
116+
free_list_overflow_count : 0,
117+
initialize : initializer,
118+
};
119+
retval.free_cell(super::AllocatedStackMemory::<T>{mem:&mut*memory_pool});
120+
return retval;
121+
}
122+
#[cfg(feature="unsafe")]
123+
pub unsafe fn new_uninitialized_memory_pool(len : usize) -> Box<[T]> {
124+
let mut v : std::vec::Vec<T> = std::vec::Vec::with_capacity(len);
125+
v.set_len(len);
126+
return v.into_boxed_slice();
127+
}
128+
}
129+

src/init.rs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -151,34 +151,6 @@ macro_rules! declare_stack_allocator_struct(
151151
calloc);
152152
declare_stack_allocator_struct!( @new_calloc_method $name, $freelist_size);
153153
};
154-
($name :ident, heap) => {
155-
struct $name<'a, T : 'a> {freelist : Box<[&'a mut [T]]>,}
156-
define_stack_allocator_traits!($name, heap);
157-
impl<'a, T: 'a> $name<'a, T> {
158-
fn make_freelist(freelist_size : usize) -> Box<[&'a mut[T]]> {
159-
let mut retval = Vec::<&'a mut[T]>::with_capacity(freelist_size);
160-
for _i in 0..freelist_size {
161-
retval.push(&mut[]);
162-
}
163-
return retval.into_boxed_slice();
164-
}
165-
fn new_allocator(freelist_size : usize,
166-
memory_pool : &'a mut Box<[T]>,
167-
initializer : fn(&mut[T])) -> StackAllocator<'a, T, $name<'a, T> > {
168-
let mut retval = StackAllocator::<T, $name<T> > {
169-
nop : &mut [],
170-
system_resources : $name::<T> {
171-
freelist : Self::make_freelist(freelist_size),
172-
},
173-
free_list_start : freelist_size,
174-
free_list_overflow_count : 0,
175-
initialize : initializer,
176-
};
177-
retval.free_cell(AllocatedStackMemory::<T>{mem:&mut*memory_pool});
178-
return retval;
179-
}
180-
}
181-
};
182154
($name :ident, $freelist_size : tt, stack) => {
183155
struct $name<'a, T : 'a> {
184156
freelist : [&'a mut [T];declare_stack_allocator_struct!(@as_expr $freelist_size)],
@@ -249,21 +221,3 @@ macro_rules! define_allocator_memory_pool(
249221
);
250222

251223

252-
/*
253-
#[macro_export]
254-
macro_rules! initialize_allocator(
255-
(@as_expr $expr:expr) => {$expr};
256-
257-
258-
($name : ident, $freelist_size : tt, $T : ty, calloc) => {
259-
StackAllocator::<$T, $name<$T> > {
260-
nop : &mut [],
261-
system_resources : $name::<$T> {
262-
freelist : static_array!(&mut[]; $freelist_size),
263-
},
264-
free_list_start : $freelist_size,
265-
free_list_overflow_count : 0,
266-
}
267-
};
268-
);
269-
*/

src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#[cfg(feature="stdlib")]
44
#[macro_use]
55
extern crate std;
6-
mod heap_alloc;
76
mod allocated_memory;
87
mod stack_allocator;
98
mod allocated_stack_memory;
9+
#[macro_use]
1010
pub mod init;
1111
pub use allocated_memory::SliceWrapper;
1212
pub use allocated_memory::SliceWrapperMut;
@@ -15,10 +15,15 @@ pub use allocated_memory::AllocatedSlice;
1515
pub use allocated_stack_memory::AllocatedStackMemory;
1616
pub use stack_allocator::Allocator;
1717
pub use stack_allocator::StackAllocator;
18+
mod heap_alloc;
19+
#[cfg(feature="stdlib")]
20+
pub use heap_alloc::HeapAlloc;
1821
#[cfg(feature="stdlib")]
19-
pub use heap_alloc::StdAlloc;
22+
pub use heap_alloc::HeapPrealloc;
2023
#[cfg(all(feature="stdlib", feature="unsafe"))]
21-
pub use heap_alloc::StdAllocUninitialized;
24+
pub use heap_alloc::HeapAllocUninitialized;
25+
//#[cfg(all(feature="stdlib", feature="unsafe"))]
26+
//pub use heap_alloc::StdAllocUninitialized;
2227
use core::default::Default;
2328
pub fn bzero<T : Default> (data : &mut [T]) {
2429
for iter in data.iter_mut() {

tests/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ use core::ops;
1212
use alloc_no_stdlib::{Allocator, SliceWrapperMut, SliceWrapper,
1313
StackAllocator, AllocatedStackMemory, uninitialized, bzero};
1414

15-
declare_stack_allocator_struct!(HeapAllocatedFreelist, heap);
15+
#[cfg(feature="stdlib")]
16+
use alloc_no_stdlib::{HeapPrealloc, HeapAlloc};
17+
18+
#[cfg(all(feature="stdlib", feature="unsafe"))]
19+
use alloc_no_stdlib::{HeapAllocUninitialized};
20+
1621
declare_stack_allocator_struct!(CallocAllocatedFreelist4096, 4096, calloc);
1722
declare_stack_allocator_struct!(MallocAllocatedFreelist4096, 4096, malloc);
1823
declare_stack_allocator_struct!(StackAllocatedFreelist4, 4, stack);
@@ -131,10 +136,11 @@ fn uninitialized_stack_pool_free_null() {
131136

132137
}
133138
#[test]
139+
#[cfg(all(feature="stdlib",feature="unsafe"))]
134140
fn uninitialized_heap_pool_test() {
135141
{
136-
let mut heap_global_buffer = define_allocator_memory_pool!(4096, u8, [0; 6 * 1024 * 1024], heap);
137-
let mut ags = HeapAllocatedFreelist::<u8>::new_allocator(4096, &mut heap_global_buffer, uninitialized);
142+
let mut heap_global_buffer = unsafe{HeapPrealloc::<u8>::new_uninitialized_memory_pool(6 * 1024 * 1024)};
143+
let mut ags = HeapPrealloc::<u8>::new_allocator(4096, &mut heap_global_buffer, uninitialized);
138144
{
139145
let mut x = ags.alloc_cell(9999);
140146
x.slice_mut()[0] = 4;
@@ -329,10 +335,11 @@ fn stack_pool_free_null() {
329335

330336
}
331337
#[test]
338+
#[cfg(feature="stdlib")]
332339
fn heap_pool_test() {
333340
{
334341
let mut heap_global_buffer = define_allocator_memory_pool!(4096, u8, [0; 6 * 1024 * 1024], heap);
335-
let mut ags = HeapAllocatedFreelist::<u8>::new_allocator(4096, &mut heap_global_buffer, bzero);
342+
let mut ags = HeapPrealloc::<u8>::new_allocator(4096, &mut heap_global_buffer, bzero);
336343
{
337344
let mut x = ags.alloc_cell(9999);
338345
x.slice_mut()[0] = 4;

0 commit comments

Comments
 (0)