@@ -14,7 +14,7 @@ use c_malloc = libc::malloc;
14
14
use c_free = libc:: free;
15
15
use managed:: raw:: { BoxHeaderRepr , BoxRepr } ;
16
16
use cast:: transmute;
17
- use unstable:: intrinsics:: { atomic_xadd, atomic_xsub} ;
17
+ use unstable:: intrinsics:: { atomic_xadd, atomic_xsub, atomic_load } ;
18
18
use ptr:: null;
19
19
use intrinsic:: TyDesc ;
20
20
@@ -34,8 +34,7 @@ pub unsafe fn malloc(td: *TypeDesc, size: uint) -> *c_void {
34
34
box. header . prev = null ( ) ;
35
35
box. header . next = null ( ) ;
36
36
37
- let exchange_count = & mut * exchange_count_ptr ( ) ;
38
- atomic_xadd ( exchange_count, 1 ) ;
37
+ inc_count ( ) ;
39
38
40
39
return transmute ( box) ;
41
40
}
@@ -48,21 +47,47 @@ pub unsafe fn malloc_raw(size: uint) -> *c_void {
48
47
if p. is_null ( ) {
49
48
fail ! ( "Failure in malloc_raw: result ptr is null" ) ;
50
49
}
50
+ inc_count ( ) ;
51
51
p
52
52
}
53
53
54
54
pub unsafe fn free ( ptr : * c_void ) {
55
- let exchange_count = & mut * exchange_count_ptr ( ) ;
56
- atomic_xsub ( exchange_count, 1 ) ;
57
-
58
55
assert ! ( ptr. is_not_null( ) ) ;
56
+ dec_count ( ) ;
59
57
c_free ( ptr) ;
60
58
}
61
59
///Thin wrapper around libc::free, as with exchange_alloc::malloc_raw
62
60
pub unsafe fn free_raw ( ptr : * c_void ) {
61
+ assert ! ( ptr. is_not_null( ) ) ;
62
+ dec_count ( ) ;
63
63
c_free ( ptr) ;
64
64
}
65
65
66
+ fn inc_count ( ) {
67
+ unsafe {
68
+ let exchange_count = & mut * exchange_count_ptr ( ) ;
69
+ atomic_xadd ( exchange_count, 1 ) ;
70
+ }
71
+ }
72
+
73
+ fn dec_count ( ) {
74
+ unsafe {
75
+ let exchange_count = & mut * exchange_count_ptr ( ) ;
76
+ atomic_xsub ( exchange_count, 1 ) ;
77
+ }
78
+ }
79
+
80
+ pub fn cleanup ( ) {
81
+ unsafe {
82
+ let count_ptr = exchange_count_ptr ( ) ;
83
+ let allocations = atomic_load ( & * count_ptr) ;
84
+ if allocations != 0 {
85
+ abort ! ( "exchange heap not empty on exit\
86
+ %i dangling allocations", allocations) ;
87
+ }
88
+ }
89
+ }
90
+
66
91
fn get_box_size ( body_size : uint , body_align : uint ) -> uint {
67
92
let header_size = size_of :: < BoxHeaderRepr > ( ) ;
68
93
// FIXME (#2699): This alignment calculation is suspicious. Is it right?
0 commit comments