1
- // Copyright 2017 the authors. See the 'Copyright and license' section of the
1
+ // Copyright 2017-2018 the authors. See the 'Copyright and license' section of the
2
2
// README.md file at the top-level directory of this repository.
3
3
//
4
4
// Licensed under the Apache License, Version 2.0 (the LICENSE-APACHE file) or
@@ -25,6 +25,8 @@ use super::general::global;
25
25
use std:: mem;
26
26
#[ cfg( feature = "c-api" ) ]
27
27
use std:: intrinsics:: unlikely;
28
+ #[ cfg( feature = "c-api" ) ]
29
+ use std:: ptr;
28
30
29
31
#[ cfg( feature = "c-api" ) ]
30
32
use self :: libc:: { size_t, c_void} ;
@@ -39,25 +41,25 @@ unsafe impl<'a> Alloc for &'a ElfMallocGlobal {
39
41
// two up to 1MiB are aligned to their size. Past that size, only page-alignment is
40
42
// guaranteed.
41
43
if l. size ( ) . is_power_of_two ( ) || l. align ( ) <= mem:: size_of :: < usize > ( ) {
42
- Ok ( global:: alloc ( l. size ( ) ) )
44
+ global:: alloc ( l. size ( ) )
43
45
} else {
44
- Ok ( global:: alloc ( l. size ( ) . next_power_of_two ( ) ) )
45
- }
46
+ global:: alloc ( l. size ( ) . next_power_of_two ( ) )
47
+ } . ok_or ( AllocErr :: Exhausted { request : l } )
46
48
}
47
49
48
50
unsafe fn dealloc ( & mut self , p : * mut u8 , _l : Layout ) {
49
51
global:: free ( p) ;
50
52
}
51
53
52
54
unsafe fn realloc ( & mut self , p : * mut u8 , _l1 : Layout , l2 : Layout ) -> Result < * mut u8 , AllocErr > {
53
- Ok ( global:: aligned_realloc ( p, l2. size ( ) , l2. align ( ) ) )
55
+ global:: aligned_realloc ( p, l2. size ( ) , l2. align ( ) ) . ok_or ( AllocErr :: Exhausted { request : l2 } )
54
56
}
55
57
}
56
58
57
59
#[ cfg( feature = "c-api" ) ]
58
60
unsafe impl Malloc for ElfMallocGlobal {
59
61
unsafe fn c_malloc ( & self , size : size_t ) -> * mut c_void {
60
- let p = global:: alloc ( size as usize ) as * mut c_void ;
62
+ let p = global:: alloc ( size as usize ) . unwrap_or ( ptr :: null_mut ( ) ) as * mut c_void ;
61
63
alloc_debug_assert_eq ! ( ( p as usize ) % MIN_ALIGN ,
62
64
0 ,
63
65
"object does not have the required alignment of {}: {:?}" ,
@@ -82,7 +84,7 @@ unsafe impl Malloc for ElfMallocGlobal {
82
84
"object does not have the required alignment of {}: {:?}" ,
83
85
MIN_ALIGN ,
84
86
p) ;
85
- global:: realloc ( p as * mut u8 , new_size as usize ) as * mut c_void
87
+ global:: realloc ( p as * mut u8 , new_size as usize ) . unwrap_or ( ptr :: null_mut ( ) ) as * mut c_void
86
88
}
87
89
}
88
90
0 commit comments