MCPcopy Create free account
hub / github.com/dropbox/rust-alloc-no-stdlib / show_heap_prealloc

Function show_heap_prealloc

alloc-stdlib/src/bin/example.rs:31–55  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

29declare_stack_allocator_struct!(StackAllocatedFreelist16, 16, stack);
30
31fn 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
57fn main() {
58 let mut global_buffer = unsafe {define_allocator_memory_pool!(4, u8, [0; 1024 * 1024 * 200], calloc)};

Callers 1

mainFunction · 0.85

Calls 3

alloc_cellMethod · 0.45
slice_mutMethod · 0.45
free_cellMethod · 0.45

Tested by

no test coverage detected