Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::allocrust-analyzer cargo std https://github.com/rust-lang/rust/library/std alloc/::{GlobalAllocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#, Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#};
2
3 pub struct GgAllocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#<A> {
4 allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc.: A,
5 }
6
7 impl<A> GgAllocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#<A> {
8 pub const fn newrust-analyzer cargo gg-alloc 1.0.0 impl#[`GgAlloc<A>`]new().(alloc: A) -> Selfrust-analyzer cargo gg-alloc 1.0.0 impl#[`GgAlloc<A>`] {
9 Selfrust-analyzer cargo gg-alloc 1.0.0 impl#[`GgAlloc<A>`] { allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc. }
10 }
11 }
12
13 fn pointer_above_2grust-analyzer cargo gg-alloc 1.0.0 pointer_above_2g().(ptr: *mut u8) -> bool {
14 //(ptr as usize) > (isize::MAX as usize)
15 (ptr as u32) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialOrd<Self>`]gt().(i32::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]MAX. as u32)
16 }
17
18 fn alloc_fully_below_2grust-analyzer cargo gg-alloc 1.0.0 alloc_fully_below_2g().(ptr: *mut u8, layout: Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#) -> bool {
19 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pointer_above_2grust-analyzer cargo gg-alloc 1.0.0 pointer_above_2g().(ptr) && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pointer_above_2grust-analyzer cargo gg-alloc 1.0.0 pointer_above_2g().(unsafe { ptr.addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(layout.sizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/impl#[Layout]size().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) })
20 }
21
22 unsafe impl<A> GlobalAllocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc# for GgAllocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#<A>
23 where
24 A: GlobalAllocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#,
25 {
26 unsafe fn allocrust-analyzer cargo gg-alloc 1.0.0 impl#[`GgAlloc<A>`][GlobalAlloc]alloc().(&self, layout: Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#) -> *mut u8 {
27 let mut ret = self.allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc..allocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#alloc().(layout);
28
29 loop {
30 if ret.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() {
31 // Failed to allocate, return null pointer
32 break;
33 }
34
35 if pointer_above_2grust-analyzer cargo gg-alloc 1.0.0 pointer_above_2g().(ret) {
36 // Allocated above 2G, return this pointer
37 break;
38 }
39
40 // Here the allocation was successful but below 2G, so fill memory a bit
41 // in chunks of 128 MB
42 let mut size = 1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[usize][`Shl<i32>`]shl().27;
43 loop {
44 let test_layout = Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#::from_size_alignrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/impl#[Layout]from_size_align().(size, 1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
45 let fill_ptr = self.allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc..allocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#alloc().(test_layout);
46 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().fill_ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() && alloc_fully_below_2grust-analyzer cargo gg-alloc 1.0.0 alloc_fully_below_2g().(fill_ptr, test_layout) {
47 // leak allocation and continue
48 } else {
49 // Free and try again with half size
50 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().fill_ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() {
51 self.allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc..deallocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#dealloc().(fill_ptr, test_layout);
52 }
53 size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`DivAssign<Self>`]div_assign()./=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`DivAssign<Self>`]div_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`DivAssign<Self>`]div_assign().2;
54
55 if size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().1 {
56 // Exit fill loop if size is small
57 break;
58 }
59 }
60 }
61
62 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().alloc_fully_below_2grust-analyzer cargo gg-alloc 1.0.0 alloc_fully_below_2g().(ret, layout) {
63 // Free and allocate something with half size
64 self.allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc..deallocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#dealloc().(ret, layout);
65 }
66
67 // fill memory a bit
68 // in chunks of same size as the requested allocation
69 loop {
70 let test_layout = layout;
71 let fill_ptr = self.allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc..allocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#alloc().(test_layout);
72 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().fill_ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() && alloc_fully_below_2grust-analyzer cargo gg-alloc 1.0.0 alloc_fully_below_2g().(fill_ptr, test_layout) {
73 // leak allocation and continue
74 } else {
75 // this allocation is either above 2GB or right at the 2GB edge, we are done
76 if pointer_above_2grust-analyzer cargo gg-alloc 1.0.0 pointer_above_2g().(fill_ptr) {
77 // above 2GB, we can use it as the return pointer
78 return fill_ptr;
79 } else {
80 // right at the edge, leak this allocation and try again in next iteration
81 // of the outer loop
82 break;
83 }
84 }
85 }
86
87 ret = self.allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc..allocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#alloc().(layout);
88 }
89
90 ret
91 }
92 unsafe fn deallocrust-analyzer cargo gg-alloc 1.0.0 impl#[`GgAlloc<A>`][GlobalAlloc]dealloc().(&self, ptr: *mut u8, layout: Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#) {
93 self.allocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#alloc..deallocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/global/GlobalAlloc#dealloc().(ptr, layout);
94 }
95 }
96
97 #[cfg(test)]
98 mod testsrust-analyzer cargo gg-alloc 1.0.0 tests/ {
99 use superrust-analyzer cargo gg-alloc 1.0.0 crate/::*;
100 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::allocrust-analyzer cargo std https://github.com/rust-lang/rust/library/std alloc/::Systemrust-analyzer cargo std https://github.com/rust-lang/rust/library/std alloc/System#;
101
102 #[global_allocatorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/global_allocator:]
103 static Arust-analyzer cargo gg-alloc 1.0.0 tests/A.: GgAllocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#<Systemrust-analyzer cargo std https://github.com/rust-lang/rust/library/std alloc/System#> = GgAllocrust-analyzer cargo gg-alloc 1.0.0 GgAlloc#::newrust-analyzer cargo gg-alloc 1.0.0 impl#[`GgAlloc<A>`]new().(Systemrust-analyzer cargo std https://github.com/rust-lang/rust/library/std alloc/System#);
104
105 fn assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(ptr: *mut u8) {
106 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(pointer_above_2grust-analyzer cargo gg-alloc 1.0.0 pointer_above_2g().(ptr), "{:p}", ptr,);
107 }
108
109 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
110 fn alloc_stringrust-analyzer cargo gg-alloc 1.0.0 tests/alloc_string().() {
111 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("allocating a string!");
112 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
113 }
114
115 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
116 fn alloc_one_byterust-analyzer cargo gg-alloc 1.0.0 tests/alloc_one_byte().() {
117 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("1");
118 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
119 pointer_above_2grust-analyzer cargo gg-alloc 1.0.0 pointer_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _),
120 "{:p}",
121 s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()
122 );
123 }
124
125 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
126 fn alloc_many_bytesrust-analyzer cargo gg-alloc 1.0.0 tests/alloc_many_bytes().() {
127 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("1");
128 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
129 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("12");
130 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
131 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("123");
132 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
133 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("1234");
134 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
135 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("12345");
136 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
137 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("123456");
138 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
139 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("1234567");
140 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
141 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("12345678");
142 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
143 let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("123456789");
144 assert_above_2grust-analyzer cargo gg-alloc 1.0.0 tests/assert_above_2g().(s.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().() as *const _ as *mut _);
145 }
146 }