25
25
extern crate libc;
26
26
27
27
use core:: ptr;
28
- use core:: sync:: atomic:: { AtomicPtr , AtomicUsize , Ordering } ;
28
+ use core:: sync:: atomic:: { AtomicBool , Ordering } ;
29
29
30
30
// The minimum alignment guaranteed by the architecture. This value is used to
31
31
// add fast paths for low alignment values. In practice, the alignment is a
@@ -48,8 +48,9 @@ const MIN_ALIGN: usize = 16;
48
48
const CHUNK_SIZE : usize = 4096 * 16 ;
49
49
const CHUNK_ALIGN : usize = 4096 ;
50
50
51
- static HEAP : AtomicPtr < u8 > = AtomicPtr :: new ( ptr:: null_mut ( ) ) ;
52
- static HEAP_LEFT : AtomicUsize = AtomicUsize :: new ( 0 ) ;
51
+ static mut HEAP : * mut u8 = ptr:: null_mut ( ) ;
52
+ static mut HEAP_LEFT : usize = 0 ;
53
+ static HEAP_MUTEX : AtomicBool = AtomicBool :: new ( false ) ;
53
54
54
55
#[ no_mangle]
55
56
pub extern "C" fn __rust_allocate ( size : usize , align : usize ) -> * mut u8 {
@@ -61,16 +62,19 @@ pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
61
62
return imp:: allocate ( size, align) ;
62
63
}
63
64
64
- let heap = HEAP . load ( Ordering :: SeqCst ) ;
65
- let heap_left = HEAP_LEFT . load ( Ordering :: SeqCst ) ;
66
- if new_size < heap_left {
67
- HEAP_LEFT . store ( heap_left - new_size, Ordering :: SeqCst ) ;
68
- HEAP . store ( heap. offset ( new_size as isize ) , Ordering :: SeqCst ) ;
69
- return heap;
65
+ while HEAP_MUTEX . compare_and_swap ( false , true , Ordering :: SeqCst ) { }
66
+
67
+ if new_size < HEAP_LEFT {
68
+ let p = HEAP ;
69
+ HEAP = p. offset ( new_size as isize ) ;
70
+ HEAP_LEFT -= new_size;
71
+ HEAP_MUTEX . store ( false , Ordering :: SeqCst ) ;
72
+ return p;
70
73
} else {
71
- HEAP_LEFT . store ( CHUNK_SIZE - new_size, Ordering :: SeqCst ) ;
72
74
let p = imp:: allocate ( CHUNK_SIZE , CHUNK_ALIGN ) ;
73
- HEAP . store ( p. offset ( new_size as isize ) , Ordering :: SeqCst ) ;
75
+ HEAP = p. offset ( new_size as isize ) ;
76
+ HEAP_LEFT = CHUNK_SIZE - new_size;
77
+ HEAP_MUTEX . store ( false , Ordering :: SeqCst ) ;
74
78
return p;
75
79
}
76
80
}
0 commit comments