Skip to content

Commit

Permalink
Change get_header to use raw pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
carbotaniuman committed Oct 18, 2020
1 parent ef8baea commit ff27e01
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions library/std/src/sys/windows/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::alloc::{GlobalAlloc, Layout, System};
use crate::ptr;
use crate::sys::c;
use crate::sys_common::alloc::{realloc_fallback, MIN_ALIGN};

Expand All @@ -10,7 +11,7 @@ struct Header(*mut u8);
/// # Safety
///
/// There must be a `Header` at `ptr.offset(-1)`.
unsafe fn get_header<'a>(ptr: *mut u8) -> &'a mut Header {
unsafe fn get_header<'a>(ptr: *mut u8) -> *mut Header {
// SAFETY: the safety contract must be upheld by the caller
unsafe { &mut *(ptr as *mut Header).offset(-1) }
}
Expand All @@ -22,7 +23,7 @@ unsafe fn align_ptr(ptr: *mut u8, align: usize) -> *mut u8 {
// SAFETY: the safety contract must be upheld by the caller
unsafe {
let aligned = ptr.add(align - (ptr as usize & (align - 1)));
*get_header(aligned) = Header(ptr);
ptr::write(get_header(aligned), Header(ptr));
aligned
}
}
Expand Down Expand Up @@ -74,7 +75,7 @@ unsafe impl GlobalAlloc for System {
c::HeapFree(c::GetProcessHeap(), 0, ptr as c::LPVOID)
} else {
let header = get_header(ptr);
c::HeapFree(c::GetProcessHeap(), 0, header.0 as c::LPVOID)
c::HeapFree(c::GetProcessHeap(), 0, (*header).0 as c::LPVOID)
}
};
// SAFETY: `c::GetLastError()` cannot fail
Expand Down

0 comments on commit ff27e01

Please sign in to comment.