| 373 | |
| 374 | #[cold] |
| 375 | fn reallocate(&mut self, capacity: usize) { |
| 376 | let new_layout = Layout::from_size_align(capacity, self.layout.align()).unwrap(); |
| 377 | if new_layout.size() == 0 { |
| 378 | if self.layout.size() != 0 { |
| 379 | // Safety: data was allocated with layout |
| 380 | unsafe { std::alloc::dealloc(self.as_mut_ptr(), self.layout) }; |
| 381 | self.layout = new_layout |
| 382 | } |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | let data = match self.layout.size() { |
| 387 | // Safety: new_layout is not empty |
| 388 | 0 => unsafe { std::alloc::alloc(new_layout) }, |
| 389 | // Safety: verified new layout is valid and not empty |
| 390 | _ => unsafe { std::alloc::realloc(self.as_mut_ptr(), self.layout, capacity) }, |
| 391 | }; |
| 392 | self.data = NonNull::new(data).unwrap_or_else(|| handle_alloc_error(new_layout)); |
| 393 | self.layout = new_layout; |
| 394 | #[cfg(feature = "pool")] |
| 395 | { |
| 396 | if let Some(reservation) = self.reservation.lock().unwrap().as_mut() { |
| 397 | reservation.resize(self.layout.size()); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /// Truncates this buffer to `len` bytes |
| 403 | /// |