@@ -4,6 +4,10 @@ use crate::util::Address;
44use crate :: vm:: VMBinding ;
55
66/// Allocate with alignment. This also guarantees the memory is zero initialized.
7+ #[ cfg( all(
8+ not( target_os = "windows" ) ,
9+ not( any( feature = "malloc_jemalloc" , feature = "malloc_mimalloc" ) )
10+ ) ) ]
711pub fn align_alloc ( size : usize , align : usize ) -> Address {
812 let mut ptr = std:: ptr:: null_mut :: < libc:: c_void > ( ) ;
913 let ptr_ptr = std:: ptr:: addr_of_mut!( ptr) ;
@@ -71,28 +75,37 @@ pub fn alloc<VM: VMBinding>(size: usize, align: usize, offset: usize) -> (Addres
7175 let mut is_offset_malloc = false ;
7276 // malloc returns 16 bytes aligned address.
7377 // So if the alignment is smaller than 16 bytes, we do not need to align.
74- if align <= 16 && offset == 0 {
75- let raw = unsafe { calloc ( 1 , size) } ;
76- address = Address :: from_mut_ptr ( raw) ;
77- debug_assert ! ( address. is_aligned_to( align) ) ;
78- } else if align > 16 && offset == 0 {
79- address = align_alloc ( size, align) ;
80- debug_assert ! (
81- address. is_aligned_to( align) ,
82- "Address: {:x} is not aligned to the given alignment: {}" ,
83- address,
84- align
85- ) ;
86- } else {
87- address = align_offset_alloc :: < VM > ( size, align, offset) ;
88- is_offset_malloc = true ;
89- debug_assert ! (
90- ( address + offset) . is_aligned_to( align) ,
91- "Address: {:x} is not aligned to the given alignment: {} at offset: {}" ,
92- address,
93- align,
94- offset
95- ) ;
78+
79+ match ( align, offset) {
80+ ( a, 0 ) if a <= 16 => {
81+ let raw = unsafe { calloc ( 1 , size) } ;
82+ address = Address :: from_mut_ptr ( raw) ;
83+ debug_assert ! ( address. is_aligned_to( align) ) ;
84+ }
85+ #[ cfg( all(
86+ not( target_os = "windows" ) ,
87+ not( any( feature = "malloc_jemalloc" , feature = "malloc_mimalloc" ) )
88+ ) ) ]
89+ ( a, 0 ) if a > 16 => {
90+ address = align_alloc ( size, align) ;
91+ debug_assert ! (
92+ address. is_aligned_to( align) ,
93+ "Address: {:x} is not aligned to the given alignment: {}" ,
94+ address,
95+ align
96+ ) ;
97+ }
98+ _ => {
99+ address = align_offset_alloc :: < VM > ( size, align, offset) ;
100+ is_offset_malloc = true ;
101+ debug_assert ! (
102+ ( address + offset) . is_aligned_to( align) ,
103+ "Address: {:x} is not aligned to the given alignment: {} at offset: {}" ,
104+ address,
105+ align,
106+ offset
107+ ) ;
108+ }
96109 }
97110 ( address, is_offset_malloc)
98111}
0 commit comments