Skip to content

Commit cfd11db

Browse files
committed
added some helpful macros to implement indexable items, but do not mandate it for slice wrappers. Also add len() function
1 parent fe3511e commit cfd11db

File tree

10 files changed

+45
-61
lines changed

10 files changed

+45
-61
lines changed

.travis.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@ language: rust
22
rust:
33
- nightly
44
- stable
5-
# The following Rust version represents the oldest supported version of Mio
6-
- 1.6.0
5+
- 1.12.0
6+
- 1.8.0
77

88
os:
99
- linux
1010
- osx
1111

1212
script:
1313
- cargo test
14+
- cargo test --features=unsafe --release
15+
- cargo test --features=no-stdlib
16+
- cargo test --release
17+
18+
matrix:
19+
exclude:
20+
- rust: 1.8.0
21+
os: osx

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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.2.0"
4+
version = "1.3.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"
88
readme = "README.md"
99
keywords = ["custom", "allocator", "calloc", "safe", "nostd"]
1010
license = "BSD-3-Clause"
1111
repository = "https://github.com/dropbox/rust-alloc-no-stdlib"
12+
autobins = false
1213

1314
[[bin]]
1415
name = "example"

src/allocated_memory/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
extern crate core;
2-
use core::ops;
2+
#[macro_use]
3+
mod index_macro;
4+
mod test;
35
use core::default::Default;
4-
6+
pub use core::ops::IndexMut;
7+
pub use core::ops::Index;
8+
pub use core::ops::Range;
59
pub trait SliceWrapper<T> {
6-
fn slice(& self) -> & [T];
10+
fn slice(& self) -> & [T];
11+
fn len(&self) -> usize{
12+
self.slice().len()
13+
}
714
}
815

9-
pub trait SliceWrapperMut<T> {
16+
pub trait SliceWrapperMut<T> : SliceWrapper<T> {
1017
fn slice_mut (&mut self) -> & mut [T];
1118
}
1219

1320
pub trait AllocatedSlice<T>
14-
: SliceWrapperMut<T> + SliceWrapper<T> + ops::IndexMut<usize> + ops::Index<usize> + Default {
21+
: SliceWrapperMut<T> + SliceWrapper<T> + Default {
1522
}
1623

17-
impl<T, U> AllocatedSlice<T> for U where U : SliceWrapperMut<T> + SliceWrapper<T> + ops::IndexMut<usize> + ops::Index<usize> + Default {
24+
impl<T, U> AllocatedSlice<T> for U where U : SliceWrapperMut<T> + SliceWrapper<T> + Default {
1825

1926
}

src/allocated_stack_memory.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
extern crate core;
2+
23
use super::allocated_memory::SliceWrapper;
34
use super::allocated_memory::SliceWrapperMut;
4-
use core::ops;
5+
56
pub struct AllocatedStackMemory<'a, T:'a> {
67
pub mem : &'a mut [T],
78
}
89

10+
define_index_ops_mut!(a, T, AllocatedStackMemory<'a, T>);
11+
912
impl<'a, T: 'a> core::default::Default for AllocatedStackMemory<'a, T> {
1013
fn default() -> Self {
1114
return AllocatedStackMemory::<'a, T>{mem : &mut[]};
1215
}
1316
}
1417

15-
impl<'a, T: 'a> ops::Index<usize> for AllocatedStackMemory<'a, T> {
16-
type Output = T;
17-
fn index<'b>(&'b self, _index : usize) -> &'b T {
18-
return &self.mem[_index];
19-
}
20-
}
21-
22-
impl<'a, T: 'a> ops::IndexMut<usize> for AllocatedStackMemory<'a, T> {
23-
fn index_mut<'b>(&'b mut self, _index : usize) -> &'b mut T {
24-
return &mut self.mem[_index];
25-
}
26-
}
2718

2819
impl<'a, T: 'a> SliceWrapper<T> for AllocatedStackMemory<'a, T> {
2920
fn slice(& self) -> & [T] {

src/bin/example.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn show_heap_prealloc() {
6161
fn main() {
6262
let mut global_buffer = unsafe {define_allocator_memory_pool!(4, u8, [0; 1024 * 1024 * 200], calloc)};
6363
{
64-
let mut gbref = &mut global_buffer;
64+
let gbref = &mut global_buffer;
6565
{
6666
let mut ags = CallocAllocatedFreelist4::<u8>::new_allocator(gbref.data, bzero);
6767

@@ -123,4 +123,4 @@ fn main() {
123123
x[0], x[9], -999, z[0]);
124124
}
125125
show_heap_prealloc();
126-
}
126+
}

src/bin/heap_alloc.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc_no_stdlib;
2-
use core::ops;
32
use core;
3+
use alloc_no_stdlib::{SliceWrapper, SliceWrapperMut};
44

55
pub struct Rebox<T> {
66
b : Box<[T]>,
@@ -13,19 +13,7 @@ impl<T> core::default::Default for Rebox<T> {
1313
return Rebox::<T>{b : b};
1414
}
1515
}
16-
17-
impl<T> ops::Index<usize> for Rebox<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 Rebox<T>{
25-
fn index_mut(&mut self, index : usize) -> &mut T {
26-
return &mut (*self.b)[index]
27-
}
28-
}
16+
define_index_ops_mut!(T, Rebox<T>);
2917

3018
impl<T> alloc_no_stdlib::SliceWrapper<T> for Rebox<T> {
3119
fn slice(&self) -> & [T] {

src/heap_alloc.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,7 @@ impl<T> core::default::Default for WrapBox<T> {
1919
return WrapBox::<T>{b : b};
2020
}
2121
}
22-
23-
impl<T> ops::Index<usize> for WrapBox<T>{
24-
type Output = T;
25-
fn index(&self, index : usize) -> &T {
26-
return &(*self.b)[index]
27-
}
28-
}
29-
30-
impl<T> ops::IndexMut<usize> for WrapBox<T>{
31-
fn index_mut(&mut self, index : usize) -> &mut T {
32-
return &mut (*self.b)[index]
33-
}
34-
}
22+
define_index_ops_mut!(T, WrapBox<T>);
3523

3624
impl<T> super::SliceWrapper<T> for WrapBox<T> {
3725
fn slice(&self) -> & [T] {

src/init.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ macro_rules! declare_stack_allocator_struct(
180180
#[macro_export]
181181
macro_rules! bind_global_buffers_to_allocator(
182182
($allocator : expr, $buffer : ident, $T : ty) => {
183-
$allocator.free_list_start = $buffer::freelist.len();
184-
$allocator.system_resources.freelist = &mut $buffer::freelist;
185-
$allocator.free_cell(AllocatedStackMemory::<$T>{mem:&mut $buffer::heap});
183+
$allocator.free_list_start = $buffer::FREELIST.len();
184+
$allocator.system_resources.freelist = &mut $buffer::FREELIST;
185+
$allocator.free_cell(AllocatedStackMemory::<$T>{mem:&mut $buffer::HEAP});
186186
};
187187
);
188188

@@ -211,10 +211,10 @@ macro_rules! define_allocator_memory_pool(
211211
};
212212
($freelist_size : tt, $T : ty, [$default_value : expr; $heap_size : expr], global, $name : ident) => {
213213
pub mod $name {
214-
pub static mut freelist : [&'static mut [$T];
214+
pub static mut FREELIST : [&'static mut [$T];
215215
define_allocator_memory_pool!(@as_expr $freelist_size)]
216216
= static_array!(&mut[]; $freelist_size);
217-
pub static mut heap : [$T; $heap_size] = [$default_value; $heap_size];
217+
pub static mut HEAP : [$T; $heap_size] = [$default_value; $heap_size];
218218
}
219219
};
220220

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#[cfg(not(feature="no-stdlib"))]
44
#[macro_use]
55
extern crate std;
6+
#[macro_use]
67
mod allocated_memory;
78
mod stack_allocator;
89
mod allocated_stack_memory;
@@ -81,4 +82,4 @@ impl<'a, T:'a> Drop for CallocBackingStore<'a, T> {
8182

8283
}
8384
}
84-
}
85+
}

src/stack_allocator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a, T : 'a, U : allocated_memory::AllocatedSlice<&'a mut[T]> >
5151
if !found {
5252
panic!("OOM");
5353
}
54-
let mut available_slice = core::mem::replace(&mut self.system_resources.slice_mut()[index],
54+
let available_slice = core::mem::replace(&mut self.system_resources.slice_mut()[index],
5555
&mut[]);
5656
if available_slice.len() == len
5757
|| (available_slice.len() < len + 32
@@ -60,7 +60,7 @@ impl<'a, T : 'a, U : allocated_memory::AllocatedSlice<&'a mut[T]> >
6060
// we must assign free_list_start
6161
if index != self.free_list_start {
6262
assert!(index > self.free_list_start);
63-
let mut farthest_free_list = core::mem::replace(
63+
let farthest_free_list = core::mem::replace(
6464
&mut self.system_resources.slice_mut()[self.free_list_start],
6565
&mut []);
6666
core::mem::replace(&mut self.system_resources.slice_mut()[index],
@@ -70,13 +70,13 @@ impl<'a, T : 'a, U : allocated_memory::AllocatedSlice<&'a mut[T]> >
7070
return self.clear_if_necessary(index,
7171
AllocatedStackMemory::<'a, T>{mem:available_slice});
7272
} else { // the memory allocated was not the entire range of items. Split and move on
73-
let (mut retval, return_to_sender) = available_slice.split_at_mut(len);
73+
let (retval, return_to_sender) = available_slice.split_at_mut(len);
7474
core::mem::replace(&mut self.system_resources.slice_mut()[index], return_to_sender);
7575
return self.clear_if_necessary(index, AllocatedStackMemory::<'a, T>{mem:retval});
7676
}
7777
}
7878
fn free_cell(self : &mut StackAllocator<'a, T, U>,
79-
mut val : AllocatedStackMemory<'a, T>) {
79+
val : AllocatedStackMemory<'a, T>) {
8080
if val.slice().len() == 0 {
8181
return;
8282
}

0 commit comments

Comments
 (0)