Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
fix(deque): correct layout size to dealloc
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Feb 16, 2021
1 parent 92f2b02 commit 97c8911
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions src/deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,19 +541,14 @@ impl<T> Drop for RawVec<T> {
/// Deallocates the underlying memory region by calculating the type layout
/// and number of elements.
///
/// This method only deallocates when containing actual sized elements.
///
/// Note that this only drop the memory block allocated by `RawVec` itself
/// without dropping the contents. Callers may need to drop the contents
/// by themselves.
/// This only drop the memory block allocated by `RawVec` itself but not
/// dropping the contents. Callers need to drop the contents by themselves.
fn drop(&mut self) {
let size = mem::size_of::<T>() * self.cap;
if size > 0 {
let align = mem::align_of::<T>();
let layout = Layout::from_size_align(size, align).unwrap();
let layout = Layout::array::<T>(self.cap).unwrap(); // 1
if layout.size() > 0 {
// This is safe because it conforms to the [safety contracts][1].
//
// [1] https://doc.rust-lang.org/1.49.0/alloc/alloc/trait.GlobalAlloc.html#safety-2
// [1]: https://doc.rust-lang.org/1.49.0/alloc/alloc/trait.GlobalAlloc.html#safety-2
unsafe { dealloc(self.ptr.cast(), layout) }
}
}
Expand Down

0 comments on commit 97c8911

Please sign in to comment.