Skip to content
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

core: Stabilize the mem module #14259

Merged
merged 1 commit into from
May 21, 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
4 changes: 2 additions & 2 deletions src/doc/guide-unsafe.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ impl<T: Send> Unique<T> {
// we *need* valid pointer.
assert!(!ptr.is_null());
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
// move_val_init moves a value into this memory without
// `overwrite` moves a value into this memory without
// attempting to drop the original value.
mem::move_val_init(&mut *ptr, value);
mem::overwrite(&mut *ptr, value);
Unique{ptr: ptr}
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/libarena/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use std::cmp;
use std::intrinsics::{TyDesc, get_tydesc};
use std::intrinsics;
use std::mem;
use std::mem::min_align_of;
use std::num;
use std::ptr::read;
use std::rc::Rc;
Expand Down Expand Up @@ -155,7 +154,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
}

// Find where the next tydesc lives
idx = round_up(start + size, mem::pref_align_of::<*TyDesc>());
idx = round_up(start + size, mem::align_of::<*TyDesc>());
}
}

Expand Down Expand Up @@ -207,9 +206,10 @@ impl Arena {
#[inline]
fn alloc_copy<'a, T>(&'a mut self, op: || -> T) -> &'a T {
unsafe {
let ptr = self.alloc_copy_inner(mem::size_of::<T>(), min_align_of::<T>());
let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
mem::min_align_of::<T>());
let ptr = ptr as *mut T;
mem::move_val_init(&mut (*ptr), op());
mem::overwrite(&mut (*ptr), op());
return &*ptr;
}
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl Arena {
return self.alloc_noncopy_grow(n_bytes, align);
}

self.head.fill.set(round_up(end, mem::pref_align_of::<*TyDesc>()));
self.head.fill.set(round_up(end, mem::align_of::<*TyDesc>()));

//debug!("idx = {}, size = {}, align = {}, fill = {}",
// start, n_bytes, align, head.fill);
Expand All @@ -254,14 +254,15 @@ impl Arena {
unsafe {
let tydesc = get_tydesc::<T>();
let (ty_ptr, ptr) =
self.alloc_noncopy_inner(mem::size_of::<T>(), min_align_of::<T>());
self.alloc_noncopy_inner(mem::size_of::<T>(),
mem::min_align_of::<T>());
let ty_ptr = ty_ptr as *mut uint;
let ptr = ptr as *mut T;
// Write in our tydesc along with a bit indicating that it
// has *not* been initialized yet.
*ty_ptr = mem::transmute(tydesc);
// Actually initialize it
mem::move_val_init(&mut(*ptr), op());
mem::overwrite(&mut(*ptr), op());
// Now that we are done, update the tydesc to indicate that
// the object is there.
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
Expand Down Expand Up @@ -357,9 +358,10 @@ impl<T> TypedArenaChunk<T> {
size = size.checked_add(&elems_size).unwrap();

let mut chunk = unsafe {
let chunk = exchange_malloc(size, min_align_of::<TypedArenaChunk<T>>());
let chunk = exchange_malloc(size,
mem::min_align_of::<TypedArenaChunk<T>>());
let mut chunk: Box<TypedArenaChunk<T>> = mem::transmute(chunk);
mem::move_val_init(&mut chunk.next, next);
mem::overwrite(&mut chunk.next, next);
chunk
};

Expand Down Expand Up @@ -396,7 +398,8 @@ impl<T> TypedArenaChunk<T> {
fn start(&self) -> *u8 {
let this: *TypedArenaChunk<T> = self;
unsafe {
mem::transmute(round_up(this.offset(1) as uint, min_align_of::<T>()))
mem::transmute(round_up(this.offset(1) as uint,
mem::min_align_of::<T>()))
}
}

Expand Down Expand Up @@ -440,7 +443,7 @@ impl<T> TypedArena<T> {
}

let ptr: &'a mut T = mem::transmute(this.ptr);
mem::move_val_init(ptr, object);
mem::overwrite(ptr, object);
this.ptr = this.ptr.offset(1);
let ptr: &'a T = ptr;
ptr
Expand Down
18 changes: 9 additions & 9 deletions src/libcollections/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#![allow(missing_doc)]

use std::clone::Clone;
use std::mem::{move_val_init, init, replace, swap};
use std::mem::{overwrite, zeroed, replace, swap};
use std::slice;

/// A priority queue implemented with a binary heap
Expand Down Expand Up @@ -157,40 +157,40 @@ impl<T: TotalOrd> PriorityQueue<T> {
// compared to using swaps, which involves twice as many moves.
fn siftup(&mut self, start: uint, mut pos: uint) {
unsafe {
let new = replace(self.data.get_mut(pos), init());
let new = replace(self.data.get_mut(pos), zeroed());

while pos > start {
let parent = (pos - 1) >> 1;
if new > *self.data.get(parent) {
let x = replace(self.data.get_mut(parent), init());
move_val_init(self.data.get_mut(pos), x);
let x = replace(self.data.get_mut(parent), zeroed());
overwrite(self.data.get_mut(pos), x);
pos = parent;
continue
}
break
}
move_val_init(self.data.get_mut(pos), new);
overwrite(self.data.get_mut(pos), new);
}
}

fn siftdown_range(&mut self, mut pos: uint, end: uint) {
unsafe {
let start = pos;
let new = replace(self.data.get_mut(pos), init());
let new = replace(self.data.get_mut(pos), zeroed());

let mut child = 2 * pos + 1;
while child < end {
let right = child + 1;
if right < end && !(*self.data.get(child) > *self.data.get(right)) {
child = right;
}
let x = replace(self.data.get_mut(child), init());
move_val_init(self.data.get_mut(pos), x);
let x = replace(self.data.get_mut(child), zeroed());
overwrite(self.data.get_mut(pos), x);
pos = child;
child = 2 * pos + 1;
}

move_val_init(self.data.get_mut(pos), new);
overwrite(self.data.get_mut(pos), new);
self.siftup(start, pos);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/libcollections/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//! Ordered containers with integer keys, implemented as radix tries (`TrieSet` and `TrieMap` types)

use std::mem::init;
use std::mem::zeroed;
use std::mem;
use std::slice::{Items, MutItems};
use std::slice;
Expand Down Expand Up @@ -522,7 +522,8 @@ macro_rules! iterator_impl {
remaining_max: 0,
length: 0,
// ick :( ... at least the compiler will tell us if we screwed up.
stack: [init(), init(), init(), init(), init(), init(), init(), init()]
stack: [zeroed(), zeroed(), zeroed(), zeroed(), zeroed(),
zeroed(), zeroed(), zeroed()]
}
}

Expand All @@ -532,8 +533,10 @@ macro_rules! iterator_impl {
remaining_min: 0,
remaining_max: 0,
length: 0,
stack: [init(), init(), init(), init(), init(), init(), init(), init(),
init(), init(), init(), init(), init(), init(), init(), init()]
stack: [zeroed(), zeroed(), zeroed(), zeroed(),
zeroed(), zeroed(), zeroed(), zeroed(),
zeroed(), zeroed(), zeroed(), zeroed(),
zeroed(), zeroed(), zeroed(), zeroed()]
}
}
}
Expand Down
Loading