8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use std:: ptr;
11
12
use std:: sync:: atomics;
12
13
use std:: os:: { errno, page_size, MemoryMap , MapReadable , MapWritable ,
13
- MapNonStandardFlags , MapVirtual , getenv} ;
14
+ MapNonStandardFlags , getenv} ;
14
15
use libc;
15
16
16
17
/// A task's stack. The name "Stack" is a vestige of segmented stacks.
17
18
pub struct Stack {
18
- buf : MemoryMap ,
19
+ buf : Option < MemoryMap > ,
19
20
min_size : uint ,
20
21
valgrind_id : libc:: c_uint ,
21
22
}
@@ -52,11 +53,11 @@ impl Stack {
52
53
// guaranteed to be aligned properly.
53
54
if !protect_last_page ( & stack) {
54
55
fail ! ( "Could not memory-protect guard page. stack={}, errno={}" ,
55
- stack. data, errno( ) ) ;
56
+ stack. data( ) , errno( ) ) ;
56
57
}
57
58
58
59
let mut stk = Stack {
59
- buf : stack,
60
+ buf : Some ( stack) ,
60
61
min_size : size,
61
62
valgrind_id : 0
62
63
} ;
@@ -71,22 +72,23 @@ impl Stack {
71
72
/// Create a 0-length stack which starts (and ends) at 0.
72
73
pub unsafe fn dummy_stack ( ) -> Stack {
73
74
Stack {
74
- buf : MemoryMap { data : 0 as * mut u8 , len : 0 , kind : MapVirtual } ,
75
+ buf : None ,
75
76
min_size : 0 ,
76
77
valgrind_id : 0
77
78
}
78
79
}
79
80
80
81
/// Point to the low end of the allocated stack
81
82
pub fn start ( & self ) -> * const uint {
82
- self . buf . data as * const uint
83
+ self . buf . as_ref ( ) . map ( |m| m. data ( ) as * const uint )
84
+ . unwrap_or ( ptr:: null ( ) )
83
85
}
84
86
85
87
/// Point one uint beyond the high end of the allocated stack
86
88
pub fn end ( & self ) -> * const uint {
87
- unsafe {
88
- self . buf . data . offset ( self . buf . len as int ) as * const uint
89
- }
89
+ self . buf . as_ref ( ) . map ( |buf| unsafe {
90
+ buf. data ( ) . offset ( buf. len ( ) as int ) as * const uint
91
+ } ) . unwrap_or ( ptr :: null ( ) )
90
92
}
91
93
}
92
94
@@ -96,7 +98,7 @@ fn protect_last_page(stack: &MemoryMap) -> bool {
96
98
// This may seem backwards: the start of the segment is the last page?
97
99
// Yes! The stack grows from higher addresses (the end of the allocated
98
100
// block) to lower addresses (the start of the allocated block).
99
- let last_page = stack. data as * mut libc:: c_void ;
101
+ let last_page = stack. data ( ) as * mut libc:: c_void ;
100
102
libc:: mprotect ( last_page, page_size ( ) as libc:: size_t ,
101
103
libc:: PROT_NONE ) != -1
102
104
}
@@ -106,7 +108,7 @@ fn protect_last_page(stack: &MemoryMap) -> bool {
106
108
fn protect_last_page ( stack : & MemoryMap ) -> bool {
107
109
unsafe {
108
110
// see above
109
- let last_page = stack. data as * mut libc:: c_void ;
111
+ let last_page = stack. data ( ) as * mut libc:: c_void ;
110
112
let mut old_prot: libc:: DWORD = 0 ;
111
113
libc:: VirtualProtect ( last_page, page_size ( ) as libc:: SIZE_T ,
112
114
libc:: PAGE_NOACCESS ,
0 commit comments