|
| 1 | +//#![feature(trace_macros)] |
| 2 | +extern crate alloc_stdlib; |
| 3 | +#[macro_use] |
| 4 | +extern crate alloc_no_stdlib; |
| 5 | + |
| 6 | +extern crate core; |
| 7 | +use core::ops; |
| 8 | + |
| 9 | +pub use alloc_stdlib::{HeapAlloc, StandardAlloc}; |
| 10 | +use alloc_stdlib::HeapPrealloc; |
| 11 | +mod tests; |
| 12 | +extern { |
| 13 | + fn calloc(n_elem : usize, el_size : usize) -> *mut u8; |
| 14 | +} |
| 15 | +extern { |
| 16 | + fn free(ptr : *mut u8); |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +//use alloc::AllocatedSlice; |
| 21 | +use alloc_no_stdlib::SliceWrapper; |
| 22 | +use alloc_no_stdlib::SliceWrapperMut; |
| 23 | +use alloc_no_stdlib::AllocatedStackMemory; |
| 24 | +use alloc_no_stdlib::Allocator; |
| 25 | +use alloc_no_stdlib::StackAllocator; |
| 26 | + |
| 27 | +use alloc_no_stdlib::bzero; |
| 28 | +declare_stack_allocator_struct!(CallocAllocatedFreelist4, 4, calloc); |
| 29 | +declare_stack_allocator_struct!(StackAllocatedFreelist16, 16, stack); |
| 30 | + |
| 31 | +fn show_heap_prealloc() { |
| 32 | + let mut zero_global_buffer = define_allocator_memory_pool!(4, u8, [0; 1024 * 1024 * 20], heap); |
| 33 | + |
| 34 | + let mut boxallocator = HeapPrealloc::<u8>::new_allocator(1024 * 1024, &mut zero_global_buffer, bzero); |
| 35 | + |
| 36 | + { |
| 37 | + let mut x = boxallocator.alloc_cell(9999); |
| 38 | + x.slice_mut()[0] = 3; |
| 39 | + let mut y = boxallocator.alloc_cell(4); |
| 40 | + y[0] = 5; |
| 41 | + boxallocator.free_cell(y); |
| 42 | + |
| 43 | + let mut three = boxallocator.alloc_cell(3); |
| 44 | + three[0] = 6; |
| 45 | + boxallocator.free_cell(three); |
| 46 | + |
| 47 | + let mut z = boxallocator.alloc_cell(4); |
| 48 | + z.slice_mut()[1] = 8; |
| 49 | + let mut reget_three = boxallocator.alloc_cell(4); |
| 50 | + reget_three.slice_mut()[1] = 9; |
| 51 | + //y.mem[0] = 6; // <-- this is an error (use after free) |
| 52 | + 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]); |
| 53 | + let mut _z = boxallocator.alloc_cell(1); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn main() { |
| 58 | + let mut global_buffer = unsafe {define_allocator_memory_pool!(4, u8, [0; 1024 * 1024 * 200], calloc)}; |
| 59 | + { |
| 60 | + let gbref = &mut global_buffer; |
| 61 | +{ |
| 62 | + let mut ags = CallocAllocatedFreelist4::<u8>::new_allocator(gbref.data, bzero); |
| 63 | + |
| 64 | + { |
| 65 | + let mut x = ags.alloc_cell(9999); |
| 66 | + x.slice_mut()[0] = 4; |
| 67 | + let mut y = ags.alloc_cell(4); |
| 68 | + y[0] = 5; |
| 69 | + ags.free_cell(y); |
| 70 | + |
| 71 | + let mut three = ags.alloc_cell(3); |
| 72 | + three[0] = 6; |
| 73 | + ags.free_cell(three); |
| 74 | + |
| 75 | + let mut z = ags.alloc_cell(4); |
| 76 | + z.slice_mut()[1] = 8; |
| 77 | + let mut reget_three = ags.alloc_cell(4); |
| 78 | + reget_three.slice_mut()[1] = 9; |
| 79 | + //y.mem[0] = 6; // <-- this is an error (use after free) |
| 80 | + 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]); |
| 81 | + let mut _z = ags.alloc_cell(1); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + let mut stack_global_buffer = define_allocator_memory_pool!(16, u8, [0; 1024 * 1024], stack); |
| 88 | + let mut stackallocator = StackAllocatedFreelist16::<u8>::new_allocator(&mut stack_global_buffer, bzero); |
| 89 | + { |
| 90 | + let mut x = stackallocator.alloc_cell(9999); |
| 91 | + x.slice_mut()[0] = 3; |
| 92 | + let mut y = stackallocator.alloc_cell(4); |
| 93 | + y[0] = 5; |
| 94 | + stackallocator.free_cell(y); |
| 95 | + |
| 96 | + let mut three = stackallocator.alloc_cell(3); |
| 97 | + three[0] = 6; |
| 98 | + stackallocator.free_cell(three); |
| 99 | + |
| 100 | + let mut z = stackallocator.alloc_cell(4); |
| 101 | + z.slice_mut()[1] = 8; |
| 102 | + let mut reget_three = stackallocator.alloc_cell(4); |
| 103 | + reget_three.slice_mut()[1] = 9; |
| 104 | + //y.mem[0] = 6; // <-- this is an error (use after free) |
| 105 | + 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]); |
| 106 | + let mut _z = stackallocator.alloc_cell(1); |
| 107 | + } |
| 108 | + |
| 109 | + let mut halloc = HeapAlloc::<u8>::default(); |
| 110 | + for _i in 1..10 { // heap test |
| 111 | + let mut x = halloc.alloc_cell(100000); |
| 112 | + x.slice_mut()[0] = 4; |
| 113 | + let mut y = halloc.alloc_cell(110000); |
| 114 | + y.slice_mut()[0] = 5; |
| 115 | + let mut z = halloc.alloc_cell(120000); |
| 116 | + z.slice_mut()[0] = 6; |
| 117 | + halloc.free_cell(y); |
| 118 | + println!("x[0] {:?} x[9] {:?} y[0] {:?} z[0] {:?}", |
| 119 | + x.slice()[0], x.slice()[9], -999, z.slice()[0]); |
| 120 | + } |
| 121 | + show_heap_prealloc(); |
| 122 | +} |
0 commit comments