88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11+ use std:: ptr;
1112use std:: sync:: atomics;
1213use std:: os:: { errno, page_size, MemoryMap , MapReadable , MapWritable ,
13- MapNonStandardFlags , MapVirtual , getenv} ;
14+ MapNonStandardFlags , getenv} ;
1415use libc;
1516
1617/// A task's stack. The name "Stack" is a vestige of segmented stacks.
1718pub struct Stack {
18- buf : MemoryMap ,
19+ buf : Option < MemoryMap > ,
1920 min_size : uint ,
2021 valgrind_id : libc:: c_uint ,
2122}
@@ -52,11 +53,11 @@ impl Stack {
5253 // guaranteed to be aligned properly.
5354 if !protect_last_page ( & stack) {
5455 fail ! ( "Could not memory-protect guard page. stack={}, errno={}" ,
55- stack. data, errno( ) ) ;
56+ stack. data( ) , errno( ) ) ;
5657 }
5758
5859 let mut stk = Stack {
59- buf : stack,
60+ buf : Some ( stack) ,
6061 min_size : size,
6162 valgrind_id : 0
6263 } ;
@@ -71,22 +72,23 @@ impl Stack {
7172 /// Create a 0-length stack which starts (and ends) at 0.
7273 pub unsafe fn dummy_stack ( ) -> Stack {
7374 Stack {
74- buf : MemoryMap { data : 0 as * mut u8 , len : 0 , kind : MapVirtual } ,
75+ buf : None ,
7576 min_size : 0 ,
7677 valgrind_id : 0
7778 }
7879 }
7980
8081 /// Point to the low end of the allocated stack
8182 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 ( ) )
8385 }
8486
8587 /// Point one uint beyond the high end of the allocated stack
8688 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 ( ) )
9092 }
9193}
9294
@@ -96,7 +98,7 @@ fn protect_last_page(stack: &MemoryMap) -> bool {
9698 // This may seem backwards: the start of the segment is the last page?
9799 // Yes! The stack grows from higher addresses (the end of the allocated
98100 // 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 ;
100102 libc:: mprotect ( last_page, page_size ( ) as libc:: size_t ,
101103 libc:: PROT_NONE ) != -1
102104 }
@@ -106,7 +108,7 @@ fn protect_last_page(stack: &MemoryMap) -> bool {
106108fn protect_last_page ( stack : & MemoryMap ) -> bool {
107109 unsafe {
108110 // see above
109- let last_page = stack. data as * mut libc:: c_void ;
111+ let last_page = stack. data ( ) as * mut libc:: c_void ;
110112 let mut old_prot: libc:: DWORD = 0 ;
111113 libc:: VirtualProtect ( last_page, page_size ( ) as libc:: SIZE_T ,
112114 libc:: PAGE_NOACCESS ,
0 commit comments