Skip to content

Commit 6032b6a

Browse files
committedSep 12, 2022
Fix #12 by cleaning up test suite. Bump version
1 parent 48709cf commit 6032b6a

File tree

5 files changed

+10
-200
lines changed

5 files changed

+10
-200
lines changed
 

‎Cargo.toml

+1-1
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 = "2.0.3"
4+
version = "2.0.4"
55
authors = ["Daniel Reiter Horn <danielrh@dropbox.com>"]
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"

‎alloc-stdlib/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "alloc-stdlib"
33
description = "A dynamic allocator example that may be used with the stdlib"
4-
version = "0.2.1"
4+
version = "0.2.2"
55
authors = ["Daniel Reiter Horn <danielrh@dropbox.com>"]
66
documentation = "https://raw.githubusercontent.com/dropbox/rust-alloc-no-stdlib/master/alloc-stdlib/tests/lib.rs"
77
homepage = "https://github.com/dropbox/rust-alloc-no-stdlib"
@@ -15,7 +15,7 @@ autobins = false
1515
name = "example"
1616

1717
[dependencies]
18-
"alloc-no-stdlib" = { version="2.0.0", path="../"}
18+
"alloc-no-stdlib" = { version="2.0.4", path="../"}
1919

2020
[features]
2121
unsafe = []

‎alloc-stdlib/src/heap_alloc.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -96,33 +96,8 @@ impl<T : Clone> super::Allocator<T> for HeapAlloc<T> {
9696
}
9797
}
9898

99-
#[cfg(feature="unsafe")]
100-
pub struct HeapAllocUninitialized<T>{
101-
#[allow(dead_code)]
102-
default_value : Option<T>,
103-
}
104-
105-
#[cfg(feature="unsafe")]
106-
impl<T> HeapAllocUninitialized<T>{
107-
pub unsafe fn new() -> HeapAllocUninitialized<T> {
108-
return HeapAllocUninitialized::<T>{default_value:None};
109-
}
110-
}
111-
112-
#[cfg(feature="unsafe")]
113-
impl<T> super::Allocator<T> for HeapAllocUninitialized<T> {
114-
type AllocatedMemory = WrapBox<T>;
115-
fn alloc_cell(self : &mut Self, len : usize) -> WrapBox<T> {
116-
117-
let mut v : std::vec::Vec<T> = std::vec::Vec::with_capacity(len);
118-
unsafe {v.set_len(len)};
119-
let b = v.into_boxed_slice();
120-
return WrapBox::<T>(b);
121-
}
122-
fn free_cell(self : &mut Self, _data : WrapBox<T>) {
123-
124-
}
125-
}
99+
#[deprecated]
100+
pub type HeapAllocUninitialized<T> = HeapAlloc<T>;
126101

127102

128103
pub struct HeapPrealloc<'a, T : 'a> {

‎alloc-stdlib/tests/lib.rs

+5-70
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use alloc_stdlib::{HeapPrealloc, HeapAlloc};
1818
use alloc_stdlib::{HeapAllocUninitialized};
1919

2020
declare_stack_allocator_struct!(CallocAllocatedFreelist4096, 4096, calloc);
21-
declare_stack_allocator_struct!(MallocAllocatedFreelist4096, 4096, malloc);
2221
declare_stack_allocator_struct!(StackAllocatedFreelist4, 4, stack);
2322
declare_stack_allocator_struct!(StackAllocatedFreelist8, 8, stack);
2423
declare_stack_allocator_struct!(GlobalAllocatedFreelist, 16, global);
@@ -364,80 +363,16 @@ fn heap_pool_test() {
364363
}
365364
}
366365
}
367-
#[test]
368-
fn calloc_pool_test() {
369-
370-
{
371-
let mut calloc_global_buffer = unsafe {define_allocator_memory_pool!(4096, u8, [0; 200 * 1024 * 1024], calloc)};
372-
let mut ags = CallocAllocatedFreelist4096::<u8>::new_allocator(&mut calloc_global_buffer.data, bzero);
373-
{
374-
let mut x = ags.alloc_cell(9999);
375-
x.slice_mut()[0] = 4;
376-
let mut y = ags.alloc_cell(4);
377-
y[0] = 5;
378-
ags.free_cell(y);
379-
380-
let mut three = ags.alloc_cell(3);
381-
three[0] = 6;
382-
ags.free_cell(three);
383-
384-
let mut z = ags.alloc_cell(4);
385-
z.slice_mut()[1] = 8;
386-
let mut reget_three = ags.alloc_cell(4);
387-
reget_three.slice_mut()[1] = 9;
388-
//y.mem[0] = 6; // <-- this is an error (use after free)
389-
assert_eq!(x[0], 4);
390-
assert_eq!(z[0], 0);
391-
assert_eq!(z[1], 8);
392-
assert_eq!(reget_three[0], 0);
393-
assert_eq!(reget_three[1], 9);
394-
let mut _z = ags.alloc_cell(1);
395-
}
396-
}
397-
}
398-
399-
400366

401-
#[test]
402-
fn calloc_leak_pool_test() {
403-
404-
{
405-
let mut calloc_global_buffer = unsafe{define_allocator_memory_pool!(4096, u8, [0; 200 * 1024 * 1024], calloc_no_free)};
406-
let mut ags = CallocAllocatedFreelist4096::<u8>::new_allocator(&mut calloc_global_buffer.data, bzero);
407-
{
408-
let mut x = ags.alloc_cell(9999);
409-
x.slice_mut()[0] = 4;
410-
let mut y = ags.alloc_cell(4);
411-
y[0] = 5;
412-
ags.free_cell(y);
413-
414-
let mut three = ags.alloc_cell(3);
415-
three[0] = 6;
416-
ags.free_cell(three);
417-
418-
let mut z = ags.alloc_cell(4);
419-
z.slice_mut()[1] = 8;
420-
let mut reget_three = ags.alloc_cell(4);
421-
reget_three.slice_mut()[1] = 9;
422-
//y.mem[0] = 6; // <-- this is an error (use after free)
423-
assert_eq!(x[0], 4);
424-
assert_eq!(z[0], 0);
425-
assert_eq!(z[1], 8);
426-
assert_eq!(reget_three[0], 0);
427-
assert_eq!(reget_three[1], 9);
428-
let mut _z = ags.alloc_cell(1);
429-
}
430-
}
431-
}
432367

433368

434369
#[test]
435-
fn malloc_pool_test() {
370+
fn calloc_pool_test() {
436371

437372
{
438-
let mut malloc_global_buffer = unsafe {define_allocator_memory_pool!(4096, u8, [0; 200 * 1024 * 1024], malloc)};
373+
let mut calloc_global_buffer = unsafe {define_allocator_memory_pool!(4096, u8, [0; 200 * 1024 * 1024], calloc)};
439374
{
440-
let mut ags = MallocAllocatedFreelist4096::<u8>::new_allocator(&mut malloc_global_buffer.data, bzero);
375+
let mut ags = CallocAllocatedFreelist4096::<u8>::new_allocator(&mut calloc_global_buffer.data, bzero);
441376
{
442377
let mut x = ags.alloc_cell(9999);
443378
x.slice_mut()[0] = 4;
@@ -463,7 +398,7 @@ fn malloc_pool_test() {
463398
}
464399
}
465400
{
466-
let mut ags = MallocAllocatedFreelist4096::<u8>::new_allocator(&mut malloc_global_buffer.data, bzero);
401+
let mut ags = CallocAllocatedFreelist4096::<u8>::new_allocator(&mut calloc_global_buffer.data, bzero);
467402
{
468403
let mut x = ags.alloc_cell(9999);
469404
x.slice_mut()[0] = 4;
@@ -488,7 +423,7 @@ fn malloc_pool_test() {
488423
let mut _z = ags.alloc_cell(1);
489424
}
490425
}
491-
drop(malloc_global_buffer);
426+
drop(calloc_global_buffer);
492427
}
493428
}
494429

‎tests/lib.rs

-100
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use alloc_no_stdlib::{Allocator, SliceWrapperMut, SliceWrapper,
1313
StackAllocator, AllocatedStackMemory, uninitialized, bzero};
1414

1515
declare_stack_allocator_struct!(CallocAllocatedFreelist4096, 4096, calloc);
16-
declare_stack_allocator_struct!(MallocAllocatedFreelist4096, 4096, malloc);
1716
declare_stack_allocator_struct!(StackAllocatedFreelist4, 4, stack);
1817
declare_stack_allocator_struct!(StackAllocatedFreelist8, 8, stack);
1918
declare_stack_allocator_struct!(GlobalAllocatedFreelist, 16, global);
@@ -23,7 +22,6 @@ define_allocator_memory_pool!(16, u8, [0; 1024 * 1024 * 100], global, global_buf
2322
define_allocator_memory_pool!(16, u8, [0; 1024 * 1024 * 100], global, global_buffer2);
2423
extern {
2524
fn calloc(n_elem : usize, el_size : usize) -> *mut u8;
26-
fn malloc(len : usize) -> *mut u8;
2725
fn free(item : *mut u8);
2826
}
2927
#[test]
@@ -332,104 +330,6 @@ fn calloc_pool_test() {
332330
}
333331

334332

335-
336-
#[test]
337-
fn calloc_leak_pool_test() {
338-
339-
{
340-
let mut calloc_global_buffer = unsafe{define_allocator_memory_pool!(4096, u8, [0; 200 * 1024 * 1024], calloc_no_free)};
341-
let mut ags = CallocAllocatedFreelist4096::<u8>::new_allocator(&mut calloc_global_buffer.data, bzero);
342-
{
343-
let mut x = ags.alloc_cell(9999);
344-
x.slice_mut()[0] = 4;
345-
let mut y = ags.alloc_cell(4);
346-
y[0] = 5;
347-
ags.free_cell(y);
348-
349-
let mut three = ags.alloc_cell(3);
350-
three[0] = 6;
351-
ags.free_cell(three);
352-
353-
let mut z = ags.alloc_cell(4);
354-
z.slice_mut()[1] = 8;
355-
let mut reget_three = ags.alloc_cell(4);
356-
reget_three.slice_mut()[1] = 9;
357-
//y.mem[0] = 6; // <-- this is an error (use after free)
358-
assert_eq!(x[0], 4);
359-
assert_eq!(z[0], 0);
360-
assert_eq!(z[1], 8);
361-
assert_eq!(reget_three[0], 0);
362-
assert_eq!(reget_three[1], 9);
363-
let mut _z = ags.alloc_cell(1);
364-
}
365-
}
366-
}
367-
368-
369-
#[test]
370-
fn malloc_pool_test() {
371-
372-
{
373-
let mut malloc_global_buffer = unsafe {define_allocator_memory_pool!(4096, u8, [0; 200 * 1024 * 1024], malloc)};
374-
{
375-
let mut ags = MallocAllocatedFreelist4096::<u8>::new_allocator(&mut malloc_global_buffer.data, bzero);
376-
{
377-
let mut x = ags.alloc_cell(9999);
378-
x.slice_mut()[0] = 4;
379-
let mut y = ags.alloc_cell(4);
380-
y[0] = 5;
381-
ags.free_cell(y);
382-
383-
let mut three = ags.alloc_cell(3);
384-
three[0] = 6;
385-
ags.free_cell(three);
386-
387-
let mut z = ags.alloc_cell(4);
388-
z.slice_mut()[1] = 8;
389-
let mut reget_three = ags.alloc_cell(4);
390-
reget_three.slice_mut()[1] = 9;
391-
//y.mem[0] = 6; // <-- this is an error (use after free)
392-
assert_eq!(x[0], 4);
393-
assert_eq!(z[0], 0);
394-
assert_eq!(z[1], 8);
395-
assert_eq!(reget_three[0], 0);
396-
assert_eq!(reget_three[1], 9);
397-
let mut _z = ags.alloc_cell(1);
398-
}
399-
}
400-
{
401-
let mut ags = MallocAllocatedFreelist4096::<u8>::new_allocator(&mut malloc_global_buffer.data, bzero);
402-
{
403-
let mut x = ags.alloc_cell(9999);
404-
x.slice_mut()[0] = 4;
405-
let mut y = ags.alloc_cell(4);
406-
y[0] = 5;
407-
ags.free_cell(y);
408-
409-
let mut three = ags.alloc_cell(3);
410-
three[0] = 6;
411-
ags.free_cell(three);
412-
413-
let mut z = ags.alloc_cell(4);
414-
z.slice_mut()[1] = 8;
415-
let mut reget_three = ags.alloc_cell(4);
416-
reget_three.slice_mut()[1] = 9;
417-
//y.mem[0] = 6; // <-- this is an error (use after free)
418-
assert_eq!(x[0], 4);
419-
assert_eq!(z[0], 0);
420-
assert_eq!(z[1], 8);
421-
assert_eq!(reget_three[0], 0);
422-
assert_eq!(reget_three[1], 9);
423-
let mut _z = ags.alloc_cell(1);
424-
}
425-
}
426-
drop(malloc_global_buffer);
427-
}
428-
}
429-
430-
431-
432-
433333
#[test]
434334
fn global_pool_test() {
435335
{

0 commit comments

Comments
 (0)
Please sign in to comment.