| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | #![stable(feature = "alloc_module", since = "1.28.0")]
|
| 6 |
|
| 7 | #[cfg(not(test))]
|
| 8 | use core::intrinsics;
|
| 9 |
|
| 10 | #[cfg(not(test))]
|
| 11 | use core::ptr::{self, NonNull};
|
| 12 |
|
| 13 | #[stable(feature = "alloc_module", since = "1.28.0")]
|
| 14 | #[doc(inline)]
|
| 15 | pub use core::alloc::*;
|
| 16 |
|
| 17 | #[cfg(test)]
|
| 18 | mod tests;
|
| 19 |
|
| 20 | extern "Rust" {
|
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | #[rustc_allocator]
|
| 29 | #[rustc_nounwind]
|
| 30 | fn __rust_alloc(size: usize, align: usize) -> *mut u8;
|
| 31 | #[rustc_deallocator]
|
| 32 | #[rustc_nounwind]
|
| 33 | fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
|
| 34 | #[rustc_reallocator]
|
| 35 | #[rustc_nounwind]
|
| 36 | fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
|
| 37 | #[rustc_allocator_zeroed]
|
| 38 | #[rustc_nounwind]
|
| 39 | fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
|
| 40 |
|
| 41 | static __rust_no_alloc_shim_is_unstable: u8;
|
| 42 | }
|
| 43 |
|
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 53 | #[derive(Copy, Clone, Default, Debug)]
|
| 54 | #[cfg(not(test))]
|
| 55 | pub struct Global;
|
| 56 |
|
| 57 | #[cfg(test)]
|
| 58 | pub use std::alloc::Global;
|
| 59 |
|
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | #[stable(feature = "global_alloc", since = "1.28.0")]
|
| 92 | #[must_use = "losing the pointer will leak memory"]
|
| 93 | #[inline]
|
| 94 | pub unsafe fn alloc(layout: Layout) -> *mut u8 {
|
| 95 | unsafe {
|
| 96 | |
| 97 | |
| 98 | core::ptr::read_volatile(&__rust_no_alloc_shim_is_unstable);
|
| 99 |
|
| 100 | __rust_alloc(layout.size(), layout.align())
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | #[stable(feature = "global_alloc", since = "1.28.0")]
|
| 117 | #[inline]
|
| 118 | pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
|
| 119 | unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
|
| 120 | }
|
| 121 |
|
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | #[stable(feature = "global_alloc", since = "1.28.0")]
|
| 135 | #[must_use = "losing the pointer will leak memory"]
|
| 136 | #[inline]
|
| 137 | pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
| 138 | unsafe { __rust_realloc(ptr, layout.size(), layout.align(), new_size) }
|
| 139 | }
|
| 140 |
|
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | #[stable(feature = "global_alloc", since = "1.28.0")]
|
| 169 | #[must_use = "losing the pointer will leak memory"]
|
| 170 | #[inline]
|
| 171 | pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
|
| 172 | unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) }
|
| 173 | }
|
| 174 |
|
| 175 | #[cfg(not(test))]
|
| 176 | impl Global {
|
| 177 | #[inline]
|
| 178 | fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
|
| 179 | match layout.size() {
|
| 180 | 0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
|
| 181 | |
| 182 | size => unsafe {
|
| 183 | let raw_ptr = if zeroed { alloc_zeroed(layout) } else { alloc(layout) };
|
| 184 | let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
|
| 185 | Ok(NonNull::slice_from_raw_parts(ptr, size))
|
| 186 | },
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | |
| 191 | #[inline]
|
| 192 | unsafe fn grow_impl(
|
| 193 | &self,
|
| 194 | ptr: NonNull<u8>,
|
| 195 | old_layout: Layout,
|
| 196 | new_layout: Layout,
|
| 197 | zeroed: bool,
|
| 198 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 199 | debug_assert!(
|
| 200 | new_layout.size() >= old_layout.size(),
|
| 201 | "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
|
| 202 | );
|
| 203 |
|
| 204 | match old_layout.size() {
|
| 205 | 0 => self.alloc_impl(new_layout, zeroed),
|
| 206 |
|
| 207 | |
| 208 | |
| 209 | old_size if old_layout.align() == new_layout.align() => unsafe {
|
| 210 | let new_size = new_layout.size();
|
| 211 |
|
| 212 | |
| 213 | intrinsics::assume(new_size >= old_layout.size());
|
| 214 |
|
| 215 | let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
|
| 216 | let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
|
| 217 | if zeroed {
|
| 218 | raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
|
| 219 | }
|
| 220 | Ok(NonNull::slice_from_raw_parts(ptr, new_size))
|
| 221 | },
|
| 222 |
|
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | old_size => unsafe {
|
| 229 | let new_ptr = self.alloc_impl(new_layout, zeroed)?;
|
| 230 | ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
|
| 231 | self.deallocate(ptr, old_layout);
|
| 232 | Ok(new_ptr)
|
| 233 | },
|
| 234 | }
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | #[unstable(feature = "allocator_api", issue = "32838")]
|
| 239 | #[cfg(not(test))]
|
| 240 | unsafe impl Allocator for Global {
|
| 241 | #[inline]
|
| 242 | fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
|
| 243 | self.alloc_impl(layout, false)
|
| 244 | }
|
| 245 |
|
| 246 | #[inline]
|
| 247 | fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
|
| 248 | self.alloc_impl(layout, true)
|
| 249 | }
|
| 250 |
|
| 251 | #[inline]
|
| 252 | unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
|
| 253 | if layout.size() != 0 {
|
| 254 | |
| 255 | |
| 256 | unsafe { dealloc(ptr.as_ptr(), layout) }
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | #[inline]
|
| 261 | unsafe fn grow(
|
| 262 | &self,
|
| 263 | ptr: NonNull<u8>,
|
| 264 | old_layout: Layout,
|
| 265 | new_layout: Layout,
|
| 266 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 267 | |
| 268 | unsafe { self.grow_impl(ptr, old_layout, new_layout, false) }
|
| 269 | }
|
| 270 |
|
| 271 | #[inline]
|
| 272 | unsafe fn grow_zeroed(
|
| 273 | &self,
|
| 274 | ptr: NonNull<u8>,
|
| 275 | old_layout: Layout,
|
| 276 | new_layout: Layout,
|
| 277 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 278 | |
| 279 | unsafe { self.grow_impl(ptr, old_layout, new_layout, true) }
|
| 280 | }
|
| 281 |
|
| 282 | #[inline]
|
| 283 | unsafe fn shrink(
|
| 284 | &self,
|
| 285 | ptr: NonNull<u8>,
|
| 286 | old_layout: Layout,
|
| 287 | new_layout: Layout,
|
| 288 | ) -> Result<NonNull<[u8]>, AllocError> {
|
| 289 | debug_assert!(
|
| 290 | new_layout.size() <= old_layout.size(),
|
| 291 | "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
|
| 292 | );
|
| 293 |
|
| 294 | match new_layout.size() {
|
| 295 | |
| 296 | 0 => unsafe {
|
| 297 | self.deallocate(ptr, old_layout);
|
| 298 | Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
|
| 299 | },
|
| 300 |
|
| 301 | |
| 302 | new_size if old_layout.align() == new_layout.align() => unsafe {
|
| 303 | |
| 304 | intrinsics::assume(new_size <= old_layout.size());
|
| 305 |
|
| 306 | let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
|
| 307 | let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
|
| 308 | Ok(NonNull::slice_from_raw_parts(ptr, new_size))
|
| 309 | },
|
| 310 |
|
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | new_size => unsafe {
|
| 317 | let new_ptr = self.allocate(new_layout)?;
|
| 318 | ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
|
| 319 | self.deallocate(ptr, old_layout);
|
| 320 | Ok(new_ptr)
|
| 321 | },
|
| 322 | }
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | |
| 327 | #[cfg(all(not(no_global_oom_handling), not(test)))]
|
| 328 | #[lang = "exchange_malloc"]
|
| 329 | #[inline]
|
| 330 | unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
|
| 331 | let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
|
| 332 | match Global.allocate(layout) {
|
| 333 | Ok(ptr) => ptr.as_mut_ptr(),
|
| 334 | Err(_) => handle_alloc_error(layout),
|
| 335 | }
|
| 336 | }
|
| 337 |
|
| 338 | |
| 339 |
|
| 340 | #[cfg(not(no_global_oom_handling))]
|
| 341 | extern "Rust" {
|
| 342 | |
| 343 | |
| 344 | |
| 345 | fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
|
| 346 | }
|
| 347 |
|
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | #[stable(feature = "global_alloc", since = "1.28.0")]
|
| 361 | #[rustc_const_unstable(feature = "const_alloc_error", issue = "92523")]
|
| 362 | #[cfg(all(not(no_global_oom_handling), not(test)))]
|
| 363 | #[cold]
|
| 364 | pub const fn handle_alloc_error(layout: Layout) -> ! {
|
| 365 | const fn ct_error(_: Layout) -> ! {
|
| 366 | panic!("allocation failed");
|
| 367 | }
|
| 368 |
|
| 369 | fn rt_error(layout: Layout) -> ! {
|
| 370 | unsafe {
|
| 371 | __rust_alloc_error_handler(layout.size(), layout.align());
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | unsafe { core::intrinsics::const_eval_select((layout,), ct_error, rt_error) }
|
| 376 | }
|
| 377 |
|
| 378 | |
| 379 | #[cfg(all(not(no_global_oom_handling), test))]
|
| 380 | pub use std::alloc::handle_alloc_error;
|
| 381 |
|
| 382 | #[cfg(all(not(no_global_oom_handling), not(test)))]
|
| 383 | #[doc(hidden)]
|
| 384 | #[allow(unused_attributes)]
|
| 385 | #[unstable(feature = "alloc_internals", issue = "none")]
|
| 386 | pub mod __alloc_error_handler {
|
| 387 | |
| 388 | |
| 389 | #[rustc_std_internal_symbol]
|
| 390 | pub unsafe fn __rdl_oom(size: usize, _align: usize) -> ! {
|
| 391 | extern "Rust" {
|
| 392 | |
| 393 | |
| 394 | static __rust_alloc_error_handler_should_panic: u8;
|
| 395 | }
|
| 396 |
|
| 397 | if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
|
| 398 | panic!("memory allocation of {size} bytes failed")
|
| 399 | } else {
|
| 400 | core::panicking::panic_nounwind_fmt(format_args!(
|
| 401 | "memory allocation of {size} bytes failed"
|
| 402 | ))
|
| 403 | }
|
| 404 | }
|
| 405 | }
|
| 406 |
|
| 407 | |
| 408 | |
| 409 | pub(crate) trait WriteCloneIntoRaw: Sized {
|
| 410 | unsafe fn write_clone_into_raw(&self, target: *mut Self);
|
| 411 | }
|
| 412 |
|
| 413 | impl<T: Clone> WriteCloneIntoRaw for T {
|
| 414 | #[inline]
|
| 415 | default unsafe fn write_clone_into_raw(&self, target: *mut Self) {
|
| 416 | |
| 417 | |
| 418 | unsafe { target.write(self.clone()) };
|
| 419 | }
|
| 420 | }
|
| 421 |
|
| 422 | impl<T: Copy> WriteCloneIntoRaw for T {
|
| 423 | #[inline]
|
| 424 | unsafe fn write_clone_into_raw(&self, target: *mut Self) {
|
| 425 | |
| 426 | unsafe { target.copy_from_nonoverlapping(self, 1) };
|
| 427 | }
|
| 428 | }
|