Skip to content

std: Move MemoryMap fields to methods #15588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/libgreen/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::ptr;
use std::sync::atomics;
use std::os::{errno, page_size, MemoryMap, MapReadable, MapWritable,
MapNonStandardFlags, MapVirtual, getenv};
MapNonStandardFlags, getenv};
use libc;

/// A task's stack. The name "Stack" is a vestige of segmented stacks.
pub struct Stack {
buf: MemoryMap,
buf: Option<MemoryMap>,
min_size: uint,
valgrind_id: libc::c_uint,
}
Expand Down Expand Up @@ -52,11 +53,11 @@ impl Stack {
// guaranteed to be aligned properly.
if !protect_last_page(&stack) {
fail!("Could not memory-protect guard page. stack={}, errno={}",
stack.data, errno());
stack.data(), errno());
}

let mut stk = Stack {
buf: stack,
buf: Some(stack),
min_size: size,
valgrind_id: 0
};
Expand All @@ -71,22 +72,23 @@ impl Stack {
/// Create a 0-length stack which starts (and ends) at 0.
pub unsafe fn dummy_stack() -> Stack {
Stack {
buf: MemoryMap { data: 0 as *mut u8, len: 0, kind: MapVirtual },
buf: None,
min_size: 0,
valgrind_id: 0
}
}

/// Point to the low end of the allocated stack
pub fn start(&self) -> *const uint {
self.buf.data as *const uint
self.buf.as_ref().map(|m| m.data() as *const uint)
.unwrap_or(ptr::null())
}

/// Point one uint beyond the high end of the allocated stack
pub fn end(&self) -> *const uint {
unsafe {
self.buf.data.offset(self.buf.len as int) as *const uint
}
self.buf.as_ref().map(|buf| unsafe {
buf.data().offset(buf.len() as int) as *const uint
}).unwrap_or(ptr::null())
}
}

Expand All @@ -96,7 +98,7 @@ fn protect_last_page(stack: &MemoryMap) -> bool {
// This may seem backwards: the start of the segment is the last page?
// Yes! The stack grows from higher addresses (the end of the allocated
// block) to lower addresses (the start of the allocated block).
let last_page = stack.data as *mut libc::c_void;
let last_page = stack.data() as *mut libc::c_void;
libc::mprotect(last_page, page_size() as libc::size_t,
libc::PROT_NONE) != -1
}
Expand All @@ -106,7 +108,7 @@ fn protect_last_page(stack: &MemoryMap) -> bool {
fn protect_last_page(stack: &MemoryMap) -> bool {
unsafe {
// see above
let last_page = stack.data as *mut libc::c_void;
let last_page = stack.data() as *mut libc::c_void;
let mut old_prot: libc::DWORD = 0;
libc::VirtualProtect(last_page, page_size() as libc::SIZE_T,
libc::PAGE_NOACCESS,
Expand Down
18 changes: 12 additions & 6 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,12 +1277,9 @@ pub fn page_size() -> uint {
/// The memory map is released (unmapped) when the destructor is run, so don't
/// let it leave scope by accident if you want it to stick around.
pub struct MemoryMap {
/// Pointer to the memory created or modified by this map.
pub data: *mut u8,
/// Number of bytes this map applies to
pub len: uint,
/// Type of mapping
pub kind: MemoryMapKind,
data: *mut u8,
len: uint,
kind: MemoryMapKind,
}

/// Type of memory map
Expand Down Expand Up @@ -1617,6 +1614,15 @@ impl Drop for MemoryMap {
}
}

impl MemoryMap {
/// Returns the pointer to the memory created or modified by this map.
pub fn data(&self) -> *mut u8 { self.data }
/// Returns the number of bytes this map applies to.
pub fn len(&self) -> uint { self.len }
/// Returns the type of mapping this represents.
pub fn kind(&self) -> MemoryMapKind { self.kind }
}

#[cfg(target_os = "linux")]
pub mod consts {
pub use os::arch_consts::ARCH;
Expand Down