about summary refs log tree commit diff
path: root/src/libstd/sys/unix
diff options
context:
space:
mode:
authorTatsuyuki Ishi <ishitatsuyuki@gmail.com>2018-03-20 21:22:45 +0900
committerTatsuyuki Ishi <ishitatsuyuki@gmail.com>2018-03-20 22:53:38 +0900
commit2928c7a8a253b655132ac9f2beb4ca74540f0e14 (patch)
tree7a524ed0c446074a400e7e0db952988aa9bbfcaa /src/libstd/sys/unix
parent8c8a72f8224b9cbf283cd120d87397c032ed62f1 (diff)
downloadrust-2928c7a8a253b655132ac9f2beb4ca74540f0e14.tar.gz
rust-2928c7a8a253b655132ac9f2beb4ca74540f0e14.zip
Refactor the stack addr aligning code into a function
Diffstat (limited to 'src/libstd/sys/unix')
-rw-r--r--src/libstd/sys/unix/thread.rs32
1 files changed, 16 insertions, 16 deletions
diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs
index d94e11a5207..4364089a181 100644
--- a/src/libstd/sys/unix/thread.rs
+++ b/src/libstd/sys/unix/thread.rs
@@ -284,10 +284,10 @@ pub mod guard {
         ret
     }
 
-    pub unsafe fn init() -> Option<Guard> {
-        PAGE_SIZE = os::page_size();
-
-        let mut stackaddr = get_stack_start()?;
+    // Precondition: PAGE_SIZE is initialized.
+    unsafe fn get_stack_start_aligned() -> Option<*mut libc::c_void> {
+        assert!(PAGE_SIZE != 0);
+        let stackaddr = get_stack_start()?;
 
         // Ensure stackaddr is page aligned! A parent process might
         // have reset RLIMIT_STACK to be non-page aligned. The
@@ -296,10 +296,17 @@ pub mod guard {
         // page-aligned, calculate the fix such that stackaddr <
         // new_page_aligned_stackaddr < stackaddr + stacksize
         let remainder = (stackaddr as usize) % PAGE_SIZE;
-        if remainder != 0 {
-            stackaddr = ((stackaddr as usize) + PAGE_SIZE - remainder)
-                as *mut libc::c_void;
-        }
+        Some(if remainder == 0 {
+            stackaddr
+        } else {
+            ((stackaddr as usize) + PAGE_SIZE - remainder) as *mut libc::c_void
+        })
+    }
+
+    pub unsafe fn init() -> Option<Guard> {
+        PAGE_SIZE = os::page_size();
+
+        let stackaddr = get_stack_start_aligned()?;
 
         if cfg!(target_os = "linux") {
             // Linux doesn't allocate the whole stack right away, and
@@ -338,14 +345,7 @@ pub mod guard {
 
     pub unsafe fn deinit() {
         if !cfg!(target_os = "linux") {
-            if let Some(mut stackaddr) = get_stack_start() {
-                // Ensure address is aligned. Same as above.
-                let remainder = (stackaddr as usize) % PAGE_SIZE;
-                if remainder != 0 {
-                    stackaddr = ((stackaddr as usize) + PAGE_SIZE - remainder)
-                        as *mut libc::c_void;
-                }
-
+            if let Some(stackaddr) = get_stack_start_aligned() {
                 // Undo the guard page mapping.
                 if munmap(stackaddr, PAGE_SIZE) != 0 {
                     panic!("unable to deallocate the guard page");