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

Function main

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

Source from the content-addressed store, hash-verified

55}
56
57fn 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;

Callers

nothing calls this directly

Calls 4

show_heap_preallocFunction · 0.85
alloc_cellMethod · 0.45
slice_mutMethod · 0.45
free_cellMethod · 0.45

Tested by

no test coverage detected