Skip to content

Commit 80f9f4d

Browse files
authored
Store allocation size to re-create layout (#41)
1 parent 269c15c commit 80f9f4d

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/raw.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn invalid_data() -> ErrorCode {
4040
}
4141

4242
#[inline(always)]
43-
fn free_dc(data: HDC) {
43+
fn free_dc(data: HDC, _: usize) {
4444
unsafe {
4545
ReleaseDC(ptr::null_mut(), data);
4646
}
@@ -738,7 +738,7 @@ pub fn get_bitmap(out: &mut alloc::vec::Vec<u8>) -> SysResult<usize> {
738738
let img_size = header.bmiHeader.biSizeImage as usize;
739739
let out_before = out.len();
740740

741-
let dc = crate::utils::Scope(unsafe { GetDC(ptr::null_mut()) }, free_dc);
741+
let dc = crate::utils::Scope(unsafe { GetDC(ptr::null_mut()) }, free_dc, 0);
742742
let mut buffer = alloc::vec![0; img_size];
743743

744744
if unsafe { GetDIBits(dc.0, clipboard_data.as_ptr() as _, 0, bitmap.bmHeight as _, buffer.as_mut_ptr() as _, header_storage.get() as _, DIB_RGB_COLORS) } == 0 {
@@ -829,7 +829,7 @@ fn set_bitmap_inner(data: &[u8], clear: EmptyFn) -> SysResult<()> {
829829
return Err(ErrorCode::new_system(ERROR_INCORRECT_SIZE as _));
830830
}
831831

832-
let dc = crate::utils::Scope(unsafe { GetDC(ptr::null_mut()) }, free_dc);
832+
let dc = crate::utils::Scope(unsafe { GetDC(ptr::null_mut()) }, free_dc, 0);
833833

834834
let handle = unsafe {
835835
CreateDIBitmap(dc.0, &info_header as _, CBM_INIT, bitmap.as_ptr() as _, &info_header as *const _ as *const BITMAPINFO, DIB_RGB_COLORS)

src/utils.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,37 @@ pub fn unlikely_last_error() -> ErrorCode {
2222
}
2323

2424
#[inline]
25-
fn noop(_: *mut c_void) {
25+
fn noop(_: *mut c_void, _: usize) {
2626
}
2727

2828
#[inline]
29-
fn free_rust_mem(data: *mut c_void) {
29+
fn free_rust_mem(data: *mut c_void, size: usize) {
30+
let layout = alloc::alloc::Layout::array::<u8>(size).unwrap_or(BYTES_LAYOUT);
3031
unsafe {
31-
alloc::alloc::dealloc(data as _, BYTES_LAYOUT)
32+
alloc::alloc::dealloc(data as _, layout)
3233
}
3334
}
3435

3536
#[inline]
36-
fn unlock_data(data: *mut c_void) {
37+
fn unlock_data(data: *mut c_void, _: usize) {
3738
unsafe {
3839
sys::GlobalUnlock(data);
3940
}
4041
}
4142

4243
#[inline]
43-
fn free_global_mem(data: *mut c_void) {
44+
fn free_global_mem(data: *mut c_void, _: usize) {
4445
unsafe {
4546
sys::GlobalFree(data);
4647
}
4748
}
4849

49-
pub struct Scope<T: Copy>(pub T, pub fn(T));
50+
pub struct Scope<T: Copy>(pub T, pub fn(T, usize), pub usize);
5051

5152
impl<T: Copy> Drop for Scope<T> {
5253
#[inline(always)]
5354
fn drop(&mut self) {
54-
(self.1)(self.0)
55+
(self.1)(self.0, self.2)
5556
}
5657
}
5758

@@ -67,7 +68,7 @@ impl RawMem {
6768
if mem.is_null() {
6869
Err(unlikely_last_error())
6970
} else {
70-
Ok(Self(Scope(mem as _, free_rust_mem)))
71+
Ok(Self(Scope(mem as _, free_rust_mem, size)))
7172
}
7273
}
7374

@@ -78,21 +79,26 @@ impl RawMem {
7879
if mem.is_null() {
7980
Err(unlikely_last_error())
8081
} else {
81-
Ok(Self(Scope(mem, free_global_mem)))
82+
Ok(Self(Scope(mem, free_global_mem, size)))
8283
}
8384
}
8485
}
8586

8687
#[inline(always)]
8788
pub fn from_borrowed(ptr: ptr::NonNull<c_void>) -> Self {
88-
Self(Scope(ptr.as_ptr(), noop))
89+
Self(Scope(ptr.as_ptr(), noop, 0))
8990
}
9091

9192
#[inline(always)]
9293
pub fn get(&self) -> *mut c_void {
9394
(self.0).0
9495
}
9596

97+
#[inline(always)]
98+
pub fn size(&self) -> usize {
99+
(self.0).2
100+
}
101+
96102
#[inline(always)]
97103
pub fn release(self) {
98104
mem::forget(self)
@@ -104,7 +110,7 @@ impl RawMem {
104110
};
105111

106112
match ptr::NonNull::new(ptr) {
107-
Some(ptr) => Ok((ptr, Scope(self.get(), unlock_data))),
113+
Some(ptr) => Ok((ptr, Scope(self.get(), unlock_data, self.size()))),
108114
None => Err(ErrorCode::last_system()),
109115
}
110116
}

0 commit comments

Comments
 (0)