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