Skip to content

Commit 19dc3b5

Browse files
committed
core: Stabilize the mem module
Excluding the functions inherited from the cast module last week (with marked stability levels), these functions received the following treatment. * size_of - this method has become #[stable] * nonzero_size_of/nonzero_size_of_val - these methods have been removed * min_align_of - this method is now #[stable] * pref_align_of - this method has been renamed without the `pref_` prefix, and it is the "default alignment" now. This decision is in line with what clang does (see url linked in comment on function). This function is now #[stable]. * init - renamed to zeroed and marked #[stable] * uninit - marked #[stable] * move_val_init - renamed to overwrite and marked #[stable] * {from,to}_{be,le}{16,32,64} - all functions marked #[stable] * swap/replace/drop - marked #[stable] * size_of_val/min_align_of_val/align_of_val - these functions are marked #[unstable], but will continue to exist in some form. Concerns have been raised about their `_val` prefix. [breaking-change]
1 parent 1ba7bd1 commit 19dc3b5

File tree

19 files changed

+222
-137
lines changed

19 files changed

+222
-137
lines changed

src/doc/guide-unsafe.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ impl<T: Send> Unique<T> {
216216
// we *need* valid pointer.
217217
assert!(!ptr.is_null());
218218
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
219-
// move_val_init moves a value into this memory without
219+
// `overwrite` moves a value into this memory without
220220
// attempting to drop the original value.
221-
mem::move_val_init(&mut *ptr, value);
221+
mem::overwrite(&mut *ptr, value);
222222
Unique{ptr: ptr}
223223
}
224224
}

src/libarena/lib.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use std::cmp;
3535
use std::intrinsics::{TyDesc, get_tydesc};
3636
use std::intrinsics;
3737
use std::mem;
38-
use std::mem::min_align_of;
3938
use std::num;
4039
use std::ptr::read;
4140
use std::rc::Rc;
@@ -155,7 +154,7 @@ unsafe fn destroy_chunk(chunk: &Chunk) {
155154
}
156155

157156
// Find where the next tydesc lives
158-
idx = round_up(start + size, mem::pref_align_of::<*TyDesc>());
157+
idx = round_up(start + size, mem::align_of::<*TyDesc>());
159158
}
160159
}
161160

@@ -207,9 +206,10 @@ impl Arena {
207206
#[inline]
208207
fn alloc_copy<'a, T>(&'a mut self, op: || -> T) -> &'a T {
209208
unsafe {
210-
let ptr = self.alloc_copy_inner(mem::size_of::<T>(), min_align_of::<T>());
209+
let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
210+
mem::min_align_of::<T>());
211211
let ptr = ptr as *mut T;
212-
mem::move_val_init(&mut (*ptr), op());
212+
mem::overwrite(&mut (*ptr), op());
213213
return &*ptr;
214214
}
215215
}
@@ -239,7 +239,7 @@ impl Arena {
239239
return self.alloc_noncopy_grow(n_bytes, align);
240240
}
241241

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

244244
//debug!("idx = {}, size = {}, align = {}, fill = {}",
245245
// start, n_bytes, align, head.fill);
@@ -254,14 +254,15 @@ impl Arena {
254254
unsafe {
255255
let tydesc = get_tydesc::<T>();
256256
let (ty_ptr, ptr) =
257-
self.alloc_noncopy_inner(mem::size_of::<T>(), min_align_of::<T>());
257+
self.alloc_noncopy_inner(mem::size_of::<T>(),
258+
mem::min_align_of::<T>());
258259
let ty_ptr = ty_ptr as *mut uint;
259260
let ptr = ptr as *mut T;
260261
// Write in our tydesc along with a bit indicating that it
261262
// has *not* been initialized yet.
262263
*ty_ptr = mem::transmute(tydesc);
263264
// Actually initialize it
264-
mem::move_val_init(&mut(*ptr), op());
265+
mem::overwrite(&mut(*ptr), op());
265266
// Now that we are done, update the tydesc to indicate that
266267
// the object is there.
267268
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
@@ -357,9 +358,10 @@ impl<T> TypedArenaChunk<T> {
357358
size = size.checked_add(&elems_size).unwrap();
358359

359360
let mut chunk = unsafe {
360-
let chunk = exchange_malloc(size, min_align_of::<TypedArenaChunk<T>>());
361+
let chunk = exchange_malloc(size,
362+
mem::min_align_of::<TypedArenaChunk<T>>());
361363
let mut chunk: Box<TypedArenaChunk<T>> = mem::transmute(chunk);
362-
mem::move_val_init(&mut chunk.next, next);
364+
mem::overwrite(&mut chunk.next, next);
363365
chunk
364366
};
365367

@@ -396,7 +398,8 @@ impl<T> TypedArenaChunk<T> {
396398
fn start(&self) -> *u8 {
397399
let this: *TypedArenaChunk<T> = self;
398400
unsafe {
399-
mem::transmute(round_up(this.offset(1) as uint, min_align_of::<T>()))
401+
mem::transmute(round_up(this.offset(1) as uint,
402+
mem::min_align_of::<T>()))
400403
}
401404
}
402405

@@ -440,7 +443,7 @@ impl<T> TypedArena<T> {
440443
}
441444

442445
let ptr: &'a mut T = mem::transmute(this.ptr);
443-
mem::move_val_init(ptr, object);
446+
mem::overwrite(ptr, object);
444447
this.ptr = this.ptr.offset(1);
445448
let ptr: &'a T = ptr;
446449
ptr

src/libcollections/priority_queue.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#![allow(missing_doc)]
1414

1515
use std::clone::Clone;
16-
use std::mem::{move_val_init, init, replace, swap};
16+
use std::mem::{overwrite, zeroed, replace, swap};
1717
use std::slice;
1818

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

162162
while pos > start {
163163
let parent = (pos - 1) >> 1;
164164
if new > *self.data.get(parent) {
165-
let x = replace(self.data.get_mut(parent), init());
166-
move_val_init(self.data.get_mut(pos), x);
165+
let x = replace(self.data.get_mut(parent), zeroed());
166+
overwrite(self.data.get_mut(pos), x);
167167
pos = parent;
168168
continue
169169
}
170170
break
171171
}
172-
move_val_init(self.data.get_mut(pos), new);
172+
overwrite(self.data.get_mut(pos), new);
173173
}
174174
}
175175

176176
fn siftdown_range(&mut self, mut pos: uint, end: uint) {
177177
unsafe {
178178
let start = pos;
179-
let new = replace(self.data.get_mut(pos), init());
179+
let new = replace(self.data.get_mut(pos), zeroed());
180180

181181
let mut child = 2 * pos + 1;
182182
while child < end {
183183
let right = child + 1;
184184
if right < end && !(*self.data.get(child) > *self.data.get(right)) {
185185
child = right;
186186
}
187-
let x = replace(self.data.get_mut(child), init());
188-
move_val_init(self.data.get_mut(pos), x);
187+
let x = replace(self.data.get_mut(child), zeroed());
188+
overwrite(self.data.get_mut(pos), x);
189189
pos = child;
190190
child = 2 * pos + 1;
191191
}
192192

193-
move_val_init(self.data.get_mut(pos), new);
193+
overwrite(self.data.get_mut(pos), new);
194194
self.siftup(start, pos);
195195
}
196196
}

src/libcollections/trie.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Ordered containers with integer keys, implemented as radix tries (`TrieSet` and `TrieMap` types)
1212
13-
use std::mem::init;
13+
use std::mem::zeroed;
1414
use std::mem;
1515
use std::slice::{Items, MutItems};
1616
use std::slice;
@@ -522,7 +522,8 @@ macro_rules! iterator_impl {
522522
remaining_max: 0,
523523
length: 0,
524524
// ick :( ... at least the compiler will tell us if we screwed up.
525-
stack: [init(), init(), init(), init(), init(), init(), init(), init()]
525+
stack: [zeroed(), zeroed(), zeroed(), zeroed(), zeroed(),
526+
zeroed(), zeroed(), zeroed()]
526527
}
527528
}
528529

@@ -532,8 +533,10 @@ macro_rules! iterator_impl {
532533
remaining_min: 0,
533534
remaining_max: 0,
534535
length: 0,
535-
stack: [init(), init(), init(), init(), init(), init(), init(), init(),
536-
init(), init(), init(), init(), init(), init(), init(), init()]
536+
stack: [zeroed(), zeroed(), zeroed(), zeroed(),
537+
zeroed(), zeroed(), zeroed(), zeroed(),
538+
zeroed(), zeroed(), zeroed(), zeroed(),
539+
zeroed(), zeroed(), zeroed(), zeroed()]
537540
}
538541
}
539542
}

0 commit comments

Comments
 (0)