1
1
use std:: {
2
- alloc:: { Layout , alloc, dealloc, realloc} ,
2
+ alloc:: { Layout , alloc, alloc_zeroed , dealloc, realloc} ,
3
3
ptr:: null_mut,
4
4
sync:: OnceLock ,
5
5
} ;
@@ -21,7 +21,7 @@ fn get_size_map() -> &'static papaya::HashMap<usize, usize> {
21
21
unsafe { ALLOC_SIZES . get ( ) . unwrap_unchecked ( ) }
22
22
}
23
23
24
- unsafe extern "C-unwind" fn aligned_malloc ( size : ecs_size_t ) -> * mut core:: ffi:: c_void {
24
+ unsafe fn aligned_alloc ( size : ecs_size_t , custom_alloc : fn ( Layout ) -> * mut u8 ) -> * mut core:: ffi:: c_void {
25
25
#[ allow( clippy:: cast_possible_wrap, clippy:: cast_sign_loss) ]
26
26
let size = size as usize ;
27
27
@@ -30,7 +30,7 @@ unsafe extern "C-unwind" fn aligned_malloc(size: ecs_size_t) -> *mut core::ffi::
30
30
return null_mut ( ) ;
31
31
} ;
32
32
33
- let ptr = unsafe { alloc ( layout) } ;
33
+ let ptr = unsafe { custom_alloc ( layout) } ;
34
34
35
35
if ptr. is_null ( ) {
36
36
return null_mut ( ) ;
@@ -42,17 +42,12 @@ unsafe extern "C-unwind" fn aligned_malloc(size: ecs_size_t) -> *mut core::ffi::
42
42
ptr. cast :: < core:: ffi:: c_void > ( )
43
43
}
44
44
45
- unsafe extern "C-unwind" fn aligned_calloc ( size : ecs_size_t ) -> * mut core:: ffi:: c_void {
46
- let ptr = unsafe { aligned_malloc ( size) } ;
47
- if !ptr. is_null ( ) {
48
- // Zero the entire allocation
49
- #[ allow( clippy:: cast_possible_wrap, clippy:: cast_sign_loss) ]
50
- unsafe {
51
- std:: ptr:: write_bytes ( ptr, 0 , size as usize ) ;
52
- } ;
53
- }
45
+ unsafe extern "C-unwind" fn aligned_malloc ( size : ecs_size_t ) -> * mut core:: ffi:: c_void {
46
+ aligned_alloc ( size, alloc)
47
+ }
54
48
55
- ptr
49
+ unsafe extern "C-unwind" fn aligned_calloc ( size : ecs_size_t ) -> * mut core:: ffi:: c_void {
50
+ aligned_alloc ( size, alloc_zeroed)
56
51
}
57
52
58
53
unsafe extern "C-unwind" fn aligned_realloc (
0 commit comments