Skip to content

Commit 7762baa

Browse files
committed
auto merge of #12282 : cmr/rust/cleanup-ptr, r=huonw
2 parents a7aa4c4 + 254c155 commit 7762baa

26 files changed

+137
-219
lines changed

src/doc/guide-ffi.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<T: Send> Drop for Unique<T> {
229229
let x = mem::uninit(); // dummy value to swap in
230230
// We need to move the object out of the box, so that
231231
// the destructor is called (at the end of this scope.)
232-
ptr::replace_ptr(self.ptr, x);
232+
ptr::replace(self.ptr, x);
233233
free(self.ptr as *mut c_void)
234234
}
235235
}
@@ -306,7 +306,7 @@ which would call back to `callback()` in Rust.
306306
The former example showed how a global function can be called from C code.
307307
However it is often desired that the callback is targetted to a special
308308
Rust object. This could be the object that represents the wrapper for the
309-
respective C object.
309+
respective C object.
310310

311311
This can be achieved by passing an unsafe pointer to the object down to the
312312
C library. The C library can then include the pointer to the Rust object in
@@ -335,7 +335,7 @@ extern {
335335
fn main() {
336336
// Create the object that will be referenced in the callback
337337
let rust_object = ~RustObject{a: 5, ...};
338-
338+
339339
unsafe {
340340
// Gets a raw pointer to the object
341341
let target_addr:*RustObject = ptr::to_unsafe_ptr(rust_object);
@@ -380,8 +380,8 @@ Rust is to use channels (in `std::comm`) to forward data from the C thread
380380
that invoked the callback into a Rust task.
381381

382382
If an asychronous callback targets a special object in the Rust address space
383-
it is also absolutely necessary that no more callbacks are performed by the
384-
C library after the respective Rust object gets destroyed.
383+
it is also absolutely necessary that no more callbacks are performed by the
384+
C library after the respective Rust object gets destroyed.
385385
This can be achieved by unregistering the callback in the object's
386386
destructor and designing the library in a way that guarantees that no
387387
callback will be performed after unregistration.

src/libcollections/dlist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<T> Rawlink<T> {
8383

8484
/// Like Option::Some for Rawlink
8585
fn some(n: &mut T) -> Rawlink<T> {
86-
Rawlink{p: ptr::to_mut_unsafe_ptr(n)}
86+
Rawlink{p: n}
8787
}
8888

8989
/// Convert the `Rawlink` into an Option value

src/librustc/middle/ty.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use std::cell::{Cell, RefCell};
3333
use std::cmp;
3434
use std::hashmap::{HashMap, HashSet};
3535
use std::ops;
36-
use std::ptr::to_unsafe_ptr;
3736
use std::rc::Rc;
3837
use std::to_bytes;
3938
use std::to_str::ToStr;
@@ -1137,7 +1136,7 @@ pub fn mk_t(cx: ctxt, st: sty) -> t {
11371136
_ => {}
11381137
};
11391138

1140-
let key = intern_key { sty: to_unsafe_ptr(&st) };
1139+
let key = intern_key { sty: &st };
11411140

11421141
{
11431142
let mut interner = cx.interner.borrow_mut();
@@ -1234,7 +1233,7 @@ pub fn mk_t(cx: ctxt, st: sty) -> t {
12341233
flags: flags,
12351234
};
12361235

1237-
let sty_ptr = to_unsafe_ptr(&t.sty);
1236+
let sty_ptr = &t.sty as *sty;
12381237

12391238
let key = intern_key {
12401239
sty: sty_ptr,

src/libstd/fmt/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,17 @@ impl<T> Pointer for *T {
12001200
}
12011201
impl<T> Pointer for *mut T {
12021202
fn fmt(&self, f: &mut Formatter) -> Result {
1203-
secret_pointer(&(*self as *T), f)
1203+
secret_pointer::<*T>(&(*self as *T), f)
1204+
}
1205+
}
1206+
impl<'a, T> Pointer for &'a T {
1207+
fn fmt(&self, f: &mut Formatter) -> Result {
1208+
secret_pointer::<*T>(&(&**self as *T), f)
1209+
}
1210+
}
1211+
impl<'a, T> Pointer for &'a mut T {
1212+
fn fmt(&self, f: &mut Formatter) -> Result {
1213+
secret_pointer::<*T>(&(&**self as *T), f)
12041214
}
12051215
}
12061216

src/libstd/io/test.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,15 @@ mod darwin_fd_limit {
155155
pub unsafe fn raise_fd_limit() {
156156
// The strategy here is to fetch the current resource limits, read the kern.maxfilesperproc
157157
// sysctl value, and bump the soft resource limit for maxfiles up to the sysctl value.
158-
use ptr::{to_unsafe_ptr, to_mut_unsafe_ptr, mut_null};
158+
use ptr::mut_null;
159159
use mem::size_of_val;
160160
use os::last_os_error;
161161

162162
// Fetch the kern.maxfilesperproc value
163163
let mut mib: [libc::c_int, ..2] = [CTL_KERN, KERN_MAXFILESPERPROC];
164164
let mut maxfiles: libc::c_int = 0;
165165
let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
166-
if sysctl(to_mut_unsafe_ptr(&mut mib[0]), 2,
167-
to_mut_unsafe_ptr(&mut maxfiles) as *mut libc::c_void,
168-
to_mut_unsafe_ptr(&mut size),
166+
if sysctl(&mut mib[0], 2, &mut maxfiles as *mut libc::c_int as *mut libc::c_void, &mut size,
169167
mut_null(), 0) != 0 {
170168
let err = last_os_error();
171169
error!("raise_fd_limit: error calling sysctl: {}", err);
@@ -174,7 +172,7 @@ mod darwin_fd_limit {
174172

175173
// Fetch the current resource limits
176174
let mut rlim = rlimit{rlim_cur: 0, rlim_max: 0};
177-
if getrlimit(RLIMIT_NOFILE, to_mut_unsafe_ptr(&mut rlim)) != 0 {
175+
if getrlimit(RLIMIT_NOFILE, &mut rlim) != 0 {
178176
let err = last_os_error();
179177
error!("raise_fd_limit: error calling getrlimit: {}", err);
180178
return;
@@ -184,7 +182,7 @@ mod darwin_fd_limit {
184182
rlim.rlim_cur = ::cmp::min(maxfiles as rlim_t, rlim.rlim_max);
185183

186184
// Set our newly-increased resource limit
187-
if setrlimit(RLIMIT_NOFILE, to_unsafe_ptr(&rlim)) != 0 {
185+
if setrlimit(RLIMIT_NOFILE, &rlim) != 0 {
188186
let err = last_os_error();
189187
error!("raise_fd_limit: error calling setrlimit: {}", err);
190188
return;

src/libstd/managed.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
//! Operations on managed box types
1212
13-
use ptr::to_unsafe_ptr;
14-
1513
#[cfg(not(test))] use cmp::*;
1614

1715
/// Returns the refcount of a shared box (as just before calling this)
@@ -24,8 +22,7 @@ pub fn refcount<T>(t: @T) -> uint {
2422
/// Determine if two shared boxes point to the same object
2523
#[inline]
2624
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
27-
let (a_ptr, b_ptr): (*T, *T) = (to_unsafe_ptr(&*a), to_unsafe_ptr(&*b));
28-
a_ptr == b_ptr
25+
&*a as *T == &*b as *T
2926
}
3027

3128
#[cfg(not(test))]

src/libstd/ptr.rs

+36-74
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) {
102102

103103
/**
104104
* Swap the values at two mutable locations of the same type, without
105-
* deinitialising or copying either one.
105+
* deinitialising either. They may overlap.
106106
*/
107107
#[inline]
108-
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
108+
pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
109109
// Give ourselves some scratch space to work with
110110
let mut tmp: T = mem::uninit();
111111
let t: *mut T = &mut tmp;
@@ -122,19 +122,19 @@ pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
122122

123123
/**
124124
* Replace the value at a mutable location with a new one, returning the old
125-
* value, without deinitialising or copying either one.
125+
* value, without deinitialising either.
126126
*/
127127
#[inline]
128-
pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T {
128+
pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
129129
mem::swap(cast::transmute(dest), &mut src); // cannot overlap
130130
src
131131
}
132132

133133
/**
134-
* Reads the value from `*src` and returns it. Does not copy `*src`.
134+
* Reads the value from `*src` and returns it.
135135
*/
136136
#[inline(always)]
137-
pub unsafe fn read_ptr<T>(src: *T) -> T {
137+
pub unsafe fn read<T>(src: *T) -> T {
138138
let mut tmp: T = mem::uninit();
139139
copy_nonoverlapping_memory(&mut tmp, src, 1);
140140
tmp
@@ -145,28 +145,16 @@ pub unsafe fn read_ptr<T>(src: *T) -> T {
145145
* This currently prevents destructors from executing.
146146
*/
147147
#[inline(always)]
148-
pub unsafe fn read_and_zero_ptr<T>(dest: *mut T) -> T {
148+
pub unsafe fn read_and_zero<T>(dest: *mut T) -> T {
149149
// Copy the data out from `dest`:
150-
let tmp = read_ptr(&*dest);
150+
let tmp = read(&*dest);
151151

152152
// Now zero out `dest`:
153153
zero_memory(dest, 1);
154154

155155
tmp
156156
}
157157

158-
/// Transform a region pointer - &T - to an unsafe pointer - *T.
159-
#[inline]
160-
pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
161-
thing as *T
162-
}
163-
164-
/// Transform a mutable region pointer - &mut T - to a mutable unsafe pointer - *mut T.
165-
#[inline]
166-
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
167-
thing as *mut T
168-
}
169-
170158
/**
171159
Given a **T (pointer to an array of pointers),
172160
iterate through each *T, up to the provided `len`,
@@ -176,7 +164,7 @@ pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
176164
*/
177165
pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) {
178166
debug!("array_each_with_len: before iterate");
179-
if arr as uint == 0 {
167+
if arr.is_null() {
180168
fail!("ptr::array_each_with_len failure: arr input is null pointer");
181169
}
182170
//let start_ptr = *arr;
@@ -197,108 +185,82 @@ pub unsafe fn array_each_with_len<T>(arr: **T, len: uint, cb: |*T|) {
197185
Dragons be here.
198186
*/
199187
pub unsafe fn array_each<T>(arr: **T, cb: |*T|) {
200-
if arr as uint == 0 {
188+
if arr.is_null() {
201189
fail!("ptr::array_each_with_len failure: arr input is null pointer");
202190
}
203191
let len = buf_len(arr);
204192
debug!("array_each inferred len: {}", len);
205193
array_each_with_len(arr, len, cb);
206194
}
207195

208-
#[allow(missing_doc)]
196+
/// Extension methods for raw pointers.
209197
pub trait RawPtr<T> {
198+
/// Returns the null pointer.
210199
fn null() -> Self;
200+
/// Returns true if the pointer is equal to the null pointer.
211201
fn is_null(&self) -> bool;
212-
fn is_not_null(&self) -> bool;
202+
/// Returns true if the pointer is not equal to the null pointer.
203+
fn is_not_null(&self) -> bool { !self.is_null() }
204+
/// Returns the value of this pointer (ie, the address it points to)
213205
fn to_uint(&self) -> uint;
206+
/// Returns `None` if the pointer is null, or else returns the value wrapped
207+
/// in `Some`.
208+
///
209+
/// # Safety Notes
210+
///
211+
/// While this method is useful for null-safety, it is important to note
212+
/// that this is still an unsafe operation because the returned value could
213+
/// be pointing to invalid memory.
214214
unsafe fn to_option(&self) -> Option<&T>;
215+
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
216+
/// the object, or one-byte-past-the-end.
215217
unsafe fn offset(self, count: int) -> Self;
216218
}
217219

218-
/// Extension methods for immutable pointers
219220
impl<T> RawPtr<T> for *T {
220-
/// Returns the null pointer.
221221
#[inline]
222222
fn null() -> *T { null() }
223223

224-
/// Returns true if the pointer is equal to the null pointer.
225224
#[inline]
226225
fn is_null(&self) -> bool { *self == RawPtr::null() }
227226

228-
/// Returns true if the pointer is not equal to the null pointer.
229227
#[inline]
230-
fn is_not_null(&self) -> bool { *self != RawPtr::null() }
228+
fn to_uint(&self) -> uint { *self as uint }
231229

232-
/// Returns the address of this pointer.
233230
#[inline]
234-
fn to_uint(&self) -> uint { *self as uint }
231+
unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) }
235232

236-
///
237-
/// Returns `None` if the pointer is null, or else returns the value wrapped
238-
/// in `Some`.
239-
///
240-
/// # Safety Notes
241-
///
242-
/// While this method is useful for null-safety, it is important to note
243-
/// that this is still an unsafe operation because the returned value could
244-
/// be pointing to invalid memory.
245-
///
246233
#[inline]
247234
unsafe fn to_option(&self) -> Option<&T> {
248-
if self.is_null() { None } else {
235+
if self.is_null() {
236+
None
237+
} else {
249238
Some(cast::transmute(*self))
250239
}
251240
}
252-
253-
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
254-
/// the object, or one-byte-past-the-end.
255-
#[inline]
256-
unsafe fn offset(self, count: int) -> *T { intrinsics::offset(self, count) }
257241
}
258242

259-
/// Extension methods for mutable pointers
260243
impl<T> RawPtr<T> for *mut T {
261-
/// Returns the null pointer.
262244
#[inline]
263245
fn null() -> *mut T { mut_null() }
264246

265-
/// Returns true if the pointer is equal to the null pointer.
266247
#[inline]
267248
fn is_null(&self) -> bool { *self == RawPtr::null() }
268249

269-
/// Returns true if the pointer is not equal to the null pointer.
270250
#[inline]
271-
fn is_not_null(&self) -> bool { *self != RawPtr::null() }
251+
fn to_uint(&self) -> uint { *self as uint }
272252

273-
/// Returns the address of this pointer.
274253
#[inline]
275-
fn to_uint(&self) -> uint { *self as uint }
254+
unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T }
276255

277-
///
278-
/// Returns `None` if the pointer is null, or else returns the value wrapped
279-
/// in `Some`.
280-
///
281-
/// # Safety Notes
282-
///
283-
/// While this method is useful for null-safety, it is important to note
284-
/// that this is still an unsafe operation because the returned value could
285-
/// be pointing to invalid memory.
286-
///
287256
#[inline]
288257
unsafe fn to_option(&self) -> Option<&T> {
289-
if self.is_null() { None } else {
258+
if self.is_null() {
259+
None
260+
} else {
290261
Some(cast::transmute(*self))
291262
}
292263
}
293-
294-
/// Calculates the offset from a pointer. The offset *must* be in-bounds of
295-
/// the object, or one-byte-past-the-end. An arithmetic overflow is also
296-
/// undefined behaviour.
297-
///
298-
/// This method should be preferred over `offset` when the guarantee can be
299-
/// satisfied, to enable better optimization.
300-
#[inline]
301-
unsafe fn offset(self, count: int) -> *mut T { intrinsics::offset(self as *T, count) as *mut T }
302264
}
303265

304266
// Equality for pointers

src/libstd/rc.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pointers, and then storing the parent pointers as `Weak` pointers.
2424
*/
2525

2626
use cast::transmute;
27-
use ops::Drop;
28-
use cmp::{Eq, Ord};
2927
use clone::{Clone, DeepClone};
28+
use cmp::{Eq, Ord};
3029
use kinds::marker;
31-
use rt::global_heap::exchange_free;
32-
use ptr::read_ptr;
30+
use ops::Drop;
3331
use option::{Option, Some, None};
32+
use ptr;
33+
use rt::global_heap::exchange_free;
3434

3535
struct RcBox<T> {
3636
value: T,
@@ -85,7 +85,7 @@ impl<T> Drop for Rc<T> {
8585
if self.ptr != 0 as *mut RcBox<T> {
8686
(*self.ptr).strong -= 1;
8787
if (*self.ptr).strong == 0 {
88-
read_ptr(self.borrow()); // destroy the contained object
88+
ptr::read(self.borrow()); // destroy the contained object
8989

9090
// remove the implicit "strong weak" pointer now
9191
// that we've destroyed the contents.

0 commit comments

Comments
 (0)