Skip to content

Commit 7a81257

Browse files
committed
allocator::win32::IMalloc: fix ZST reallocs by always requesting at least one byte
relates to #32
1 parent 1c7a7c8 commit 7a81257

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/allocator/win32/imalloc.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ impl Meta for IMalloc {
7070
const MIN_ALIGN : Alignment = super::MEMORY_ALLOCATION_ALIGNMENT; // Verified through testing
7171
const MAX_ALIGN : Alignment = super::MEMORY_ALLOCATION_ALIGNMENT; // Verified through testing
7272
const MAX_SIZE : usize = usize::MAX;
73+
74+
/// - `IMalloc::Alloc(0)` allocates successfully.
75+
/// - `IMalloc::Realloc(ptr, 0)` **frees**.
76+
/// Note that [`thin::Realloc`] and [`fat::Realloc`] resolve this by always requesting at least 1 byte.
77+
/// - `IMalloc::GetSize(ptr)` will return inconsistent results for ZSTs as a result.
78+
///
7379
const ZST_SUPPORTED : bool = true;
7480
}
7581

@@ -120,7 +126,7 @@ unsafe impl thin::Realloc for IMalloc {
120126
const CAN_REALLOC_ZEROED : bool = false;
121127

122128
unsafe fn realloc_uninit(&self, ptr: AllocNN, new_size: usize) -> Result<AllocNN, Self::Error> {
123-
let alloc = unsafe { self.0.Realloc(ptr.as_ptr().cast(), new_size) };
129+
let alloc = unsafe { self.0.Realloc(ptr.as_ptr().cast(), new_size.max(1)) };
124130
NonNull::new(alloc.cast()).ok_or(())
125131
}
126132

@@ -187,11 +193,15 @@ unsafe impl thin::SizeOf for IMalloc {}
187193
#[test] fn thin_nullable() { thin::test::nullable(IMalloc::co_get_malloc_1().unwrap()) }
188194
#[test] fn thin_size() { thin::test::size_exact_alloc(IMalloc::co_get_malloc_1().unwrap()) }
189195
#[test] fn thin_uninit() { unsafe { thin::test::uninit_alloc_unsound(IMalloc::co_get_malloc_1().unwrap()) } }
196+
#[test] fn thin_uninit_realloc() { thin::test::uninit_realloc(IMalloc::co_get_malloc_1().unwrap()) }
190197
#[test] fn thin_zeroed() { thin::test::zeroed_alloc(IMalloc::co_get_malloc_1().unwrap()) }
198+
#[test] fn thin_zeroed_realloc() { thin::test::zeroed_realloc(IMalloc::co_get_malloc_1().unwrap()) }
191199
#[test] fn thin_zst_support() { thin::test::zst_supported_accurate(IMalloc::co_get_malloc_1().unwrap()) }
192200

193201
#[test] fn fat_alignment() { fat::test::alignment(IMalloc::co_get_malloc_1().unwrap()) }
194202
#[test] fn fat_edge_case_sizes() { fat::test::edge_case_sizes(IMalloc::co_get_malloc_1().unwrap()) }
195203
#[test] fn fat_uninit() { unsafe { fat::test::uninit_alloc_unsound(IMalloc::co_get_malloc_1().unwrap()) } }
204+
#[test] fn fat_uninit_realloc() { fat::test::uninit_realloc(IMalloc::co_get_malloc_1().unwrap()) }
196205
#[test] fn fat_zeroed() { fat::test::zeroed_alloc(IMalloc::co_get_malloc_1().unwrap()) }
206+
#[test] fn fat_zeroed_realloc() { fat::test::zeroed_realloc(IMalloc::co_get_malloc_1().unwrap()) }
197207
#[test] fn fat_zst_support() { fat::test::zst_supported_accurate(IMalloc::co_get_malloc_1().unwrap()) }

0 commit comments

Comments
 (0)